@minatojs/driver-mysql
Advanced tools
Comparing version 2.5.0 to 2.5.1
@@ -25,2 +25,3 @@ import type { Pool, PoolConfig } from 'mysql'; | ||
escape(value: any, field?: Field<any>): string; | ||
toUpdateExpr(item: any, key: string, field?: Field, upsert?: boolean): string; | ||
} | ||
@@ -51,3 +52,2 @@ export declare namespace MySQLDriver { | ||
eval(sel: Selection.Immutable, expr: Eval.Expr): Promise<any>; | ||
private toUpdateExpr; | ||
set(sel: Selection.Mutable, data: {}): Promise<void>; | ||
@@ -54,0 +54,0 @@ remove(sel: Selection.Mutable): Promise<void>; |
@@ -142,5 +142,47 @@ "use strict"; | ||
value = import_cosmokit.Time.template("yyyy-MM-dd hh:mm:ss", value); | ||
} else if (!field && !!value && typeof value === "object") { | ||
return `json_extract(${this.quote(JSON.stringify(value))}, '$')`; | ||
} | ||
return super.escape(value, field); | ||
} | ||
toUpdateExpr(item, key, field, upsert) { | ||
const escaped = (0, import_sql_utils.escapeId)(key); | ||
if (key in item) { | ||
if (!(0, import_core.isEvalExpr)(item[key]) && upsert) { | ||
return `VALUES(${escaped})`; | ||
} else if ((0, import_core.isEvalExpr)(item[key])) { | ||
return this.parseEval(item[key]); | ||
} else { | ||
return this.escape(item[key], field); | ||
} | ||
} | ||
const jsonInit = {}; | ||
for (const prop in item) { | ||
if (!prop.startsWith(key + ".")) | ||
continue; | ||
const rest = prop.slice(key.length + 1).split("."); | ||
if (rest.length === 1) | ||
continue; | ||
rest.reduce((obj, k) => { | ||
var _a; | ||
return (_a = obj[k]) != null ? _a : obj[k] = {}; | ||
}, jsonInit); | ||
} | ||
const valueInit = `ifnull(${escaped}, '{}')`; | ||
let value = valueInit; | ||
if (Object.keys(jsonInit).length !== 0) { | ||
value = `json_merge(${value}, ${this.quote(JSON.stringify(jsonInit))})`; | ||
} | ||
for (const prop in item) { | ||
if (!prop.startsWith(key + ".")) | ||
continue; | ||
const rest = prop.slice(key.length + 1).split("."); | ||
value = `json_set(${value}, '$${rest.map((key2) => `."${key2}"`).join("")}', ${this.parseEval(item[prop])})`; | ||
} | ||
if (value === valueInit) { | ||
return escaped; | ||
} else { | ||
return value; | ||
} | ||
} | ||
}; | ||
@@ -394,30 +436,6 @@ __name(_MySQLBuilder, "MySQLBuilder"); | ||
} | ||
toUpdateExpr(item, key, field, upsert) { | ||
const escaped = (0, import_sql_utils.escapeId)(key); | ||
if (key in item) { | ||
if (!(0, import_core.isEvalExpr)(item[key]) && upsert) { | ||
return `VALUES(${escaped})`; | ||
} else if ((0, import_core.isEvalExpr)(item[key])) { | ||
return this.sql.parseEval(item[key]); | ||
} else { | ||
return this.sql.escape(item[key], field); | ||
} | ||
} | ||
const valueInit = `ifnull(${escaped}, '{}')`; | ||
let value = valueInit; | ||
for (const prop in item) { | ||
if (!prop.startsWith(key + ".")) | ||
continue; | ||
const rest = prop.slice(key.length + 1).split("."); | ||
value = `json_set(${value}, '$${rest.map((key2) => `."${key2}"`).join("")}', ${this.sql.parseEval(item[prop])})`; | ||
} | ||
if (value === valueInit) { | ||
return escaped; | ||
} else { | ||
return value; | ||
} | ||
} | ||
async set(sel, data) { | ||
const { model, query, table } = sel; | ||
const filter = this.sql.parseQuery(query); | ||
const { model, query, table, tables, ref } = sel; | ||
const builder = new MySQLBuilder(tables); | ||
const filter = builder.parseQuery(query); | ||
const { fields } = model; | ||
@@ -431,9 +449,10 @@ if (filter === "0") | ||
const escaped = (0, import_sql_utils.escapeId)(field); | ||
return `${escaped} = ${this.toUpdateExpr(data, field, fields[field], false)}`; | ||
return `${escaped} = ${builder.toUpdateExpr(data, field, fields[field], false)}`; | ||
}).join(", "); | ||
await this.query(`UPDATE ${(0, import_sql_utils.escapeId)(table)} SET ${update} WHERE ${filter}`); | ||
await this.query(`UPDATE ${(0, import_sql_utils.escapeId)(table)} ${ref} SET ${update} WHERE ${filter}`); | ||
} | ||
async remove(sel) { | ||
const { query, table } = sel; | ||
const filter = this.sql.parseQuery(query); | ||
const { query, table, tables } = sel; | ||
const builder = new MySQLBuilder(tables); | ||
const filter = builder.parseQuery(query); | ||
if (filter === "0") | ||
@@ -459,3 +478,4 @@ return; | ||
return; | ||
const { model, table, ref } = sel; | ||
const { model, table, tables, ref } = sel; | ||
const builder = new MySQLBuilder(tables); | ||
const merged = {}; | ||
@@ -474,3 +494,3 @@ const insertion = data.map((item) => { | ||
const updateFields = (0, import_cosmokit.difference)(dataFields, keys); | ||
const createFilter = /* @__PURE__ */ __name((item) => this.sql.parseQuery((0, import_cosmokit.pick)(item, keys)), "createFilter"); | ||
const createFilter = /* @__PURE__ */ __name((item) => builder.parseQuery((0, import_cosmokit.pick)(item, keys)), "createFilter"); | ||
const createMultiFilter = /* @__PURE__ */ __name((items) => { | ||
@@ -481,3 +501,3 @@ if (items.length === 1) { | ||
const key = keys[0]; | ||
return this.sql.parseQuery({ [key]: items.map((item) => item[key]) }); | ||
return builder.parseQuery({ [key]: items.map((item) => item[key]) }); | ||
} else { | ||
@@ -492,3 +512,3 @@ return items.map(createFilter).join(" OR "); | ||
var _a, _b; | ||
((_b = branches[_a = this.toUpdateExpr(item, field, model.fields[field], true)]) != null ? _b : branches[_a] = []).push(item); | ||
((_b = branches[_a = builder.toUpdateExpr(item, field, model.fields[field], true)]) != null ? _b : branches[_a] = []).push(item); | ||
}); | ||
@@ -495,0 +515,0 @@ const entries = Object.entries(branches).map(([expr, items]) => [createMultiFilter(items), expr]).sort(([a], [b]) => a.length - b.length).reverse(); |
{ | ||
"name": "@minatojs/driver-mysql", | ||
"version": "2.5.0", | ||
"version": "2.5.1", | ||
"description": "MySQL Driver for Minato", | ||
@@ -28,14 +28,14 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"@minatojs/core": "^2.4.0" | ||
"@minatojs/core": "^2.4.2" | ||
}, | ||
"devDependencies": { | ||
"@minatojs/tests": "^1.5.0", | ||
"@minatojs/tests": "^1.6.0", | ||
"@types/mysql": "^2.15.21" | ||
}, | ||
"dependencies": { | ||
"@minatojs/sql-utils": "^4.0.5", | ||
"@minatojs/sql-utils": "^4.0.6", | ||
"@vlasky/mysql": "^2.18.6", | ||
"cosmokit": "^1.4.5", | ||
"reggol": "^1.4.4" | ||
"reggol": "^1.5.1" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54755
584
Updated@minatojs/sql-utils@^4.0.6
Updatedreggol@^1.5.1