Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { DropboxAuth } from "./dropbox";
- export type StorageClient = {
- dataBase: string,
- collection: string,
- auth: DropboxAuth
- }
- export type StorageChildren<T> = {
- type: T,
- default?: keyof T,
- required?: boolean,
- unique?: boolean,
- lowercase?: boolean,
- uppercase?: boolean,
- trim?: boolean,
- max?: boolean,
- min?: boolean,
- expires?: number
- }
- export type StorageType = {
- [key: string]: StorageChildren<any>
- }
- export interface StoragePlain {
- readonly id?: string
- }
- /////////////////////////////////////////////////////////////////////////////////////////////
- export abstract class IStorageEmitter {
- public schema: StorageType
- constructor(schema: StorageType){
- this.schema = schema
- }
- abstract on(event: string, callback: Function): void;
- abstract pre(): void;
- abstract emit(): void;
- }
- export abstract class IStoragePattern<T> extends IStorageEmitter {
- abstract loadClass(): void;
- }
- export abstract class IStorageQuery<T> {
- abstract insertOrUpdate(): void;
- abstract insert(...object: T[]): void;
- abstract find(): void;
- abstract findOne(): void;
- abstract findById(): void;
- abstract findByIdAndUpdate(): void;
- abstract delete(): void;
- abstract deleteById(): void;
- }
- export abstract class IStorageModel<T> extends IStorageQuery<T> {
- private cache: Map<any, any>
- schema: IStoragePattern<T>
- constructor(schema: IStoragePattern<T>){
- super()
- this.cache = new Map()
- this.schema = schema
- }
- }
- export abstract class $StoragePattern<T> extends IStoragePattern<T> {
- loadClass(): void {
- }
- on(event: string, callback: Function): void {
- throw new Error("Method not implemented.");
- }
- pre(): void {
- throw new Error("Method not implemented.");
- }
- emit(): void {
- throw new Error("Method not implemented.");
- }
- }
- /**
- * StorageModel pattern
- */
- export class $StorageModel<T> extends IStorageModel<T> {
- insertOrUpdate(): void {
- throw new Error("Method not implemented.");
- }
- insert(...object: T[]): void {
- throw new Error("Method not implemented.");
- }
- find(): void {
- throw new Error("Method not implemented.");
- }
- findOne(): void {
- throw new Error("Method not implemented.");
- }
- findById(): void {
- throw new Error("Method not implemented.");
- }
- findByIdAndUpdate(): void {
- throw new Error("Method not implemented.");
- }
- delete(): void {
- throw new Error("Method not implemented.");
- }
- deleteById(): void {
- throw new Error("Method not implemented.");
- }
- }
- /**
- * Core from storage module
- */
- export class StorageManager {
- private client: StorageClient
- private cache: {[key: string]: object} = {}
- /**
- * Instance an storage manager, used to create object models
- * @param client it's the default configurations
- */
- constructor(client: StorageClient){
- this.client = client
- this.loadStorage()
- }
- /**
- * Load content from provided cdn (dropbox, drive, amazon)
- */
- async loadStorage(){
- let { auth, collection, dataBase } = this.client
- let storageURI = "/" + dataBase + "/" + collection
- let content = await auth.readFile(storageURI)
- this.cache = content.jsonContent as {}
- }
- /**
- * Create an object model
- * @param model it's model name, used to separe collections
- * @param schema IStoragePattern type, use StoragePattern class to instead it
- */
- model<T extends StoragePlain>(model: string, schema: IStoragePattern<T>): IStorageModel<T> {
- return new $StorageModel(schema)
- }
- }
- /**
- * Use to create pattern model objects
- */
- export class StoragePattern<T extends StoragePlain> extends $StoragePattern<T> {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement