@itrocks/mysql 0.0.14 → 0.0.15
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 +1 -0
- package/cjs/mysql.js +28 -8
- package/esm/mysql.d.ts +1 -0
- package/esm/mysql.js +27 -8
- 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: {
|
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
|
}
|
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: {
|
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
|
}
|
package/package.json
CHANGED