@spinajs/orm 2.0.180 → 2.0.182
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cjs/builders.d.ts +643 -643
- package/lib/cjs/builders.js +1602 -1602
- package/lib/cjs/builders.js.map +1 -1
- package/lib/cjs/converters.d.ts +34 -34
- package/lib/cjs/converters.js +104 -104
- package/lib/cjs/decorators.d.ts +152 -152
- package/lib/cjs/decorators.js +449 -449
- package/lib/cjs/dehydrators.d.ts +10 -10
- package/lib/cjs/dehydrators.js +47 -47
- package/lib/cjs/driver.d.ts +82 -82
- package/lib/cjs/driver.js +102 -102
- package/lib/cjs/driver.js.map +1 -1
- package/lib/cjs/enums.d.ts +116 -116
- package/lib/cjs/enums.js +126 -126
- package/lib/cjs/enums.js.map +1 -1
- package/lib/cjs/exceptions.d.ts +6 -6
- package/lib/cjs/exceptions.js +10 -10
- package/lib/cjs/hydrators.d.ts +19 -19
- package/lib/cjs/hydrators.js +132 -132
- package/lib/cjs/hydrators.js.map +1 -1
- package/lib/cjs/index.d.ts +17 -17
- package/lib/cjs/index.js +33 -33
- package/lib/cjs/interfaces.d.ts +921 -919
- package/lib/cjs/interfaces.d.ts.map +1 -1
- package/lib/cjs/interfaces.js +279 -279
- package/lib/cjs/interfaces.js.map +1 -1
- package/lib/cjs/middlewares.d.ts +62 -62
- package/lib/cjs/middlewares.js +258 -258
- package/lib/cjs/model.d.ts +288 -284
- package/lib/cjs/model.d.ts.map +1 -1
- package/lib/cjs/model.js +826 -810
- package/lib/cjs/model.js.map +1 -1
- package/lib/cjs/orm.d.ts +61 -61
- package/lib/cjs/orm.js +333 -333
- package/lib/cjs/orm.js.map +1 -1
- package/lib/cjs/relation-objects.d.ts +108 -108
- package/lib/cjs/relation-objects.js +221 -221
- package/lib/cjs/relations.d.ts +61 -61
- package/lib/cjs/relations.js +194 -194
- package/lib/cjs/relations.js.map +1 -1
- package/lib/cjs/statements.d.ts +143 -143
- package/lib/cjs/statements.js +309 -309
- package/lib/cjs/statements.js.map +1 -1
- package/lib/cjs/types.d.ts +32 -32
- package/lib/cjs/types.js +2 -2
- package/lib/cjs/wrappers.d.ts +5 -5
- package/lib/cjs/wrappers.js +12 -12
- package/lib/mjs/builders.d.ts +643 -643
- package/lib/mjs/builders.js +1594 -1594
- package/lib/mjs/builders.js.map +1 -1
- package/lib/mjs/converters.d.ts +34 -34
- package/lib/mjs/converters.js +96 -96
- package/lib/mjs/decorators.d.ts +152 -152
- package/lib/mjs/decorators.js +422 -422
- package/lib/mjs/dehydrators.d.ts +10 -10
- package/lib/mjs/dehydrators.js +41 -41
- package/lib/mjs/driver.d.ts +82 -82
- package/lib/mjs/driver.js +98 -98
- package/lib/mjs/driver.js.map +1 -1
- package/lib/mjs/enums.d.ts +116 -116
- package/lib/mjs/enums.js +123 -123
- package/lib/mjs/enums.js.map +1 -1
- package/lib/mjs/exceptions.d.ts +6 -6
- package/lib/mjs/exceptions.js +6 -6
- package/lib/mjs/hydrators.d.ts +19 -19
- package/lib/mjs/hydrators.js +128 -128
- package/lib/mjs/hydrators.js.map +1 -1
- package/lib/mjs/index.d.ts +17 -17
- package/lib/mjs/index.js +17 -17
- package/lib/mjs/interfaces.d.ts +921 -919
- package/lib/mjs/interfaces.d.ts.map +1 -1
- package/lib/mjs/interfaces.js +267 -267
- package/lib/mjs/interfaces.js.map +1 -1
- package/lib/mjs/middlewares.d.ts +62 -62
- package/lib/mjs/middlewares.js +249 -249
- package/lib/mjs/model.d.ts +288 -284
- package/lib/mjs/model.d.ts.map +1 -1
- package/lib/mjs/model.js +816 -800
- package/lib/mjs/model.js.map +1 -1
- package/lib/mjs/orm.d.ts +61 -61
- package/lib/mjs/orm.js +326 -326
- package/lib/mjs/orm.js.map +1 -1
- package/lib/mjs/relation-objects.d.ts +108 -108
- package/lib/mjs/relation-objects.js +211 -211
- package/lib/mjs/relations.d.ts +61 -61
- package/lib/mjs/relations.js +191 -191
- package/lib/mjs/relations.js.map +1 -1
- package/lib/mjs/statements.d.ts +143 -143
- package/lib/mjs/statements.js +301 -301
- package/lib/mjs/statements.js.map +1 -1
- package/lib/mjs/types.d.ts +32 -32
- package/lib/mjs/types.js +1 -1
- package/lib/mjs/wrappers.d.ts +5 -5
- package/lib/mjs/wrappers.js +9 -9
- package/lib/tsconfig.cjs.tsbuildinfo +1 -1
- package/lib/tsconfig.mjs.tsbuildinfo +1 -1
- package/package.json +5 -5
package/lib/mjs/orm.js
CHANGED
|
@@ -1,327 +1,327 @@
|
|
|
1
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
-
};
|
|
10
|
-
import { DatetimeValueConverter } from './interfaces.js';
|
|
11
|
-
import { Configuration } from '@spinajs/configuration-common';
|
|
12
|
-
import { AsyncService, Autoinject, Container, DI } from '@spinajs/di';
|
|
13
|
-
import { Log, Logger } from '@spinajs/log-common';
|
|
14
|
-
import _ from 'lodash';
|
|
15
|
-
import { MigrationTransactionMode } from './interfaces.js';
|
|
16
|
-
import { MODEL_STATIC_MIXINS, extractModelDescriptor } from './model.js';
|
|
17
|
-
import { MIGRATION_DESCRIPTION_SYMBOL, MODEL_DESCTRIPTION_SYMBOL } from './decorators.js';
|
|
18
|
-
import { InvalidOperation } from '@spinajs/exceptions';
|
|
19
|
-
import { OrmException } from './exceptions.js';
|
|
20
|
-
import { DateTime } from 'luxon';
|
|
21
|
-
/**
|
|
22
|
-
* Used to exclude sensitive data to others. eg. removed password field from cfg
|
|
23
|
-
*/
|
|
24
|
-
const CFG_PROPS = ['Database', 'User', 'Host', 'Port', 'Filename', 'Driver', 'Name'];
|
|
25
|
-
const MIGRATION_TABLE_NAME = 'spinajs_migration';
|
|
26
|
-
const MIGRATION_FILE_REGEXP = /(.*)_([0-9]{4}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2})/;
|
|
27
|
-
export class Orm extends AsyncService {
|
|
28
|
-
constructor() {
|
|
29
|
-
super(...arguments);
|
|
30
|
-
this.Models = [];
|
|
31
|
-
this.Migrations = [];
|
|
32
|
-
this.Connections = new Map();
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
*
|
|
36
|
-
* Migrates schema up ( fill function is not executed )
|
|
37
|
-
*
|
|
38
|
-
* @param name - migration file name
|
|
39
|
-
*/
|
|
40
|
-
async migrateUp(name, force = true) {
|
|
41
|
-
this.Log.info('DB migration UP started ...');
|
|
42
|
-
await this.executeAvaibleMigrations(name, async (migration, driver) => {
|
|
43
|
-
const trFunction = async (driver) => {
|
|
44
|
-
await migration.up(driver);
|
|
45
|
-
await driver
|
|
46
|
-
.insert()
|
|
47
|
-
.into(driver.Options.Migration?.Table ?? MIGRATION_TABLE_NAME)
|
|
48
|
-
.values({
|
|
49
|
-
Migration: migration.constructor.name,
|
|
50
|
-
CreatedAt: new Date(),
|
|
51
|
-
});
|
|
52
|
-
this.Log.info(`Migration ${migration.constructor.name}:up() success !`);
|
|
53
|
-
};
|
|
54
|
-
if (driver.Options.Migration?.Transaction?.Mode === MigrationTransactionMode.PerMigration) {
|
|
55
|
-
await driver.transaction(trFunction);
|
|
56
|
-
}
|
|
57
|
-
else {
|
|
58
|
-
await trFunction(driver);
|
|
59
|
-
}
|
|
60
|
-
}, false, force);
|
|
61
|
-
this.Log.info('DB migration ended ...');
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
*
|
|
65
|
-
* Migrates schema up ( fill function is not executed )
|
|
66
|
-
*
|
|
67
|
-
* @param name - migration file name
|
|
68
|
-
*/
|
|
69
|
-
async migrateDown(name, force = true) {
|
|
70
|
-
this.Log.info('DB migration DOWN started ...');
|
|
71
|
-
await this.executeAvaibleMigrations(name, async (migration, driver) => {
|
|
72
|
-
const trFunction = async (driver) => {
|
|
73
|
-
await migration.down(driver);
|
|
74
|
-
await driver
|
|
75
|
-
.del()
|
|
76
|
-
.from(driver.Options.Migration?.Table ?? MIGRATION_TABLE_NAME)
|
|
77
|
-
.where({
|
|
78
|
-
Migration: migration.constructor.name,
|
|
79
|
-
});
|
|
80
|
-
this.Log.info(`Migration down ${migration.constructor.name}:DOWN success !`);
|
|
81
|
-
};
|
|
82
|
-
if (driver.Options.Migration?.Transaction?.Mode === MigrationTransactionMode.PerMigration) {
|
|
83
|
-
await driver.transaction(trFunction);
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
await trFunction(driver);
|
|
87
|
-
}
|
|
88
|
-
}, true, force);
|
|
89
|
-
this.Log.info('DB migration ended ...');
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* This function is exposed mainly for unit testing purposes. It reloads table information for models
|
|
93
|
-
* ORM always try to load table at resolve time
|
|
94
|
-
*/
|
|
95
|
-
async reloadTableInfo() {
|
|
96
|
-
for (const m of this.Models) {
|
|
97
|
-
const descriptor = extractModelDescriptor(m.type);
|
|
98
|
-
if (descriptor) {
|
|
99
|
-
const connection = this.Connections.get(descriptor.Connection);
|
|
100
|
-
if (connection) {
|
|
101
|
-
m.type[MODEL_DESCTRIPTION_SYMBOL].Driver = connection;
|
|
102
|
-
const columns = await connection.tableInfo(descriptor.TableName, connection.Options.Database);
|
|
103
|
-
if (columns) {
|
|
104
|
-
m.type[MODEL_DESCTRIPTION_SYMBOL].Columns = _.uniqBy(_.map(columns, (c) => {
|
|
105
|
-
return _.assign(c, _.find(descriptor.Columns, { Name: c.Name }));
|
|
106
|
-
}), 'Name');
|
|
107
|
-
// m.type[MODEL_DESCTRIPTION_SYMBOL].Schema = buildJsonSchema(columns);
|
|
108
|
-
}
|
|
109
|
-
for (const [key, val] of descriptor.Converters) {
|
|
110
|
-
const column = m.type[MODEL_DESCTRIPTION_SYMBOL].Columns.find((c) => c.Name === key);
|
|
111
|
-
if (column) {
|
|
112
|
-
column.Converter = connection.Container.hasRegistered(val.Class) ? connection.Container.resolve(val.Class) : null;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
async resolve() {
|
|
120
|
-
await this.createConnections();
|
|
121
|
-
// add all registered migrations via DI
|
|
122
|
-
const migrations = DI.getRegisteredTypes('__migrations__');
|
|
123
|
-
if (migrations) {
|
|
124
|
-
migrations.forEach((m) => {
|
|
125
|
-
this.registerMigration(m);
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
const models = DI.getRegisteredTypes('__models__');
|
|
129
|
-
if (models) {
|
|
130
|
-
models.forEach((m) => {
|
|
131
|
-
this.registerModel(m);
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
await this.migrateUp(undefined, false);
|
|
135
|
-
await this.reloadTableInfo();
|
|
136
|
-
this.wireRelations();
|
|
137
|
-
this.applyModelMixins();
|
|
138
|
-
this.registerDefaultConverters();
|
|
139
|
-
}
|
|
140
|
-
registerDefaultConverters() {
|
|
141
|
-
this.Container.register(DatetimeValueConverter).asMapValue('__orm_db_value_converters__', Date.name);
|
|
142
|
-
this.Container.register(DatetimeValueConverter).asMapValue('__orm_db_value_converters__', DateTime.name);
|
|
143
|
-
}
|
|
144
|
-
wireRelations() {
|
|
145
|
-
this.Models.forEach((x) => {
|
|
146
|
-
const desc = extractModelDescriptor(x.type);
|
|
147
|
-
if (!desc)
|
|
148
|
-
return;
|
|
149
|
-
desc.Relations.forEach((rel) => {
|
|
150
|
-
const found = this.Models.find((y) => {
|
|
151
|
-
const type = _.isString(rel.TargetModelType) ? rel.TargetModelType : rel.TargetModelType.name;
|
|
152
|
-
return y.name === type;
|
|
153
|
-
});
|
|
154
|
-
if (!found) {
|
|
155
|
-
throw new OrmException(`type ${rel.TargetModelType} not found for relation ${rel.Name} in model ${x.name} in file ${x.file}`);
|
|
156
|
-
}
|
|
157
|
-
rel.TargetModel = found.type;
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
*
|
|
163
|
-
* Register model to ORM programatically so ORM can see it and use it. Sometimes dynamical model discovery is not possible eg.
|
|
164
|
-
* in webpack evnironment. In such case we must tell ORM manually what to load.
|
|
165
|
-
*
|
|
166
|
-
* NOTE: use it in ORM constructor before ORM is resolved & model list used.
|
|
167
|
-
*
|
|
168
|
-
* @param model - model to register
|
|
169
|
-
*/
|
|
170
|
-
registerModel(model) {
|
|
171
|
-
this.Models.push({
|
|
172
|
-
file: `${model.name}.registered`,
|
|
173
|
-
name: model.name,
|
|
174
|
-
type: model,
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
*
|
|
179
|
-
* Register migration to ORM programatically so ORM can see it and use it. Sometimes dynamical migration discovery is not possible eg.
|
|
180
|
-
* in webpack evnironment. In such case we must tell ORM manually what to load.
|
|
181
|
-
*
|
|
182
|
-
* NOTE: use it in ORM constructor before ORM is resolved & migrate function used.
|
|
183
|
-
*
|
|
184
|
-
* @param model - model to register
|
|
185
|
-
*/
|
|
186
|
-
registerMigration(migration) {
|
|
187
|
-
const created = this.getMigrationDate(migration);
|
|
188
|
-
if (created === null) {
|
|
189
|
-
throw new OrmException(`Migration file ${migration.name} have invalid name format ( invalid migration name, expected: some_name_yyyy_MM_dd_HH_mm_ss got ${migration.name})`);
|
|
190
|
-
}
|
|
191
|
-
this.Migrations.push({
|
|
192
|
-
file: `${migration.name}.registered`,
|
|
193
|
-
name: `${migration.name}`,
|
|
194
|
-
type: migration,
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
async createConnections() {
|
|
198
|
-
const cConnections = this.Configuration.get('db.Connections', []);
|
|
199
|
-
for (const c of cConnections) {
|
|
200
|
-
this.Log.trace(`Trying to create connection name: ${c.Name}, driver: ${c.Driver}`);
|
|
201
|
-
if (!this.Container.hasRegistered(c.Driver)) {
|
|
202
|
-
throw new OrmException(`ORM connection driver ${c.Driver} not registerd`);
|
|
203
|
-
}
|
|
204
|
-
const driver = await this.Container.resolve(c.Driver, [c]);
|
|
205
|
-
await driver.connect();
|
|
206
|
-
this.Connections.set(c.Name, driver);
|
|
207
|
-
this.Log.success(`Created ORM connection ${c.Name} with parametes ${JSON.stringify(_.pick(c, CFG_PROPS))}`);
|
|
208
|
-
}
|
|
209
|
-
const defaultConnection = this.Configuration.get('db.DefaultConnection');
|
|
210
|
-
if (defaultConnection) {
|
|
211
|
-
if (!this.Connections.has(defaultConnection)) {
|
|
212
|
-
throw new InvalidOperation(`default connection ${defaultConnection} not exists`);
|
|
213
|
-
}
|
|
214
|
-
this.Connections.set('default', this.Connections.get(defaultConnection));
|
|
215
|
-
}
|
|
216
|
-
// wire connection aliases
|
|
217
|
-
// for example if we have module that uses conn name of db-user-session
|
|
218
|
-
// and we want to wire it to some existinc connection instead creating new one
|
|
219
|
-
const aliases = this.Configuration.get('db.Aliases', {});
|
|
220
|
-
for (const a in aliases) {
|
|
221
|
-
const conn = aliases[a];
|
|
222
|
-
if (!this.Connections.has(conn)) {
|
|
223
|
-
throw new InvalidOperation(`default connection ${conn} not exists`);
|
|
224
|
-
}
|
|
225
|
-
this.Connections.set(a, this.Connections.get(conn));
|
|
226
|
-
}
|
|
227
|
-
// register in continaer factory func for retrieving db connections
|
|
228
|
-
// it will allow for easy access to it in modules
|
|
229
|
-
DI.register((_container, connectionName) => {
|
|
230
|
-
if (this.Connections.has(connectionName)) {
|
|
231
|
-
return this.Connections.get(connectionName);
|
|
232
|
-
}
|
|
233
|
-
return null;
|
|
234
|
-
}).as("OrmConnection");
|
|
235
|
-
}
|
|
236
|
-
applyModelMixins() {
|
|
237
|
-
this.Models.forEach((m) => {
|
|
238
|
-
// tslint:disable-next-line: forin
|
|
239
|
-
for (const mixin in MODEL_STATIC_MIXINS) {
|
|
240
|
-
m.type[mixin] = MODEL_STATIC_MIXINS[mixin].bind(m.type);
|
|
241
|
-
}
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
getMigrationDate(migration) {
|
|
245
|
-
const match = migration.name.match(MIGRATION_FILE_REGEXP);
|
|
246
|
-
if (match === null || match.length !== 3) {
|
|
247
|
-
return null;
|
|
248
|
-
}
|
|
249
|
-
const created = DateTime.fromFormat(match[2], 'yyyy_MM_dd_HH_mm_ss');
|
|
250
|
-
if (!created.isValid) {
|
|
251
|
-
return null;
|
|
252
|
-
}
|
|
253
|
-
return created;
|
|
254
|
-
}
|
|
255
|
-
async executeAvaibleMigrations(name, callback, down, force) {
|
|
256
|
-
const toMigrate = name ? this.Migrations.filter((m) => m.name === name) : this.Migrations;
|
|
257
|
-
let migrations = toMigrate
|
|
258
|
-
.map((x) => {
|
|
259
|
-
const created = this.getMigrationDate(x.type);
|
|
260
|
-
if (created === null) {
|
|
261
|
-
throw new OrmException(`Migration file ${x.name} have invalid name format ( invalid migration name, expected: some_name_yyyy_MM_dd_HH_mm_ss got ${x.name})`);
|
|
262
|
-
}
|
|
263
|
-
return {
|
|
264
|
-
created,
|
|
265
|
-
...x,
|
|
266
|
-
};
|
|
267
|
-
})
|
|
268
|
-
.filter((x) => x !== null)
|
|
269
|
-
.sort((a, b) => {
|
|
270
|
-
if (a.created < b.created) {
|
|
271
|
-
return -1;
|
|
272
|
-
}
|
|
273
|
-
return 1;
|
|
274
|
-
});
|
|
275
|
-
if (down) {
|
|
276
|
-
migrations = migrations.reverse();
|
|
277
|
-
}
|
|
278
|
-
for (const m of migrations) {
|
|
279
|
-
const md = m.type[MIGRATION_DESCRIPTION_SYMBOL];
|
|
280
|
-
const cn = this.Connections.get(md.Connection);
|
|
281
|
-
if (!cn) {
|
|
282
|
-
this.Log.warn(`Connection ${md.Connection} not exists for migration ${m.name} at file ${m.file}`);
|
|
283
|
-
continue;
|
|
284
|
-
}
|
|
285
|
-
const migrationTableName = cn.Options.Migration?.Table ?? MIGRATION_TABLE_NAME;
|
|
286
|
-
if (!cn.Options.Migration?.OnStartup) {
|
|
287
|
-
if (!force) {
|
|
288
|
-
this.Log.warn(`Migration for connection ${md.Connection} is disabled on startup, please check conf file for db.[connection].migration.OnStartup property`);
|
|
289
|
-
continue;
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
// if there is no info on migraiton table
|
|
293
|
-
const migrationTableExists = await cn.schema().tableExists(migrationTableName, cn.Options.Database);
|
|
294
|
-
if (!migrationTableExists) {
|
|
295
|
-
this.Log.info(`No migration table in database, recreating migration information ...`);
|
|
296
|
-
await cn.schema().createTable(migrationTableName, (table) => {
|
|
297
|
-
table.string('Migration').unique().notNull();
|
|
298
|
-
table.dateTime('CreatedAt').notNull();
|
|
299
|
-
});
|
|
300
|
-
}
|
|
301
|
-
const exists = await cn.select().from(migrationTableName).where({ Migration: m.name }).orderByDescending('CreatedAt').first();
|
|
302
|
-
if (!exists) {
|
|
303
|
-
const migration = await this.Container.resolve(m.type, [cn]);
|
|
304
|
-
this.Log.info(`Setting up migration ${m.name} from file ${m.file} created at ${m.created} mode: ${down ? 'migrate down' : 'migrate up'}`);
|
|
305
|
-
await callback(migration, cn);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
}
|
|
309
|
-
async dispose() {
|
|
310
|
-
for (const [, value] of this.Connections) {
|
|
311
|
-
await value.disconnect();
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
__decorate([
|
|
316
|
-
Autoinject(),
|
|
317
|
-
__metadata("design:type", Container)
|
|
318
|
-
], Orm.prototype, "Container", void 0);
|
|
319
|
-
__decorate([
|
|
320
|
-
Logger('ORM'),
|
|
321
|
-
__metadata("design:type", Log)
|
|
322
|
-
], Orm.prototype, "Log", void 0);
|
|
323
|
-
__decorate([
|
|
324
|
-
Autoinject(),
|
|
325
|
-
__metadata("design:type", Configuration)
|
|
326
|
-
], Orm.prototype, "Configuration", void 0);
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { DatetimeValueConverter } from './interfaces.js';
|
|
11
|
+
import { Configuration } from '@spinajs/configuration-common';
|
|
12
|
+
import { AsyncService, Autoinject, Container, DI } from '@spinajs/di';
|
|
13
|
+
import { Log, Logger } from '@spinajs/log-common';
|
|
14
|
+
import _ from 'lodash';
|
|
15
|
+
import { MigrationTransactionMode } from './interfaces.js';
|
|
16
|
+
import { MODEL_STATIC_MIXINS, extractModelDescriptor } from './model.js';
|
|
17
|
+
import { MIGRATION_DESCRIPTION_SYMBOL, MODEL_DESCTRIPTION_SYMBOL } from './decorators.js';
|
|
18
|
+
import { InvalidOperation } from '@spinajs/exceptions';
|
|
19
|
+
import { OrmException } from './exceptions.js';
|
|
20
|
+
import { DateTime } from 'luxon';
|
|
21
|
+
/**
|
|
22
|
+
* Used to exclude sensitive data to others. eg. removed password field from cfg
|
|
23
|
+
*/
|
|
24
|
+
const CFG_PROPS = ['Database', 'User', 'Host', 'Port', 'Filename', 'Driver', 'Name'];
|
|
25
|
+
const MIGRATION_TABLE_NAME = 'spinajs_migration';
|
|
26
|
+
const MIGRATION_FILE_REGEXP = /(.*)_([0-9]{4}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2})/;
|
|
27
|
+
export class Orm extends AsyncService {
|
|
28
|
+
constructor() {
|
|
29
|
+
super(...arguments);
|
|
30
|
+
this.Models = [];
|
|
31
|
+
this.Migrations = [];
|
|
32
|
+
this.Connections = new Map();
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* Migrates schema up ( fill function is not executed )
|
|
37
|
+
*
|
|
38
|
+
* @param name - migration file name
|
|
39
|
+
*/
|
|
40
|
+
async migrateUp(name, force = true) {
|
|
41
|
+
this.Log.info('DB migration UP started ...');
|
|
42
|
+
await this.executeAvaibleMigrations(name, async (migration, driver) => {
|
|
43
|
+
const trFunction = async (driver) => {
|
|
44
|
+
await migration.up(driver);
|
|
45
|
+
await driver
|
|
46
|
+
.insert()
|
|
47
|
+
.into(driver.Options.Migration?.Table ?? MIGRATION_TABLE_NAME)
|
|
48
|
+
.values({
|
|
49
|
+
Migration: migration.constructor.name,
|
|
50
|
+
CreatedAt: new Date(),
|
|
51
|
+
});
|
|
52
|
+
this.Log.info(`Migration ${migration.constructor.name}:up() success !`);
|
|
53
|
+
};
|
|
54
|
+
if (driver.Options.Migration?.Transaction?.Mode === MigrationTransactionMode.PerMigration) {
|
|
55
|
+
await driver.transaction(trFunction);
|
|
56
|
+
}
|
|
57
|
+
else {
|
|
58
|
+
await trFunction(driver);
|
|
59
|
+
}
|
|
60
|
+
}, false, force);
|
|
61
|
+
this.Log.info('DB migration ended ...');
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
*
|
|
65
|
+
* Migrates schema up ( fill function is not executed )
|
|
66
|
+
*
|
|
67
|
+
* @param name - migration file name
|
|
68
|
+
*/
|
|
69
|
+
async migrateDown(name, force = true) {
|
|
70
|
+
this.Log.info('DB migration DOWN started ...');
|
|
71
|
+
await this.executeAvaibleMigrations(name, async (migration, driver) => {
|
|
72
|
+
const trFunction = async (driver) => {
|
|
73
|
+
await migration.down(driver);
|
|
74
|
+
await driver
|
|
75
|
+
.del()
|
|
76
|
+
.from(driver.Options.Migration?.Table ?? MIGRATION_TABLE_NAME)
|
|
77
|
+
.where({
|
|
78
|
+
Migration: migration.constructor.name,
|
|
79
|
+
});
|
|
80
|
+
this.Log.info(`Migration down ${migration.constructor.name}:DOWN success !`);
|
|
81
|
+
};
|
|
82
|
+
if (driver.Options.Migration?.Transaction?.Mode === MigrationTransactionMode.PerMigration) {
|
|
83
|
+
await driver.transaction(trFunction);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
await trFunction(driver);
|
|
87
|
+
}
|
|
88
|
+
}, true, force);
|
|
89
|
+
this.Log.info('DB migration ended ...');
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* This function is exposed mainly for unit testing purposes. It reloads table information for models
|
|
93
|
+
* ORM always try to load table at resolve time
|
|
94
|
+
*/
|
|
95
|
+
async reloadTableInfo() {
|
|
96
|
+
for (const m of this.Models) {
|
|
97
|
+
const descriptor = extractModelDescriptor(m.type);
|
|
98
|
+
if (descriptor) {
|
|
99
|
+
const connection = this.Connections.get(descriptor.Connection);
|
|
100
|
+
if (connection) {
|
|
101
|
+
m.type[MODEL_DESCTRIPTION_SYMBOL].Driver = connection;
|
|
102
|
+
const columns = await connection.tableInfo(descriptor.TableName, connection.Options.Database);
|
|
103
|
+
if (columns) {
|
|
104
|
+
m.type[MODEL_DESCTRIPTION_SYMBOL].Columns = _.uniqBy(_.map(columns, (c) => {
|
|
105
|
+
return _.assign(c, _.find(descriptor.Columns, { Name: c.Name }));
|
|
106
|
+
}), 'Name');
|
|
107
|
+
// m.type[MODEL_DESCTRIPTION_SYMBOL].Schema = buildJsonSchema(columns);
|
|
108
|
+
}
|
|
109
|
+
for (const [key, val] of descriptor.Converters) {
|
|
110
|
+
const column = m.type[MODEL_DESCTRIPTION_SYMBOL].Columns.find((c) => c.Name === key);
|
|
111
|
+
if (column) {
|
|
112
|
+
column.Converter = connection.Container.hasRegistered(val.Class) ? connection.Container.resolve(val.Class) : null;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async resolve() {
|
|
120
|
+
await this.createConnections();
|
|
121
|
+
// add all registered migrations via DI
|
|
122
|
+
const migrations = DI.getRegisteredTypes('__migrations__');
|
|
123
|
+
if (migrations) {
|
|
124
|
+
migrations.forEach((m) => {
|
|
125
|
+
this.registerMigration(m);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
const models = DI.getRegisteredTypes('__models__');
|
|
129
|
+
if (models) {
|
|
130
|
+
models.forEach((m) => {
|
|
131
|
+
this.registerModel(m);
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
await this.migrateUp(undefined, false);
|
|
135
|
+
await this.reloadTableInfo();
|
|
136
|
+
this.wireRelations();
|
|
137
|
+
this.applyModelMixins();
|
|
138
|
+
this.registerDefaultConverters();
|
|
139
|
+
}
|
|
140
|
+
registerDefaultConverters() {
|
|
141
|
+
this.Container.register(DatetimeValueConverter).asMapValue('__orm_db_value_converters__', Date.name);
|
|
142
|
+
this.Container.register(DatetimeValueConverter).asMapValue('__orm_db_value_converters__', DateTime.name);
|
|
143
|
+
}
|
|
144
|
+
wireRelations() {
|
|
145
|
+
this.Models.forEach((x) => {
|
|
146
|
+
const desc = extractModelDescriptor(x.type);
|
|
147
|
+
if (!desc)
|
|
148
|
+
return;
|
|
149
|
+
desc.Relations.forEach((rel) => {
|
|
150
|
+
const found = this.Models.find((y) => {
|
|
151
|
+
const type = _.isString(rel.TargetModelType) ? rel.TargetModelType : rel.TargetModelType.name;
|
|
152
|
+
return y.name === type;
|
|
153
|
+
});
|
|
154
|
+
if (!found) {
|
|
155
|
+
throw new OrmException(`type ${rel.TargetModelType} not found for relation ${rel.Name} in model ${x.name} in file ${x.file}`);
|
|
156
|
+
}
|
|
157
|
+
rel.TargetModel = found.type;
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
*
|
|
163
|
+
* Register model to ORM programatically so ORM can see it and use it. Sometimes dynamical model discovery is not possible eg.
|
|
164
|
+
* in webpack evnironment. In such case we must tell ORM manually what to load.
|
|
165
|
+
*
|
|
166
|
+
* NOTE: use it in ORM constructor before ORM is resolved & model list used.
|
|
167
|
+
*
|
|
168
|
+
* @param model - model to register
|
|
169
|
+
*/
|
|
170
|
+
registerModel(model) {
|
|
171
|
+
this.Models.push({
|
|
172
|
+
file: `${model.name}.registered`,
|
|
173
|
+
name: model.name,
|
|
174
|
+
type: model,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
*
|
|
179
|
+
* Register migration to ORM programatically so ORM can see it and use it. Sometimes dynamical migration discovery is not possible eg.
|
|
180
|
+
* in webpack evnironment. In such case we must tell ORM manually what to load.
|
|
181
|
+
*
|
|
182
|
+
* NOTE: use it in ORM constructor before ORM is resolved & migrate function used.
|
|
183
|
+
*
|
|
184
|
+
* @param model - model to register
|
|
185
|
+
*/
|
|
186
|
+
registerMigration(migration) {
|
|
187
|
+
const created = this.getMigrationDate(migration);
|
|
188
|
+
if (created === null) {
|
|
189
|
+
throw new OrmException(`Migration file ${migration.name} have invalid name format ( invalid migration name, expected: some_name_yyyy_MM_dd_HH_mm_ss got ${migration.name})`);
|
|
190
|
+
}
|
|
191
|
+
this.Migrations.push({
|
|
192
|
+
file: `${migration.name}.registered`,
|
|
193
|
+
name: `${migration.name}`,
|
|
194
|
+
type: migration,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
async createConnections() {
|
|
198
|
+
const cConnections = this.Configuration.get('db.Connections', []);
|
|
199
|
+
for (const c of cConnections) {
|
|
200
|
+
this.Log.trace(`Trying to create connection name: ${c.Name}, driver: ${c.Driver}`);
|
|
201
|
+
if (!this.Container.hasRegistered(c.Driver)) {
|
|
202
|
+
throw new OrmException(`ORM connection driver ${c.Driver} not registerd`);
|
|
203
|
+
}
|
|
204
|
+
const driver = await this.Container.resolve(c.Driver, [c]);
|
|
205
|
+
await driver.connect();
|
|
206
|
+
this.Connections.set(c.Name, driver);
|
|
207
|
+
this.Log.success(`Created ORM connection ${c.Name} with parametes ${JSON.stringify(_.pick(c, CFG_PROPS))}`);
|
|
208
|
+
}
|
|
209
|
+
const defaultConnection = this.Configuration.get('db.DefaultConnection');
|
|
210
|
+
if (defaultConnection) {
|
|
211
|
+
if (!this.Connections.has(defaultConnection)) {
|
|
212
|
+
throw new InvalidOperation(`default connection ${defaultConnection} not exists`);
|
|
213
|
+
}
|
|
214
|
+
this.Connections.set('default', this.Connections.get(defaultConnection));
|
|
215
|
+
}
|
|
216
|
+
// wire connection aliases
|
|
217
|
+
// for example if we have module that uses conn name of db-user-session
|
|
218
|
+
// and we want to wire it to some existinc connection instead creating new one
|
|
219
|
+
const aliases = this.Configuration.get('db.Aliases', {});
|
|
220
|
+
for (const a in aliases) {
|
|
221
|
+
const conn = aliases[a];
|
|
222
|
+
if (!this.Connections.has(conn)) {
|
|
223
|
+
throw new InvalidOperation(`default connection ${conn} not exists`);
|
|
224
|
+
}
|
|
225
|
+
this.Connections.set(a, this.Connections.get(conn));
|
|
226
|
+
}
|
|
227
|
+
// register in continaer factory func for retrieving db connections
|
|
228
|
+
// it will allow for easy access to it in modules
|
|
229
|
+
DI.register((_container, connectionName) => {
|
|
230
|
+
if (this.Connections.has(connectionName)) {
|
|
231
|
+
return this.Connections.get(connectionName);
|
|
232
|
+
}
|
|
233
|
+
return null;
|
|
234
|
+
}).as("OrmConnection");
|
|
235
|
+
}
|
|
236
|
+
applyModelMixins() {
|
|
237
|
+
this.Models.forEach((m) => {
|
|
238
|
+
// tslint:disable-next-line: forin
|
|
239
|
+
for (const mixin in MODEL_STATIC_MIXINS) {
|
|
240
|
+
m.type[mixin] = MODEL_STATIC_MIXINS[mixin].bind(m.type);
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
getMigrationDate(migration) {
|
|
245
|
+
const match = migration.name.match(MIGRATION_FILE_REGEXP);
|
|
246
|
+
if (match === null || match.length !== 3) {
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
const created = DateTime.fromFormat(match[2], 'yyyy_MM_dd_HH_mm_ss');
|
|
250
|
+
if (!created.isValid) {
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
return created;
|
|
254
|
+
}
|
|
255
|
+
async executeAvaibleMigrations(name, callback, down, force) {
|
|
256
|
+
const toMigrate = name ? this.Migrations.filter((m) => m.name === name) : this.Migrations;
|
|
257
|
+
let migrations = toMigrate
|
|
258
|
+
.map((x) => {
|
|
259
|
+
const created = this.getMigrationDate(x.type);
|
|
260
|
+
if (created === null) {
|
|
261
|
+
throw new OrmException(`Migration file ${x.name} have invalid name format ( invalid migration name, expected: some_name_yyyy_MM_dd_HH_mm_ss got ${x.name})`);
|
|
262
|
+
}
|
|
263
|
+
return {
|
|
264
|
+
created,
|
|
265
|
+
...x,
|
|
266
|
+
};
|
|
267
|
+
})
|
|
268
|
+
.filter((x) => x !== null)
|
|
269
|
+
.sort((a, b) => {
|
|
270
|
+
if (a.created < b.created) {
|
|
271
|
+
return -1;
|
|
272
|
+
}
|
|
273
|
+
return 1;
|
|
274
|
+
});
|
|
275
|
+
if (down) {
|
|
276
|
+
migrations = migrations.reverse();
|
|
277
|
+
}
|
|
278
|
+
for (const m of migrations) {
|
|
279
|
+
const md = m.type[MIGRATION_DESCRIPTION_SYMBOL];
|
|
280
|
+
const cn = this.Connections.get(md.Connection);
|
|
281
|
+
if (!cn) {
|
|
282
|
+
this.Log.warn(`Connection ${md.Connection} not exists for migration ${m.name} at file ${m.file}`);
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
const migrationTableName = cn.Options.Migration?.Table ?? MIGRATION_TABLE_NAME;
|
|
286
|
+
if (!cn.Options.Migration?.OnStartup) {
|
|
287
|
+
if (!force) {
|
|
288
|
+
this.Log.warn(`Migration for connection ${md.Connection} is disabled on startup, please check conf file for db.[connection].migration.OnStartup property`);
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
// if there is no info on migraiton table
|
|
293
|
+
const migrationTableExists = await cn.schema().tableExists(migrationTableName, cn.Options.Database);
|
|
294
|
+
if (!migrationTableExists) {
|
|
295
|
+
this.Log.info(`No migration table in database, recreating migration information ...`);
|
|
296
|
+
await cn.schema().createTable(migrationTableName, (table) => {
|
|
297
|
+
table.string('Migration').unique().notNull();
|
|
298
|
+
table.dateTime('CreatedAt').notNull();
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
const exists = await cn.select().from(migrationTableName).where({ Migration: m.name }).orderByDescending('CreatedAt').first();
|
|
302
|
+
if (!exists) {
|
|
303
|
+
const migration = await this.Container.resolve(m.type, [cn]);
|
|
304
|
+
this.Log.info(`Setting up migration ${m.name} from file ${m.file} created at ${m.created} mode: ${down ? 'migrate down' : 'migrate up'}`);
|
|
305
|
+
await callback(migration, cn);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
async dispose() {
|
|
310
|
+
for (const [, value] of this.Connections) {
|
|
311
|
+
await value.disconnect();
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
__decorate([
|
|
316
|
+
Autoinject(),
|
|
317
|
+
__metadata("design:type", Container)
|
|
318
|
+
], Orm.prototype, "Container", void 0);
|
|
319
|
+
__decorate([
|
|
320
|
+
Logger('ORM'),
|
|
321
|
+
__metadata("design:type", Log)
|
|
322
|
+
], Orm.prototype, "Log", void 0);
|
|
323
|
+
__decorate([
|
|
324
|
+
Autoinject(),
|
|
325
|
+
__metadata("design:type", Configuration)
|
|
326
|
+
], Orm.prototype, "Configuration", void 0);
|
|
327
327
|
//# sourceMappingURL=orm.js.map
|