@spinajs/orm 1.0.56 → 1.2.26
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/README.md +11 -2
- package/lib/builders.d.ts +30 -23
- package/lib/builders.js +71 -200
- package/lib/builders.js.map +1 -1
- package/lib/decorators.d.ts +18 -19
- package/lib/decorators.js +21 -21
- package/lib/decorators.js.map +1 -1
- package/lib/driver.d.ts +8 -9
- package/lib/driver.js +5 -20
- package/lib/driver.js.map +1 -1
- package/lib/exceptions.d.ts +6 -0
- package/lib/exceptions.js +11 -0
- package/lib/exceptions.js.map +1 -0
- package/lib/helpers.js +2 -2
- package/lib/helpers.js.map +1 -1
- package/lib/hydrators.d.ts +4 -4
- package/lib/hydrators.js +6 -6
- package/lib/hydrators.js.map +1 -1
- package/lib/index.d.ts +0 -1
- package/lib/index.js +1 -2
- package/lib/index.js.map +1 -1
- package/lib/interfaces.d.ts +19 -27
- package/lib/interfaces.js +14 -14
- package/lib/interfaces.js.map +1 -1
- package/lib/model.d.ts +55 -53
- package/lib/model.js +132 -149
- package/lib/model.js.map +1 -1
- package/lib/orm.d.ts +9 -9
- package/lib/orm.js +66 -40
- package/lib/orm.js.map +1 -1
- package/lib/relations.d.ts +14 -13
- package/lib/relations.js +60 -81
- package/lib/relations.js.map +1 -1
- package/lib/statements.d.ts +1 -1
- package/lib/statements.js +19 -21
- package/lib/statements.js.map +1 -1
- package/lib/wrappers.d.ts +1 -1
- package/lib/wrappers.js.map +1 -1
- package/package.json +37 -61
- package/LICENSE +0 -674
package/lib/orm.js
CHANGED
|
@@ -8,26 +8,34 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
8
8
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
10
|
};
|
|
11
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
12
|
exports.Orm = void 0;
|
|
13
|
+
/* eslint-disable prettier/prettier */
|
|
16
14
|
const configuration_1 = require("@spinajs/configuration");
|
|
17
15
|
const di_1 = require("@spinajs/di");
|
|
18
|
-
const di_2 = require("@spinajs/di");
|
|
19
16
|
const log_1 = require("@spinajs/log");
|
|
20
17
|
const reflection_1 = require("@spinajs/reflection");
|
|
21
|
-
const
|
|
18
|
+
const _ = require("lodash");
|
|
22
19
|
const interfaces_1 = require("./interfaces");
|
|
23
20
|
const model_1 = require("./model");
|
|
24
21
|
const decorators_1 = require("./decorators");
|
|
25
22
|
const exceptions_1 = require("@spinajs/exceptions");
|
|
23
|
+
const luxon_1 = require("luxon");
|
|
24
|
+
const exceptions_2 = require("./exceptions");
|
|
26
25
|
/**
|
|
27
26
|
* Used to exclude sensitive data to others. eg. removed password field from cfg
|
|
28
27
|
*/
|
|
29
28
|
const CFG_PROPS = ['Database', 'User', 'Host', 'Port', 'Filename', 'Driver', 'Name'];
|
|
30
29
|
const MIGRATION_TABLE_NAME = 'spinajs_migration';
|
|
30
|
+
const MIGRATION_FILE_REGEXP = /(.*)_([0-9]{4}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2})\.(.*)/;
|
|
31
|
+
const MIGRATION_TYPE_REGEXP = /(.*)_([0-9]{4}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2}_[0-9]{2})/;
|
|
32
|
+
function migrationFileTypeMatcher(name) {
|
|
33
|
+
const match = name.match(MIGRATION_TYPE_REGEXP);
|
|
34
|
+
if (match === null || match.length !== 3) {
|
|
35
|
+
throw new exceptions_2.OrmException(`Invalid migration file name ${name}, expected: ${name}_YYYY_MM_DD_HH_mm_ss`);
|
|
36
|
+
}
|
|
37
|
+
return match[1];
|
|
38
|
+
}
|
|
31
39
|
class Orm extends di_1.AsyncModule {
|
|
32
40
|
constructor() {
|
|
33
41
|
super(...arguments);
|
|
@@ -37,7 +45,7 @@ class Orm extends di_1.AsyncModule {
|
|
|
37
45
|
*
|
|
38
46
|
* Migrates schema up ( fill function is not executed )
|
|
39
47
|
*
|
|
40
|
-
* @param name migration file name
|
|
48
|
+
* @param name - migration file name
|
|
41
49
|
*/
|
|
42
50
|
async migrateUp(name) {
|
|
43
51
|
const self = this;
|
|
@@ -67,7 +75,7 @@ class Orm extends di_1.AsyncModule {
|
|
|
67
75
|
*
|
|
68
76
|
* Migrates schema up ( fill function is not executed )
|
|
69
77
|
*
|
|
70
|
-
* @param name migration file name
|
|
78
|
+
* @param name - migration file name
|
|
71
79
|
*/
|
|
72
80
|
async migrateDown(name) {
|
|
73
81
|
const self = this;
|
|
@@ -98,18 +106,18 @@ class Orm extends di_1.AsyncModule {
|
|
|
98
106
|
*/
|
|
99
107
|
async reloadTableInfo() {
|
|
100
108
|
for (const m of this.Models) {
|
|
101
|
-
const descriptor = model_1.extractModelDescriptor(m.type);
|
|
109
|
+
const descriptor = (0, model_1.extractModelDescriptor)(m.type);
|
|
102
110
|
if (descriptor) {
|
|
103
111
|
const connection = this.Connections.get(descriptor.Connection);
|
|
104
112
|
if (connection) {
|
|
105
113
|
const columns = await connection.tableInfo(descriptor.TableName, connection.Options.Database);
|
|
106
114
|
if (columns) {
|
|
107
|
-
m.type[decorators_1.MODEL_DESCTRIPTION_SYMBOL].Columns =
|
|
108
|
-
return
|
|
115
|
+
m.type[decorators_1.MODEL_DESCTRIPTION_SYMBOL].Columns = _.uniqBy(_.map(columns, (c) => {
|
|
116
|
+
return _.assign(c, _.find(descriptor.Columns, { Name: c.Name }));
|
|
109
117
|
}), 'Name');
|
|
110
118
|
}
|
|
111
119
|
for (const [key, val] of descriptor.Converters) {
|
|
112
|
-
const column = m.type[decorators_1.MODEL_DESCTRIPTION_SYMBOL].Columns.find(c => c.Name === key);
|
|
120
|
+
const column = m.type[decorators_1.MODEL_DESCTRIPTION_SYMBOL].Columns.find((c) => c.Name === key);
|
|
113
121
|
if (column) {
|
|
114
122
|
column.Converter = connection.Container.hasRegistered(val) ? connection.Container.resolve(val) : null;
|
|
115
123
|
}
|
|
@@ -118,8 +126,7 @@ class Orm extends di_1.AsyncModule {
|
|
|
118
126
|
}
|
|
119
127
|
}
|
|
120
128
|
}
|
|
121
|
-
async resolveAsync(
|
|
122
|
-
this.Container = container;
|
|
129
|
+
async resolveAsync() {
|
|
123
130
|
const migrateOnStartup = this.Configuration.get('db.Migration.Startup', false);
|
|
124
131
|
await this.createConnections();
|
|
125
132
|
if (migrateOnStartup) {
|
|
@@ -136,7 +143,7 @@ class Orm extends di_1.AsyncModule {
|
|
|
136
143
|
*
|
|
137
144
|
* NOTE: use it in ORM constructor before ORM is resolved & model list used.
|
|
138
145
|
*
|
|
139
|
-
* @param model model to register
|
|
146
|
+
* @param model - model to register
|
|
140
147
|
*/
|
|
141
148
|
registerModel(model) {
|
|
142
149
|
this.Models.push({
|
|
@@ -152,25 +159,26 @@ class Orm extends di_1.AsyncModule {
|
|
|
152
159
|
*
|
|
153
160
|
* NOTE: use it in ORM constructor before ORM is resolved & migrate function used.
|
|
154
161
|
*
|
|
155
|
-
* @param model model to register
|
|
162
|
+
* @param model - model to register
|
|
156
163
|
*/
|
|
157
164
|
registerMigration(migration) {
|
|
165
|
+
const date = luxon_1.DateTime.now().toFormat('yyyy_MM_dd_HH_mm_ss');
|
|
158
166
|
this.Migrations.push({
|
|
159
|
-
file: `${migration.name}.registered`,
|
|
160
|
-
name: migration.name
|
|
167
|
+
file: `${migration.name}_${date}.registered`,
|
|
168
|
+
name: `${migration.name}_${date}`,
|
|
161
169
|
type: migration,
|
|
162
170
|
});
|
|
163
171
|
}
|
|
164
172
|
async createConnections() {
|
|
165
173
|
const connections = await Promise.all(this.Configuration.get('db.Connections', [])
|
|
166
|
-
.map(c => {
|
|
174
|
+
.map((c) => {
|
|
167
175
|
return this.Container.resolve(c.Driver, [c]);
|
|
168
176
|
})
|
|
169
|
-
.filter(c => c !== null)
|
|
170
|
-
.map(c => c.connect()));
|
|
171
|
-
connections.forEach(c => {
|
|
177
|
+
.filter((c) => c !== null)
|
|
178
|
+
.map((c) => c.connect()));
|
|
179
|
+
connections.forEach((c) => {
|
|
172
180
|
this.Connections.set(c.Options.Name, c);
|
|
173
|
-
this.Log.info(`Found ORM driver ${c.Options.Name} with parameters ${JSON.stringify(
|
|
181
|
+
this.Log.info(`Found ORM driver ${c.Options.Name} with parameters ${JSON.stringify(_.pick(c.Options, CFG_PROPS))}`);
|
|
174
182
|
});
|
|
175
183
|
const defaultConnection = this.Configuration.get('db.DefaultConnection');
|
|
176
184
|
if (defaultConnection) {
|
|
@@ -181,7 +189,7 @@ class Orm extends di_1.AsyncModule {
|
|
|
181
189
|
}
|
|
182
190
|
}
|
|
183
191
|
applyModelMixins() {
|
|
184
|
-
this.Models.forEach(m => {
|
|
192
|
+
this.Models.forEach((m) => {
|
|
185
193
|
// tslint:disable-next-line: forin
|
|
186
194
|
for (const mixin in model_1.MODEL_STATIC_MIXINS) {
|
|
187
195
|
m.type[mixin] = model_1.MODEL_STATIC_MIXINS[mixin].bind(m.type);
|
|
@@ -190,7 +198,26 @@ class Orm extends di_1.AsyncModule {
|
|
|
190
198
|
}
|
|
191
199
|
async executeAvaibleMigrations(name, callback, down) {
|
|
192
200
|
var _a, _b;
|
|
193
|
-
|
|
201
|
+
const toMigrate = name ? this.Migrations.filter((m) => m.name === name) : this.Migrations;
|
|
202
|
+
let migrations = toMigrate
|
|
203
|
+
.map((x) => {
|
|
204
|
+
const match = x.file.match(MIGRATION_FILE_REGEXP);
|
|
205
|
+
if (match === null || match.length !== 4) {
|
|
206
|
+
throw new exceptions_2.OrmException(`Migration file name have invalid format ( expected: some_name_yyyy_MM_dd_HH_mm_ss got ${x.file})`);
|
|
207
|
+
}
|
|
208
|
+
const created = luxon_1.DateTime.fromFormat(match[2], 'yyyy_MM_dd_HH_mm_ss');
|
|
209
|
+
if (!created.isValid) {
|
|
210
|
+
throw new exceptions_2.OrmException(`Migration file ${x.file} have invalid name format ( invalid migration date )`);
|
|
211
|
+
}
|
|
212
|
+
return Object.assign({ created }, x);
|
|
213
|
+
})
|
|
214
|
+
.filter((x) => x !== null)
|
|
215
|
+
.sort((a, b) => {
|
|
216
|
+
if (a.created < b.created) {
|
|
217
|
+
return -1;
|
|
218
|
+
}
|
|
219
|
+
return 1;
|
|
220
|
+
});
|
|
194
221
|
if (down) {
|
|
195
222
|
migrations = migrations.reverse();
|
|
196
223
|
}
|
|
@@ -198,13 +225,10 @@ class Orm extends di_1.AsyncModule {
|
|
|
198
225
|
const md = m.type[decorators_1.MIGRATION_DESCRIPTION_SYMBOL];
|
|
199
226
|
const cn = this.Connections.get(md.Connection);
|
|
200
227
|
const migrationTableName = (_b = (_a = cn.Options.Migration) === null || _a === void 0 ? void 0 : _a.Table) !== null && _b !== void 0 ? _b : MIGRATION_TABLE_NAME;
|
|
201
|
-
const exists = await cn
|
|
202
|
-
.select()
|
|
203
|
-
.from(migrationTableName)
|
|
204
|
-
.where({ Migration: m.name })
|
|
205
|
-
.first();
|
|
228
|
+
const exists = await cn.select().from(migrationTableName).where({ Migration: m.name }).first();
|
|
206
229
|
if (!exists) {
|
|
207
|
-
const migration =
|
|
230
|
+
const migration = await this.Container.resolve(m.type, [cn]);
|
|
231
|
+
this.Log.info(`Setting up migration ${m.name} from file ${m.file} created at ${m.created} mode: ${down ? 'migrate down' : 'migrate up'}`);
|
|
208
232
|
await callback(migration, cn);
|
|
209
233
|
}
|
|
210
234
|
}
|
|
@@ -221,11 +245,9 @@ class Orm extends di_1.AsyncModule {
|
|
|
221
245
|
}
|
|
222
246
|
catch (_c) { }
|
|
223
247
|
if (!migrationTable) {
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
.unique()
|
|
228
|
-
.notNull();
|
|
248
|
+
this.Log.info(`No migration table in database, recreating migration information ...`);
|
|
249
|
+
await connection.schema().createTable(migrationTableName, (table) => {
|
|
250
|
+
table.string('Migration').unique().notNull();
|
|
229
251
|
table.dateTime('CreatedAt').notNull();
|
|
230
252
|
});
|
|
231
253
|
}
|
|
@@ -233,19 +255,23 @@ class Orm extends di_1.AsyncModule {
|
|
|
233
255
|
}
|
|
234
256
|
}
|
|
235
257
|
__decorate([
|
|
236
|
-
reflection_1.ListFromFiles('/**/!(*.d).{ts,js}', 'system.dirs.models'),
|
|
258
|
+
(0, reflection_1.ListFromFiles)('/**/!(*.d).{ts,js}', 'system.dirs.models'),
|
|
237
259
|
__metadata("design:type", Array)
|
|
238
260
|
], Orm.prototype, "Models", void 0);
|
|
239
261
|
__decorate([
|
|
240
|
-
reflection_1.ListFromFiles('/**/!(*.d).{ts,js}', 'system.dirs.migrations'),
|
|
262
|
+
(0, reflection_1.ListFromFiles)('/**/!(*.d).{ts,js}', 'system.dirs.migrations', migrationFileTypeMatcher),
|
|
241
263
|
__metadata("design:type", Array)
|
|
242
264
|
], Orm.prototype, "Migrations", void 0);
|
|
243
265
|
__decorate([
|
|
244
|
-
|
|
245
|
-
__metadata("design:type",
|
|
266
|
+
(0, di_1.Autoinject)(),
|
|
267
|
+
__metadata("design:type", di_1.Container)
|
|
268
|
+
], Orm.prototype, "Container", void 0);
|
|
269
|
+
__decorate([
|
|
270
|
+
(0, log_1.Logger)('ORM'),
|
|
271
|
+
__metadata("design:type", log_1.Log)
|
|
246
272
|
], Orm.prototype, "Log", void 0);
|
|
247
273
|
__decorate([
|
|
248
|
-
|
|
274
|
+
(0, di_1.Autoinject)(),
|
|
249
275
|
__metadata("design:type", configuration_1.Configuration)
|
|
250
276
|
], Orm.prototype, "Configuration", void 0);
|
|
251
277
|
exports.Orm = Orm;
|
package/lib/orm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"orm.js","sourceRoot":"","sources":["../src/orm.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"orm.js","sourceRoot":"","sources":["../src/orm.ts"],"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elDescrtiptor, IBuilderMiddleware, InsertBehaviour, ForwardRefFunction } from './interfaces';
|
|
2
|
+
import { Constructor } from '@spinajs/di';
|
|
2
3
|
import { SelectQueryBuilder } from './builders';
|
|
3
4
|
import { ModelBase } from './model';
|
|
4
5
|
import { Orm } from './orm';
|
|
@@ -10,7 +11,7 @@ export declare abstract class OrmRelation implements IOrmRelation {
|
|
|
10
11
|
protected _query: SelectQueryBuilder<any>;
|
|
11
12
|
_description: IRelationDescriptor;
|
|
12
13
|
parentRelation?: OrmRelation;
|
|
13
|
-
protected _targetModel: Constructor<ModelBase
|
|
14
|
+
protected _targetModel: Constructor<ModelBase> | ForwardRefFunction;
|
|
14
15
|
protected _targetModelDescriptor: IModelDescrtiptor;
|
|
15
16
|
protected _relationQuery: SelectQueryBuilder;
|
|
16
17
|
get Alias(): string;
|
|
@@ -21,18 +22,18 @@ export declare class DiscriminationMapMiddleware implements IBuilderMiddleware {
|
|
|
21
22
|
protected _description: IModelDescrtiptor;
|
|
22
23
|
constructor(_description: IModelDescrtiptor);
|
|
23
24
|
afterData(data: any[]): any[];
|
|
24
|
-
modelCreation(data: any): ModelBase
|
|
25
|
-
afterHydration(_data:
|
|
25
|
+
modelCreation(data: any): ModelBase;
|
|
26
|
+
afterHydration(_data: ModelBase[]): Promise<void>;
|
|
26
27
|
}
|
|
27
28
|
export declare class BelongsToRelation extends OrmRelation {
|
|
28
|
-
protected _targetModel: Constructor<ModelBase
|
|
29
|
+
protected _targetModel: Constructor<ModelBase>;
|
|
29
30
|
protected _targetModelDescriptor: IModelDescrtiptor;
|
|
30
31
|
protected _relationQuery: SelectQueryBuilder;
|
|
31
32
|
constructor(_orm: Orm, _query: SelectQueryBuilder<any>, _description: IRelationDescriptor, _parentRelation?: OrmRelation);
|
|
32
33
|
execute(callback: (this: SelectQueryBuilder, relation: OrmRelation) => void): void;
|
|
33
34
|
}
|
|
34
35
|
export declare class BelongsToRecursiveRelation extends OrmRelation {
|
|
35
|
-
protected _targetModel: Constructor<ModelBase
|
|
36
|
+
protected _targetModel: Constructor<ModelBase>;
|
|
36
37
|
protected _targetModelDescriptor: IModelDescrtiptor;
|
|
37
38
|
protected _relationQuery: SelectQueryBuilder;
|
|
38
39
|
constructor(_orm: Orm, _query: SelectQueryBuilder<any>, _description: IRelationDescriptor, _parentRelation?: OrmRelation);
|
|
@@ -43,7 +44,7 @@ export declare class OneToManyRelation extends OrmRelation {
|
|
|
43
44
|
execute(callback?: (this: SelectQueryBuilder<any>, relation: OrmRelation) => void): void;
|
|
44
45
|
}
|
|
45
46
|
export declare class ManyToManyRelation extends OrmRelation {
|
|
46
|
-
protected _joinModel: Constructor<ModelBase
|
|
47
|
+
protected _joinModel: Constructor<ModelBase>;
|
|
47
48
|
protected _joinModelDescriptor: IModelDescrtiptor;
|
|
48
49
|
protected _joinQuery: SelectQueryBuilder;
|
|
49
50
|
get TableJoinQuery(): SelectQueryBuilder<any>;
|
|
@@ -56,24 +57,24 @@ export declare class ManyToManyRelation extends OrmRelation {
|
|
|
56
57
|
*
|
|
57
58
|
* It allows to add / remove objects to relation
|
|
58
59
|
*/
|
|
59
|
-
export declare abstract class Relation<R extends ModelBase
|
|
60
|
-
protected owner: ModelBase
|
|
60
|
+
export declare abstract class Relation<R extends ModelBase> extends Array<R> {
|
|
61
|
+
protected owner: ModelBase;
|
|
61
62
|
protected model: Constructor<R> | ForwardRefFunction;
|
|
62
63
|
protected Relation: IRelationDescriptor;
|
|
63
64
|
protected TargetModelDescriptor: IModelDescrtiptor;
|
|
64
65
|
protected Orm: Orm;
|
|
65
|
-
constructor(owner: ModelBase
|
|
66
|
+
constructor(owner: ModelBase, model: Constructor<R> | ForwardRefFunction, Relation: IRelationDescriptor, objects?: R[]);
|
|
66
67
|
/**
|
|
67
68
|
* Removes from relation & deletes from db
|
|
68
69
|
*
|
|
69
|
-
* @param obj data to remove
|
|
70
|
+
* @param obj - data to remove
|
|
70
71
|
*/
|
|
71
72
|
abstract remove(obj: R | R[]): Promise<void>;
|
|
72
73
|
/**
|
|
73
74
|
*
|
|
74
75
|
* Add to relation & saves to db
|
|
75
76
|
*
|
|
76
|
-
* @param obj data to add
|
|
77
|
+
* @param obj - data to add
|
|
77
78
|
*/
|
|
78
79
|
abstract add(obj: R | R[], mode?: InsertBehaviour): Promise<void>;
|
|
79
80
|
/**
|
|
@@ -85,11 +86,11 @@ export declare abstract class Relation<R extends ModelBase<any>> extends Array<R
|
|
|
85
86
|
*/
|
|
86
87
|
populate(callback?: (this: SelectQueryBuilder<this>, relation: OrmRelation) => void): Promise<void>;
|
|
87
88
|
}
|
|
88
|
-
export declare class ManyToManyRelationList<T extends ModelBase
|
|
89
|
+
export declare class ManyToManyRelationList<T extends ModelBase> extends Relation<T> {
|
|
89
90
|
remove(obj: T | T[]): Promise<void>;
|
|
90
91
|
add(obj: T | T[], mode?: InsertBehaviour): Promise<void>;
|
|
91
92
|
}
|
|
92
|
-
export declare class OneToManyRelationList<T extends ModelBase
|
|
93
|
+
export declare class OneToManyRelationList<T extends ModelBase> extends Relation<T> {
|
|
93
94
|
remove(obj: T | T[]): Promise<void>;
|
|
94
95
|
add(obj: T | T[], mode?: InsertBehaviour): Promise<void>;
|
|
95
96
|
}
|