UNPKG

6.91 kBPlain TextView Raw
1/// <reference path="../core/dynamic/dynamic-repository.ts" />
2import {DynamicSchema} from './dynamic-schema';
3import {repositoryMap} from '../core/exports';
4import * as Utils from '../core/utils';
5import {MetaUtils} from '../core/metadata/utils';
6import {Decorators as CoreDecorators} from '../core/constants';
7import {Decorators} from './constants';
8import {IEntityParams} from './decorators/interfaces/entity-params';
9import {IRepositoryParams} from '../core/decorators/interfaces/repository-params';
10import {pathRepoMap, updateModelEntity, getModel, repoFromModel} from '../core/dynamic/model-entity';
11import Mongoose = require('mongoose');
12import * as Enumerable from 'linq';
13import {sequelizeService} from './sequelizeService';
14import {GetRepositoryForName} from '../core/dynamic/dynamic-repository';
15
16export function generateSchema() {
17 if (Utils.config().SqlConfig.isSqlEnabled == false)
18 return;
19
20 // register entity service
21 Utils.entityService(Decorators.ENTITY, sequelizeService);
22 var entities = MetaUtils.getMetaDataForDecorators([CoreDecorators.ENTITY]);
23 var allDynamicSchemas: Array<DynamicSchema> = new Array<DynamicSchema>();
24 entities.forEach(x => {
25 let entityMeta = x.metadata[0];
26 let schemaName = (<IEntityParams>entityMeta.params).tableName;
27 let schema = new DynamicSchema(entityMeta.target, schemaName, <IEntityParams>entityMeta.params);
28 allDynamicSchemas.push(schema);
29 let entitySchema = schema.getSchema();
30 //let model = Mongoose.model(schemaName, <any>mongooseSchema);
31 updateModelEntity(schemaName, entityMeta.target, entitySchema, schema);
32 });
33
34 allDynamicSchemas.forEach(schema => {
35 schema.getRelations()[CoreDecorators.ONETOMANY].forEach(oneToManyRelation => {
36 if(!oneToManyRelation.itemType){
37 oneToManyRelation.itemType = getModel(oneToManyRelation.rel);
38 }
39 let sourceDynamicSchema = schema;
40 let targetDynamicSchema = Enumerable.from(allDynamicSchemas)
41 .where(dynamicSchema => dynamicSchema.schemaName == oneToManyRelation.rel).first();
42 if(oneToManyRelation.properties){
43 if(oneToManyRelation.properties.indexOf(targetDynamicSchema.getSchema().primaryKeyAttribute)<0){
44 oneToManyRelation.properties.push(targetDynamicSchema.getSchema().primaryKeyAttribute);
45 }
46 }
47 sequelizeService.addRelationInSchema(sourceDynamicSchema.getSchema(), targetDynamicSchema.getSchema(), CoreDecorators.ONETOMANY, oneToManyRelation);
48 });
49 })
50
51 allDynamicSchemas.forEach(schema => {
52 schema.getRelations()[CoreDecorators.MANYTOONE].forEach(manyToOne => {
53 let sourceDynamicSchema = schema;
54 if(!manyToOne.itemType){
55 manyToOne.itemType = getModel(manyToOne.rel);
56 }
57 let targetDynamicSchema:any = Enumerable.from(allDynamicSchemas)
58 .where(dynamicSchema => dynamicSchema.schemaName == manyToOne.rel).first();
59 if(manyToOne.properties){
60 if(manyToOne.properties.indexOf(targetDynamicSchema.getSchema().primaryKeyAttribute)<0){
61 manyToOne.properties.push(targetDynamicSchema.getSchema().primaryKeyAttribute);
62 }
63 }
64 sequelizeService.addRelationInSchema(sourceDynamicSchema.getSchema(), targetDynamicSchema.getSchema(), CoreDecorators.MANYTOONE, manyToOne);
65
66 });
67 })
68
69 allDynamicSchemas.forEach(schema => {
70 schema.getRelations()[CoreDecorators.ONETOONE].forEach(onetoone => {
71 let sourceDynamicSchema = schema;
72 if(!onetoone.itemType){
73 onetoone.itemType = getModel(onetoone.rel);
74 }
75 let targetDynamicSchema:any = Enumerable.from(allDynamicSchemas)
76 .where(dynamicSchema => dynamicSchema.schemaName == onetoone.rel).first();
77 if(onetoone.properties){
78 if(onetoone.properties.indexOf(targetDynamicSchema.getSchema().primaryKeyAttribute)<0){
79 onetoone.properties.push(targetDynamicSchema.getSchema().primaryKeyAttribute);
80 }
81 }
82 sequelizeService.addRelationInSchema(sourceDynamicSchema.getSchema(), targetDynamicSchema.getSchema(), CoreDecorators.ONETOONE, onetoone);
83
84 });
85 })
86
87
88 var repositoryMetadata = MetaUtils.getMetaDataForDecorators([CoreDecorators.REPOSITORY]);
89 repositoryMetadata.forEach(x => {
90 if (!x.metadata || !x.metadata.length) {
91 return;
92 }
93 let repositoryParams = <IRepositoryParams>x.metadata[0].params;
94 let entity = (<IRepositoryParams>x.metadata[0].params).model;
95 let meta = MetaUtils.getMetaData(entity, Decorators.ENTITY);
96 if (meta && meta.length > 0) {
97 let entityMeta = meta[0];
98 if (entityMeta) {
99 let schemaName = (<IEntityParams>entityMeta.params).tableName;
100 pathRepoMap[repositoryParams.path] = { schemaName: schemaName, modelType: Decorators.ENTITY };
101 //repoFromModel[entityMeta.params.name] = repositoryMap[repositoryParams.path];
102 }
103 }
104 });
105 sequelizeService.init();
106
107}
108 // need to pass this via reference
109// var visitedNodes = new Map();
110
111// export function validateModels() {
112// var modelsMeta = metaUtils.getMetaDataForDecoratorInAllTargets(Decorators.DOCUMENT);
113// Enumerable.from(modelsMeta).forEach(x => {
114// var m: MetaData = x;
115// var res = this.hasLoop(m.target, new Array<MetaData>());
116// if (res) {
117// throw 'Cannot start server. Please correct the model ' + m.target.constructor.name;
118// }
119// });
120//}
121
122// private function hasLoop(target: Object, vis: Array<MetaData>): boolean {
123// var rel = metaUtils.getAllRelationsForTargetInternal(target);
124// Enumerable.from(rel).forEach(y => {
125// var r: MetaData = <MetaData>y;
126// var param: IAssociationParams = <IAssociationParams>r.params;
127// if (param.embedded || param.eagerLoading) {
128// var res = false;
129// if (this.visitedNodes.has(r)) {
130// // no need to go ahead, path from this node is already checked
131// res = false;
132// }
133// else if (vis.indexOf(r) > -1) {
134// // loop found
135// res = true;
136// }
137// else {
138// vis.push(r);
139// this.visitedNodes.set(r, true);
140// res = this.hasLoop(param.itemType, vis);
141// }
142
143// // if any loop
144// if (res)
145// return true;
146// }
147// });
148
149// return false;
150// }
\No newline at end of file