UNPKG

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