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