@nuxodin/item 0.6.10 → 0.6.12
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/package.json +1 -1
- package/tools/db/sql.d.ts +2 -0
- package/tools/db/sql.js +20 -0
- package/tools/schema/db/sqlite/to-db.js +5 -3
package/package.json
CHANGED
package/tools/db/sql.d.ts
CHANGED
|
@@ -10,6 +10,8 @@ export interface SqlTag {
|
|
|
10
10
|
id(name: string | { toString(): string } | Promise<string | { toString(): string }>): Sql;
|
|
11
11
|
raw(text: string): Sql;
|
|
12
12
|
join(frags: Sql[], separator?: string): Sql;
|
|
13
|
+
in(col: string | Sql, values: Iterable<unknown>): Sql;
|
|
14
|
+
notIn(col: string | Sql, values: Iterable<unknown>): Sql;
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
export const sql: SqlTag;
|
package/tools/db/sql.js
CHANGED
|
@@ -38,6 +38,26 @@ sql.join = (frags, separator = ', ') => {
|
|
|
38
38
|
return new Sql(parts);
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
+
/** A column: a name is quoted per dialect, `a.b` in its parts; anything else is a fragment already. */
|
|
42
|
+
const column = (c) => typeof c === 'string' ? sql.join(c.split('.').map(n => sql.id(n)), '.') : c;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* `x IN (…)`, and a plain `false` where there is nothing to be in — `IN ()` is a syntax error, and
|
|
46
|
+
* an empty subquery cannot be typed for every column (`IN (SELECT NULL WHERE FALSE)` is a type
|
|
47
|
+
* error in postgres). The column belongs inside for exactly that reason: only there can the empty
|
|
48
|
+
* case be a boolean instead of a comparison.
|
|
49
|
+
*/
|
|
50
|
+
sql.in = (col, values) => {
|
|
51
|
+
const list = [...values];
|
|
52
|
+
return list.length ? sql`${column(col)} IN (${sql.join(list.map(v => sql`${v}`))})` : sql.raw('false');
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** The other way round — and nothing to be in means everything qualifies. */
|
|
56
|
+
sql.notIn = (col, values) => {
|
|
57
|
+
const list = [...values];
|
|
58
|
+
return list.length ? sql`${column(col)} NOT IN (${sql.join(list.map(v => sql`${v}`))})` : sql.raw('true');
|
|
59
|
+
};
|
|
60
|
+
|
|
41
61
|
// Objects with their own toString bind as strings; plain objects/arrays are
|
|
42
62
|
// mistakes (drivers would mangle them, e.g. mysql2's `key`='value' expansion).
|
|
43
63
|
function toParam(v) {
|
|
@@ -30,10 +30,12 @@ function emittable(prop, isSoloPk) {
|
|
|
30
30
|
return out
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
function createBody(fields, primaries) {
|
|
33
|
+
function createBody(table, fields, primaries) {
|
|
34
34
|
const solo = soloIntPrimary(fields, primaries)
|
|
35
35
|
const cols = fields.map(([n, f]) => {
|
|
36
36
|
const isPk = n === solo
|
|
37
|
+
if (f['x-autoincrement'] && !isPk)
|
|
38
|
+
console.warn(`Skip AUTOINCREMENT ${table}.${n}: SQLite auto-increments only a sole INTEGER PRIMARY KEY — nothing fills this column`)
|
|
37
39
|
// Only `INTEGER PRIMARY KEY` is the rowid alias, and AUTOINCREMENT is legal nowhere else —
|
|
38
40
|
// so a boolean key keeps the integer spelling, which SQLite would otherwise reject.
|
|
39
41
|
let def = toFieldDef(n, isPk ? { ...f, type: 'integer' } : f)
|
|
@@ -99,7 +101,7 @@ export async function schemaToDb(schema, query, { force = false, patch = false }
|
|
|
99
101
|
const primaries = fields.filter(([, f]) => f['x-index'] === 'primary').map(([n]) => n)
|
|
100
102
|
|
|
101
103
|
if (!currTables.includes(table)) {
|
|
102
|
-
stmts.push(`CREATE TABLE ${quoteId(table)} (\n${createBody(fields, primaries)}\n);`)
|
|
104
|
+
stmts.push(`CREATE TABLE ${quoteId(table)} (\n${createBody(table, fields, primaries)}\n);`)
|
|
103
105
|
stmts.push(...indexStatements(table, fields, primaries))
|
|
104
106
|
} else {
|
|
105
107
|
const currFields = new Map(tableFields(current.properties[table]))
|
|
@@ -116,7 +118,7 @@ export async function schemaToDb(schema, query, { force = false, patch = false }
|
|
|
116
118
|
const keep = fields.map(([n]) => n).filter(n => currFields.has(n))
|
|
117
119
|
const colsList = keep.map(quoteId).join(', ')
|
|
118
120
|
|
|
119
|
-
stmts.push(`CREATE TABLE ${quoteId(tmp)} (\n${createBody(fields, primaries)}\n);`)
|
|
121
|
+
stmts.push(`CREATE TABLE ${quoteId(tmp)} (\n${createBody(table, fields, primaries)}\n);`)
|
|
120
122
|
if (keep.length) stmts.push(`INSERT INTO ${quoteId(tmp)} (${colsList}) SELECT ${colsList} FROM ${quoteId(table)};`)
|
|
121
123
|
stmts.push(`DROP TABLE ${quoteId(table)};`)
|
|
122
124
|
stmts.push(`ALTER TABLE ${quoteId(tmp)} RENAME TO ${quoteId(table)};`)
|