@mlagie/sql-connector 3.0.0 → 3.0.2

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/README.md CHANGED
@@ -188,7 +188,7 @@ await Model.syncAllTables();
188
188
  await userModel.save({ email: 'user@example.com', status: 'active' });
189
189
 
190
190
  const user = await userModel.find({ where: { email: 'user@example.com' }});
191
- await user[0].deleteOne();
191
+ await user[0].delete();
192
192
  ```
193
193
 
194
194
  ## save function
@@ -471,14 +471,14 @@ await User.save({ email: "user@example.com", status: "active", uuid: uuid, my_uu
471
471
 
472
472
  - `updateOne(model)` updates the row
473
473
  - `delete(model)` deletes the row using a filter
474
- - `deleteOne()` deletes the instance row
474
+ - `delete()` deletes the instance row
475
475
  - `customRequest(custom)` runs a custom query
476
476
 
477
477
  ```js
478
478
  const userInstance = await find({ select: "users", where: { email: 'user@example.com' }})[0];
479
479
 
480
480
  await userInstance.updateOne({ status: 'inactive' });
481
- await userInstance.deleteOne();
481
+ await userInstance.delete();
482
482
  ```
483
483
 
484
484
  ## SQL types
@@ -158,7 +158,7 @@ await Model.syncAllTables();
158
158
  await userModel.save({ email: 'user@example.com', status: 'active' });
159
159
 
160
160
  const user = await userModel.find({ where: { email: 'user@example.com' }});
161
- await user[0].deleteOne();
161
+ await user[0].delete();
162
162
  ```
163
163
 
164
164
  ## Fonction save
@@ -444,14 +444,14 @@ await User.save({ email: "user@example.com", status: "active", uuid: uuid, my_uu
444
444
 
445
445
  - `updateOne(model)` met à jour la ligne
446
446
  - `delete(model)` supprime la ligne avec un filtre
447
- - `deleteOne()` supprime la ligne de l'instance
447
+ - `delete()` supprime la ligne de l'instance
448
448
  - `customRequest(custom)` exécute une requête personnalisée
449
449
 
450
450
  ```javascript
451
451
  const userInstance = new ModelInstance('users', { email: 'user@example.com' });
452
452
 
453
453
  await userInstance.updateOne({ status: 'inactive' });
454
- await userInstance.deleteOne();
454
+ await userInstance.delete();
455
455
  ```
456
456
 
457
457
  ## Types SQL
package/index.d.ts CHANGED
@@ -351,12 +351,7 @@ export class ModelInstance<TData extends Record<string, any> = Record<string, an
351
351
  * @throws {Error} Throws an error if the deletion fails.
352
352
  */
353
353
  delete(filter: Record<string, any>): Promise<number>;
354
- /**
355
- * Deletes a single entry in the database table based on the instance data.
356
- * @returns {Promise<number>} A promise that resolves to the number of rows deleted.
357
- * @throws {Error} Throws an error if the deletion fails.
358
- */
359
- deleteOne(): Promise<number>;
354
+
360
355
  /**
361
356
  * Runs a custom SQL_request query.
362
357
  * @param {string} custom The custom SQL_request query to execute.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mlagie/sql-connector",
3
- "version": "3.0.0",
3
+ "version": "3.0.2",
4
4
  "description": "The sql-connector module allows you to manage connections to a MySQL database, define table schemas, and interact with data in a simple and efficient way.",
5
5
  "main": "index.js",
6
6
  "exports": {
@@ -105,7 +105,6 @@ class Model {
105
105
  const deps = getSafe(dependencies, table)
106
106
  for (const dep of deps) {
107
107
  if (dep !== table && getSafe(modelMap, dep)) {
108
- console.log(`Table ${table} depends on ${dep}.`);
109
108
  visit(dep, [...stack, table]);
110
109
  }
111
110
  }
@@ -157,11 +157,9 @@ class ModelInstance {
157
157
  * @returns {Promise<Object>} A promise that resolves with the data deleted.
158
158
  * @throws {Error} Throws an error if the deletion fails.
159
159
  */
160
- async delete(filter) {
160
+ async delete(filter = { where: this.getRecordData() }) {
161
161
  const { sql: whereClause, values } = buildQueryParts(filter);
162
-
163
- const sql_request = `DELETE FROM ${this._name} ${whereClause}`;
164
-
162
+ const sql_request = `DELETE FROM ${getDialect().escape(this._name)} ${whereClause}`;
165
163
  const result = await getDialect().execute(getConnexion(), sql_request, values).catch((err) => {
166
164
  error(`Error executing query delete: ${err}`);
167
165
  throw err;
@@ -170,22 +168,6 @@ class ModelInstance {
170
168
  return getDialect().getAffectedRows(result) === 0 ? 0 : 1;
171
169
  }
172
170
 
173
- /**
174
- * Deletes a single entry in the database table based on the instance data.
175
- * @returns {Promise<number>} A promise that resolves to the number of rows deleted.
176
- * @throws {Error} Throws an error if the deletion fails.
177
- */
178
- async deleteOne() {
179
- const { sql: whereClause, values } = buildQueryParts(this.getRecordData());
180
- const sql_request = `DELETE FROM ${this._name} ${whereClause}`;
181
- const result = await getDialect().execute(getConnexion(), sql_request, values).catch((err) => {
182
- error(`Error executing query deleteOne: ${err}`);
183
- throw err;
184
- });
185
-
186
- return getDialect().getAffectedRows(result) === 0 ? 0 : 1;
187
- }
188
-
189
171
  /**
190
172
  * Runs a custom SQL_request query.
191
173
  * @param {string} custom The custom SQL_request query to execute.
@@ -106,15 +106,17 @@ function buildWhere(where, values) {
106
106
  for (const [operator, operatorValue] of Object.entries(value)) {
107
107
  if (operator === "IN" || operator === "NOT IN") {
108
108
  const placeholders = operatorValue
109
- .map(() => "?")
109
+ .map((_, i) => getDialect().getPlaceholder(values.length + i))
110
110
  .join(",");
111
111
 
112
112
  conditions.push(
113
113
  `${getDialect().escape(key)} ${operator} (${placeholders})`
114
114
  );
115
115
  values.push(...operatorValue);
116
+ } else if (operatorValue === null && (operator === "=" || operator === "!=")) {
117
+ conditions.push(`${getDialect().escape(key)} IS ${operator === "!=" ? "NOT " : ""}NULL`);
116
118
  } else {
117
- conditions.push(`${getDialect().escape(key)} ${operator} ?`);
119
+ conditions.push(`${getDialect().escape(key)} ${operator} ${getDialect().getPlaceholder(values.length)}`);
118
120
  values.push(operatorValue);
119
121
  }
120
122
  }
@@ -122,7 +124,12 @@ function buildWhere(where, values) {
122
124
  }
123
125
  }
124
126
 
125
- conditions.push(`${getDialect().escape(key)} = ?`);
127
+ if (value === null) {
128
+ conditions.push(`${getDialect().escape(key)} IS NULL`);
129
+ continue;
130
+ }
131
+
132
+ conditions.push(`${getDialect().escape(key)} = ${getDialect().getPlaceholder(values.length)}`);
126
133
  values.push(value);
127
134
  }
128
135
 
@@ -169,7 +176,7 @@ function buildQueryParts(options) {
169
176
  if (!Number.isInteger(options.limit) || options.limit < 0) {
170
177
  throw new Error("Invalid LIMIT value");
171
178
  }
172
-
179
+
173
180
  parts.push(`LIMIT ?`);
174
181
  values.push(options.limit);
175
182
  }