@minatojs/driver-sqlite
Advanced tools
Comparing version 3.0.2 to 3.0.3
@@ -6,2 +6,3 @@ /// <reference types="node" /> | ||
export interface SQLiteFieldInfo { | ||
cid: number; | ||
name: string; | ||
@@ -26,3 +27,2 @@ type: string; | ||
constructor(database: Database, config: SQLiteDriver.Config); | ||
private _getColDefs; | ||
/** synchronize table schema */ | ||
@@ -29,0 +29,0 @@ prepare(table: string): Promise<void>; |
118
lib/index.js
@@ -53,3 +53,3 @@ "use strict"; | ||
var logger = new import_reggol.default("sqlite"); | ||
function getTypeDefinition({ type }) { | ||
function getTypeDef({ type }) { | ||
switch (type) { | ||
@@ -75,3 +75,3 @@ case "boolean": | ||
} | ||
__name(getTypeDefinition, "getTypeDefinition"); | ||
__name(getTypeDef, "getTypeDef"); | ||
var SQLiteBuilder = class extends import_sql_utils.Builder { | ||
@@ -126,50 +126,75 @@ constructor(tables) { | ||
} | ||
_getColDefs(table, key) { | ||
const model = this.model(table); | ||
const { initial, nullable = true } = model.fields[key]; | ||
let def = `\`${key}\``; | ||
if (key === model.primary && model.autoInc) { | ||
def += " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT"; | ||
} else { | ||
const typedef = getTypeDefinition(model.fields[key]); | ||
def += " " + typedef + (nullable ? " " : " NOT ") + "NULL"; | ||
if (initial !== void 0 && initial !== null) { | ||
def += " DEFAULT " + this.sql.escape(this.sql.dump(model, { [key]: initial })[key]); | ||
} | ||
} | ||
return def; | ||
} | ||
async prepare(table) { | ||
const info = __privateMethod(this, _all, all_fn).call(this, `PRAGMA table_info(${(0, import_sql_utils.escapeId)(table)})`); | ||
const config = this.model(table); | ||
const keys = Object.keys(config.fields); | ||
if (info.length) { | ||
let hasUpdate = false; | ||
for (const key of keys) { | ||
if (info.some(({ name }) => name === key)) | ||
continue; | ||
const def = this._getColDefs(table, key); | ||
__privateMethod(this, _run, run_fn).call(this, `ALTER TABLE ${(0, import_sql_utils.escapeId)(table)} ADD COLUMN ${def}`); | ||
hasUpdate = true; | ||
const model = this.model(table); | ||
const columnDefs = []; | ||
const indexDefs = []; | ||
const alter = []; | ||
const mapping = {}; | ||
let shouldMigrate = false; | ||
for (const key in model.fields) { | ||
const legacy = [key, ...model.fields[key].legacy || []]; | ||
const column = info.find(({ name }) => legacy.includes(name)); | ||
const { initial, nullable = true } = model.fields[key]; | ||
const typedef = getTypeDef(model.fields[key]); | ||
let def = `${(0, import_sql_utils.escapeId)(key)} ${typedef}`; | ||
if (key === model.primary && model.autoInc) { | ||
def += " NOT NULL PRIMARY KEY AUTOINCREMENT"; | ||
} else { | ||
def += (nullable ? " " : " NOT ") + "NULL"; | ||
if (!(0, import_cosmokit.isNullable)(initial)) { | ||
def += " DEFAULT " + this.sql.escape(this.sql.dump(model, { [key]: initial })[key]); | ||
} | ||
} | ||
if (hasUpdate) { | ||
logger.info("auto updating table %c", table); | ||
columnDefs.push(def); | ||
if (!column) { | ||
alter.push("ADD " + def); | ||
} else { | ||
mapping[column.name] = key; | ||
shouldMigrate || (shouldMigrate = column.name !== key || column.type !== typedef); | ||
} | ||
} else { | ||
} | ||
if (model.primary && !model.autoInc) { | ||
indexDefs.push(`PRIMARY KEY (${__privateMethod(this, _joinKeys, joinKeys_fn).call(this, (0, import_cosmokit.makeArray)(model.primary))})`); | ||
} | ||
if (model.unique) { | ||
indexDefs.push(...model.unique.map((keys) => `UNIQUE (${__privateMethod(this, _joinKeys, joinKeys_fn).call(this, (0, import_cosmokit.makeArray)(keys))})`)); | ||
} | ||
if (model.foreign) { | ||
indexDefs.push(...Object.entries(model.foreign).map(([key, value]) => { | ||
const [table2, key2] = value; | ||
return `FOREIGN KEY (\`${key}\`) REFERENCES ${(0, import_sql_utils.escapeId)(table2)} (\`${key2}\`)`; | ||
})); | ||
} | ||
if (!info.length) { | ||
logger.info("auto creating table %c", table); | ||
const defs = keys.map((key) => this._getColDefs(table, key)); | ||
const constraints = []; | ||
if (config.primary && !config.autoInc) { | ||
constraints.push(`PRIMARY KEY (${__privateMethod(this, _joinKeys, joinKeys_fn).call(this, (0, import_cosmokit.makeArray)(config.primary))})`); | ||
__privateMethod(this, _run, run_fn).call(this, `CREATE TABLE ${(0, import_sql_utils.escapeId)(table)} (${[...columnDefs, ...indexDefs].join(", ")})`); | ||
} else if (shouldMigrate) { | ||
for (const column of info) { | ||
if (mapping[column.name]) | ||
continue; | ||
let def = `${(0, import_sql_utils.escapeId)(column.name)} ${column.type}`; | ||
def += (column.notnull ? " NOT " : " ") + "NULL"; | ||
if (column.pk) | ||
def += " PRIMARY KEY"; | ||
if (column.dflt_value !== null) | ||
def += " DEFAULT " + (0, import_sql_utils.escape)(column.dflt_value); | ||
columnDefs.push(def); | ||
mapping[column.name] = column.name; | ||
} | ||
if (config.unique) { | ||
constraints.push(...config.unique.map((keys2) => `UNIQUE (${__privateMethod(this, _joinKeys, joinKeys_fn).call(this, (0, import_cosmokit.makeArray)(keys2))})`)); | ||
const temp = table + "_temp"; | ||
const fields = Object.keys(mapping).map(import_sql_utils.escapeId).join(", "); | ||
logger.info("auto migrating table %c", table); | ||
__privateMethod(this, _run, run_fn).call(this, `CREATE TABLE ${(0, import_sql_utils.escapeId)(temp)} (${columnDefs.join(", ")})`); | ||
try { | ||
__privateMethod(this, _run, run_fn).call(this, `INSERT INTO ${(0, import_sql_utils.escapeId)(temp)} SELECT ${fields} FROM ${(0, import_sql_utils.escapeId)(table)}`); | ||
__privateMethod(this, _run, run_fn).call(this, `DROP TABLE ${(0, import_sql_utils.escapeId)(table)}`); | ||
__privateMethod(this, _run, run_fn).call(this, `CREATE TABLE ${(0, import_sql_utils.escapeId)(table)} (${[...columnDefs, ...indexDefs].join(", ")})`); | ||
__privateMethod(this, _run, run_fn).call(this, `INSERT INTO ${(0, import_sql_utils.escapeId)(table)} SELECT * FROM ${(0, import_sql_utils.escapeId)(temp)}`); | ||
} finally { | ||
__privateMethod(this, _run, run_fn).call(this, `DROP TABLE ${(0, import_sql_utils.escapeId)(temp)}`); | ||
} | ||
if (config.foreign) { | ||
constraints.push(...Object.entries(config.foreign).map(([key, value]) => { | ||
const [table2, key2] = value; | ||
return `FOREIGN KEY (\`${key}\`) REFERENCES ${(0, import_sql_utils.escapeId)(table2)} (\`${key2}\`)`; | ||
})); | ||
} | ||
__privateMethod(this, _run, run_fn).call(this, `CREATE TABLE ${(0, import_sql_utils.escapeId)(table)} (${[...defs, ...constraints].join(",")})`); | ||
} else if (alter.length) { | ||
logger.info("auto updating table %c", table); | ||
__privateMethod(this, _run, run_fn).call(this, `ALTER TABLE ${(0, import_sql_utils.escapeId)(table)} ${alter.join(", ")}`); | ||
} | ||
@@ -276,3 +301,3 @@ } | ||
joinKeys_fn = /* @__PURE__ */ __name(function(keys) { | ||
return (keys == null ? void 0 : keys.length) ? keys.map((key) => `\`${key}\``).join(",") : "*"; | ||
return (keys == null ? void 0 : keys.length) ? keys.map((key) => `\`${key}\``).join(", ") : "*"; | ||
}, "#joinKeys"); | ||
@@ -285,5 +310,6 @@ _exec = new WeakSet(); | ||
stmt.free(); | ||
logger.debug("> %s", sql); | ||
return result; | ||
} catch (e) { | ||
logger.warn("SQL > %c", sql, params); | ||
logger.warn("> %s", sql); | ||
throw e; | ||
@@ -338,3 +364,3 @@ } | ||
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`)); | ||
return __privateMethod(this, _run, run_fn).call(this, sql, [], () => __privateMethod(this, _get, get_fn).call(this, `SELECT last_insert_rowid() AS id`)); | ||
}, "#create"); | ||
@@ -341,0 +367,0 @@ var src_default = SQLiteDriver; |
{ | ||
"name": "@minatojs/driver-sqlite", | ||
"version": "3.0.2", | ||
"version": "3.0.3", | ||
"description": "SQLite Driver for Minato", | ||
@@ -28,3 +28,3 @@ "main": "lib/index.js", | ||
"peerDependencies": { | ||
"@minatojs/core": "^2.0.2" | ||
"@minatojs/core": "^2.0.3" | ||
}, | ||
@@ -31,0 +31,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
37821
405