@twin.org/entity-storage-connector-mysql 0.0.1-next.21 → 0.0.1-next.22

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.
@@ -134,7 +134,13 @@ class MySqlEntityStorageConnector {
134
134
  values.push(id);
135
135
  }
136
136
  else {
137
- whereClauses.push("`id` = ?");
137
+ const primaryKeyProp = this._entitySchema.properties?.find(prop => prop.isPrimary);
138
+ if (primaryKeyProp) {
139
+ whereClauses.push(`\`${String(primaryKeyProp.property)}\` = ?`);
140
+ }
141
+ else {
142
+ whereClauses.push("`id` = ?");
143
+ }
138
144
  values.push(id);
139
145
  }
140
146
  if (conditions) {
@@ -162,11 +168,10 @@ class MySqlEntityStorageConnector {
162
168
  * @param conditions The optional conditions to match for the entities.
163
169
  * @returns The id of the entity.
164
170
  */
165
- async set(entity, conditions) {
166
- core.Guards.object(this.CLASS_NAME, "entity", entity);
167
- // Validate that the entity matches the schema
168
- this.entitySqlVerification(entity);
169
- const id = entity["id"];
171
+ async set(entity$1, conditions) {
172
+ core.Guards.object(this.CLASS_NAME, "entity", entity$1);
173
+ entity.EntitySchemaHelper.validateEntity(entity$1, this.getSchema());
174
+ const id = entity$1["id"];
170
175
  try {
171
176
  if (core.Is.arrayValue(conditions)) {
172
177
  const itemData = await this.get(id);
@@ -174,16 +179,24 @@ class MySqlEntityStorageConnector {
174
179
  return;
175
180
  }
176
181
  }
177
- const columns = Object.keys(entity)
182
+ const columns = Object.keys(entity$1)
178
183
  .map(key => `\`${key}\``)
179
184
  .join(", ");
180
- const values = Object.values(entity);
185
+ const values = Object.values(entity$1);
186
+ for (const [index, value] of values.entries()) {
187
+ const property = Object.keys(entity$1)[index];
188
+ const schemaProp = this._entitySchema.properties?.find(p => p.property === property);
189
+ if (schemaProp?.type === entity.EntitySchemaPropertyType.Object ||
190
+ schemaProp?.type === entity.EntitySchemaPropertyType.Array) {
191
+ values[index] = JSON.stringify(value);
192
+ }
193
+ }
181
194
  const placeholders = values.map(() => "?").join(", ");
182
195
  const dbConnection = await this.createConnection();
183
196
  await dbConnection.query(`INSERT INTO \`${this._config.database}\`.\`${this._config.tableName}\` (${columns}) VALUES (${placeholders}) ON DUPLICATE KEY UPDATE ${columns
184
197
  .split(", ")
185
198
  .map(col => `${col} = VALUES(${col})`)
186
- .join(", ")};`, values.map(value => (typeof value === "object" ? JSON.stringify(value) : value)));
199
+ .join(", ")};`, values);
187
200
  }
188
201
  catch (err) {
189
202
  throw new core.GeneralError(this.CLASS_NAME, "setFailed", {
@@ -461,46 +474,18 @@ class MySqlEntityStorageConnector {
461
474
  const nullable = prop.optional ? " NULL" : " NOT NULL";
462
475
  if (prop.isPrimary) {
463
476
  if (sqlType === "LONGTEXT" || sqlType === "TEXT") {
464
- primaryKeys.push(`${columnName}(255)`);
477
+ primaryKeys.push(`\`${columnName}\`(255)`);
465
478
  }
466
479
  else {
467
- primaryKeys.push(columnName);
480
+ primaryKeys.push(`\`${columnName}\``);
468
481
  }
469
482
  }
470
- return `${columnName} ${sqlType}${nullable}`;
483
+ return `\`${columnName}\` ${sqlType}${nullable}`;
471
484
  })
472
485
  .join(", ");
473
486
  const primaryKeyDefinition = primaryKeys.length > 0 ? `, PRIMARY KEY (${primaryKeys.join(", ")})` : "";
474
487
  return columnDefinitions + primaryKeyDefinition;
475
488
  }
476
- /**
477
- * Validate that the entity matches the schema.
478
- * @param entity The entity to validate.
479
- * @throws GeneralError if the entity schema properties are undefined or if the entity does not match the schema.
480
- */
481
- entitySqlVerification(entity) {
482
- // Validate that the entity matches the schema
483
- if (!this._entitySchema.properties) {
484
- throw new core.GeneralError(this.CLASS_NAME, "entitySchemaPropertiesUndefined");
485
- }
486
- for (const prop of this._entitySchema.properties) {
487
- const value = entity[prop.property];
488
- if (value === undefined || value === null) {
489
- if (!prop.optional) {
490
- throw new core.GeneralError(this.CLASS_NAME, "invalidEntity", {
491
- entity,
492
- entitySchema: this._entitySchema
493
- });
494
- }
495
- }
496
- else if (typeof value !== prop.type && (prop.type !== "array" || !core.Is.array(value))) {
497
- throw new core.GeneralError(this.CLASS_NAME, "invalidEntity", {
498
- entity,
499
- entitySchema: this._entitySchema
500
- });
501
- }
502
- }
503
- }
504
489
  }
505
490
 
506
491
  exports.MySqlEntityStorageConnector = MySqlEntityStorageConnector;
@@ -1,5 +1,5 @@
1
1
  import { Guards, BaseError, GeneralError, Is, ObjectHelper } from '@twin.org/core';
2
- import { EntitySchemaFactory, SortDirection, ComparisonOperator, LogicalOperator, EntitySchemaPropertyType } from '@twin.org/entity';
2
+ import { EntitySchemaFactory, EntitySchemaHelper, EntitySchemaPropertyType, SortDirection, ComparisonOperator, LogicalOperator } from '@twin.org/entity';
3
3
  import { LoggingConnectorFactory } from '@twin.org/logging-models';
4
4
  import { createConnection } from 'mysql2/promise';
5
5
 
@@ -132,7 +132,13 @@ class MySqlEntityStorageConnector {
132
132
  values.push(id);
133
133
  }
134
134
  else {
135
- whereClauses.push("`id` = ?");
135
+ const primaryKeyProp = this._entitySchema.properties?.find(prop => prop.isPrimary);
136
+ if (primaryKeyProp) {
137
+ whereClauses.push(`\`${String(primaryKeyProp.property)}\` = ?`);
138
+ }
139
+ else {
140
+ whereClauses.push("`id` = ?");
141
+ }
136
142
  values.push(id);
137
143
  }
138
144
  if (conditions) {
@@ -162,8 +168,7 @@ class MySqlEntityStorageConnector {
162
168
  */
163
169
  async set(entity, conditions) {
164
170
  Guards.object(this.CLASS_NAME, "entity", entity);
165
- // Validate that the entity matches the schema
166
- this.entitySqlVerification(entity);
171
+ EntitySchemaHelper.validateEntity(entity, this.getSchema());
167
172
  const id = entity["id"];
168
173
  try {
169
174
  if (Is.arrayValue(conditions)) {
@@ -176,12 +181,20 @@ class MySqlEntityStorageConnector {
176
181
  .map(key => `\`${key}\``)
177
182
  .join(", ");
178
183
  const values = Object.values(entity);
184
+ for (const [index, value] of values.entries()) {
185
+ const property = Object.keys(entity)[index];
186
+ const schemaProp = this._entitySchema.properties?.find(p => p.property === property);
187
+ if (schemaProp?.type === EntitySchemaPropertyType.Object ||
188
+ schemaProp?.type === EntitySchemaPropertyType.Array) {
189
+ values[index] = JSON.stringify(value);
190
+ }
191
+ }
179
192
  const placeholders = values.map(() => "?").join(", ");
180
193
  const dbConnection = await this.createConnection();
181
194
  await dbConnection.query(`INSERT INTO \`${this._config.database}\`.\`${this._config.tableName}\` (${columns}) VALUES (${placeholders}) ON DUPLICATE KEY UPDATE ${columns
182
195
  .split(", ")
183
196
  .map(col => `${col} = VALUES(${col})`)
184
- .join(", ")};`, values.map(value => (typeof value === "object" ? JSON.stringify(value) : value)));
197
+ .join(", ")};`, values);
185
198
  }
186
199
  catch (err) {
187
200
  throw new GeneralError(this.CLASS_NAME, "setFailed", {
@@ -459,46 +472,18 @@ class MySqlEntityStorageConnector {
459
472
  const nullable = prop.optional ? " NULL" : " NOT NULL";
460
473
  if (prop.isPrimary) {
461
474
  if (sqlType === "LONGTEXT" || sqlType === "TEXT") {
462
- primaryKeys.push(`${columnName}(255)`);
475
+ primaryKeys.push(`\`${columnName}\`(255)`);
463
476
  }
464
477
  else {
465
- primaryKeys.push(columnName);
478
+ primaryKeys.push(`\`${columnName}\``);
466
479
  }
467
480
  }
468
- return `${columnName} ${sqlType}${nullable}`;
481
+ return `\`${columnName}\` ${sqlType}${nullable}`;
469
482
  })
470
483
  .join(", ");
471
484
  const primaryKeyDefinition = primaryKeys.length > 0 ? `, PRIMARY KEY (${primaryKeys.join(", ")})` : "";
472
485
  return columnDefinitions + primaryKeyDefinition;
473
486
  }
474
- /**
475
- * Validate that the entity matches the schema.
476
- * @param entity The entity to validate.
477
- * @throws GeneralError if the entity schema properties are undefined or if the entity does not match the schema.
478
- */
479
- entitySqlVerification(entity) {
480
- // Validate that the entity matches the schema
481
- if (!this._entitySchema.properties) {
482
- throw new GeneralError(this.CLASS_NAME, "entitySchemaPropertiesUndefined");
483
- }
484
- for (const prop of this._entitySchema.properties) {
485
- const value = entity[prop.property];
486
- if (value === undefined || value === null) {
487
- if (!prop.optional) {
488
- throw new GeneralError(this.CLASS_NAME, "invalidEntity", {
489
- entity,
490
- entitySchema: this._entitySchema
491
- });
492
- }
493
- }
494
- else if (typeof value !== prop.type && (prop.type !== "array" || !Is.array(value))) {
495
- throw new GeneralError(this.CLASS_NAME, "invalidEntity", {
496
- entity,
497
- entitySchema: this._entitySchema
498
- });
499
- }
500
- }
501
- }
502
487
  }
503
488
 
504
489
  export { MySqlEntityStorageConnector };
@@ -85,10 +85,4 @@ export declare class MySqlEntityStorageConnector<T = unknown> implements IEntity
85
85
  * @throws GeneralError if the entity properties do not exist.
86
86
  */
87
87
  private mapMySqlProperties;
88
- /**
89
- * Validate that the entity matches the schema.
90
- * @param entity The entity to validate.
91
- * @throws GeneralError if the entity schema properties are undefined or if the entity does not match the schema.
92
- */
93
- private entitySqlVerification;
94
88
  }
package/docs/changelog.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @twin.org/entity-storage-connector-mysql - Changelog
2
2
 
3
- ## v0.0.1-next.21
3
+ ## v0.0.1-next.22
4
4
 
5
5
  - Initial Release
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.21",
3
+ "version": "0.0.1-next.22",
4
4
  "description": "Entity Storage connector implementation using MySQL storage",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,8 +15,9 @@
15
15
  },
16
16
  "dependencies": {
17
17
  "@twin.org/core": "next",
18
+ "@twin.org/data-json-ld": "^0.0.1-next.3",
18
19
  "@twin.org/entity": "next",
19
- "@twin.org/entity-storage-models": "0.0.1-next.21",
20
+ "@twin.org/entity-storage-models": "0.0.1-next.22",
20
21
  "@twin.org/logging-models": "next",
21
22
  "@twin.org/nameof": "next",
22
23
  "mysql2": "^3.12.0"