Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.94 KB | None | 0 0
  1. import { DropboxAuth } from "./dropbox";
  2.  
  3. export type StorageClient = {
  4. dataBase: string,
  5. collection: string,
  6. auth: DropboxAuth
  7. }
  8.  
  9. export type StorageChildren<T> = {
  10. type: T,
  11. default?: keyof T,
  12. required?: boolean,
  13. unique?: boolean,
  14. lowercase?: boolean,
  15. uppercase?: boolean,
  16. trim?: boolean,
  17. max?: boolean,
  18. min?: boolean,
  19. expires?: number
  20. }
  21.  
  22. export type StorageType = {
  23. [key: string]: StorageChildren<any>
  24. }
  25.  
  26. export interface StoragePlain {
  27. readonly id?: string
  28. }
  29.  
  30. /////////////////////////////////////////////////////////////////////////////////////////////
  31.  
  32. export abstract class IStorageEmitter {
  33.  
  34. public schema: StorageType
  35. constructor(schema: StorageType){
  36. this.schema = schema
  37. }
  38.  
  39. abstract on(event: string, callback: Function): void;
  40. abstract pre(): void;
  41. abstract emit(): void;
  42. }
  43.  
  44. export abstract class IStoragePattern<T> extends IStorageEmitter {
  45. abstract loadClass(): void;
  46. }
  47.  
  48. export abstract class IStorageQuery<T> {
  49. abstract insertOrUpdate(): void;
  50. abstract insert(...object: T[]): void;
  51. abstract find(): void;
  52. abstract findOne(): void;
  53. abstract findById(): void;
  54. abstract findByIdAndUpdate(): void;
  55. abstract delete(): void;
  56. abstract deleteById(): void;
  57. }
  58.  
  59. export abstract class IStorageModel<T> extends IStorageQuery<T> {
  60.  
  61. private cache: Map<any, any>
  62. schema: IStoragePattern<T>
  63. constructor(schema: IStoragePattern<T>){
  64. super()
  65. this.cache = new Map()
  66. this.schema = schema
  67. }
  68. }
  69.  
  70. export abstract class $StoragePattern<T> extends IStoragePattern<T> {
  71.  
  72. loadClass(): void {
  73. }
  74. on(event: string, callback: Function): void {
  75. throw new Error("Method not implemented.");
  76. }
  77. pre(): void {
  78. throw new Error("Method not implemented.");
  79. }
  80. emit(): void {
  81. throw new Error("Method not implemented.");
  82. }
  83. }
  84.  
  85. /**
  86. * StorageModel pattern
  87. */
  88. export class $StorageModel<T> extends IStorageModel<T> {
  89.  
  90. insertOrUpdate(): void {
  91. throw new Error("Method not implemented.");
  92. }
  93. insert(...object: T[]): void {
  94. throw new Error("Method not implemented.");
  95. }
  96. find(): void {
  97. throw new Error("Method not implemented.");
  98. }
  99. findOne(): void {
  100. throw new Error("Method not implemented.");
  101. }
  102. findById(): void {
  103. throw new Error("Method not implemented.");
  104. }
  105. findByIdAndUpdate(): void {
  106. throw new Error("Method not implemented.");
  107. }
  108. delete(): void {
  109. throw new Error("Method not implemented.");
  110. }
  111. deleteById(): void {
  112. throw new Error("Method not implemented.");
  113. }
  114. }
  115.  
  116. /**
  117. * Core from storage module
  118. */
  119. export class StorageManager {
  120.  
  121. private client: StorageClient
  122. private cache: {[key: string]: object} = {}
  123.  
  124. /**
  125. * Instance an storage manager, used to create object models
  126. * @param client it's the default configurations
  127. */
  128. constructor(client: StorageClient){
  129. this.client = client
  130. this.loadStorage()
  131. }
  132.  
  133. /**
  134. * Load content from provided cdn (dropbox, drive, amazon)
  135. */
  136. async loadStorage(){
  137. let { auth, collection, dataBase } = this.client
  138. let storageURI = "/" + dataBase + "/" + collection
  139. let content = await auth.readFile(storageURI)
  140. this.cache = content.jsonContent as {}
  141. }
  142.  
  143. /**
  144. * Create an object model
  145. * @param model it's model name, used to separe collections
  146. * @param schema IStoragePattern type, use StoragePattern class to instead it
  147. */
  148. model<T extends StoragePlain>(model: string, schema: IStoragePattern<T>): IStorageModel<T> {
  149. return new $StorageModel(schema)
  150. }
  151. }
  152.  
  153. /**
  154. * Use to create pattern model objects
  155. */
  156. export class StoragePattern<T extends StoragePlain> extends $StoragePattern<T> {}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement