import { IChassisContext, IChassisDataStore, IChassisPlugin, IOperation, } from "../interfaces"; import { CRUDMiddleware } from "./CRUDMiddleware"; import _ = require("lodash"); import { APIContext } from "../helpers/APIs"; import assert = require("assert"); /************************************************************************* * * Troven CONFIDENTIAL * __________________ * * (c) 2017-2020 Troven Ventures Pty Ltd. All Rights Reserved. * * NOTICE: All information contained herein is, and remains * the property of Troven Pty Ltd and its licensors, * if any. The intellectual and technical concepts contained * herein are proprietary to Troven Pty Ltd * and its suppliers and may be covered by International and Regional Patents, * patents in process, and are protected by trade secret or copyright law. * Dissemination of this information or reproduction of this material * is strictly forbidden unless prior written permission is obtained * from Troven Pty Ltd. */ export abstract class AbstractDataStore implements IChassisPlugin, IChassisDataStore { abstract name; id_field_name: string = "_id"; // defaults to mongodb-friendly collection_field_name: string = "collection"; // defaults to mongodb-friendly public options: any = {}; public context: IChassisContext = null; constructor() {} getIDField(): string { return this.id_field_name; } getCollectionField(): string { return this.collection_field_name; } getStoreMiddleware() { return new CRUDMiddleware(this); } install(context: IChassisContext, options: any): void { this.context = context; this.options = options; this.id_field_name = options.id_field || this.id_field_name; assert(this.options.database, "missing options.database"); context.log({ code: "plugin:crud:installed", database: this.options.database, id_field: this.id_field_name, collection_field: this.collection_field_name, }); context.middleware.set(this.getStoreMiddleware()); // let middleware = this.getStoreMiddleware(); // if (options.preload) { // this.preload(options.preload, middleware.name); // } } // preload(preload: any, operationId: string) { // let self = this; // self.context.log({ // code: "plugin:crud:bulk:uploading", // files: _.keys(preload), // }); // let database = this.options.database; // // bulk upload a set of files into named collections // _.each(preload, function (options, name) { // let data_file = options.file || name; // options.collection = options.collection || name; // options.database = options.database || database; // let clobber = options.clobber || false; // self.context.log({ // code: "plugin:crud:bulk:upload", // file: data_file, // database: options.database, // collection: options.collection, // clobber: clobber, // }); // let json = Vars.load(data_file); // let read_operation = new Operation(self.context, "read", "/dummy", { // operationId: operationId, // feature: options, // }); // // bulk insert works only if collection is empty // self.list(read_operation, {}, {}, options, function ( // err: any, // items: any // ) { // let do_install = false; // items = items || []; // if (err) { // self.context.error({ // code: "plugin:crud:bulk:failed", // file: data_file, // collection: options.collection, // error: err.message, // }); // } else if (items && items.length > 0) { // self.context.log({ // code: "plugin:crud:bulk:not-empty", // file: data_file, // collection: options.collection, // count: items.length, // }); // do_install = clobber; // } else { // do_install = true; // } // if (do_install) { // self.bulk(null, clobber, json, options, (_err, r) => { // if (_err) throw new Error(_err); // self.context.log({ // code: "plugin:crud:bulk:loaded", // file: data_file, // collection: options.collection, // uploaded: json.length, // found: r.length, // }); // }); // } // }); // }); // } getOptions( operation: IOperation, _ctx: APIContext, _options: any ): Promise { let options = _.extend( {}, _.pick(this.options, ["database", "collection"]), _options ); this.context.log({ code: "plugin:crud:options", name: this.name, operationId: operation.operationId, store: this.name, options: options, }); return Promise.resolve(options); } //constructor (context: IChassisContext, security: Security, method:string, resource: string, operation_options: any) abstract healthy(req_options: any, callback: Function): void; abstract query( operation: IOperation, meta: any, req_options: any, options: any, callback: Function ): void; abstract create( operation: IOperation, meta: any, req_options: any, options: any, callback: Function ): void; abstract read( operation: IOperation, meta: any, req_options: any, options: any, callback: Function ): void; abstract list( operation: IOperation, meta: any, req_options: any, options: any, callback: Function ): void; abstract aggregate( operation: IOperation, meta: any, req_options: any, options: any, callback: Function ): void; abstract update( operation: IOperation, meta: any, req_options: any, options: any, callback: Function ): void; abstract delete( operation: IOperation, meta: any, req_options: any, options: any, callback: Function ): void; abstract bulk( operation: IOperation, delete_first: boolean, items: any[], req_options: any, options: any, callback: Function ): void; }