mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-22 06:29:29 +01:00
feat: impl database structure
This commit is contained in:
44
packages/core/database/collection.js
Normal file
44
packages/core/database/collection.js
Normal file
@@ -0,0 +1,44 @@
|
||||
import Indexer from "./indexer";
|
||||
|
||||
export default class Collection extends Indexer {
|
||||
constructor(context, type) {
|
||||
super(context, type);
|
||||
this.collection = new Map();
|
||||
}
|
||||
|
||||
async initCollection() {
|
||||
await this.initIndexer();
|
||||
}
|
||||
|
||||
async addItem(item) {
|
||||
this.collection.set(item.id, item);
|
||||
await this.write(item.id, item);
|
||||
await this.index(item.id);
|
||||
}
|
||||
|
||||
async removeItem(id) {
|
||||
if (this.collection.delete(id)) {
|
||||
this.remove(id);
|
||||
await this.deindex(id);
|
||||
}
|
||||
}
|
||||
|
||||
async getItem(id) {
|
||||
if (this.collection.has(id)) {
|
||||
return this.collection.get(id);
|
||||
} else {
|
||||
return await this.read(id);
|
||||
}
|
||||
}
|
||||
|
||||
async getAllItems() {
|
||||
let items = [];
|
||||
for (let id of await this.getIndices()) {
|
||||
let item = await this.getItem(id);
|
||||
if (item) {
|
||||
items[items.length] = item;
|
||||
}
|
||||
}
|
||||
return items;
|
||||
}
|
||||
}
|
||||
33
packages/core/database/indexer.js
Normal file
33
packages/core/database/indexer.js
Normal file
@@ -0,0 +1,33 @@
|
||||
import Storage from "./storage";
|
||||
|
||||
export default class Indexer extends Storage {
|
||||
constructor(context, type) {
|
||||
super(context);
|
||||
this.type = type;
|
||||
this.indices = [];
|
||||
}
|
||||
|
||||
async initIndexer() {
|
||||
this.indices = (await this.read(this.type)) || [];
|
||||
}
|
||||
|
||||
async index(key) {
|
||||
if (this.indices.length <= 0) {
|
||||
this.indices = (await this.read(this.type)) || [];
|
||||
}
|
||||
this.indices[this.indices.length] = key;
|
||||
await this.write(this.type, this.indices);
|
||||
}
|
||||
|
||||
async getIndices() {
|
||||
if (this.indices.length <= 0) {
|
||||
this.indices = (await this.read(this.type)) || [];
|
||||
}
|
||||
return this.indices;
|
||||
}
|
||||
|
||||
async deindex(key) {
|
||||
this.indices.splice(this.indices.indexOf(key), 1);
|
||||
await this.write(this.type, this.indices);
|
||||
}
|
||||
}
|
||||
26
packages/core/database/storage.js
Normal file
26
packages/core/database/storage.js
Normal file
@@ -0,0 +1,26 @@
|
||||
import Convert from "../utils/convert";
|
||||
|
||||
export default class Storage {
|
||||
constructor(context) {
|
||||
this.storage = context;
|
||||
}
|
||||
async write(key, data) {
|
||||
await this.storage.write(key, Convert.toString(data));
|
||||
}
|
||||
async read(key) {
|
||||
let data = await this.storage.read(key);
|
||||
return Convert.fromString(data);
|
||||
}
|
||||
clear() {
|
||||
this.storage.clear();
|
||||
}
|
||||
remove(key) {
|
||||
this.storage.remove(key);
|
||||
}
|
||||
encrypt(password, data) {
|
||||
return this.storage.encrypt(password, data);
|
||||
}
|
||||
decrypt(password, cipher) {
|
||||
return this.storage.decrypt(password, cipher);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user