import * as Enumerable from 'linq'; import express = require("express"); var router = express.Router(); import Q = require('q'); import {IEntityService} from "../interfaces/entity-service"; import {Container} from '../../di'; import * as Utils from '../utils'; import {pathRepoMap, getEntity, getModel} from './model-entity'; import {InstanceService} from '../services/instance-service'; import {MetaUtils} from "../metadata/utils"; import {Decorators} from '../constants'; import {QueryOptions} from '../interfaces/queryOptions'; import {MetaData} from '../metadata/metadata'; var modelNameRepoModelMap: { [key: string]: IDynamicRepository } = {}; export function GetRepositoryForName(name: string): IDynamicRepository { return modelNameRepoModelMap[name] } export interface IDynamicRepository { getEntity(); getModel(); modelName(); getEntityType(): any; getRootRepo(): IDynamicRepository; bulkPost(objArr: Array); bulkPut(objArr: Array); bulkPatch(objArr: Array); bulkPutMany(objIds: Array, obj: any); bulkDel(objArr: Array); findOne(id: any): Q.Promise; findMany(ids: Array, toLoadEmbeddedChilds?: boolean): Q.Promise; findAll(): Q.Promise; //findWhere(query, selectedFields?: Array): Q.Promise; findWhere(query, selectedFields?: Array, queryOptions?: QueryOptions): Q.Promise; countWhere(query); distinctWhere(query); findByField(fieldName, value): Q.Promise; findChild(id, prop): Q.Promise; put(id: any, obj: any): Q.Promise; post(obj: any): Q.Promise; delete(id: any); patch(id: any, obj); getSearchIndexes(); } export class DynamicRepository implements IDynamicRepository { private path: string; private model: any; private metaModel: any; private entity: any; private entityService: IEntityService; private rootLevelRep: IDynamicRepository; private searchIndex: Array = []; //private modelRepo: any; public initialize(repositoryPath: string, target: Function | Object, model?: any, rootRepo?: IDynamicRepository) { //console.log(schema); this.path = repositoryPath; this.entity = target; this.rootLevelRep = rootRepo; if (target instanceof DynamicRepository) { target.rootLevelRep = rootRepo; } modelNameRepoModelMap[this.path] = this; if (model) { let decoratorFields = MetaUtils.getMetaData(model.prototype, Decorators.FIELD); let decoratorDocument = MetaUtils.getMetaData(model.prototype, Decorators.DOCUMENT); let selfMetadata = decoratorDocument.length && decoratorDocument[0].params.searchIndex; this.searchIndex = Enumerable.from(decoratorFields) .where(ele => { if (selfMetadata) return true; var meta: MetaData = ele as MetaData; return meta.propertyKey && meta && meta.params && (meta.params).searchIndex; }).select(x => x.propertyKey).toArray(); } } public getSearchIndexes() { return this.searchIndex; } public getEntity() { return getEntity(pathRepoMap[this.path].schemaName); } public getModel() { return Utils.entityService(pathRepoMap[this.path].modelType).getModel(pathRepoMap[this.path].schemaName); } public getRootRepo() { return this.rootLevelRep; } public bulkPost(objArr: Array) { var objs = []; objArr.forEach(x => { objs.push(InstanceService.getInstance(this.getEntity(), null, x)); }); return Utils.entityService(pathRepoMap[this.path].modelType).bulkPost(this.path, objs).then(result => { if (result && result.length > 0) { var res = []; result.forEach(x => { res.push(InstanceService.getObjectFromJson(this.getEntity(), x)); }); return res; } return result; }); } public bulkPut(objArr: Array) { var objs = []; objArr.forEach(x => { objs.push(InstanceService.getInstance(this.getEntity(), null, x)); }); return Utils.entityService(pathRepoMap[this.path].modelType).bulkPut(this.path, objs); } public bulkPatch(objArr: Array) { var objs = []; objArr.forEach(x => { objs.push(InstanceService.getInstance(this.getEntity(), null, x)); }); return Utils.entityService(pathRepoMap[this.path].modelType).bulkPatch(this.path, objs); } public bulkPutMany(objIds: Array, obj: any) { obj = InstanceService.getInstance(this.getEntity(), null, obj); return Utils.entityService(pathRepoMap[this.path].modelType).bulkPutMany(this.path, objIds, obj); } public bulkDel(objArr: Array) { return Utils.entityService(pathRepoMap[this.path].modelType).bulkDel(this.path, objArr); } public modelName() { return this.path; } public getEntityType() { return this.entity; } /** * Returns all the items in a collection */ public findAll(): Q.Promise { return Utils.entityService(pathRepoMap[this.path].modelType).findAll(this.path).then(result => { if (result && result.length > 0) { var res = []; result.forEach(x => { res.push(InstanceService.getObjectFromJson(this.getEntity(), x)); }); return res; } return result; }); } // public findWhere(query, selectedFields?: Array): Q.Promise { // return Utils.entityService(pathRepoMap[this.path].modelType).findWhere(this.path, query, selectedFields).then(result => { // if (result && result.length > 0) { // var res = []; // result.forEach(x => { // res.push(InstanceService.getObjectFromJson(this.getEntity(), x)); // }); // return res; // } // return result; // }); // } public findWhere(query, selectedFields?: Array, queryOptions?: QueryOptions, toLoadChilds?: boolean): Q.Promise { return Utils.entityService(pathRepoMap[this.path].modelType).findWhere(this.path, query, selectedFields,queryOptions, toLoadChilds).then(result => { if (result && result.length > 0) { var res = []; result.forEach(x => { res.push(InstanceService.getObjectFromJson(this.getEntity(), x)); }); return res; } return result; }); } public countWhere(query) { return Utils.entityService(pathRepoMap[this.path].modelType).countWhere(this.path, query).then(result => { return result; }); } public distinctWhere(query) { return Utils.entityService(pathRepoMap[this.path].modelType).distinctWhere(this.path, query).then(result => { return result; }); } public findOne(id) { return Utils.entityService(pathRepoMap[this.path].modelType).findOne(this.path, id).then(result => { return InstanceService.getObjectFromJson(this.getEntity(), result); }); } public findByField(fieldName, value): Q.Promise { return Utils.entityService(pathRepoMap[this.path].modelType).findByField(this.path, fieldName, value); } public findMany(ids: Array, toLoadEmbeddedChilds?: boolean) { return Utils.entityService(pathRepoMap[this.path].modelType).findMany(this.path, ids, toLoadEmbeddedChilds).then(result => { if (result && result.length > 0) { var res = []; result.forEach(x => { res.push(InstanceService.getObjectFromJson(this.getEntity(), x)); }); return res; } return result; }); } public findChild(id, prop): Q.Promise { //check if child model is diffrent from parent model (parent is doc and child is entity) //get child repo //call parent's find one and get the array of ids //return child repo.findmany (ids) //var childMeta:string = Utils.getRepoPathForChildIfDifferent(this.getEntity(), prop); //if (childMeta) // return this.findOne(id).then(parent => { // var chilldIds = parent[prop]; // if (!(chilldIds instanceof Array)) { // chilldIds = [chilldIds]; // } // return Utils.entityService(pathRepoMap[childMeta].modelType).findMany(childMeta,chilldIds); // }); return Utils.entityService(pathRepoMap[this.path].modelType).findChild(this.path, id, prop); } /** * case 1: all new - create main item and child separately and embed if true * case 2: some new, some update - create main item and update/create child accordingly and embed if true * @param obj */ public post(obj: any): Q.Promise { obj = InstanceService.getInstance(this.getEntity(), null, obj); return Utils.entityService(pathRepoMap[this.path].modelType).post(this.path, obj); } public put(id: any, obj: any) { obj = InstanceService.getInstance(this.getEntity(), id, obj); return Utils.entityService(pathRepoMap[this.path].modelType).put(this.path, id, obj); } public delete(id: any) { return Utils.entityService(pathRepoMap[this.path].modelType).del(this.path, id); } public patch(id: any, obj) { obj = InstanceService.getInstance(this.getEntity(), id, obj); return Utils.entityService(pathRepoMap[this.path].modelType).patch(this.path, id, obj);; } }