query-engine.ts

Non-parsing query-engine on top of key-value storage
git clone git://git.finwo.net/lib/query-engine.ts
Log | Files | Refs | README | LICENSE

index.ts (1093B)


      1 import {AbstractAdapter} from "./adapter/abstract";
      2 import {Constructable} from "./types/constructable";
      3 
      4 export type DocumentDefinition = {
      5   model: Constructable<{}>
      6 }
      7 
      8 export class Query<P extends Constructable<{}>> {
      9   constructor(protected documentType: P) {
     10     // TBD
     11   }
     12 }
     13 
     14 export class QueryEngine<DocumentDefinitions extends Record<string, DocumentDefinition>> {
     15   protected documents: DocumentDefinitions;
     16 
     17   constructor(options: {
     18     defaultAdapter?: AbstractAdapter,
     19     documents?: DocumentDefinitions,
     20   } = {
     21 
     22   }) {
     23     // Sanitize inputs
     24     if ('object' !== typeof options) options = {};
     25     options = Object.assign({ documents: {} }, options || {});
     26     if ('object' !== typeof options.documents) options.documents = {} as DocumentDefinitions;
     27     options.documents = Object.assign({}, options.documents || {} as DocumentDefinitions);
     28 
     29     // clean enough
     30     this.documents = options.documents;
     31   }
     32 
     33   query<M extends keyof DocumentDefinitions>(documentType: M): Query<DocumentDefinitions[M]['model']> {
     34     return new Query(this.documents[documentType].model);
     35   }
     36 
     37 }
     38 
     39