@minatojs/driver-sqlite
Advanced tools
Comparing version 2.1.2 to 3.0.0
/// <reference types="node" /> | ||
import { Database, Driver, Eval, Executable, Modifier } from '@minatojs/core'; | ||
import { Builder, Caster } from '@minatojs/sql-utils'; | ||
import { Database, Driver, Eval, Selection } from '@minatojs/core'; | ||
import { Builder } from '@minatojs/sql-utils'; | ||
import init from '@minatojs/sql.js'; | ||
@@ -22,3 +22,2 @@ export interface SQLiteFieldInfo { | ||
sql: Builder; | ||
caster: Caster; | ||
writeTask: NodeJS.Timeout; | ||
@@ -37,9 +36,9 @@ sqlite: init.SqlJsStatic; | ||
}>; | ||
remove(sel: Executable): Promise<void>; | ||
get(sel: Executable, modifier: Modifier): Promise<any>; | ||
eval(sel: Executable, expr: Eval.Expr): Promise<any>; | ||
set(sel: Executable, update: {}): Promise<void>; | ||
create(sel: Executable, data: {}): Promise<any>; | ||
upsert(sel: Executable, data: any[], keys: string[]): Promise<void>; | ||
remove(sel: Selection.Mutable): Promise<void>; | ||
get(sel: Selection.Immutable): Promise<any>; | ||
eval(sel: Selection.Immutable, expr: Eval.Expr): Promise<any>; | ||
set(sel: Selection.Mutable, update: {}): Promise<void>; | ||
create(sel: Selection.Mutable, data: {}): Promise<any>; | ||
upsert(sel: Selection.Mutable, data: any[], keys: string[]): Promise<void>; | ||
} | ||
export default SQLiteDriver; |
132
lib/index.js
@@ -39,3 +39,3 @@ var __create = Object.create; | ||
// minato/packages/sqlite/src/index.ts | ||
// packages/sqlite/src/index.ts | ||
var src_exports = {}; | ||
@@ -49,3 +49,2 @@ __export(src_exports, { | ||
var import_sql_utils = require("@minatojs/sql-utils"); | ||
var import_sqlstring_sqlite = require("sqlstring-sqlite"); | ||
var import_fs = require("fs"); | ||
@@ -77,32 +76,7 @@ var import_sql = __toESM(require("@minatojs/sql.js")); | ||
__name(getTypeDefinition, "getTypeDefinition"); | ||
var _joinKeys, joinKeys_fn, _exec, exec_fn, _all, all_fn, _get, get_fn, _run, run_fn, _update, update_fn, _create, create_fn; | ||
var SQLiteDriver = class extends import_core.Driver { | ||
constructor(database, config) { | ||
super(database); | ||
this.config = config; | ||
__privateAdd(this, _joinKeys); | ||
__privateAdd(this, _exec); | ||
__privateAdd(this, _all); | ||
__privateAdd(this, _get); | ||
__privateAdd(this, _run); | ||
__privateAdd(this, _update); | ||
__privateAdd(this, _create); | ||
this.sql = new class extends import_sql_utils.Builder { | ||
constructor() { | ||
super(...arguments); | ||
this.format = import_sqlstring_sqlite.format; | ||
this.escapeId = import_sqlstring_sqlite.escapeId; | ||
} | ||
escape(value) { | ||
if (value instanceof Date) { | ||
return +value + ""; | ||
} | ||
return (0, import_sqlstring_sqlite.escape)(value); | ||
} | ||
createElementQuery(key, value) { | ||
return `(',' || ${key} || ',') LIKE ${this.escape("%," + value + ",%")}`; | ||
} | ||
}(); | ||
this.caster = new import_sql_utils.Caster(this.database.tables); | ||
this.caster.register({ | ||
var SQLiteBuilder = class extends import_sql_utils.Builder { | ||
constructor(tables) { | ||
super(tables); | ||
this.evalOperators.$if = (args) => `iif(${args.map((arg) => this.parseEval(arg)).join(", ")})`; | ||
this.define({ | ||
types: ["boolean"], | ||
@@ -112,3 +86,3 @@ dump: (value) => +value, | ||
}); | ||
this.caster.register({ | ||
this.define({ | ||
types: ["json"], | ||
@@ -118,3 +92,3 @@ dump: (value) => JSON.stringify(value), | ||
}); | ||
this.caster.register({ | ||
this.define({ | ||
types: ["list"], | ||
@@ -124,3 +98,3 @@ dump: (value) => value.join(","), | ||
}); | ||
this.caster.register({ | ||
this.define({ | ||
types: ["date", "time", "timestamp"], | ||
@@ -131,13 +105,37 @@ dump: (value) => value === null ? null : +value, | ||
} | ||
escape(value, field) { | ||
if (value instanceof Date) | ||
value = +value; | ||
return super.escape(value, field); | ||
} | ||
createElementQuery(key, value) { | ||
return `(',' || ${key} || ',') LIKE ${this.escape("%," + value + ",%")}`; | ||
} | ||
}; | ||
__name(SQLiteBuilder, "SQLiteBuilder"); | ||
var _joinKeys, joinKeys_fn, _exec, exec_fn, _all, all_fn, _get, get_fn, _run, run_fn, _update, update_fn, _create, create_fn; | ||
var SQLiteDriver = class extends import_core.Driver { | ||
constructor(database, config) { | ||
super(database); | ||
this.config = config; | ||
__privateAdd(this, _joinKeys); | ||
__privateAdd(this, _exec); | ||
__privateAdd(this, _all); | ||
__privateAdd(this, _get); | ||
__privateAdd(this, _run); | ||
__privateAdd(this, _update); | ||
__privateAdd(this, _create); | ||
this.sql = new SQLiteBuilder(database.tables); | ||
} | ||
_getColDefs(table, key) { | ||
const config = this.model(table); | ||
const { initial, nullable = true } = config.fields[key]; | ||
const model = this.model(table); | ||
const { initial, nullable = true } = model.fields[key]; | ||
let def = `\`${key}\``; | ||
if (key === config.primary && config.autoInc) { | ||
if (key === model.primary && model.autoInc) { | ||
def += " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT"; | ||
} else { | ||
const typedef = getTypeDefinition(config.fields[key]); | ||
const typedef = getTypeDefinition(model.fields[key]); | ||
def += " " + typedef + (nullable ? " " : " NOT ") + "NULL"; | ||
if (initial !== void 0 && initial !== null) { | ||
def += " DEFAULT " + this.sql.escape(this.caster.dump(table, { [key]: initial })[key]); | ||
def += " DEFAULT " + this.sql.escape(this.sql.dump(model, { [key]: initial })[key]); | ||
} | ||
@@ -148,3 +146,3 @@ } | ||
async prepare(table) { | ||
const info = __privateMethod(this, _all, all_fn).call(this, `PRAGMA table_info(${this.sql.escapeId(table)})`); | ||
const info = __privateMethod(this, _all, all_fn).call(this, `PRAGMA table_info(${(0, import_sql_utils.escapeId)(table)})`); | ||
const config = this.model(table); | ||
@@ -158,3 +156,3 @@ const keys = Object.keys(config.fields); | ||
const def = this._getColDefs(table, key); | ||
__privateMethod(this, _run, run_fn).call(this, `ALTER TABLE ${this.sql.escapeId(table)} ADD COLUMN ${def}`); | ||
__privateMethod(this, _run, run_fn).call(this, `ALTER TABLE ${(0, import_sql_utils.escapeId)(table)} ADD COLUMN ${def}`); | ||
hasUpdate = true; | ||
@@ -177,6 +175,6 @@ } | ||
constraints.push(...Object.entries(config.foreign).map(([key, [table2, key2]]) => { | ||
return `FOREIGN KEY (\`${key}\`) REFERENCES ${this.sql.escapeId(table2)} (\`${key2}\`)`; | ||
return `FOREIGN KEY (\`${key}\`) REFERENCES ${(0, import_sql_utils.escapeId)(table2)} (\`${key2}\`)`; | ||
})); | ||
} | ||
__privateMethod(this, _run, run_fn).call(this, `CREATE TABLE ${this.sql.escapeId(table)} (${[...defs, ...constraints].join(",")})`); | ||
__privateMethod(this, _run, run_fn).call(this, `CREATE TABLE ${(0, import_sql_utils.escapeId)(table)} (${[...defs, ...constraints].join(",")})`); | ||
} | ||
@@ -202,3 +200,3 @@ } | ||
for (const table of tables) { | ||
__privateMethod(this, _run, run_fn).call(this, `DROP TABLE ${this.sql.escapeId(table)}`); | ||
__privateMethod(this, _run, run_fn).call(this, `DROP TABLE ${(0, import_sql_utils.escapeId)(table)}`); | ||
} | ||
@@ -215,25 +213,23 @@ } | ||
return; | ||
__privateMethod(this, _run, run_fn).call(this, `DELETE FROM ${this.sql.escapeId(table)} WHERE ${filter}`); | ||
__privateMethod(this, _run, run_fn).call(this, `DELETE FROM ${(0, import_sql_utils.escapeId)(table)} WHERE ${filter}`); | ||
} | ||
async get(sel, modifier) { | ||
const { table, fields, query } = sel; | ||
const filter = this.sql.parseQuery(query); | ||
if (filter === "0") | ||
async get(sel) { | ||
const { tables } = sel; | ||
const builder = new SQLiteBuilder(tables); | ||
const sql = builder.get(sel); | ||
if (!sql) | ||
return []; | ||
const { limit, offset, sort } = modifier; | ||
let sql = `SELECT ${__privateMethod(this, _joinKeys, joinKeys_fn).call(this, fields ? Object.keys(fields) : null)} FROM ${this.sql.escapeId(table)} WHERE ${filter}`; | ||
if (sort.length) | ||
sql += " ORDER BY " + sort.map(([key, order]) => `\`${key["$"][1]}\` ${order}`).join(", "); | ||
if (limit < Infinity) | ||
sql += " LIMIT " + limit; | ||
if (offset > 0) | ||
sql += " OFFSET " + offset; | ||
const rows = __privateMethod(this, _all, all_fn).call(this, sql); | ||
return rows.map((row) => this.caster.load(table, row)); | ||
return rows.map((row) => this.sql.load(sel.model, row)); | ||
} | ||
async eval(sel, expr) { | ||
const { table, query } = sel; | ||
const filter = this.sql.parseQuery(query); | ||
const output = this.sql.parseEval(expr); | ||
const { value } = __privateMethod(this, _get, get_fn).call(this, `SELECT ${output} AS value FROM ${this.sql.escapeId(table)} WHERE ${filter}`); | ||
let sql = this.sql.get(sel.table); | ||
const prefix = `SELECT ${output} AS value `; | ||
if (sql.startsWith("SELECT * ")) { | ||
sql = prefix + sql.slice(9); | ||
} else { | ||
sql = `${prefix}FROM (${sql}) ${sql.ref}`; | ||
} | ||
const { value } = __privateMethod(this, _get, get_fn).call(this, sql); | ||
return value; | ||
@@ -334,13 +330,15 @@ } | ||
const { ref, table } = sel; | ||
const row = this.caster.dump(table, (0, import_core.executeUpdate)(data, update, ref)); | ||
const assignment = updateFields.map((key) => `\`${key}\` = ${this.sql.escape(row[key])}`).join(","); | ||
const model = this.model(table); | ||
const row = this.sql.dump(model, (0, import_core.executeUpdate)(data, update, ref)); | ||
const assignment = updateFields.map((key) => `${(0, import_sql_utils.escapeId)(key)} = ${this.sql.escape(row[key])}`).join(","); | ||
const query = Object.fromEntries(indexFields.map((key) => [key, row[key]])); | ||
const filter = this.sql.parseQuery(query); | ||
__privateMethod(this, _run, run_fn).call(this, `UPDATE ${this.sql.escapeId(table)} SET ${assignment} WHERE ${filter}`); | ||
__privateMethod(this, _run, run_fn).call(this, `UPDATE ${(0, import_sql_utils.escapeId)(table)} SET ${assignment} WHERE ${filter}`); | ||
}, "#update"); | ||
_create = new WeakSet(); | ||
create_fn = /* @__PURE__ */ __name(function(table, data) { | ||
data = this.caster.dump(table, data); | ||
const model = this.model(table); | ||
data = this.sql.dump(model, data); | ||
const keys = Object.keys(data); | ||
const sql = `INSERT INTO ${this.sql.escapeId(table)} (${__privateMethod(this, _joinKeys, joinKeys_fn).call(this, keys)}) VALUES (${keys.map((key) => this.sql.escape(data[key])).join(", ")})`; | ||
const sql = `INSERT INTO ${(0, import_sql_utils.escapeId)(table)} (${__privateMethod(this, _joinKeys, joinKeys_fn).call(this, keys)}) VALUES (${keys.map((key) => this.sql.escape(data[key])).join(", ")})`; | ||
return __privateMethod(this, _run, run_fn).call(this, sql, [], () => __privateMethod(this, _get, get_fn).call(this, `select last_insert_rowid() as id`)); | ||
@@ -347,0 +345,0 @@ }, "#create"); |
{ | ||
"name": "@minatojs/driver-sqlite", | ||
"version": "2.1.2", | ||
"version": "3.0.0", | ||
"description": "SQLite Driver for Minato", | ||
@@ -28,3 +28,3 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"@minatojs/core": "^1.3.2" | ||
"@minatojs/core": "^2.0.0" | ||
}, | ||
@@ -31,0 +31,0 @@ "devDependencies": { |
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
33907
377