@tekbox/node-storage
v0.0.5
Published
NodeStorage provides an interface to save all classes that extends StorageNode. Any storage backend that is implemented by @tekbox/unified-storage can be used. #### Usage
Downloads
3
Readme
@tekbox/node-storage
NodeStorage provides an interface to save all classes that extends StorageNode. Any storage backend that is implemented by @tekbox/unified-storage can be used.
Usage
Available methods:
export interface INodeHandler {
saveNode<T extends INode>(node: T): ThenPromise<T>;
getNode<T extends INode>(db: T, id: string, dbName?: string): ThenPromise<T>;
deleteNode(node: INode, dbName?: string): ThenPromise<boolean>;
getAllNodes<T extends INode>(type: T, dbName?: string): ThenPromise<T[]>;
dumpAll(): ThenPromise<string>;
}
Usage:
export class TestNode extends StorageNode {
public name: string;
constructor(id?: string, name?: string) {
super();
this.id = id != null ? id : "";
this.name = name != null ? name : "";
}
}
export class TestHandler extends AbstractStorageHandler {
getStorageAdapter(storageName: string): ThenPromise<UnifiedStorage> {
return new ThenPromise<UnifiedStorage>(((resolve, reject) => {
if (!this.storageAdapters.has(storageName)) {
const us = new UnifiedStorage("test", new FilesystemStorageAdapter("./test", "testdata"));
us.name = storageName;
this.storageAdapters.set(storageName, us);
}
const adapter = this.storageAdapters.get(storageName);
resolve(adapter);
}));
}
}
NodeHandlerStatic.Setup(new NodeHandler(new TestHandler()));
const nodeStorage = NodeHandlerStatic.GetInstance();
nodeStorage.getNode(new TestNode(), "node-id").then((node: TestNode) => {
console.log(node);
}).catch((err) => {
console.log(err);
});