@minatojs/driver-sqlite
Advanced tools
Comparing version 3.7.0 to 3.8.0
@@ -30,9 +30,16 @@ import { Database, Driver, Eval, Selection } from '@minatojs/core'; | ||
stats(): Promise<Driver.Stats>; | ||
remove(sel: Selection.Mutable): Promise<void>; | ||
remove(sel: Selection.Mutable): Promise<{ | ||
removed?: undefined; | ||
} | { | ||
removed: any; | ||
}>; | ||
get(sel: Selection.Immutable): Promise<any>; | ||
eval(sel: Selection.Immutable, expr: Eval.Expr): Promise<any>; | ||
set(sel: Selection.Mutable, update: {}): Promise<void>; | ||
set(sel: Selection.Mutable, update: {}): Promise<{ | ||
matched: number; | ||
modified: number; | ||
}>; | ||
create(sel: Selection.Mutable, data: {}): Promise<any>; | ||
upsert(sel: Selection.Mutable, data: any[], keys: string[]): Promise<void>; | ||
upsert(sel: Selection.Mutable, data: any[], keys: string[]): Promise<{}>; | ||
} | ||
export default SQLiteDriver; |
@@ -330,4 +330,5 @@ "use strict"; | ||
if (filter === "0") | ||
return; | ||
__privateMethod(this, _run, run_fn).call(this, `DELETE FROM ${(0, import_sql_utils.escapeId)(table)} WHERE ${filter}`); | ||
return {}; | ||
const result = __privateMethod(this, _run, run_fn).call(this, `DELETE FROM ${(0, import_sql_utils.escapeId)(table)} WHERE ${filter}`, [], () => __privateMethod(this, _get, get_fn).call(this, `SELECT changes() AS count`)); | ||
return { removed: result.count }; | ||
} | ||
@@ -358,5 +359,7 @@ async get(sel) { | ||
const data = await this.database.get(table, query); | ||
let modified = 0; | ||
for (const row of data) { | ||
__privateMethod(this, _update, update_fn).call(this, sel, primaryFields, updateFields, update, row); | ||
modified += __privateMethod(this, _update, update_fn).call(this, sel, primaryFields, updateFields, update, row); | ||
} | ||
return { matched: data.length, modified }; | ||
} | ||
@@ -374,4 +377,5 @@ async create(sel, data) { | ||
if (!data.length) | ||
return; | ||
return {}; | ||
const { model, table, ref } = sel; | ||
const result = { inserted: 0, matched: 0, modified: 0 }; | ||
const dataFields = [...new Set(Object.keys(Object.assign({}, ...data)).map((key) => { | ||
@@ -392,8 +396,11 @@ return Object.keys(model.fields).find((field) => field === key || key.startsWith(field + ".")); | ||
if (row) { | ||
__privateMethod(this, _update, update_fn).call(this, sel, keys, updateFields, item, row); | ||
result.modified += __privateMethod(this, _update, update_fn).call(this, sel, keys, updateFields, item, row); | ||
result.matched++; | ||
} else { | ||
__privateMethod(this, _create, create_fn).call(this, table, (0, import_core.executeUpdate)(model.create(), item, ref)); | ||
result.inserted++; | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
@@ -448,3 +455,6 @@ }; | ||
const model = this.model(table); | ||
const row = this.sql.dump(model, (0, import_core.executeUpdate)(data, update, ref)); | ||
const modified = !(0, import_cosmokit.deepEqual)((0, import_cosmokit.clone)(data), (0, import_core.executeUpdate)(data, update, ref)); | ||
if (!modified) | ||
return 0; | ||
const row = this.sql.dump(model, data); | ||
const assignment = updateFields.map((key) => `${(0, import_sql_utils.escapeId)(key)} = ${this.sql.escape(row[key])}`).join(","); | ||
@@ -454,2 +464,3 @@ const query = Object.fromEntries(indexFields.map((key) => [key, row[key]])); | ||
__privateMethod(this, _run, run_fn).call(this, `UPDATE ${(0, import_sql_utils.escapeId)(table)} SET ${assignment} WHERE ${filter}`); | ||
return 1; | ||
}, "#update"); | ||
@@ -456,0 +467,0 @@ _create = new WeakSet(); |
{ | ||
"name": "@minatojs/driver-sqlite", | ||
"version": "3.7.0", | ||
"version": "3.8.0", | ||
"description": "SQLite Driver for Minato", | ||
@@ -29,9 +29,9 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"@minatojs/core": "^2.5.0" | ||
"@minatojs/core": "^2.6.0" | ||
}, | ||
"devDependencies": { | ||
"@minatojs/tests": "^1.7.0" | ||
"@minatojs/tests": "^1.8.0" | ||
}, | ||
"dependencies": { | ||
"@minatojs/sql-utils": "^4.1.0", | ||
"@minatojs/sql-utils": "^4.2.0", | ||
"@minatojs/sql.js": "^3.1.0", | ||
@@ -38,0 +38,0 @@ "cosmokit": "^1.5.1", |
@@ -1,2 +0,2 @@ | ||
import { deepEqual, Dict, difference, isNullable, makeArray } from 'cosmokit' | ||
import { clone, deepEqual, Dict, difference, isNullable, makeArray } from 'cosmokit' | ||
import { Database, Driver, Eval, executeUpdate, Field, Model, randomId, Selection } from '@minatojs/core' | ||
@@ -345,4 +345,5 @@ import { Builder, escapeId } from '@minatojs/sql-utils' | ||
const filter = this.sql.parseQuery(query) | ||
if (filter === '0') return | ||
this.#run(`DELETE FROM ${escapeId(table)} WHERE ${filter}`) | ||
if (filter === '0') return {} | ||
const result = this.#run(`DELETE FROM ${escapeId(table)} WHERE ${filter}`, [], () => this.#get(`SELECT changes() AS count`)) | ||
return { removed: result.count } | ||
} | ||
@@ -370,3 +371,5 @@ | ||
const model = this.model(table) | ||
const row = this.sql.dump(model, executeUpdate(data, update, ref)) | ||
const modified = !deepEqual(clone(data), executeUpdate(data, update, ref)) | ||
if (!modified) return 0 | ||
const row = this.sql.dump(model, data) | ||
const assignment = updateFields.map((key) => `${escapeId(key)} = ${this.sql.escape(row[key])}`).join(',') | ||
@@ -376,2 +379,3 @@ const query = Object.fromEntries(indexFields.map(key => [key, row[key]])) | ||
this.#run(`UPDATE ${escapeId(table)} SET ${assignment} WHERE ${filter}`) | ||
return 1 | ||
} | ||
@@ -387,5 +391,7 @@ | ||
const data = await this.database.get(table, query) | ||
let modified = 0 | ||
for (const row of data) { | ||
this.#update(sel, primaryFields, updateFields, update, row) | ||
modified += this.#update(sel, primaryFields, updateFields, update, row) | ||
} | ||
return { matched: data.length, modified } | ||
} | ||
@@ -411,4 +417,5 @@ | ||
async upsert(sel: Selection.Mutable, data: any[], keys: string[]) { | ||
if (!data.length) return | ||
if (!data.length) return {} | ||
const { model, table, ref } = sel | ||
const result = { inserted: 0, matched: 0, modified: 0 } | ||
const dataFields = [...new Set(Object.keys(Object.assign({}, ...data)).map((key) => { | ||
@@ -429,8 +436,11 @@ return Object.keys(model.fields).find(field => field === key || key.startsWith(field + '.'))! | ||
if (row) { | ||
this.#update(sel, keys, updateFields, item, row) | ||
result.modified += this.#update(sel, keys, updateFields, item, row) | ||
result.matched++ | ||
} else { | ||
this.#create(table, executeUpdate(model.create(), item, ref)) | ||
result.inserted++ | ||
} | ||
} | ||
} | ||
return result | ||
} | ||
@@ -437,0 +447,0 @@ } |
Sorry, the diff of this file is not supported yet
49806
914
Updated@minatojs/sql-utils@^4.2.0