@itrocks/mysql 0.0.15 → 0.0.17
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/cjs/mysql.d.ts +3 -0
- package/cjs/mysql.js +42 -1
- package/esm/mysql.d.ts +3 -0
- package/esm/mysql.js +42 -1
- package/package.json +1 -1
package/cjs/mysql.d.ts
CHANGED
|
@@ -51,6 +51,9 @@ export declare class Mysql extends DataSource {
|
|
|
51
51
|
readCollectionIds<T extends object, PT extends object>(object: Entity<T>, property: KeyOf<T>, type?: Type<PT>): Promise<Identifier[]>;
|
|
52
52
|
readMultiple<T extends object>(type: Type<T>, ids: Identifier[]): Promise<Entity<T>[]>;
|
|
53
53
|
save<T extends object>(object: MayEntity<T>): Promise<Entity<T>>;
|
|
54
|
+
saveCollection<T extends object>(object: Entity<T>, property: KeyOf<T>, value: (Identifier | MayEntity)[]): Promise<void>;
|
|
55
|
+
saveComponents<T extends object>(object: Entity<T>, property: KeyOf<T>, components: (Identifier | MayEntity)[]): Promise<void>;
|
|
56
|
+
saveLinks<T extends object>(object: Entity<T>, property: KeyOf<T>, links: (Identifier | MayEntity)[]): Promise<void>;
|
|
54
57
|
search<T extends object>(type: Type<T>, search?: SearchType<T>): Promise<Entity<T>[]>;
|
|
55
58
|
update<T extends object>(object: Entity<T>): Promise<Entity<T>>;
|
|
56
59
|
valuesFromDb<T extends object>(record: Entity<T>, type: Type<T>): Promise<Entity<T>>;
|
package/cjs/mysql.js
CHANGED
|
@@ -200,6 +200,42 @@ class Mysql extends storage_1.DataSource {
|
|
|
200
200
|
? this.update(object)
|
|
201
201
|
: this.insert(object);
|
|
202
202
|
}
|
|
203
|
+
async saveCollection(object, property, value) {
|
|
204
|
+
return depends.componentOf(object, property.endsWith('Ids') ? property.slice(0, -3) : property)
|
|
205
|
+
? this.saveComponents(object, property, value)
|
|
206
|
+
: this.saveLinks(object, property, value);
|
|
207
|
+
}
|
|
208
|
+
async saveComponents(object, property, components) {
|
|
209
|
+
// TODO
|
|
210
|
+
}
|
|
211
|
+
async saveLinks(object, property, links) {
|
|
212
|
+
property = property.endsWith('Ids') ? property.slice(0, -3) : property;
|
|
213
|
+
const connection = this.connection ?? await this.connect();
|
|
214
|
+
const objectTable = depends.storeOf(object);
|
|
215
|
+
const propertyType = new reflect_2.ReflectProperty(object, property).collectionType.elementType.type;
|
|
216
|
+
const propertyTable = depends.storeOf(propertyType);
|
|
217
|
+
const linkColumn = depends.columnOf(propertyTable) + '_id';
|
|
218
|
+
const linkTable = joinTableName(objectTable, propertyTable);
|
|
219
|
+
const objectColumn = depends.columnOf(objectTable) + '_id';
|
|
220
|
+
const objectId = object.id;
|
|
221
|
+
const stored = await this.readCollectionIds(object, property, propertyType);
|
|
222
|
+
const saved = [];
|
|
223
|
+
for (const link of links) {
|
|
224
|
+
const linkId = (typeof link === 'object')
|
|
225
|
+
? (this.isObjectConnected(link) ? link.id : (await this.save(link)).id)
|
|
226
|
+
: link;
|
|
227
|
+
saved.push(linkId);
|
|
228
|
+
if (stored.includes(linkId))
|
|
229
|
+
continue;
|
|
230
|
+
await connection.query('INSERT INTO `' + linkTable + '` SET ' + objectColumn + ' = ?, ' + linkColumn + ' = ?', [objectId, linkId]);
|
|
231
|
+
stored.push(linkId);
|
|
232
|
+
}
|
|
233
|
+
for (const storedId of stored) {
|
|
234
|
+
if (saved.includes(storedId))
|
|
235
|
+
continue;
|
|
236
|
+
await connection.query('DELETE FROM `' + linkTable + '` WHERE ' + objectColumn + ' = ? AND ' + linkColumn + ' = ?', [objectId, storedId]);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
203
239
|
async search(type, search = {}) {
|
|
204
240
|
const connection = this.connection ?? await this.connect();
|
|
205
241
|
const propertiesSql = this.propertiesToSqlSelect(type);
|
|
@@ -243,8 +279,13 @@ class Mysql extends storage_1.DataSource {
|
|
|
243
279
|
const record = {};
|
|
244
280
|
for (const property of Object.keys(object)) {
|
|
245
281
|
const value = await depends.applySaveTransformer(object, property, record);
|
|
246
|
-
if (value === depends.ignoreTransformedValue)
|
|
282
|
+
if (value === depends.ignoreTransformedValue) {
|
|
247
283
|
continue;
|
|
284
|
+
}
|
|
285
|
+
if (Array.isArray(value)) {
|
|
286
|
+
deferred.push((object) => this.saveCollection(object, property, value));
|
|
287
|
+
continue;
|
|
288
|
+
}
|
|
248
289
|
if ((0, class_type_1.isAnyFunction)(value)) {
|
|
249
290
|
deferred.push(value);
|
|
250
291
|
continue;
|
package/esm/mysql.d.ts
CHANGED
|
@@ -51,6 +51,9 @@ export declare class Mysql extends DataSource {
|
|
|
51
51
|
readCollectionIds<T extends object, PT extends object>(object: Entity<T>, property: KeyOf<T>, type?: Type<PT>): Promise<Identifier[]>;
|
|
52
52
|
readMultiple<T extends object>(type: Type<T>, ids: Identifier[]): Promise<Entity<T>[]>;
|
|
53
53
|
save<T extends object>(object: MayEntity<T>): Promise<Entity<T>>;
|
|
54
|
+
saveCollection<T extends object>(object: Entity<T>, property: KeyOf<T>, value: (Identifier | MayEntity)[]): Promise<void>;
|
|
55
|
+
saveComponents<T extends object>(object: Entity<T>, property: KeyOf<T>, components: (Identifier | MayEntity)[]): Promise<void>;
|
|
56
|
+
saveLinks<T extends object>(object: Entity<T>, property: KeyOf<T>, links: (Identifier | MayEntity)[]): Promise<void>;
|
|
54
57
|
search<T extends object>(type: Type<T>, search?: SearchType<T>): Promise<Entity<T>[]>;
|
|
55
58
|
update<T extends object>(object: Entity<T>): Promise<Entity<T>>;
|
|
56
59
|
valuesFromDb<T extends object>(record: Entity<T>, type: Type<T>): Promise<Entity<T>>;
|
package/esm/mysql.js
CHANGED
|
@@ -195,6 +195,42 @@ export class Mysql extends DataSource {
|
|
|
195
195
|
? this.update(object)
|
|
196
196
|
: this.insert(object);
|
|
197
197
|
}
|
|
198
|
+
async saveCollection(object, property, value) {
|
|
199
|
+
return depends.componentOf(object, property.endsWith('Ids') ? property.slice(0, -3) : property)
|
|
200
|
+
? this.saveComponents(object, property, value)
|
|
201
|
+
: this.saveLinks(object, property, value);
|
|
202
|
+
}
|
|
203
|
+
async saveComponents(object, property, components) {
|
|
204
|
+
// TODO
|
|
205
|
+
}
|
|
206
|
+
async saveLinks(object, property, links) {
|
|
207
|
+
property = property.endsWith('Ids') ? property.slice(0, -3) : property;
|
|
208
|
+
const connection = this.connection ?? await this.connect();
|
|
209
|
+
const objectTable = depends.storeOf(object);
|
|
210
|
+
const propertyType = new ReflectProperty(object, property).collectionType.elementType.type;
|
|
211
|
+
const propertyTable = depends.storeOf(propertyType);
|
|
212
|
+
const linkColumn = depends.columnOf(propertyTable) + '_id';
|
|
213
|
+
const linkTable = joinTableName(objectTable, propertyTable);
|
|
214
|
+
const objectColumn = depends.columnOf(objectTable) + '_id';
|
|
215
|
+
const objectId = object.id;
|
|
216
|
+
const stored = await this.readCollectionIds(object, property, propertyType);
|
|
217
|
+
const saved = [];
|
|
218
|
+
for (const link of links) {
|
|
219
|
+
const linkId = (typeof link === 'object')
|
|
220
|
+
? (this.isObjectConnected(link) ? link.id : (await this.save(link)).id)
|
|
221
|
+
: link;
|
|
222
|
+
saved.push(linkId);
|
|
223
|
+
if (stored.includes(linkId))
|
|
224
|
+
continue;
|
|
225
|
+
await connection.query('INSERT INTO `' + linkTable + '` SET ' + objectColumn + ' = ?, ' + linkColumn + ' = ?', [objectId, linkId]);
|
|
226
|
+
stored.push(linkId);
|
|
227
|
+
}
|
|
228
|
+
for (const storedId of stored) {
|
|
229
|
+
if (saved.includes(storedId))
|
|
230
|
+
continue;
|
|
231
|
+
await connection.query('DELETE FROM `' + linkTable + '` WHERE ' + objectColumn + ' = ? AND ' + linkColumn + ' = ?', [objectId, storedId]);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
198
234
|
async search(type, search = {}) {
|
|
199
235
|
const connection = this.connection ?? await this.connect();
|
|
200
236
|
const propertiesSql = this.propertiesToSqlSelect(type);
|
|
@@ -238,8 +274,13 @@ export class Mysql extends DataSource {
|
|
|
238
274
|
const record = {};
|
|
239
275
|
for (const property of Object.keys(object)) {
|
|
240
276
|
const value = await depends.applySaveTransformer(object, property, record);
|
|
241
|
-
if (value === depends.ignoreTransformedValue)
|
|
277
|
+
if (value === depends.ignoreTransformedValue) {
|
|
242
278
|
continue;
|
|
279
|
+
}
|
|
280
|
+
if (Array.isArray(value)) {
|
|
281
|
+
deferred.push((object) => this.saveCollection(object, property, value));
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
243
284
|
if (isAnyFunction(value)) {
|
|
244
285
|
deferred.push(value);
|
|
245
286
|
continue;
|
package/package.json
CHANGED