@simonbackx/simple-database 1.25.0 → 1.26.0
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/dist/index.d.ts +9 -9
- package/dist/index.js.map +1 -1
- package/dist/migrations.d.ts +1 -1
- package/dist/migrations.js +6 -4
- package/dist/migrations.js.map +1 -1
- package/dist/src/classes/Column.d.ts +10 -6
- package/dist/src/classes/Column.js +62 -31
- package/dist/src/classes/Column.js.map +1 -1
- package/dist/src/classes/Database.d.ts +3 -2
- package/dist/src/classes/Database.js +26 -27
- package/dist/src/classes/Database.js.map +1 -1
- package/dist/src/classes/Factory.d.ts +1 -1
- package/dist/src/classes/Factory.js +5 -5
- package/dist/src/classes/ManyToManyRelation.d.ts +3 -3
- package/dist/src/classes/ManyToManyRelation.js +51 -51
- package/dist/src/classes/ManyToManyRelation.js.map +1 -1
- package/dist/src/classes/ManyToOneRelation.d.ts +1 -1
- package/dist/src/classes/ManyToOneRelation.js +2 -2
- package/dist/src/classes/ManyToOneRelation.js.map +1 -1
- package/dist/src/classes/Migration.js +19 -19
- package/dist/src/classes/Migration.js.map +1 -1
- package/dist/src/classes/Model.d.ts +41 -10
- package/dist/src/classes/Model.js +132 -80
- package/dist/src/classes/Model.js.map +1 -1
- package/dist/src/classes/Model.test.js +144 -144
- package/dist/src/classes/Model.test.js.map +1 -1
- package/dist/src/classes/OneToManyRelation.d.ts +3 -3
- package/dist/src/classes/OneToManyRelation.js +9 -9
- package/dist/src/classes/OneToManyRelation.js.map +1 -1
- package/dist/src/classes/data/boys.js +1000 -1000
- package/dist/src/classes/data/family-names.js +997 -997
- package/dist/src/classes/data/girls.js +999 -999
- package/dist/src/classes/data/streets.js +291 -291
- package/dist/src/decorators/Column.d.ts +4 -4
- package/dist/src/decorators/Column.js +1 -1
- package/dist/src/decorators/Column.js.map +1 -1
- package/dist/src/models/Migration.d.ts +1 -1
- package/dist/src/models/Migration.js +8 -8
- package/dist/src/models/Migration.js.map +1 -1
- package/dist/tests/jest.global.setup.d.ts +1 -0
- package/dist/tests/jest.global.setup.js +5 -3
- package/dist/tests/jest.global.setup.js.map +1 -1
- package/package.json +5 -8
|
@@ -7,31 +7,31 @@ const ManyToManyRelation_1 = require("./ManyToManyRelation");
|
|
|
7
7
|
const ManyToOneRelation_1 = require("./ManyToOneRelation");
|
|
8
8
|
const Model_1 = require("./Model");
|
|
9
9
|
const OneToManyRelation_1 = require("./OneToManyRelation");
|
|
10
|
-
describe(
|
|
10
|
+
describe('Model', () => {
|
|
11
11
|
class TestDecoder {
|
|
12
12
|
constructor(id) {
|
|
13
13
|
this.id = id;
|
|
14
14
|
}
|
|
15
|
-
encode(
|
|
15
|
+
encode(_) {
|
|
16
16
|
return {
|
|
17
17
|
id: this.id,
|
|
18
18
|
};
|
|
19
19
|
}
|
|
20
20
|
static decode(data) {
|
|
21
|
-
return new TestDecoder(data.field(
|
|
21
|
+
return new TestDecoder(data.field('id').number);
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
class TestModelFriend extends Model_1.Model {
|
|
25
25
|
}
|
|
26
|
-
TestModelFriend.table =
|
|
26
|
+
TestModelFriend.table = '_testModels_testModels';
|
|
27
27
|
tslib_1.__decorate([
|
|
28
|
-
(0, Column_1.column)({ type:
|
|
28
|
+
(0, Column_1.column)({ type: 'integer' })
|
|
29
29
|
], TestModelFriend.prototype, "testModelsIdA", void 0);
|
|
30
30
|
tslib_1.__decorate([
|
|
31
|
-
(0, Column_1.column)({ type:
|
|
31
|
+
(0, Column_1.column)({ type: 'integer' })
|
|
32
32
|
], TestModelFriend.prototype, "testModelsIdB", void 0);
|
|
33
33
|
tslib_1.__decorate([
|
|
34
|
-
(0, Column_1.column)({ type:
|
|
34
|
+
(0, Column_1.column)({ type: 'integer' })
|
|
35
35
|
], TestModelFriend.prototype, "priority", void 0);
|
|
36
36
|
// Create a new class
|
|
37
37
|
class TestModel extends Model_1.Model {
|
|
@@ -43,27 +43,27 @@ describe("Model", () => {
|
|
|
43
43
|
this.parentId = null; // null = no address
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
|
-
TestModel.table =
|
|
47
|
-
TestModel.parent = new ManyToOneRelation_1.ManyToOneRelation(TestModel,
|
|
48
|
-
TestModel.friends = new ManyToManyRelation_1.ManyToManyRelation(TestModel, TestModel,
|
|
49
|
-
TestModel.sortedFriends = new ManyToManyRelation_1.ManyToManyRelation(TestModel, TestModel,
|
|
50
|
-
TestModel.children = new OneToManyRelation_1.OneToManyRelation(TestModel, TestModel,
|
|
51
|
-
TestModel.sortedChildren = new OneToManyRelation_1.OneToManyRelation(TestModel, TestModel,
|
|
46
|
+
TestModel.table = 'testModels';
|
|
47
|
+
TestModel.parent = new ManyToOneRelation_1.ManyToOneRelation(TestModel, 'parent');
|
|
48
|
+
TestModel.friends = new ManyToManyRelation_1.ManyToManyRelation(TestModel, TestModel, 'friends', TestModelFriend);
|
|
49
|
+
TestModel.sortedFriends = new ManyToManyRelation_1.ManyToManyRelation(TestModel, TestModel, 'sortedFriends').setSort('priority');
|
|
50
|
+
TestModel.children = new OneToManyRelation_1.OneToManyRelation(TestModel, TestModel, 'children', 'parentId');
|
|
51
|
+
TestModel.sortedChildren = new OneToManyRelation_1.OneToManyRelation(TestModel, TestModel, 'sortedChildren', 'parentId').setSort('count');
|
|
52
52
|
tslib_1.__decorate([
|
|
53
|
-
(0, Column_1.column)({ primary: true, type:
|
|
53
|
+
(0, Column_1.column)({ primary: true, type: 'integer' })
|
|
54
54
|
], TestModel.prototype, "id", void 0);
|
|
55
55
|
tslib_1.__decorate([
|
|
56
|
-
(0, Column_1.column)({ type:
|
|
56
|
+
(0, Column_1.column)({ type: 'string' })
|
|
57
57
|
], TestModel.prototype, "name", void 0);
|
|
58
58
|
tslib_1.__decorate([
|
|
59
|
-
(0, Column_1.column)({ type:
|
|
59
|
+
(0, Column_1.column)({ type: 'integer' })
|
|
60
60
|
], TestModel.prototype, "count", void 0);
|
|
61
61
|
tslib_1.__decorate([
|
|
62
|
-
(0, Column_1.column)({ type:
|
|
62
|
+
(0, Column_1.column)({ type: 'boolean' })
|
|
63
63
|
], TestModel.prototype, "isActive", void 0);
|
|
64
64
|
tslib_1.__decorate([
|
|
65
65
|
(0, Column_1.column)({
|
|
66
|
-
type:
|
|
66
|
+
type: 'datetime',
|
|
67
67
|
beforeSave: (old) => {
|
|
68
68
|
if (old === undefined) {
|
|
69
69
|
const date = new Date();
|
|
@@ -75,13 +75,13 @@ describe("Model", () => {
|
|
|
75
75
|
})
|
|
76
76
|
], TestModel.prototype, "createdOn", void 0);
|
|
77
77
|
tslib_1.__decorate([
|
|
78
|
-
(0, Column_1.column)({ type:
|
|
78
|
+
(0, Column_1.column)({ type: 'date', nullable: true })
|
|
79
79
|
], TestModel.prototype, "birthDay", void 0);
|
|
80
80
|
tslib_1.__decorate([
|
|
81
|
-
(0, Column_1.column)({ type:
|
|
81
|
+
(0, Column_1.column)({ type: 'json', decoder: TestDecoder })
|
|
82
82
|
], TestModel.prototype, "testDecoder", void 0);
|
|
83
83
|
tslib_1.__decorate([
|
|
84
|
-
(0, Column_1.column)({ foreignKey: TestModel.parent, type:
|
|
84
|
+
(0, Column_1.column)({ foreignKey: TestModel.parent, type: 'integer', nullable: true })
|
|
85
85
|
], TestModel.prototype, "parentId", void 0);
|
|
86
86
|
// Create a new class
|
|
87
87
|
class Dog extends Model_1.Model {
|
|
@@ -90,31 +90,31 @@ describe("Model", () => {
|
|
|
90
90
|
this.id = null;
|
|
91
91
|
}
|
|
92
92
|
}
|
|
93
|
-
Dog.table =
|
|
93
|
+
Dog.table = 'dogs';
|
|
94
94
|
tslib_1.__decorate([
|
|
95
|
-
(0, Column_1.column)({ primary: true, type:
|
|
95
|
+
(0, Column_1.column)({ primary: true, type: 'integer' })
|
|
96
96
|
], Dog.prototype, "id", void 0);
|
|
97
97
|
tslib_1.__decorate([
|
|
98
|
-
(0, Column_1.column)({ type:
|
|
98
|
+
(0, Column_1.column)({ type: 'string' })
|
|
99
99
|
], Dog.prototype, "name", void 0);
|
|
100
100
|
tslib_1.__decorate([
|
|
101
101
|
(0, Column_1.column)({
|
|
102
|
-
type:
|
|
103
|
-
skipUpdate: true
|
|
102
|
+
type: 'datetime',
|
|
103
|
+
skipUpdate: true,
|
|
104
104
|
})
|
|
105
105
|
], Dog.prototype, "updatedAt", void 0);
|
|
106
|
-
test(
|
|
106
|
+
test('Not possible to choose own primary key for integer type primary', async () => {
|
|
107
107
|
const m = new TestModel();
|
|
108
108
|
m.id = 123;
|
|
109
|
-
m.name =
|
|
109
|
+
m.name = 'My name';
|
|
110
110
|
m.count = 1;
|
|
111
111
|
m.isActive = true;
|
|
112
112
|
m.birthDay = new Date(1990, 0, 1);
|
|
113
113
|
await expect(m.save()).rejects.toThrow(/primary/i);
|
|
114
114
|
});
|
|
115
|
-
test(
|
|
115
|
+
test('Before save test', async () => {
|
|
116
116
|
const m = new TestModel();
|
|
117
|
-
m.name =
|
|
117
|
+
m.name = 'My name';
|
|
118
118
|
m.isActive = true;
|
|
119
119
|
m.count = 1;
|
|
120
120
|
m.birthDay = new Date(1990, 0, 1);
|
|
@@ -125,11 +125,11 @@ describe("Model", () => {
|
|
|
125
125
|
await m.save();
|
|
126
126
|
expect(m.createdOn).toBe(date);
|
|
127
127
|
});
|
|
128
|
-
test(
|
|
128
|
+
test('Skip update fields', async () => {
|
|
129
129
|
const now = new Date();
|
|
130
130
|
now.setMilliseconds(0);
|
|
131
131
|
const dog = new Dog();
|
|
132
|
-
dog.name =
|
|
132
|
+
dog.name = 'My dog';
|
|
133
133
|
dog.updatedAt = now;
|
|
134
134
|
await expect(dog.save()).resolves.toEqual(true);
|
|
135
135
|
dog.updatedAt = new Date(0);
|
|
@@ -138,42 +138,42 @@ describe("Model", () => {
|
|
|
138
138
|
expect(loadedDog).toBeDefined();
|
|
139
139
|
expect(loadedDog.updatedAt).toEqual(now);
|
|
140
140
|
});
|
|
141
|
-
test(
|
|
141
|
+
test('Force save skip updated field', async () => {
|
|
142
142
|
const now = new Date();
|
|
143
143
|
now.setMilliseconds(0);
|
|
144
144
|
const dog = new Dog();
|
|
145
|
-
dog.name =
|
|
145
|
+
dog.name = 'My dog';
|
|
146
146
|
dog.updatedAt = now;
|
|
147
147
|
await expect(dog.save()).resolves.toEqual(true);
|
|
148
148
|
dog.updatedAt = new Date(0);
|
|
149
|
-
dog.forceSaveProperty(
|
|
149
|
+
dog.forceSaveProperty('updatedAt');
|
|
150
150
|
await expect(dog.save()).resolves.toEqual(true);
|
|
151
151
|
const loadedDog = await Dog.getByID(dog.id);
|
|
152
152
|
expect(loadedDog).toBeDefined();
|
|
153
153
|
expect(loadedDog.updatedAt).toEqual(new Date(0));
|
|
154
154
|
});
|
|
155
|
-
test(
|
|
155
|
+
test('Force save field', async () => {
|
|
156
156
|
const now = new Date();
|
|
157
157
|
now.setMilliseconds(0);
|
|
158
158
|
const dog = new Dog();
|
|
159
|
-
dog.name =
|
|
159
|
+
dog.name = 'My dog';
|
|
160
160
|
dog.updatedAt = now;
|
|
161
161
|
await expect(dog.save()).resolves.toEqual(true);
|
|
162
|
-
dog.forceSaveProperty(
|
|
162
|
+
dog.forceSaveProperty('name');
|
|
163
163
|
await expect(dog.save()).resolves.toEqual(true);
|
|
164
164
|
const loadedDog = await Dog.getByID(dog.id);
|
|
165
165
|
expect(loadedDog).toBeDefined();
|
|
166
166
|
});
|
|
167
|
-
test(
|
|
167
|
+
test('Creating a model requires to set all properties', async () => {
|
|
168
168
|
const m = new TestModel();
|
|
169
|
-
m.name =
|
|
169
|
+
m.name = 'My name';
|
|
170
170
|
m.isActive = true;
|
|
171
171
|
m.createdOn = new Date();
|
|
172
172
|
await expect(m.save()).rejects.toThrow(/count/i);
|
|
173
173
|
});
|
|
174
|
-
test(
|
|
174
|
+
test('Saving a new instance', async () => {
|
|
175
175
|
const m = new TestModel();
|
|
176
|
-
m.name =
|
|
176
|
+
m.name = 'My name';
|
|
177
177
|
m.isActive = true;
|
|
178
178
|
m.count = 1;
|
|
179
179
|
m.birthDay = new Date(1990, 0, 1);
|
|
@@ -181,40 +181,40 @@ describe("Model", () => {
|
|
|
181
181
|
expect(m.existsInDatabase).toEqual(true);
|
|
182
182
|
expect(m.createdOn).toBeDate();
|
|
183
183
|
expect(m.id).toBeGreaterThanOrEqual(1);
|
|
184
|
-
const [rows] = await Database_1.Database.select(
|
|
184
|
+
const [rows] = await Database_1.Database.select('SELECT * from testModels where id = ?', [m.id]);
|
|
185
185
|
expect(rows).toHaveLength(1);
|
|
186
186
|
const row = rows[0];
|
|
187
|
-
expect(row).toHaveProperty(
|
|
188
|
-
const selected = TestModel.fromRow(row[
|
|
187
|
+
expect(row).toHaveProperty('testModels');
|
|
188
|
+
const selected = TestModel.fromRow(row['testModels']);
|
|
189
189
|
expect(selected).toEqual(m);
|
|
190
190
|
expect(selected.id).toEqual(m.id);
|
|
191
191
|
});
|
|
192
|
-
test(
|
|
192
|
+
test('You cannot set a relation directly', async () => {
|
|
193
193
|
const other = new TestModel();
|
|
194
|
-
other.name =
|
|
194
|
+
other.name = 'My parent';
|
|
195
195
|
other.isActive = true;
|
|
196
196
|
other.count = 1;
|
|
197
197
|
await other.save();
|
|
198
198
|
expect(other.createdOn).toBeDate();
|
|
199
199
|
expect(other.existsInDatabase).toEqual(true);
|
|
200
200
|
const m = new TestModel();
|
|
201
|
-
m.name =
|
|
201
|
+
m.name = 'My name';
|
|
202
202
|
m.isActive = true;
|
|
203
203
|
m.count = 1;
|
|
204
204
|
m.birthDay = new Date(1990, 0, 1);
|
|
205
205
|
// setRelation is the correct way to do it (it would throw now):
|
|
206
|
-
//m.setRelation(TestModel.parent, other);
|
|
206
|
+
// m.setRelation(TestModel.parent, other);
|
|
207
207
|
// but we test what happens if we set the relation the wrong way
|
|
208
208
|
m.parent = other;
|
|
209
209
|
await expect(m.save()).rejects.toThrow(/foreign key/i);
|
|
210
210
|
});
|
|
211
|
-
test(
|
|
211
|
+
test('Save before setting a many to one relation', () => {
|
|
212
212
|
const other = new TestModel();
|
|
213
|
-
other.name =
|
|
213
|
+
other.name = 'My parent';
|
|
214
214
|
other.isActive = true;
|
|
215
215
|
other.count = 1;
|
|
216
216
|
const m = new TestModel();
|
|
217
|
-
m.name =
|
|
217
|
+
m.name = 'My name';
|
|
218
218
|
m.isActive = true;
|
|
219
219
|
m.count = 1;
|
|
220
220
|
m.birthDay = new Date(1990, 0, 1);
|
|
@@ -222,20 +222,20 @@ describe("Model", () => {
|
|
|
222
222
|
m.setRelation(TestModel.parent, other);
|
|
223
223
|
}).toThrow(/not yet saved/i);
|
|
224
224
|
});
|
|
225
|
-
test(
|
|
225
|
+
test('Setting a many to one relation', async () => {
|
|
226
226
|
const other = new TestModel();
|
|
227
|
-
other.name =
|
|
227
|
+
other.name = 'My parent';
|
|
228
228
|
other.isActive = true;
|
|
229
229
|
other.count = 1;
|
|
230
230
|
await other.save();
|
|
231
231
|
expect(other.existsInDatabase).toEqual(true);
|
|
232
232
|
if (!other.id) {
|
|
233
|
-
throw new Error(
|
|
233
|
+
throw new Error('Save failed');
|
|
234
234
|
}
|
|
235
235
|
const counts = [4, 5, 1];
|
|
236
236
|
for (const count of counts) {
|
|
237
237
|
const m = new TestModel();
|
|
238
|
-
m.name =
|
|
238
|
+
m.name = 'My name ' + count;
|
|
239
239
|
m.isActive = true;
|
|
240
240
|
m.count = count;
|
|
241
241
|
m.birthDay = new Date(1990, 0, 1);
|
|
@@ -251,7 +251,7 @@ describe("Model", () => {
|
|
|
251
251
|
const o = await TestModel.getByID(other.id);
|
|
252
252
|
expect(o).toEqual(other);
|
|
253
253
|
if (!o) {
|
|
254
|
-
throw new Error(
|
|
254
|
+
throw new Error('Save failed');
|
|
255
255
|
}
|
|
256
256
|
const children = await TestModel.children.load(o);
|
|
257
257
|
expect(children).toHaveLength(3);
|
|
@@ -259,11 +259,11 @@ describe("Model", () => {
|
|
|
259
259
|
const sortedChildren = await TestModel.sortedChildren.load(o);
|
|
260
260
|
expect(sortedChildren).toHaveLength(3);
|
|
261
261
|
expect(TestModel.sortedChildren.isLoaded(o)).toBeTrue();
|
|
262
|
-
expect(sortedChildren.map(
|
|
262
|
+
expect(sortedChildren.map(c => c.count)).toEqual([1, 4, 5]);
|
|
263
263
|
});
|
|
264
|
-
test(
|
|
264
|
+
test('Setting a many to one relation by ID', async () => {
|
|
265
265
|
const other = new TestModel();
|
|
266
|
-
other.name =
|
|
266
|
+
other.name = 'My parent';
|
|
267
267
|
other.isActive = true;
|
|
268
268
|
other.count = 1;
|
|
269
269
|
await other.save();
|
|
@@ -272,7 +272,7 @@ describe("Model", () => {
|
|
|
272
272
|
expect(other.birthDay).toEqual(null);
|
|
273
273
|
expect(other.createdOn).toBeDate();
|
|
274
274
|
const m = new TestModel();
|
|
275
|
-
m.name =
|
|
275
|
+
m.name = 'My name';
|
|
276
276
|
m.isActive = true;
|
|
277
277
|
m.count = 1;
|
|
278
278
|
m.birthDay = new Date(1990, 0, 1);
|
|
@@ -282,24 +282,24 @@ describe("Model", () => {
|
|
|
282
282
|
expect(m.parentId).toEqual(other.id);
|
|
283
283
|
expect(TestModel.parent.isLoaded(m)).toEqual(false);
|
|
284
284
|
expect(TestModel.parent.isSet(m)).toEqual(false);
|
|
285
|
-
const [rows] = await Database_1.Database.select(
|
|
285
|
+
const [rows] = await Database_1.Database.select('SELECT * from testModels where id = ?', [m.id]);
|
|
286
286
|
expect(rows).toHaveLength(1);
|
|
287
287
|
const row = rows[0];
|
|
288
|
-
expect(row).toHaveProperty(
|
|
289
|
-
const selected = TestModel.fromRow(row[
|
|
288
|
+
expect(row).toHaveProperty('testModels');
|
|
289
|
+
const selected = TestModel.fromRow(row['testModels']);
|
|
290
290
|
expect(TestModel.parent.isLoaded(selected)).toEqual(false);
|
|
291
291
|
expect(TestModel.parent.isSet(selected)).toEqual(false);
|
|
292
292
|
expect(selected.parentId).toEqual(other.id);
|
|
293
293
|
});
|
|
294
|
-
test(
|
|
294
|
+
test('Clearing a many to one relation', async () => {
|
|
295
295
|
const other = new TestModel();
|
|
296
|
-
other.name =
|
|
296
|
+
other.name = 'My parent';
|
|
297
297
|
other.isActive = true;
|
|
298
298
|
other.count = 1;
|
|
299
299
|
await other.save();
|
|
300
300
|
expect(other.existsInDatabase).toEqual(true);
|
|
301
301
|
const m = new TestModel();
|
|
302
|
-
m.name =
|
|
302
|
+
m.name = 'My name';
|
|
303
303
|
m.isActive = true;
|
|
304
304
|
m.count = 1;
|
|
305
305
|
m.parentId = other.id;
|
|
@@ -312,22 +312,22 @@ describe("Model", () => {
|
|
|
312
312
|
await m.save();
|
|
313
313
|
expect(m.parentId).toEqual(null);
|
|
314
314
|
expect(m.parent).toEqual(null);
|
|
315
|
-
const [rows] = await Database_1.Database.select(
|
|
315
|
+
const [rows] = await Database_1.Database.select('SELECT * from testModels where id = ?', [m.id]);
|
|
316
316
|
expect(rows).toHaveLength(1);
|
|
317
317
|
const row = rows[0];
|
|
318
|
-
expect(row).toHaveProperty(
|
|
319
|
-
const selected = TestModel.fromRow(row[
|
|
318
|
+
expect(row).toHaveProperty('testModels');
|
|
319
|
+
const selected = TestModel.fromRow(row['testModels']);
|
|
320
320
|
expect(selected.parentId).toEqual(null);
|
|
321
321
|
});
|
|
322
|
-
test(
|
|
322
|
+
test('Clearing a many to one relation by ID', async () => {
|
|
323
323
|
const other = new TestModel();
|
|
324
|
-
other.name =
|
|
324
|
+
other.name = 'My parent';
|
|
325
325
|
other.isActive = true;
|
|
326
326
|
other.count = 1;
|
|
327
327
|
expect(await other.save()).toEqual(true);
|
|
328
328
|
expect(other.existsInDatabase).toEqual(true);
|
|
329
329
|
const m = new TestModel();
|
|
330
|
-
m.name =
|
|
330
|
+
m.name = 'My name';
|
|
331
331
|
m.isActive = true;
|
|
332
332
|
m.count = 1;
|
|
333
333
|
m.parentId = other.id;
|
|
@@ -337,16 +337,16 @@ describe("Model", () => {
|
|
|
337
337
|
m.parentId = null;
|
|
338
338
|
expect(await m.save()).toEqual(true);
|
|
339
339
|
expect(m.parentId).toEqual(null);
|
|
340
|
-
const [rows] = await Database_1.Database.select(
|
|
340
|
+
const [rows] = await Database_1.Database.select('SELECT * from testModels where id = ?', [m.id]);
|
|
341
341
|
expect(rows).toHaveLength(1);
|
|
342
342
|
const row = rows[0];
|
|
343
|
-
expect(row).toHaveProperty(
|
|
344
|
-
const selected = TestModel.fromRow(row[
|
|
343
|
+
expect(row).toHaveProperty('testModels');
|
|
344
|
+
const selected = TestModel.fromRow(row['testModels']);
|
|
345
345
|
expect(selected.parentId).toEqual(null);
|
|
346
346
|
});
|
|
347
|
-
test(
|
|
347
|
+
test('No query if no changes', async () => {
|
|
348
348
|
const other = new TestModel();
|
|
349
|
-
other.name =
|
|
349
|
+
other.name = 'No query if no changes';
|
|
350
350
|
other.isActive = true;
|
|
351
351
|
other.count = 1;
|
|
352
352
|
const firstSave = await other.save();
|
|
@@ -358,9 +358,9 @@ describe("Model", () => {
|
|
|
358
358
|
const saved = await other.save();
|
|
359
359
|
expect(saved).toEqual(false);
|
|
360
360
|
});
|
|
361
|
-
test(
|
|
361
|
+
test('Update a model', async () => {
|
|
362
362
|
const other = new TestModel();
|
|
363
|
-
other.name =
|
|
363
|
+
other.name = 'Update a model';
|
|
364
364
|
other.isActive = true;
|
|
365
365
|
other.count = 1;
|
|
366
366
|
expect(other.existsInDatabase).toEqual(false);
|
|
@@ -372,24 +372,24 @@ describe("Model", () => {
|
|
|
372
372
|
});
|
|
373
373
|
let friend1, friend2, friend3, meWithoutFriends;
|
|
374
374
|
let meWithFriends;
|
|
375
|
-
test(
|
|
375
|
+
test('Set a many to many relationship', async () => {
|
|
376
376
|
friend1 = new TestModel();
|
|
377
|
-
friend1.name =
|
|
377
|
+
friend1.name = 'Friend 1';
|
|
378
378
|
friend1.isActive = true;
|
|
379
379
|
friend1.count = 1;
|
|
380
380
|
expect(await friend1.save()).toEqual(true);
|
|
381
381
|
friend2 = new TestModel();
|
|
382
|
-
friend2.name =
|
|
382
|
+
friend2.name = 'Friend 2';
|
|
383
383
|
friend2.isActive = true;
|
|
384
384
|
friend2.count = 2;
|
|
385
385
|
expect(await friend2.save()).toEqual(true);
|
|
386
386
|
friend3 = new TestModel();
|
|
387
|
-
friend3.name =
|
|
387
|
+
friend3.name = 'Friend 3';
|
|
388
388
|
friend3.isActive = true;
|
|
389
389
|
friend3.count = 3;
|
|
390
390
|
expect(await friend3.save()).toEqual(true);
|
|
391
391
|
meWithFriends = new TestModel().setManyRelation(TestModel.friends, []);
|
|
392
|
-
meWithFriends.name =
|
|
392
|
+
meWithFriends.name = 'Me';
|
|
393
393
|
meWithFriends.isActive = true;
|
|
394
394
|
meWithFriends.count = 1;
|
|
395
395
|
expect(await meWithFriends.save()).toEqual(true);
|
|
@@ -400,41 +400,41 @@ describe("Model", () => {
|
|
|
400
400
|
expect(meWithFriends.friends[1].id).toEqual(friend2.id);
|
|
401
401
|
}
|
|
402
402
|
else {
|
|
403
|
-
expect(
|
|
403
|
+
expect('other friends not loaded').toEqual('other friends loaded');
|
|
404
404
|
}
|
|
405
405
|
// Check also saved in database
|
|
406
|
-
const [rows] = await Database_1.Database.select(
|
|
406
|
+
const [rows] = await Database_1.Database.select('SELECT * from testModels ' + TestModel.friends.joinQuery('testModels', 'friends') + ' where testModels.id = ?', [
|
|
407
407
|
meWithFriends.id,
|
|
408
408
|
]);
|
|
409
|
-
const _meWithoutFriends = TestModel.fromRow(rows[0][
|
|
409
|
+
const _meWithoutFriends = TestModel.fromRow(rows[0]['testModels']);
|
|
410
410
|
expect(_meWithoutFriends).toBeDefined();
|
|
411
411
|
if (!_meWithoutFriends)
|
|
412
412
|
return;
|
|
413
413
|
meWithoutFriends = _meWithoutFriends;
|
|
414
414
|
expect(meWithoutFriends.id).toEqual(meWithFriends.id);
|
|
415
|
-
const friends = TestModel.fromRows(rows,
|
|
415
|
+
const friends = TestModel.fromRows(rows, 'friends');
|
|
416
416
|
expect(friends).toHaveLength(2);
|
|
417
417
|
expect(friends[0].id).toEqual(friend1.id);
|
|
418
418
|
expect(friends[1].id).toEqual(friend2.id);
|
|
419
419
|
});
|
|
420
|
-
test(
|
|
420
|
+
test('Set a sorted many to many relationship', async () => {
|
|
421
421
|
const friend1 = new TestModel();
|
|
422
|
-
friend1.name =
|
|
422
|
+
friend1.name = 'Friend 1';
|
|
423
423
|
friend1.isActive = true;
|
|
424
424
|
friend1.count = 1;
|
|
425
425
|
expect(await friend1.save()).toEqual(true);
|
|
426
426
|
const friend2 = new TestModel();
|
|
427
|
-
friend2.name =
|
|
427
|
+
friend2.name = 'Friend 2';
|
|
428
428
|
friend2.isActive = true;
|
|
429
429
|
friend2.count = 2;
|
|
430
430
|
expect(await friend2.save()).toEqual(true);
|
|
431
431
|
const friend3 = new TestModel();
|
|
432
|
-
friend3.name =
|
|
432
|
+
friend3.name = 'Friend 3';
|
|
433
433
|
friend3.isActive = true;
|
|
434
434
|
friend3.count = 3;
|
|
435
435
|
expect(await friend3.save()).toEqual(true);
|
|
436
436
|
const meWithFriends = new TestModel().setManyRelation(TestModel.sortedFriends, []);
|
|
437
|
-
meWithFriends.name =
|
|
437
|
+
meWithFriends.name = 'Me';
|
|
438
438
|
meWithFriends.isActive = true;
|
|
439
439
|
meWithFriends.count = 1;
|
|
440
440
|
expect(await meWithFriends.save()).toEqual(true);
|
|
@@ -446,19 +446,19 @@ describe("Model", () => {
|
|
|
446
446
|
expect(meWithFriends.sortedFriends[2].id).toEqual(friend3.id);
|
|
447
447
|
}
|
|
448
448
|
else {
|
|
449
|
-
expect(
|
|
449
|
+
expect('other friends not loaded').toEqual('other friends loaded');
|
|
450
450
|
}
|
|
451
451
|
// Check also saved in database
|
|
452
|
-
const [rows] = await Database_1.Database.select(
|
|
453
|
-
TestModel.sortedFriends.joinQuery(
|
|
454
|
-
|
|
455
|
-
TestModel.sortedFriends.orderByQuery(
|
|
456
|
-
const meWithoutFriends = TestModel.fromRow(rows[0][
|
|
452
|
+
const [rows] = await Database_1.Database.select('SELECT * from testModels '
|
|
453
|
+
+ TestModel.sortedFriends.joinQuery('testModels', 'sortedFriends')
|
|
454
|
+
+ ' where testModels.id = ? '
|
|
455
|
+
+ TestModel.sortedFriends.orderByQuery('testModels', 'sortedFriends'), [meWithFriends.id]);
|
|
456
|
+
const meWithoutFriends = TestModel.fromRow(rows[0]['testModels']);
|
|
457
457
|
expect(meWithoutFriends).toBeDefined();
|
|
458
458
|
if (!meWithoutFriends)
|
|
459
459
|
return;
|
|
460
460
|
expect(meWithoutFriends.id).toEqual(meWithFriends.id);
|
|
461
|
-
const friends = TestModel.fromRows(rows,
|
|
461
|
+
const friends = TestModel.fromRows(rows, 'sortedFriends');
|
|
462
462
|
expect(friends).toHaveLength(3);
|
|
463
463
|
expect(friends[0].id).toEqual(friend3.id);
|
|
464
464
|
expect(friends[1].id).toEqual(friend2.id);
|
|
@@ -473,7 +473,7 @@ describe("Model", () => {
|
|
|
473
473
|
expect(__friends[1].id).toEqual(friend3.id);
|
|
474
474
|
expect(__friends[2].id).toEqual(friend1.id);
|
|
475
475
|
});
|
|
476
|
-
test(
|
|
476
|
+
test('Unlink a many to many relationship', async () => {
|
|
477
477
|
// Now unlink one
|
|
478
478
|
await TestModel.friends.unlink(meWithFriends, friend1);
|
|
479
479
|
if (TestModel.friends.isLoaded(meWithFriends)) {
|
|
@@ -481,121 +481,121 @@ describe("Model", () => {
|
|
|
481
481
|
expect(meWithFriends.friends[0].id).toEqual(friend2.id);
|
|
482
482
|
}
|
|
483
483
|
else {
|
|
484
|
-
expect(
|
|
484
|
+
expect('other friends not loaded').toEqual('other friends loaded');
|
|
485
485
|
}
|
|
486
|
-
const [rows] = await Database_1.Database.select(
|
|
486
|
+
const [rows] = await Database_1.Database.select('SELECT * from testModels ' + TestModel.friends.joinQuery('testModels', 'friends') + ' where testModels.id = ?', [
|
|
487
487
|
meWithFriends.id,
|
|
488
488
|
]);
|
|
489
|
-
const meAgain = TestModel.fromRow(rows[0][
|
|
489
|
+
const meAgain = TestModel.fromRow(rows[0]['testModels']);
|
|
490
490
|
expect(meAgain).toBeDefined();
|
|
491
491
|
if (!meAgain)
|
|
492
492
|
return;
|
|
493
493
|
expect(meAgain.id).toEqual(meWithFriends.id);
|
|
494
|
-
const friendsAgain = TestModel.fromRows(rows,
|
|
494
|
+
const friendsAgain = TestModel.fromRows(rows, 'friends');
|
|
495
495
|
expect(friendsAgain).toHaveLength(1);
|
|
496
496
|
expect(friendsAgain[0].id).toEqual(friend2.id);
|
|
497
497
|
});
|
|
498
|
-
test(
|
|
498
|
+
test('Clear a many to many relationship', async () => {
|
|
499
499
|
// Now unlink one
|
|
500
500
|
await TestModel.friends.clear(meWithFriends);
|
|
501
501
|
if (TestModel.friends.isLoaded(meWithFriends)) {
|
|
502
502
|
expect(meWithFriends.friends).toHaveLength(0);
|
|
503
503
|
}
|
|
504
504
|
else {
|
|
505
|
-
expect(
|
|
505
|
+
expect('other friends not loaded').toEqual('other friends loaded');
|
|
506
506
|
}
|
|
507
|
-
const [rows] = await Database_1.Database.select(
|
|
507
|
+
const [rows] = await Database_1.Database.select('SELECT * from testModels ' + TestModel.friends.joinQuery('testModels', 'friends') + ' where testModels.id = ?', [
|
|
508
508
|
meWithFriends.id,
|
|
509
509
|
]);
|
|
510
|
-
const meAgain = TestModel.fromRow(rows[0][
|
|
510
|
+
const meAgain = TestModel.fromRow(rows[0]['testModels']);
|
|
511
511
|
expect(meAgain).toBeDefined();
|
|
512
512
|
if (!meAgain)
|
|
513
513
|
return;
|
|
514
514
|
expect(meAgain.id).toEqual(meWithFriends.id);
|
|
515
|
-
const friendsAgain = TestModel.fromRows(rows,
|
|
515
|
+
const friendsAgain = TestModel.fromRows(rows, 'friends');
|
|
516
516
|
expect(friendsAgain).toHaveLength(0);
|
|
517
517
|
});
|
|
518
|
-
test(
|
|
518
|
+
test('Link a not loaded many to many relation', async () => {
|
|
519
519
|
// Now unlink one
|
|
520
520
|
await TestModel.friends.link(meWithoutFriends, [friend3, friend2, friend1], { priority: [30, 20, 10] });
|
|
521
521
|
expect(TestModel.friends.isLoaded(meWithoutFriends)).toEqual(false);
|
|
522
|
-
const [rows] = await Database_1.Database.select(
|
|
522
|
+
const [rows] = await Database_1.Database.select('SELECT * from testModels ' + TestModel.friends.joinQuery('testModels', 'friends') + ' where testModels.id = ?', [
|
|
523
523
|
meWithoutFriends.id,
|
|
524
524
|
]);
|
|
525
|
-
const meAgain = TestModel.fromRow(rows[0][
|
|
525
|
+
const meAgain = TestModel.fromRow(rows[0]['testModels']);
|
|
526
526
|
expect(meAgain).toBeDefined();
|
|
527
527
|
if (!meAgain)
|
|
528
528
|
return;
|
|
529
529
|
expect(meAgain.id).toEqual(meWithoutFriends.id);
|
|
530
|
-
const friendsAgain = TestModel.fromRows(rows,
|
|
530
|
+
const friendsAgain = TestModel.fromRows(rows, 'friends');
|
|
531
531
|
expect(friendsAgain).toHaveLength(3);
|
|
532
|
-
expect(friendsAgain.map(
|
|
532
|
+
expect(friendsAgain.map(f => f.id)).toIncludeSameMembers([friend3.id, friend2.id, friend1.id]);
|
|
533
533
|
});
|
|
534
|
-
test(
|
|
534
|
+
test('Unlink a not loaded many to many relation', async () => {
|
|
535
535
|
// Now unlink one
|
|
536
536
|
await TestModel.friends.unlink(meWithoutFriends, friend3);
|
|
537
537
|
expect(TestModel.friends.isLoaded(meWithoutFriends)).toEqual(false);
|
|
538
|
-
const [rows] = await Database_1.Database.select(
|
|
538
|
+
const [rows] = await Database_1.Database.select('SELECT * from testModels ' + TestModel.friends.joinQuery('testModels', 'friends') + ' where testModels.id = ?', [
|
|
539
539
|
meWithoutFriends.id,
|
|
540
540
|
]);
|
|
541
|
-
const meAgain = TestModel.fromRow(rows[0][
|
|
541
|
+
const meAgain = TestModel.fromRow(rows[0]['testModels']);
|
|
542
542
|
expect(meAgain).toBeDefined();
|
|
543
543
|
if (!meAgain)
|
|
544
544
|
return;
|
|
545
545
|
expect(meAgain.id).toEqual(meWithoutFriends.id);
|
|
546
|
-
const friendsAgain = TestModel.fromRows(rows,
|
|
547
|
-
expect(friendsAgain.map(
|
|
546
|
+
const friendsAgain = TestModel.fromRows(rows, 'friends');
|
|
547
|
+
expect(friendsAgain.map(f => f.id)).toIncludeSameMembers([friend2.id, friend1.id]);
|
|
548
548
|
});
|
|
549
|
-
test(
|
|
549
|
+
test('Load a M2M relation', async () => {
|
|
550
550
|
// Get a clean one, because we don't want to affect the other tests
|
|
551
|
-
const [rows] = await Database_1.Database.select(
|
|
551
|
+
const [rows] = await Database_1.Database.select('SELECT * from testModels where id = ? LIMIT 1', [meWithFriends.id]);
|
|
552
552
|
expect(rows).toHaveLength(1);
|
|
553
553
|
const row = rows[0];
|
|
554
|
-
expect(row).toHaveProperty(
|
|
555
|
-
const selected = TestModel.fromRow(row[
|
|
554
|
+
expect(row).toHaveProperty('testModels');
|
|
555
|
+
const selected = TestModel.fromRow(row['testModels']);
|
|
556
556
|
expect(selected).toBeDefined();
|
|
557
557
|
expect(TestModel.friends.isLoaded(selected)).toEqual(false);
|
|
558
558
|
const friends = await TestModel.friends.load(selected);
|
|
559
559
|
expect(TestModel.friends.isLoaded(selected)).toEqual(true);
|
|
560
560
|
expect(selected.friends).toEqual(friends);
|
|
561
|
-
expect(friends.map(
|
|
561
|
+
expect(friends.map(f => f.id)).toIncludeSameMembers([friend2.id, friend1.id]);
|
|
562
562
|
expect(friends[0]._link).toMatchObject({
|
|
563
|
-
priority: friends[0].id
|
|
563
|
+
priority: friends[0].id === friend2.id ? 20 : 10,
|
|
564
564
|
testModelsIdA: meWithFriends.id,
|
|
565
|
-
testModelsIdB: friends[0].id
|
|
565
|
+
testModelsIdB: friends[0].id,
|
|
566
566
|
});
|
|
567
567
|
expect(friends[1]._link).toMatchObject({
|
|
568
|
-
priority: friends[1].id
|
|
568
|
+
priority: friends[1].id === friend2.id ? 20 : 10,
|
|
569
569
|
testModelsIdA: meWithFriends.id,
|
|
570
|
-
testModelsIdB: friends[1].id
|
|
570
|
+
testModelsIdB: friends[1].id,
|
|
571
571
|
});
|
|
572
572
|
const sortedFriends = await TestModel.sortedFriends.load(selected);
|
|
573
573
|
expect(TestModel.sortedFriends.isLoaded(selected)).toEqual(true);
|
|
574
|
-
expect(sortedFriends.map(
|
|
574
|
+
expect(sortedFriends.map(f => f.id)).toEqual([friend1.id, friend2.id]);
|
|
575
575
|
});
|
|
576
|
-
test(
|
|
576
|
+
test('Clear a not loaded many to many relation', async () => {
|
|
577
577
|
// Now unlink one
|
|
578
578
|
await TestModel.friends.clear(meWithoutFriends);
|
|
579
579
|
expect(TestModel.friends.isLoaded(meWithoutFriends)).toEqual(true); // should be true, because we can automatically load the relation if it is not yet loaded
|
|
580
580
|
expect(meWithoutFriends.friends).toHaveLength(0);
|
|
581
|
-
const [rows] = await Database_1.Database.select(
|
|
581
|
+
const [rows] = await Database_1.Database.select('SELECT * from testModels ' + TestModel.friends.joinQuery('testModels', 'friends') + ' where testModels.id = ?', [
|
|
582
582
|
meWithoutFriends.id,
|
|
583
583
|
]);
|
|
584
|
-
const meAgain = TestModel.fromRow(rows[0][
|
|
584
|
+
const meAgain = TestModel.fromRow(rows[0]['testModels']);
|
|
585
585
|
expect(meAgain).toBeDefined();
|
|
586
586
|
if (!meAgain)
|
|
587
587
|
return;
|
|
588
588
|
expect(meAgain.id).toEqual(meWithoutFriends.id);
|
|
589
|
-
const friendsAgain = TestModel.fromRows(rows,
|
|
589
|
+
const friendsAgain = TestModel.fromRows(rows, 'friends');
|
|
590
590
|
expect(friendsAgain).toHaveLength(0);
|
|
591
591
|
});
|
|
592
|
-
test(
|
|
592
|
+
test('Load an empty M2M relation', async () => {
|
|
593
593
|
// Get a clean one, because we don't want to affect the other tests
|
|
594
|
-
const [rows] = await Database_1.Database.select(
|
|
594
|
+
const [rows] = await Database_1.Database.select('SELECT * from testModels where id = ? LIMIT 1', [meWithFriends.id]);
|
|
595
595
|
expect(rows).toHaveLength(1);
|
|
596
596
|
const row = rows[0];
|
|
597
|
-
expect(row).toHaveProperty(
|
|
598
|
-
const selected = TestModel.fromRow(row[
|
|
597
|
+
expect(row).toHaveProperty('testModels');
|
|
598
|
+
const selected = TestModel.fromRow(row['testModels']);
|
|
599
599
|
expect(selected).toBeDefined();
|
|
600
600
|
expect(TestModel.friends.isLoaded(selected)).toEqual(false);
|
|
601
601
|
const friends = await TestModel.friends.load(selected);
|
|
@@ -605,25 +605,25 @@ describe("Model", () => {
|
|
|
605
605
|
});
|
|
606
606
|
test("You can't set many to many if not yet saved", async () => {
|
|
607
607
|
const friend1 = new TestModel();
|
|
608
|
-
friend1.name =
|
|
608
|
+
friend1.name = 'Friend 1';
|
|
609
609
|
friend1.isActive = true;
|
|
610
610
|
friend1.count = 1;
|
|
611
611
|
expect(await friend1.save()).toEqual(true);
|
|
612
612
|
const friend2 = new TestModel();
|
|
613
|
-
friend2.name =
|
|
613
|
+
friend2.name = 'Friend 2';
|
|
614
614
|
friend2.isActive = true;
|
|
615
615
|
friend2.count = 1;
|
|
616
616
|
const other = new TestModel().setManyRelation(TestModel.friends, []);
|
|
617
|
-
other.name =
|
|
617
|
+
other.name = 'Me';
|
|
618
618
|
other.isActive = true;
|
|
619
619
|
other.count = 1;
|
|
620
620
|
expect(await other.save()).toEqual(true);
|
|
621
621
|
await expect(TestModel.friends.link(other, [friend1, friend2])).rejects.toThrow(/not saved yet/);
|
|
622
622
|
});
|
|
623
|
-
test(
|
|
623
|
+
test('Get by IDs', async () => {
|
|
624
624
|
const models = await TestModel.getByIDs(friend1.id, friend2.id, friend3.id, meWithFriends.id);
|
|
625
625
|
expect(models).toHaveLength(4);
|
|
626
|
-
expect(models.map(
|
|
626
|
+
expect(models.map(e => e.id)).toIncludeAllMembers([friend1.id, friend2.id, friend3.id, meWithFriends.id]);
|
|
627
627
|
});
|
|
628
628
|
});
|
|
629
629
|
//# sourceMappingURL=Model.test.js.map
|