@itrocks/mysql 0.0.15 → 0.0.16
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 +44 -1
- package/esm/mysql.d.ts +3 -0
- package/esm/mysql.js +44 -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,44 @@ 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
|
+
const isLinkBigInt = typeof links[0] === 'bigint';
|
|
224
|
+
const isStoredBigInt = typeof stored[0] === 'bigint';
|
|
225
|
+
const isSameType = isLinkBigInt === isStoredBigInt;
|
|
226
|
+
for (let link of links) {
|
|
227
|
+
const linkId = (typeof link === 'object')
|
|
228
|
+
? (link = this.isObjectConnected(link) ? link.id : (await this.save(link)).id)
|
|
229
|
+
: link;
|
|
230
|
+
if (!stored.includes(isSameType ? linkId : (isStoredBigInt ? BigInt(linkId) : Number(linkId)))) {
|
|
231
|
+
await connection.query('INSERT INTO `' + linkTable + '` SET ' + objectColumn + ' = ?, ' + linkColumn + ' = ?', [objectId, linkId]);
|
|
232
|
+
}
|
|
233
|
+
saved.push(linkId);
|
|
234
|
+
}
|
|
235
|
+
for (let storedId of stored) {
|
|
236
|
+
if (!saved.includes(isSameType ? storedId : (isLinkBigInt ? BigInt(storedId) : Number(storedId)))) {
|
|
237
|
+
await connection.query('DELETE FROM `' + linkTable + '` WHERE ' + objectColumn + ' = ? AND ' + linkColumn + ' = ?', [objectId, storedId]);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
203
241
|
async search(type, search = {}) {
|
|
204
242
|
const connection = this.connection ?? await this.connect();
|
|
205
243
|
const propertiesSql = this.propertiesToSqlSelect(type);
|
|
@@ -243,8 +281,13 @@ class Mysql extends storage_1.DataSource {
|
|
|
243
281
|
const record = {};
|
|
244
282
|
for (const property of Object.keys(object)) {
|
|
245
283
|
const value = await depends.applySaveTransformer(object, property, record);
|
|
246
|
-
if (value === depends.ignoreTransformedValue)
|
|
284
|
+
if (value === depends.ignoreTransformedValue) {
|
|
247
285
|
continue;
|
|
286
|
+
}
|
|
287
|
+
if (Array.isArray(value)) {
|
|
288
|
+
deferred.push((object) => this.saveCollection(object, property, value));
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
248
291
|
if ((0, class_type_1.isAnyFunction)(value)) {
|
|
249
292
|
deferred.push(value);
|
|
250
293
|
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,44 @@ 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
|
+
const isLinkBigInt = typeof links[0] === 'bigint';
|
|
219
|
+
const isStoredBigInt = typeof stored[0] === 'bigint';
|
|
220
|
+
const isSameType = isLinkBigInt === isStoredBigInt;
|
|
221
|
+
for (let link of links) {
|
|
222
|
+
const linkId = (typeof link === 'object')
|
|
223
|
+
? (link = this.isObjectConnected(link) ? link.id : (await this.save(link)).id)
|
|
224
|
+
: link;
|
|
225
|
+
if (!stored.includes(isSameType ? linkId : (isStoredBigInt ? BigInt(linkId) : Number(linkId)))) {
|
|
226
|
+
await connection.query('INSERT INTO `' + linkTable + '` SET ' + objectColumn + ' = ?, ' + linkColumn + ' = ?', [objectId, linkId]);
|
|
227
|
+
}
|
|
228
|
+
saved.push(linkId);
|
|
229
|
+
}
|
|
230
|
+
for (let storedId of stored) {
|
|
231
|
+
if (!saved.includes(isSameType ? storedId : (isLinkBigInt ? BigInt(storedId) : Number(storedId)))) {
|
|
232
|
+
await connection.query('DELETE FROM `' + linkTable + '` WHERE ' + objectColumn + ' = ? AND ' + linkColumn + ' = ?', [objectId, storedId]);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
198
236
|
async search(type, search = {}) {
|
|
199
237
|
const connection = this.connection ?? await this.connect();
|
|
200
238
|
const propertiesSql = this.propertiesToSqlSelect(type);
|
|
@@ -238,8 +276,13 @@ export class Mysql extends DataSource {
|
|
|
238
276
|
const record = {};
|
|
239
277
|
for (const property of Object.keys(object)) {
|
|
240
278
|
const value = await depends.applySaveTransformer(object, property, record);
|
|
241
|
-
if (value === depends.ignoreTransformedValue)
|
|
279
|
+
if (value === depends.ignoreTransformedValue) {
|
|
242
280
|
continue;
|
|
281
|
+
}
|
|
282
|
+
if (Array.isArray(value)) {
|
|
283
|
+
deferred.push((object) => this.saveCollection(object, property, value));
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
243
286
|
if (isAnyFunction(value)) {
|
|
244
287
|
deferred.push(value);
|
|
245
288
|
continue;
|
package/package.json
CHANGED