@itrocks/mysql 0.0.14 → 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 +4 -0
- package/cjs/mysql.js +72 -9
- package/esm/mysql.d.ts +4 -0
- package/esm/mysql.js +71 -9
- package/package.json +1 -1
package/cjs/mysql.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ interface Dependencies<QF extends object = object> {
|
|
|
19
19
|
queryFunctionCall: (value: QF) => [any, string];
|
|
20
20
|
storeOf: <T extends object>(target: ObjectOrType<T>) => string | false;
|
|
21
21
|
}
|
|
22
|
+
export declare function joinTableName(object1: string | ObjectOrType, object2: string | ObjectOrType): string;
|
|
22
23
|
export declare function mysqlDependsOn<QF extends object = object>(dependencies: Partial<Dependencies<QF>>): void;
|
|
23
24
|
export declare class Mysql extends DataSource {
|
|
24
25
|
config: {
|
|
@@ -50,6 +51,9 @@ export declare class Mysql extends DataSource {
|
|
|
50
51
|
readCollectionIds<T extends object, PT extends object>(object: Entity<T>, property: KeyOf<T>, type?: Type<PT>): Promise<Identifier[]>;
|
|
51
52
|
readMultiple<T extends object>(type: Type<T>, ids: Identifier[]): Promise<Entity<T>[]>;
|
|
52
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>;
|
|
53
57
|
search<T extends object>(type: Type<T>, search?: SearchType<T>): Promise<Entity<T>[]>;
|
|
54
58
|
update<T extends object>(object: Entity<T>): Promise<Entity<T>>;
|
|
55
59
|
valuesFromDb<T extends object>(record: Entity<T>, type: Type<T>): Promise<Entity<T>>;
|
package/cjs/mysql.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Mysql = exports.DEBUG = void 0;
|
|
4
|
+
exports.joinTableName = joinTableName;
|
|
4
5
|
exports.mysqlDependsOn = mysqlDependsOn;
|
|
5
6
|
const class_type_1 = require("@itrocks/class-type");
|
|
6
7
|
const class_type_2 = require("@itrocks/class-type");
|
|
@@ -22,6 +23,13 @@ const depends = {
|
|
|
22
23
|
queryFunctionCall: () => [undefined, ' = ?'],
|
|
23
24
|
storeOf: target => (0, class_type_3.typeOf)(target).name.toLowerCase()
|
|
24
25
|
};
|
|
26
|
+
function joinTableName(object1, object2) {
|
|
27
|
+
if (typeof object1 !== 'string')
|
|
28
|
+
object1 = depends.storeOf(object1);
|
|
29
|
+
if (typeof object2 !== 'string')
|
|
30
|
+
object2 = depends.storeOf(object2);
|
|
31
|
+
return [object1, object2].sort().join('_');
|
|
32
|
+
}
|
|
25
33
|
function mysqlDependsOn(dependencies) {
|
|
26
34
|
Object.assign(depends, dependencies);
|
|
27
35
|
}
|
|
@@ -53,7 +61,10 @@ class Mysql extends storage_1.DataSource {
|
|
|
53
61
|
const connection = this.connection ?? await this.connect();
|
|
54
62
|
const objectTable = depends.storeOf(object);
|
|
55
63
|
const propertyTable = depends.storeOf(new reflect_2.ReflectProperty(object, property).collectionType.elementType.type);
|
|
56
|
-
|
|
64
|
+
if (!objectTable || !propertyTable) {
|
|
65
|
+
throw 'Collection objects are not stored';
|
|
66
|
+
}
|
|
67
|
+
const joinTable = joinTableName(objectTable, propertyTable);
|
|
57
68
|
const query = 'DELETE FROM `' + joinTable + '` WHERE ' + objectTable + '_id = ? AND ' + propertyTable + '_id = ?';
|
|
58
69
|
const values = [object.id, id];
|
|
59
70
|
if (exports.DEBUG)
|
|
@@ -79,7 +90,10 @@ class Mysql extends storage_1.DataSource {
|
|
|
79
90
|
const connection = this.connection ?? await this.connect();
|
|
80
91
|
const objectTable = depends.storeOf(object);
|
|
81
92
|
const propertyTable = depends.storeOf(new reflect_2.ReflectProperty(object, property).collectionType.elementType.type);
|
|
82
|
-
|
|
93
|
+
if (!objectTable || !propertyTable) {
|
|
94
|
+
throw 'Collection objects are not stored';
|
|
95
|
+
}
|
|
96
|
+
const joinTable = joinTableName(objectTable, propertyTable);
|
|
83
97
|
const query = 'INSERT INTO `' + joinTable + '` SET ' + objectTable + '_id = ?, ' + propertyTable + '_id = ?';
|
|
84
98
|
const values = [object.id, id];
|
|
85
99
|
if (exports.DEBUG)
|
|
@@ -134,15 +148,18 @@ class Mysql extends storage_1.DataSource {
|
|
|
134
148
|
const connection = this.connection ?? await this.connect();
|
|
135
149
|
const propertiesSql = this.propertiesToSqlSelect(type);
|
|
136
150
|
const objectTable = depends.storeOf(object);
|
|
137
|
-
const
|
|
151
|
+
const propertyTable = depends.storeOf(type);
|
|
152
|
+
if (!objectTable || !propertyTable) {
|
|
153
|
+
throw 'Collection objects are not stored';
|
|
154
|
+
}
|
|
138
155
|
let query;
|
|
139
156
|
if (depends.componentOf(object, property)) {
|
|
140
|
-
query = 'SELECT ' + propertiesSql + ' FROM `' +
|
|
157
|
+
query = 'SELECT ' + propertiesSql + ' FROM `' + propertyTable + '` WHERE ' + objectTable + '_id = ?';
|
|
141
158
|
}
|
|
142
159
|
else {
|
|
143
|
-
const joinTable =
|
|
144
|
-
query = 'SELECT `' +
|
|
145
|
-
+ ' INNER JOIN `' + joinTable + '` ON `' + joinTable + '`.' +
|
|
160
|
+
const joinTable = joinTableName(objectTable, propertyTable);
|
|
161
|
+
query = 'SELECT `' + propertyTable + '`.' + propertiesSql + ' FROM `' + propertyTable + '`'
|
|
162
|
+
+ ' INNER JOIN `' + joinTable + '` ON `' + joinTable + '`.' + propertyTable + '_id = `' + propertyTable + '`.id'
|
|
146
163
|
+ ' WHERE `' + joinTable + '`.' + objectTable + '_id = ?';
|
|
147
164
|
}
|
|
148
165
|
const rows = await connection.query(query, [object.id]);
|
|
@@ -152,12 +169,15 @@ class Mysql extends storage_1.DataSource {
|
|
|
152
169
|
const connection = this.connection ?? await this.connect();
|
|
153
170
|
const objectTable = depends.storeOf(object);
|
|
154
171
|
const propertyTable = depends.storeOf(type);
|
|
172
|
+
if (!objectTable || !propertyTable) {
|
|
173
|
+
throw 'Collection objects are not stored';
|
|
174
|
+
}
|
|
155
175
|
let query;
|
|
156
176
|
if (depends.componentOf(object, property)) {
|
|
157
177
|
query = 'SELECT id FROM `' + propertyTable + '` WHERE ' + objectTable + '_id = ?';
|
|
158
178
|
}
|
|
159
179
|
else {
|
|
160
|
-
const joinTable =
|
|
180
|
+
const joinTable = joinTableName(objectTable, propertyTable);
|
|
161
181
|
query = 'SELECT ' + propertyTable + '_id id FROM `' + joinTable + '`'
|
|
162
182
|
+ ' WHERE `' + joinTable + '`.' + objectTable + '_id = ?';
|
|
163
183
|
}
|
|
@@ -180,6 +200,44 @@ class Mysql extends storage_1.DataSource {
|
|
|
180
200
|
? this.update(object)
|
|
181
201
|
: this.insert(object);
|
|
182
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
|
+
}
|
|
183
241
|
async search(type, search = {}) {
|
|
184
242
|
const connection = this.connection ?? await this.connect();
|
|
185
243
|
const propertiesSql = this.propertiesToSqlSelect(type);
|
|
@@ -223,8 +281,13 @@ class Mysql extends storage_1.DataSource {
|
|
|
223
281
|
const record = {};
|
|
224
282
|
for (const property of Object.keys(object)) {
|
|
225
283
|
const value = await depends.applySaveTransformer(object, property, record);
|
|
226
|
-
if (value === depends.ignoreTransformedValue)
|
|
284
|
+
if (value === depends.ignoreTransformedValue) {
|
|
285
|
+
continue;
|
|
286
|
+
}
|
|
287
|
+
if (Array.isArray(value)) {
|
|
288
|
+
deferred.push((object) => this.saveCollection(object, property, value));
|
|
227
289
|
continue;
|
|
290
|
+
}
|
|
228
291
|
if ((0, class_type_1.isAnyFunction)(value)) {
|
|
229
292
|
deferred.push(value);
|
|
230
293
|
continue;
|
package/esm/mysql.d.ts
CHANGED
|
@@ -19,6 +19,7 @@ interface Dependencies<QF extends object = object> {
|
|
|
19
19
|
queryFunctionCall: (value: QF) => [any, string];
|
|
20
20
|
storeOf: <T extends object>(target: ObjectOrType<T>) => string | false;
|
|
21
21
|
}
|
|
22
|
+
export declare function joinTableName(object1: string | ObjectOrType, object2: string | ObjectOrType): string;
|
|
22
23
|
export declare function mysqlDependsOn<QF extends object = object>(dependencies: Partial<Dependencies<QF>>): void;
|
|
23
24
|
export declare class Mysql extends DataSource {
|
|
24
25
|
config: {
|
|
@@ -50,6 +51,9 @@ export declare class Mysql extends DataSource {
|
|
|
50
51
|
readCollectionIds<T extends object, PT extends object>(object: Entity<T>, property: KeyOf<T>, type?: Type<PT>): Promise<Identifier[]>;
|
|
51
52
|
readMultiple<T extends object>(type: Type<T>, ids: Identifier[]): Promise<Entity<T>[]>;
|
|
52
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>;
|
|
53
57
|
search<T extends object>(type: Type<T>, search?: SearchType<T>): Promise<Entity<T>[]>;
|
|
54
58
|
update<T extends object>(object: Entity<T>): Promise<Entity<T>>;
|
|
55
59
|
valuesFromDb<T extends object>(record: Entity<T>, type: Type<T>): Promise<Entity<T>>;
|
package/esm/mysql.js
CHANGED
|
@@ -18,6 +18,13 @@ const depends = {
|
|
|
18
18
|
queryFunctionCall: () => [undefined, ' = ?'],
|
|
19
19
|
storeOf: target => typeOf(target).name.toLowerCase()
|
|
20
20
|
};
|
|
21
|
+
export function joinTableName(object1, object2) {
|
|
22
|
+
if (typeof object1 !== 'string')
|
|
23
|
+
object1 = depends.storeOf(object1);
|
|
24
|
+
if (typeof object2 !== 'string')
|
|
25
|
+
object2 = depends.storeOf(object2);
|
|
26
|
+
return [object1, object2].sort().join('_');
|
|
27
|
+
}
|
|
21
28
|
export function mysqlDependsOn(dependencies) {
|
|
22
29
|
Object.assign(depends, dependencies);
|
|
23
30
|
}
|
|
@@ -49,7 +56,10 @@ export class Mysql extends DataSource {
|
|
|
49
56
|
const connection = this.connection ?? await this.connect();
|
|
50
57
|
const objectTable = depends.storeOf(object);
|
|
51
58
|
const propertyTable = depends.storeOf(new ReflectProperty(object, property).collectionType.elementType.type);
|
|
52
|
-
|
|
59
|
+
if (!objectTable || !propertyTable) {
|
|
60
|
+
throw 'Collection objects are not stored';
|
|
61
|
+
}
|
|
62
|
+
const joinTable = joinTableName(objectTable, propertyTable);
|
|
53
63
|
const query = 'DELETE FROM `' + joinTable + '` WHERE ' + objectTable + '_id = ? AND ' + propertyTable + '_id = ?';
|
|
54
64
|
const values = [object.id, id];
|
|
55
65
|
if (DEBUG)
|
|
@@ -75,7 +85,10 @@ export class Mysql extends DataSource {
|
|
|
75
85
|
const connection = this.connection ?? await this.connect();
|
|
76
86
|
const objectTable = depends.storeOf(object);
|
|
77
87
|
const propertyTable = depends.storeOf(new ReflectProperty(object, property).collectionType.elementType.type);
|
|
78
|
-
|
|
88
|
+
if (!objectTable || !propertyTable) {
|
|
89
|
+
throw 'Collection objects are not stored';
|
|
90
|
+
}
|
|
91
|
+
const joinTable = joinTableName(objectTable, propertyTable);
|
|
79
92
|
const query = 'INSERT INTO `' + joinTable + '` SET ' + objectTable + '_id = ?, ' + propertyTable + '_id = ?';
|
|
80
93
|
const values = [object.id, id];
|
|
81
94
|
if (DEBUG)
|
|
@@ -130,15 +143,18 @@ export class Mysql extends DataSource {
|
|
|
130
143
|
const connection = this.connection ?? await this.connect();
|
|
131
144
|
const propertiesSql = this.propertiesToSqlSelect(type);
|
|
132
145
|
const objectTable = depends.storeOf(object);
|
|
133
|
-
const
|
|
146
|
+
const propertyTable = depends.storeOf(type);
|
|
147
|
+
if (!objectTable || !propertyTable) {
|
|
148
|
+
throw 'Collection objects are not stored';
|
|
149
|
+
}
|
|
134
150
|
let query;
|
|
135
151
|
if (depends.componentOf(object, property)) {
|
|
136
|
-
query = 'SELECT ' + propertiesSql + ' FROM `' +
|
|
152
|
+
query = 'SELECT ' + propertiesSql + ' FROM `' + propertyTable + '` WHERE ' + objectTable + '_id = ?';
|
|
137
153
|
}
|
|
138
154
|
else {
|
|
139
|
-
const joinTable =
|
|
140
|
-
query = 'SELECT `' +
|
|
141
|
-
+ ' INNER JOIN `' + joinTable + '` ON `' + joinTable + '`.' +
|
|
155
|
+
const joinTable = joinTableName(objectTable, propertyTable);
|
|
156
|
+
query = 'SELECT `' + propertyTable + '`.' + propertiesSql + ' FROM `' + propertyTable + '`'
|
|
157
|
+
+ ' INNER JOIN `' + joinTable + '` ON `' + joinTable + '`.' + propertyTable + '_id = `' + propertyTable + '`.id'
|
|
142
158
|
+ ' WHERE `' + joinTable + '`.' + objectTable + '_id = ?';
|
|
143
159
|
}
|
|
144
160
|
const rows = await connection.query(query, [object.id]);
|
|
@@ -148,12 +164,15 @@ export class Mysql extends DataSource {
|
|
|
148
164
|
const connection = this.connection ?? await this.connect();
|
|
149
165
|
const objectTable = depends.storeOf(object);
|
|
150
166
|
const propertyTable = depends.storeOf(type);
|
|
167
|
+
if (!objectTable || !propertyTable) {
|
|
168
|
+
throw 'Collection objects are not stored';
|
|
169
|
+
}
|
|
151
170
|
let query;
|
|
152
171
|
if (depends.componentOf(object, property)) {
|
|
153
172
|
query = 'SELECT id FROM `' + propertyTable + '` WHERE ' + objectTable + '_id = ?';
|
|
154
173
|
}
|
|
155
174
|
else {
|
|
156
|
-
const joinTable =
|
|
175
|
+
const joinTable = joinTableName(objectTable, propertyTable);
|
|
157
176
|
query = 'SELECT ' + propertyTable + '_id id FROM `' + joinTable + '`'
|
|
158
177
|
+ ' WHERE `' + joinTable + '`.' + objectTable + '_id = ?';
|
|
159
178
|
}
|
|
@@ -176,6 +195,44 @@ export class Mysql extends DataSource {
|
|
|
176
195
|
? this.update(object)
|
|
177
196
|
: this.insert(object);
|
|
178
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
|
+
}
|
|
179
236
|
async search(type, search = {}) {
|
|
180
237
|
const connection = this.connection ?? await this.connect();
|
|
181
238
|
const propertiesSql = this.propertiesToSqlSelect(type);
|
|
@@ -219,8 +276,13 @@ export class Mysql extends DataSource {
|
|
|
219
276
|
const record = {};
|
|
220
277
|
for (const property of Object.keys(object)) {
|
|
221
278
|
const value = await depends.applySaveTransformer(object, property, record);
|
|
222
|
-
if (value === depends.ignoreTransformedValue)
|
|
279
|
+
if (value === depends.ignoreTransformedValue) {
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
if (Array.isArray(value)) {
|
|
283
|
+
deferred.push((object) => this.saveCollection(object, property, value));
|
|
223
284
|
continue;
|
|
285
|
+
}
|
|
224
286
|
if (isAnyFunction(value)) {
|
|
225
287
|
deferred.push(value);
|
|
226
288
|
continue;
|
package/package.json
CHANGED