@twin.org/entity-storage-connector-mysql 0.0.1-next.18 → 0.0.1-next.20
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/dist/cjs/index.cjs
CHANGED
|
@@ -47,7 +47,7 @@ class MySqlEntityStorageConnector {
|
|
|
47
47
|
core.Guards.stringValue(this.CLASS_NAME, "options.config.user", options.config.user);
|
|
48
48
|
core.Guards.stringValue(this.CLASS_NAME, "options.config.password", options.config.password);
|
|
49
49
|
core.Guards.stringValue(this.CLASS_NAME, "options.config.database", options.config.database);
|
|
50
|
-
core.Guards.stringValue(this.CLASS_NAME, "options.config.
|
|
50
|
+
core.Guards.stringValue(this.CLASS_NAME, "options.config.tableName", options.config.tableName);
|
|
51
51
|
this._entitySchema = entity.EntitySchemaFactory.get(options.entitySchema);
|
|
52
52
|
this._config = options.config;
|
|
53
53
|
}
|
|
@@ -80,20 +80,18 @@ class MySqlEntityStorageConnector {
|
|
|
80
80
|
database: this._config.database
|
|
81
81
|
}
|
|
82
82
|
});
|
|
83
|
-
await dbConnection.query(`CREATE TABLE IF NOT EXISTS \`${this._config.database}\`.\`${this._config.
|
|
83
|
+
await dbConnection.query(`CREATE TABLE IF NOT EXISTS \`${this._config.database}\`.\`${this._config.tableName}\` (${this.mapMySqlProperties(this._entitySchema)})`);
|
|
84
84
|
await nodeLogging?.log({
|
|
85
85
|
level: "info",
|
|
86
86
|
source: this.CLASS_NAME,
|
|
87
87
|
ts: Date.now(),
|
|
88
88
|
message: "tableExists",
|
|
89
89
|
data: {
|
|
90
|
-
table: this._config.
|
|
90
|
+
table: this._config.tableName
|
|
91
91
|
}
|
|
92
92
|
});
|
|
93
93
|
}
|
|
94
94
|
catch (error) {
|
|
95
|
-
// eslint-disable-next-line no-console
|
|
96
|
-
console.log("error", error);
|
|
97
95
|
const errors = error instanceof AggregateError ? error.errors : [error];
|
|
98
96
|
for (const err of errors) {
|
|
99
97
|
await nodeLogging?.log({
|
|
@@ -145,7 +143,7 @@ class MySqlEntityStorageConnector {
|
|
|
145
143
|
values.push(condition.value);
|
|
146
144
|
}
|
|
147
145
|
}
|
|
148
|
-
const query = `SELECT * FROM \`${this._config.database}\`.\`${this._config.
|
|
146
|
+
const query = `SELECT * FROM \`${this._config.database}\`.\`${this._config.tableName}\` WHERE ${whereClauses.join(" AND ")} LIMIT 1`;
|
|
149
147
|
const [rows] = await dbConnection.query(query, values);
|
|
150
148
|
if (Array.isArray(rows) && rows.length === 1) {
|
|
151
149
|
return rows[0];
|
|
@@ -182,7 +180,7 @@ class MySqlEntityStorageConnector {
|
|
|
182
180
|
const values = Object.values(entity);
|
|
183
181
|
const placeholders = values.map(() => "?").join(", ");
|
|
184
182
|
const dbConnection = await this.createConnection();
|
|
185
|
-
await dbConnection.query(`INSERT INTO \`${this._config.database}\`.\`${this._config.
|
|
183
|
+
await dbConnection.query(`INSERT INTO \`${this._config.database}\`.\`${this._config.tableName}\` (${columns}) VALUES (${placeholders}) ON DUPLICATE KEY UPDATE ${columns
|
|
186
184
|
.split(", ")
|
|
187
185
|
.map(col => `${col} = VALUES(${col})`)
|
|
188
186
|
.join(", ")};`, values.map(value => (typeof value === "object" ? JSON.stringify(value) : value)));
|
|
@@ -213,7 +211,7 @@ class MySqlEntityStorageConnector {
|
|
|
213
211
|
return `\`${String(condition.property)}\` = ?`;
|
|
214
212
|
});
|
|
215
213
|
}
|
|
216
|
-
const query = `DELETE FROM \`${this._config.database}\`.\`${this._config.
|
|
214
|
+
const query = `DELETE FROM \`${this._config.database}\`.\`${this._config.tableName}\` WHERE \`id\` = ?${whereClauses.length > 0 ? ` AND ${whereClauses.join(" AND ")}` : ""}`;
|
|
217
215
|
await dbConnection.query(query, values);
|
|
218
216
|
}
|
|
219
217
|
}
|
|
@@ -251,7 +249,7 @@ class MySqlEntityStorageConnector {
|
|
|
251
249
|
if (conditions) {
|
|
252
250
|
this.buildQueryParameters("", conditions, whereClauses, values);
|
|
253
251
|
}
|
|
254
|
-
const query = `SELECT ${properties ? properties.map(p => `\`${String(p)}\``).join(", ") : "*"} FROM \`${this._config.database}\`.\`${this._config.
|
|
252
|
+
const query = `SELECT ${properties ? properties.map(p => `\`${String(p)}\``).join(", ") : "*"} FROM \`${this._config.database}\`.\`${this._config.tableName}\` WHERE ${whereClauses.length > 0 ? whereClauses.join(" AND ") : "1"} ${orderByClause} LIMIT ${returnSize} OFFSET ${cursor ? Number(cursor) : 0}`;
|
|
255
253
|
const dbConnection = await this.createConnection();
|
|
256
254
|
const [rows] = (await dbConnection?.query(query, values)) ?? [];
|
|
257
255
|
return {
|
|
@@ -272,7 +270,7 @@ class MySqlEntityStorageConnector {
|
|
|
272
270
|
async tableDrop() {
|
|
273
271
|
try {
|
|
274
272
|
const dbConnection = await this.createConnection();
|
|
275
|
-
await dbConnection?.query(`DROP TABLE \`${this._config.database}\`.\`${this._config.
|
|
273
|
+
await dbConnection?.query(`DROP TABLE \`${this._config.database}\`.\`${this._config.tableName}\`;`);
|
|
276
274
|
}
|
|
277
275
|
catch {
|
|
278
276
|
// Ignore errors
|
|
@@ -280,7 +278,7 @@ class MySqlEntityStorageConnector {
|
|
|
280
278
|
}
|
|
281
279
|
/**
|
|
282
280
|
* Create a new DB connection.
|
|
283
|
-
* @returns The
|
|
281
|
+
* @returns The MySql connection.
|
|
284
282
|
* @internal
|
|
285
283
|
*/
|
|
286
284
|
async createConnection() {
|
|
@@ -293,7 +291,7 @@ class MySqlEntityStorageConnector {
|
|
|
293
291
|
}
|
|
294
292
|
/**
|
|
295
293
|
* Create a new DB connection configuration.
|
|
296
|
-
* @returns The
|
|
294
|
+
* @returns The MySql connection configuration.
|
|
297
295
|
* @internal
|
|
298
296
|
*/
|
|
299
297
|
createConnectionConfig() {
|
package/dist/esm/index.mjs
CHANGED
|
@@ -45,7 +45,7 @@ class MySqlEntityStorageConnector {
|
|
|
45
45
|
Guards.stringValue(this.CLASS_NAME, "options.config.user", options.config.user);
|
|
46
46
|
Guards.stringValue(this.CLASS_NAME, "options.config.password", options.config.password);
|
|
47
47
|
Guards.stringValue(this.CLASS_NAME, "options.config.database", options.config.database);
|
|
48
|
-
Guards.stringValue(this.CLASS_NAME, "options.config.
|
|
48
|
+
Guards.stringValue(this.CLASS_NAME, "options.config.tableName", options.config.tableName);
|
|
49
49
|
this._entitySchema = EntitySchemaFactory.get(options.entitySchema);
|
|
50
50
|
this._config = options.config;
|
|
51
51
|
}
|
|
@@ -78,20 +78,18 @@ class MySqlEntityStorageConnector {
|
|
|
78
78
|
database: this._config.database
|
|
79
79
|
}
|
|
80
80
|
});
|
|
81
|
-
await dbConnection.query(`CREATE TABLE IF NOT EXISTS \`${this._config.database}\`.\`${this._config.
|
|
81
|
+
await dbConnection.query(`CREATE TABLE IF NOT EXISTS \`${this._config.database}\`.\`${this._config.tableName}\` (${this.mapMySqlProperties(this._entitySchema)})`);
|
|
82
82
|
await nodeLogging?.log({
|
|
83
83
|
level: "info",
|
|
84
84
|
source: this.CLASS_NAME,
|
|
85
85
|
ts: Date.now(),
|
|
86
86
|
message: "tableExists",
|
|
87
87
|
data: {
|
|
88
|
-
table: this._config.
|
|
88
|
+
table: this._config.tableName
|
|
89
89
|
}
|
|
90
90
|
});
|
|
91
91
|
}
|
|
92
92
|
catch (error) {
|
|
93
|
-
// eslint-disable-next-line no-console
|
|
94
|
-
console.log("error", error);
|
|
95
93
|
const errors = error instanceof AggregateError ? error.errors : [error];
|
|
96
94
|
for (const err of errors) {
|
|
97
95
|
await nodeLogging?.log({
|
|
@@ -143,7 +141,7 @@ class MySqlEntityStorageConnector {
|
|
|
143
141
|
values.push(condition.value);
|
|
144
142
|
}
|
|
145
143
|
}
|
|
146
|
-
const query = `SELECT * FROM \`${this._config.database}\`.\`${this._config.
|
|
144
|
+
const query = `SELECT * FROM \`${this._config.database}\`.\`${this._config.tableName}\` WHERE ${whereClauses.join(" AND ")} LIMIT 1`;
|
|
147
145
|
const [rows] = await dbConnection.query(query, values);
|
|
148
146
|
if (Array.isArray(rows) && rows.length === 1) {
|
|
149
147
|
return rows[0];
|
|
@@ -180,7 +178,7 @@ class MySqlEntityStorageConnector {
|
|
|
180
178
|
const values = Object.values(entity);
|
|
181
179
|
const placeholders = values.map(() => "?").join(", ");
|
|
182
180
|
const dbConnection = await this.createConnection();
|
|
183
|
-
await dbConnection.query(`INSERT INTO \`${this._config.database}\`.\`${this._config.
|
|
181
|
+
await dbConnection.query(`INSERT INTO \`${this._config.database}\`.\`${this._config.tableName}\` (${columns}) VALUES (${placeholders}) ON DUPLICATE KEY UPDATE ${columns
|
|
184
182
|
.split(", ")
|
|
185
183
|
.map(col => `${col} = VALUES(${col})`)
|
|
186
184
|
.join(", ")};`, values.map(value => (typeof value === "object" ? JSON.stringify(value) : value)));
|
|
@@ -211,7 +209,7 @@ class MySqlEntityStorageConnector {
|
|
|
211
209
|
return `\`${String(condition.property)}\` = ?`;
|
|
212
210
|
});
|
|
213
211
|
}
|
|
214
|
-
const query = `DELETE FROM \`${this._config.database}\`.\`${this._config.
|
|
212
|
+
const query = `DELETE FROM \`${this._config.database}\`.\`${this._config.tableName}\` WHERE \`id\` = ?${whereClauses.length > 0 ? ` AND ${whereClauses.join(" AND ")}` : ""}`;
|
|
215
213
|
await dbConnection.query(query, values);
|
|
216
214
|
}
|
|
217
215
|
}
|
|
@@ -249,7 +247,7 @@ class MySqlEntityStorageConnector {
|
|
|
249
247
|
if (conditions) {
|
|
250
248
|
this.buildQueryParameters("", conditions, whereClauses, values);
|
|
251
249
|
}
|
|
252
|
-
const query = `SELECT ${properties ? properties.map(p => `\`${String(p)}\``).join(", ") : "*"} FROM \`${this._config.database}\`.\`${this._config.
|
|
250
|
+
const query = `SELECT ${properties ? properties.map(p => `\`${String(p)}\``).join(", ") : "*"} FROM \`${this._config.database}\`.\`${this._config.tableName}\` WHERE ${whereClauses.length > 0 ? whereClauses.join(" AND ") : "1"} ${orderByClause} LIMIT ${returnSize} OFFSET ${cursor ? Number(cursor) : 0}`;
|
|
253
251
|
const dbConnection = await this.createConnection();
|
|
254
252
|
const [rows] = (await dbConnection?.query(query, values)) ?? [];
|
|
255
253
|
return {
|
|
@@ -270,7 +268,7 @@ class MySqlEntityStorageConnector {
|
|
|
270
268
|
async tableDrop() {
|
|
271
269
|
try {
|
|
272
270
|
const dbConnection = await this.createConnection();
|
|
273
|
-
await dbConnection?.query(`DROP TABLE \`${this._config.database}\`.\`${this._config.
|
|
271
|
+
await dbConnection?.query(`DROP TABLE \`${this._config.database}\`.\`${this._config.tableName}\`;`);
|
|
274
272
|
}
|
|
275
273
|
catch {
|
|
276
274
|
// Ignore errors
|
|
@@ -278,7 +276,7 @@ class MySqlEntityStorageConnector {
|
|
|
278
276
|
}
|
|
279
277
|
/**
|
|
280
278
|
* Create a new DB connection.
|
|
281
|
-
* @returns The
|
|
279
|
+
* @returns The MySql connection.
|
|
282
280
|
* @internal
|
|
283
281
|
*/
|
|
284
282
|
async createConnection() {
|
|
@@ -291,7 +289,7 @@ class MySqlEntityStorageConnector {
|
|
|
291
289
|
}
|
|
292
290
|
/**
|
|
293
291
|
* Create a new DB connection configuration.
|
|
294
|
-
* @returns The
|
|
292
|
+
* @returns The MySql connection configuration.
|
|
295
293
|
* @internal
|
|
296
294
|
*/
|
|
297
295
|
createConnectionConfig() {
|
package/docs/changelog.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@twin.org/entity-storage-connector-mysql",
|
|
3
|
-
"version": "0.0.1-next.
|
|
3
|
+
"version": "0.0.1-next.20",
|
|
4
4
|
"description": "Entity Storage connector implementation using MySQL storage",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"dependencies": {
|
|
17
17
|
"@twin.org/core": "next",
|
|
18
18
|
"@twin.org/entity": "next",
|
|
19
|
-
"@twin.org/entity-storage-models": "0.0.1-next.
|
|
19
|
+
"@twin.org/entity-storage-models": "0.0.1-next.20",
|
|
20
20
|
"@twin.org/logging-models": "next",
|
|
21
21
|
"@twin.org/nameof": "next",
|
|
22
22
|
"mysql2": "^3.12.0"
|