diff --git a/cli/package.json b/cli/package.json index 964a1191..08dcd832 100644 --- a/cli/package.json +++ b/cli/package.json @@ -1,6 +1,6 @@ { "name": "strokedb", - "version": "0.1.1", + "version": "0.2.0", "description": "Open your project's database in Stroke: the desktop app if it's installed, otherwise the web studio at stroke.click/studio", "license": "SEE LICENSE IN LICENSE", "homepage": "https://stroke.click/studio", diff --git a/cli/src/bridge.js b/cli/src/bridge.js index b3d2dbf5..04ae8bd4 100644 --- a/cli/src/bridge.js +++ b/cli/src/bridge.js @@ -15,6 +15,9 @@ import http from 'node:http' import crypto from 'node:crypto' export const STUDIO_ORIGIN = 'https://stroke.click' +/** Bumped with the package; the page reads `features` to know what it may offer. */ +export const BRIDGE_VERSION = '0.2.0' +export const FEATURES = ['structure', 'where', 'edit', 'rowCounts'] const MAX_BODY = 1 << 20 /** @@ -37,10 +40,24 @@ export async function startBridge({ engine, name, origins = [STUDIO_ORIGIN], por /** @type {Record Promise>} */ const routes = { - '/info': async () => ({ ...(await engine.info()), name }), + '/info': async () => ({ ...(await engine.info()), name, bridge: BRIDGE_VERSION, features: FEATURES }), '/tables': () => engine.tables(), '/columns': (b) => engine.columns(str(b.schema), str(b.table)), - '/rows': (b) => engine.rows({ schema: str(b.schema), table: str(b.table), limit: Number(b.limit) || 100, offset: Number(b.offset) || 0, sort: b.sort ?? null }), + '/structure': (b) => engine.structure(str(b.schema), str(b.table)), + '/rows': (b) => engine.rows({ + schema: str(b.schema), + table: str(b.table), + limit: Number(b.limit) || 100, + offset: Number(b.offset) || 0, + sort: b.sort ?? null, + where: typeof b.where === 'string' ? b.where : '', + }), + '/update': (b) => engine.update({ schema: str(b.schema), table: str(b.table), key: obj(b.key), set: obj(b.set) }), + '/insert': (b) => engine.insert({ schema: str(b.schema), table: str(b.table), values: obj(b.values) }), + '/delete': (b) => { + if (!Array.isArray(b.keys) || !b.keys.length) throw new Error('Expected keys') + return engine.remove({ schema: str(b.schema), table: str(b.table), keys: b.keys.map(obj) }) + }, '/query': async (b) => { const t = performance.now() const r = await engine.query(str(b.sql)) @@ -100,6 +117,12 @@ export async function startBridge({ engine, name, origins = [STUDIO_ORIGIN], por } } +/** @param {unknown} v @returns {Record} */ +function obj(v) { + if (!v || typeof v !== 'object' || Array.isArray(v)) throw new Error('Expected an object') + return /** @type {Record} */ (v) +} + /** @param {unknown} v */ function str(v) { if (typeof v !== 'string') throw new Error('Expected a string') diff --git a/cli/src/engines.js b/cli/src/engines.js index 397f1167..815a3fa0 100644 --- a/cli/src/engines.js +++ b/cli/src/engines.js @@ -1,17 +1,28 @@ -// The three engines the bridge speaks, behind one small interface the web -// studio calls: tables, columns, a page of rows, and a SQL console. +// The three engines the bridge speaks, behind one interface the web studio +// calls: tables, structure, a page of rows, edits by primary key, and a SQL +// console. /** - * @typedef {{ schema: string, name: string, kind: 'table' | 'view' }} TableRef - * @typedef {{ name: string, type: string, nullable: boolean, primaryKey: boolean }} Column + * @typedef {{ schema: string, name: string, kind: 'table' | 'view', rows: number | null }} TableRef + * @typedef {{ name: string, type: string, nullable: boolean, primaryKey: boolean, default: string | null }} Column + * @typedef {{ name: string, columns: string[], unique: boolean, primary: boolean }} Index + * @typedef {{ name: string, columns: string[], refSchema: string, refTable: string, refColumns: string[] }} ForeignKey + * @typedef {{ columns: Column[], indexes: Index[], foreignKeys: ForeignKey[] }} Structure * @typedef {{ columns: string[], rows: unknown[][], rowCount?: number | null, command?: string }} Result * @typedef {{ column: string, dir: 'asc' | 'desc' }} Sort + * @typedef {{ schema: string, table: string, limit: number, offset: number, sort?: Sort | null, where?: string }} RowsQuery + * @typedef {Record} Values + * @typedef {{ sql: string, params: unknown[], atMostOne?: boolean }} Step * @typedef {{ * engine: 'postgres' | 'mysql' | 'sqlite', * info(): Promise<{ engine: string, database: string, host: string, version: string }>, * tables(): Promise, * columns(schema: string, table: string): Promise, - * rows(o: { schema: string, table: string, limit: number, offset: number, sort?: Sort | null }): Promise, + * structure(schema: string, table: string): Promise, + * rows(o: RowsQuery): Promise, + * update(o: { schema: string, table: string, key: Values, set: Values }): Promise<{ rowCount: number }>, + * insert(o: { schema: string, table: string, values: Values }): Promise<{ rowCount: number }>, + * remove(o: { schema: string, table: string, keys: Values[] }): Promise<{ rowCount: number }>, * query(sql: string): Promise, * close(): Promise, * }} Engine @@ -35,20 +46,84 @@ export function cell(v) { return v } +/** + * A value from the page as a driver parameter: objects (json columns) as JSON + * text, everything else as is - the database casts text to the column's type. + * @param {unknown} v + */ +export function param(v) { + if (v === undefined) return null + if (v !== null && typeof v === 'object') return JSON.stringify(v) + return v +} + /** @param {string} id */ -const pgId = (id) => `"${String(id).replace(/"/g, '""')}"` +const dq = (id) => `"${String(id).replace(/"/g, '""')}"` /** @param {string} id */ -const myId = (id) => `\`${String(id).replace(/`/g, '``')}\`` - -/** @param {Sort | null | undefined} sort @param {(id: string) => string} q */ -function orderBy(sort, q) { - if (!sort?.column) return '' - return ` ORDER BY ${q(sort.column)} ${sort.dir === 'desc' ? 'DESC' : 'ASC'}` -} +const bq = (id) => `\`${String(id).replace(/`/g, '``')}\`` /** @param {number} n @param {number} max */ const clampInt = (n, max) => Math.max(0, Math.min(max, Math.floor(Number(n) || 0))) +/** + * SQL builders for one dialect. + * @param {{ q: (id: string) => string, ph: (i: number) => string, from: (schema: string, table: string) => string }} d + */ +export function dialect(d) { + /** @param {Values} key @param {number} start */ + const whereKey = (key, start) => { + const cols = Object.keys(key) + if (!cols.length) throw new Error('No primary key: this table can only be changed with SQL.') + return { + sql: cols.map((c, i) => `${d.q(c)} = ${d.ph(start + i)}`).join(' AND '), + params: cols.map((c) => param(key[c])), + } + } + return { + ...d, + /** @param {RowsQuery} o */ + page(o) { + const where = o.where?.trim() ? ` WHERE (${o.where})` : '' + const order = o.sort?.column ? ` ORDER BY ${d.q(o.sort.column)} ${o.sort.dir === 'desc' ? 'DESC' : 'ASC'}` : '' + return { + sql: `SELECT * FROM ${d.from(o.schema, o.table)}${where}${order} LIMIT ${clampInt(o.limit, 5000)} OFFSET ${clampInt(o.offset, 1e12)}`, + count: `SELECT count(*) AS n FROM ${d.from(o.schema, o.table)}${where}`, + } + }, + /** @param {string} schema @param {string} table @param {Values} key @param {Values} set @returns {Step} */ + update(schema, table, key, set) { + const cols = Object.keys(set) + if (!cols.length) throw new Error('Nothing to change.') + const w = whereKey(key, cols.length + 1) + return { + sql: `UPDATE ${d.from(schema, table)} SET ${cols.map((c, i) => `${d.q(c)} = ${d.ph(i + 1)}`).join(', ')} WHERE ${w.sql}`, + params: [...cols.map((c) => param(set[c])), ...w.params], + atMostOne: true, + } + }, + /** @param {string} schema @param {string} table @param {Values} values @returns {Step} */ + insert(schema, table, values) { + const cols = Object.keys(values) + if (!cols.length) return { sql: d.emptyInsert(d.from(schema, table)), params: [] } + return { + sql: `INSERT INTO ${d.from(schema, table)} (${cols.map(d.q).join(', ')}) VALUES (${cols.map((_, i) => d.ph(i + 1)).join(', ')})`, + params: cols.map((c) => param(values[c])), + } + }, + /** @param {string} schema @param {string} table @param {Values} key @returns {Step} */ + remove(schema, table, key) { + const w = whereKey(key, 1) + return { sql: `DELETE FROM ${d.from(schema, table)} WHERE ${w.sql}`, params: w.params, atMostOne: true } + }, + emptyInsert: d.emptyInsert, + } +} + +/** @param {number} n @param {Step} step */ +function checkOne(n, step) { + if (step.atMostOne && n > 1) throw new Error(`That would change ${n} rows, not one. Nothing was changed.`) +} + /** * @param {string} uri * @returns {Promise} @@ -59,6 +134,12 @@ export async function openPostgres(uri) { for (const oid of [1082, 1114, 1184, 1083, 1266]) pg.types.setTypeParser(oid, (v) => v) const pool = new pg.Pool({ connectionString: uri, max: 4, connectionTimeoutMillis: 15_000 }) await (await pool.connect()).release() + const sql = dialect({ + q: dq, + ph: (i) => `$${i}`, + from: (s, t) => `${dq(s)}.${dq(t)}`, + emptyInsert: (from) => `INSERT INTO ${from} DEFAULT VALUES`, + }) /** @param {string} text @param {unknown[]} [values] */ const arr = async (text, values) => { @@ -71,6 +152,44 @@ export async function openPostgres(uri) { command: last.command ?? '', } } + /** @param {Step[]} steps */ + const tx = async (steps) => { + const c = await pool.connect() + let total = 0 + try { + await c.query('BEGIN') + for (const s of steps) { + const r = await c.query(s.sql, s.params) + checkOne(r.rowCount ?? 0, s) + total += r.rowCount ?? 0 + } + await c.query('COMMIT') + return { rowCount: total } + } catch (e) { + await c.query('ROLLBACK').catch(() => {}) + throw e + } finally { + c.release() + } + } + + /** @param {string} schema @param {string} table */ + const columns = async (schema, table) => { + const r = await pool.query( + `select a.attname as name, format_type(a.atttypid, a.atttypmod) as type, not a.attnotnull as nullable, + coalesce(a.attnum = any(i.indkey), false) as "primaryKey", + pg_get_expr(d.adbin, d.adrelid) as default + from pg_attribute a + join pg_class c on c.oid = a.attrelid + join pg_namespace n on n.oid = c.relnamespace + left join pg_index i on i.indrelid = c.oid and i.indisprimary + left join pg_attrdef d on d.adrelid = c.oid and d.adnum = a.attnum + where n.nspname = $1 and c.relname = $2 and a.attnum > 0 and not a.attisdropped + order by a.attnum`, + [schema, table], + ) + return r.rows + } return { engine: 'postgres', @@ -81,42 +200,55 @@ export async function openPostgres(uri) { }, async tables() { const r = await pool.query(` - select table_schema as schema, table_name as name, - case when table_type = 'VIEW' then 'view' else 'table' end as kind - from information_schema.tables - where table_schema not in ('pg_catalog', 'information_schema') and table_schema not like 'pg_toast%' and table_schema not like 'pg_temp%' - union all - select schemaname, matviewname, 'view' from pg_matviews + select n.nspname as schema, c.relname as name, + case when c.relkind in ('v', 'm') then 'view' else 'table' end as kind, + case when c.relkind in ('r', 'p') then coalesce(nullif(c.reltuples, -1)::bigint, st.n_live_tup) end as rows + from pg_class c join pg_namespace n on n.oid = c.relnamespace + left join pg_stat_user_tables st on st.relid = c.oid + where c.relkind in ('r', 'p', 'v', 'm', 'f') + and n.nspname not in ('pg_catalog', 'information_schema') and n.nspname not like 'pg_toast%' and n.nspname not like 'pg_temp%' + and not c.relispartition order by 1, 2`) - return r.rows + return r.rows.map((t) => ({ ...t, rows: t.rows == null ? null : Number(t.rows) })) }, - async columns(schema, table) { - const r = await pool.query( - `select a.attname as name, format_type(a.atttypid, a.atttypmod) as type, not a.attnotnull as nullable, - coalesce(a.attnum = any(i.indkey), false) as "primaryKey" - from pg_attribute a - join pg_class c on c.oid = a.attrelid - join pg_namespace n on n.oid = c.relnamespace - left join pg_index i on i.indrelid = c.oid and i.indisprimary - where n.nspname = $1 and c.relname = $2 and a.attnum > 0 and not a.attisdropped - order by a.attnum`, + columns, + async structure(schema, table) { + const idx = await pool.query( + `select i.relname as name, ix.indisunique as unique, ix.indisprimary as primary, + array(select a.attname from unnest(ix.indkey) with ordinality k(n, o) join pg_attribute a on a.attrelid = t.oid and a.attnum = k.n order by k.o)::text[] as columns + from pg_index ix join pg_class t on t.oid = ix.indrelid join pg_class i on i.oid = ix.indexrelid join pg_namespace n on n.oid = t.relnamespace + where n.nspname = $1 and t.relname = $2 order by ix.indisprimary desc, i.relname`, [schema, table], ) - return r.rows - }, - async rows({ schema, table, limit, offset, sort }) { - const from = `${pgId(schema)}.${pgId(table)}` - const page = await arr(`select * from ${from}${orderBy(sort, pgId)} limit $1 offset $2`, [clampInt(limit, 5000), clampInt(offset, 1e12)]) - const est = await pool.query( - `select c.reltuples::bigint as n from pg_class c join pg_namespace n on n.oid = c.relnamespace where n.nspname = $1 and c.relname = $2`, + const fks = await pool.query( + `select con.conname as name, rn.nspname as "refSchema", rt.relname as "refTable", + array(select a.attname from unnest(con.conkey) with ordinality k(n, o) join pg_attribute a on a.attrelid = con.conrelid and a.attnum = k.n order by k.o)::text[] as columns, + array(select a.attname from unnest(con.confkey) with ordinality k(n, o) join pg_attribute a on a.attrelid = con.confrelid and a.attnum = k.n order by k.o)::text[] as "refColumns" + from pg_constraint con join pg_class t on t.oid = con.conrelid join pg_namespace n on n.oid = t.relnamespace + join pg_class rt on rt.oid = con.confrelid join pg_namespace rn on rn.oid = rt.relnamespace + where con.contype = 'f' and n.nspname = $1 and t.relname = $2 order by con.conname`, [schema, table], ) - const guess = Number(est.rows[0]?.n ?? -1) - if (guess > EXACT_COUNT_MAX) return { ...page, total: guess, estimated: true } - const exact = await pool.query(`select count(*)::bigint as n from ${from}`) + return { columns: await columns(schema, table), indexes: idx.rows, foreignKeys: fks.rows } + }, + async rows(o) { + const q = sql.page(o) + const page = await arr(q.sql) + if (!o.where?.trim()) { + const est = await pool.query( + `select c.reltuples::bigint as n from pg_class c join pg_namespace n on n.oid = c.relnamespace where n.nspname = $1 and c.relname = $2`, + [o.schema, o.table], + ) + const guess = Number(est.rows[0]?.n ?? -1) + if (guess > EXACT_COUNT_MAX) return { ...page, total: guess, estimated: true } + } + const exact = await pool.query(q.count) return { ...page, total: Number(exact.rows[0].n), estimated: false } }, - query: (sql) => arr(sql), + update: async (o) => tx([sql.update(o.schema, o.table, o.key, o.set)]), + insert: async (o) => tx([sql.insert(o.schema, o.table, o.values)]), + remove: async (o) => tx(o.keys.map((k) => sql.remove(o.schema, o.table, k))), + query: (text) => arr(text), close: () => pool.end(), } } @@ -129,13 +261,19 @@ export async function openMysql(uri) { const mysql = await import('mysql2/promise') const pool = mysql.createPool({ uri: uri.replace(/^(mariadb|mysql2):/i, 'mysql:'), connectionLimit: 4, dateStrings: true, supportBigNumbers: true, bigNumberStrings: true, connectTimeout: 15_000 }) await (await pool.getConnection()).release() + const sql = dialect({ + q: bq, + ph: () => '?', + from: (s, t) => `${bq(s)}.${bq(t)}`, + emptyInsert: (from) => `INSERT INTO ${from} () VALUES ()`, + }) - /** @param {string} sql @param {unknown[]} [values] */ - const arr = async (sql, values) => { - const [rows, fields] = await pool.query({ sql, values, rowsAsArray: true }) + /** @param {string} text @param {unknown[]} [values] */ + const arr = async (text, values) => { + const [rows, fields] = await pool.query({ sql: text, values, rowsAsArray: true }) if (!Array.isArray(rows)) { const h = /** @type {{ affectedRows?: number }} */ (rows) - return { columns: [], rows: [], rowCount: h.affectedRows ?? null, command: sql.trim().split(/\s+/)[0].toUpperCase() } + return { columns: [], rows: [], rowCount: h.affectedRows ?? null, command: text.trim().split(/\s+/)[0].toUpperCase() } } return { columns: (fields ?? []).map((f) => f.name), @@ -144,6 +282,35 @@ export async function openMysql(uri) { command: 'SELECT', } } + /** @param {Step[]} steps */ + const tx = async (steps) => { + const c = await pool.getConnection() + let total = 0 + try { + await c.beginTransaction() + for (const s of steps) { + const [r] = /** @type {any} */ (await c.query(s.sql, s.params)) + checkOne(r.affectedRows ?? 0, s) + total += r.affectedRows ?? 0 + } + await c.commit() + return { rowCount: total } + } catch (e) { + await c.rollback().catch(() => {}) + throw e + } finally { + c.release() + } + } + /** @param {string} schema @param {string} table */ + const columns = async (schema, table) => { + const [r] = /** @type {any} */ (await pool.query( + `select column_name as name, column_type as type, is_nullable = 'YES' as nullable, column_key = 'PRI' as primaryKey, column_default as \`default\` + from information_schema.columns where table_schema = ? and table_name = ? order by ordinal_position`, + [schema, table], + )) + return r.map((/** @type {any} */ c) => ({ ...c, nullable: !!c.nullable, primaryKey: !!c.primaryKey, default: c.default === 'NULL' ? null : c.default })) + } return { engine: 'mysql', @@ -154,35 +321,54 @@ export async function openMysql(uri) { }, async tables() { const [r] = /** @type {any} */ (await pool.query( - `select table_schema as \`schema\`, table_name as name, if(table_type = 'VIEW', 'view', 'table') as kind + `select table_schema as \`schema\`, table_name as name, if(table_type = 'VIEW', 'view', 'table') as kind, table_rows as \`rows\` from information_schema.tables where table_schema = coalesce(database(), table_schema) and table_schema not in ('mysql', 'information_schema', 'performance_schema', 'sys') order by 1, 2`, )) - return r + return r.map((/** @type {any} */ t) => ({ ...t, rows: t.rows == null ? null : Number(t.rows) })) }, - async columns(schema, table) { - const [r] = /** @type {any} */ (await pool.query( - `select column_name as name, column_type as type, is_nullable = 'YES' as nullable, column_key = 'PRI' as primaryKey - from information_schema.columns where table_schema = ? and table_name = ? order by ordinal_position`, + columns, + async structure(schema, table) { + const [idx] = /** @type {any} */ (await pool.query( + `select index_name as name, non_unique = 0 as \`unique\`, index_name = 'PRIMARY' as \`primary\`, group_concat(column_name order by seq_in_index separator '\\n') as cols + from information_schema.statistics where table_schema = ? and table_name = ? group by index_name, non_unique order by \`primary\` desc, name`, [schema, table], )) - return r.map((/** @type {any} */ c) => ({ ...c, nullable: !!c.nullable, primaryKey: !!c.primaryKey })) - }, - async rows({ schema, table, limit, offset, sort }) { - const from = `${myId(schema)}.${myId(table)}` - const page = await arr(`select * from ${from}${orderBy(sort, myId)} limit ? offset ?`, [clampInt(limit, 5000), clampInt(offset, 1e12)]) - const [est] = /** @type {any} */ (await pool.query( - 'select table_rows as n from information_schema.tables where table_schema = ? and table_name = ?', + const [fks] = /** @type {any} */ (await pool.query( + `select constraint_name as name, referenced_table_schema as refSchema, referenced_table_name as refTable, + group_concat(column_name order by ordinal_position separator '\\n') as cols, + group_concat(referenced_column_name order by ordinal_position separator '\\n') as refCols + from information_schema.key_column_usage + where table_schema = ? and table_name = ? and referenced_table_name is not null + group by constraint_name, referenced_table_schema, referenced_table_name order by name`, [schema, table], )) - const guess = Number(est[0]?.n ?? -1) - if (guess > EXACT_COUNT_MAX) return { ...page, total: guess, estimated: true } - const [exact] = /** @type {any} */ (await pool.query(`select count(*) as n from ${from}`)) + return { + columns: await columns(schema, table), + indexes: idx.map((/** @type {any} */ i) => ({ name: i.name, columns: String(i.cols).split('\n'), unique: !!i.unique, primary: !!i.primary })), + foreignKeys: fks.map((/** @type {any} */ f) => ({ name: f.name, refSchema: f.refSchema, refTable: f.refTable, columns: String(f.cols).split('\n'), refColumns: String(f.refCols).split('\n') })), + } + }, + async rows(o) { + const q = sql.page(o) + const page = await arr(q.sql) + if (!o.where?.trim()) { + const [est] = /** @type {any} */ (await pool.query( + 'select table_rows as n from information_schema.tables where table_schema = ? and table_name = ?', + [o.schema, o.table], + )) + const guess = Number(est[0]?.n ?? -1) + if (guess > EXACT_COUNT_MAX) return { ...page, total: guess, estimated: true } + } + const [exact] = /** @type {any} */ (await pool.query(q.count)) return { ...page, total: Number(exact[0].n), estimated: false } }, - query: (sql) => arr(sql), + update: async (o) => tx([sql.update(o.schema, o.table, o.key, o.set)]), + insert: async (o) => tx([sql.insert(o.schema, o.table, o.values)]), + remove: async (o) => tx(o.keys.map((k) => sql.remove(o.schema, o.table, k))), + query: (text) => arr(text), close: () => pool.end(), } } @@ -200,27 +386,45 @@ export async function openSqlite(file) { throw new Error(`SQLite needs Node 22.5 or newer (this is ${process.version}). Or open the file in the Stroke app.`) } const db = new sqlite.DatabaseSync(file) + // One schema per file; the page still sends "main". + const sql = dialect({ q: dq, ph: () => '?', from: (_s, t) => dq(t), emptyInsert: (from) => `INSERT INTO ${from} DEFAULT VALUES` }) + /** @param {unknown} v */ + const bind = (v) => (typeof v === 'boolean' ? Number(v) : v) - /** @param {string} sql @param {unknown[]} [params] */ - const arr = (sql, params = []) => { - const stmt = db.prepare(sql) + /** @param {string} text @param {unknown[]} [params] */ + const arr = (text, params = []) => { + const stmt = db.prepare(text) const cols = typeof stmt.columns === 'function' ? stmt.columns().map((c) => c.name) : null - const returnsRows = cols ? cols.length > 0 : /^\s*(select|with|pragma|explain|values)\b/i.test(sql) + const returnsRows = cols ? cols.length > 0 : /^\s*(select|with|pragma|explain|values)\b/i.test(text) if (!returnsRows) { - const r = stmt.run(.../** @type {any[]} */ (params)) - return { columns: [], rows: [], rowCount: Number(r.changes), command: sql.trim().split(/\s+/)[0].toUpperCase() } + const r = stmt.run(.../** @type {any[]} */ (params.map(bind))) + return { columns: [], rows: [], rowCount: Number(r.changes), command: text.trim().split(/\s+/)[0].toUpperCase() } } - const objs = /** @type {Record[]} */ (stmt.all(.../** @type {any[]} */ (params))) + const objs = /** @type {Record[]} */ (stmt.all(.../** @type {any[]} */ (params.map(bind)))) const columns = cols ?? Object.keys(objs[0] ?? {}) return { columns, rows: objs.map((o) => columns.map((c) => cell(o[c]))), rowCount: objs.length, command: 'SELECT' } } - /** @param {string} sql */ - const multi = (sql) => { - // node:sqlite prepares one statement at a time; run all but the last with - // exec and return the last one's rows, like the other consoles do. - const parts = splitStatements(sql) - if (parts.length > 1) db.exec(parts.slice(0, -1).join(';\n')) - return arr(parts[parts.length - 1] ?? sql) + /** @param {Step[]} steps */ + const tx = async (steps) => { + let total = 0 + db.exec('BEGIN') + try { + for (const s of steps) { + const n = Number(db.prepare(s.sql).run(.../** @type {any[]} */ (s.params.map(bind))).changes) + checkOne(n, s) + total += n + } + db.exec('COMMIT') + return { rowCount: total } + } catch (e) { + try { db.exec('ROLLBACK') } catch { /* already ended */ } + throw e + } + } + /** @param {string} _schema @param {string} table */ + const columns = async (_schema, table) => { + const r = db.prepare('select name, type, "notnull" as nn, pk, dflt_value as dflt from pragma_table_info(?)').all(table) + return r.map((c) => ({ name: String(c.name), type: String(c.type || ''), nullable: !c.nn, primaryKey: Number(c.pk) > 0, default: c.dflt == null ? null : String(c.dflt) })) } return { @@ -231,19 +435,50 @@ export async function openSqlite(file) { }, async tables() { const r = db.prepare(`select name, type from sqlite_schema where type in ('table', 'view') and name not like 'sqlite_%' order by name`).all() - return r.map((t) => ({ schema: 'main', name: String(t.name), kind: t.type === 'view' ? 'view' : 'table' })) + return r.map((t) => { + const kind = t.type === 'view' ? 'view' : 'table' + let rows = null + if (kind === 'table') { + try { rows = Number(/** @type {any} */ (db.prepare(`select count(*) as n from ${dq(String(t.name))}`).get()).n) } catch { /* virtual table */ } + } + return { schema: 'main', name: String(t.name), kind, rows } + }) }, - async columns(_schema, table) { - const r = db.prepare('select name, type, "notnull" as nn, pk from pragma_table_info(?)').all(table) - return r.map((c) => ({ name: String(c.name), type: String(c.type || ''), nullable: !c.nn, primaryKey: Number(c.pk) > 0 })) + columns, + async structure(schema, table) { + const list = db.prepare('select name, "unique" as u, origin from pragma_index_list(?)').all(table) + const indexes = list.map((i) => ({ + name: String(i.name), + unique: !!i.u, + primary: i.origin === 'pk', + columns: db.prepare('select name from pragma_index_info(?) order by seqno').all(String(i.name)).map((c) => String(c.name)), + })) + /** @type {Map} */ + const byId = new Map() + for (const f of db.prepare('select id, "table" as t, "from" as f, "to" as c from pragma_foreign_key_list(?) order by id, seq').all(table)) { + const id = Number(f.id) + const fk = byId.get(id) ?? { name: `fk_${id}`, refSchema: 'main', refTable: String(f.t), columns: [], refColumns: [] } + fk.columns.push(String(f.f)) + fk.refColumns.push(f.c == null ? '' : String(f.c)) + byId.set(id, fk) + } + return { columns: await columns(schema, table), indexes, foreignKeys: [...byId.values()] } }, - async rows({ table, limit, offset, sort }) { - const page = arr(`select * from ${pgId(table)}${orderBy(sort, pgId)} limit ? offset ?`, [clampInt(limit, 5000), clampInt(offset, 1e12)]) - const total = Number(/** @type {any} */ (db.prepare(`select count(*) as n from ${pgId(table)}`).get()).n) + async rows(o) { + const q = sql.page(o) + const page = arr(q.sql) + const total = Number(/** @type {any} */ (db.prepare(q.count).get()).n) return { ...page, total, estimated: false } }, - async query(sql) { - return multi(sql) + update: async (o) => tx([sql.update(o.schema, o.table, o.key, o.set)]), + insert: async (o) => tx([sql.insert(o.schema, o.table, o.values)]), + remove: async (o) => tx(o.keys.map((k) => sql.remove(o.schema, o.table, k))), + async query(text) { + // node:sqlite prepares one statement at a time; run all but the last + // with exec and return the last one's rows, like the other consoles do. + const parts = splitStatements(text) + if (parts.length > 1) db.exec(parts.slice(0, -1).join(';\n')) + return arr(parts[parts.length - 1] ?? text) }, async close() { db.close() @@ -253,25 +488,25 @@ export async function openSqlite(file) { /** * Split on `;` outside quotes and comments. Enough for a console; not a parser. - * @param {string} sql + * @param {string} text */ -export function splitStatements(sql) { +export function splitStatements(text) { /** @type {string[]} */ const out = [] let cur = '' - for (let i = 0; i < sql.length; i++) { - const c = sql[i] + for (let i = 0; i < text.length; i++) { + const c = text[i] if (c === "'" || c === '"' || c === '`') { - const end = sql.indexOf(c, i + 1) - const j = end < 0 ? sql.length : end - cur += sql.slice(i, j + 1) + const end = text.indexOf(c, i + 1) + const j = end < 0 ? text.length : end + cur += text.slice(i, j + 1) i = j - } else if (c === '-' && sql[i + 1] === '-') { - const end = sql.indexOf('\n', i) - i = end < 0 ? sql.length : end - 1 - } else if (c === '/' && sql[i + 1] === '*') { - const end = sql.indexOf('*/', i + 2) - i = end < 0 ? sql.length : end + 1 + } else if (c === '-' && text[i + 1] === '-') { + const end = text.indexOf('\n', i) + i = end < 0 ? text.length : end - 1 + } else if (c === '/' && text[i + 1] === '*') { + const end = text.indexOf('*/', i + 2) + i = end < 0 ? text.length : end + 1 } else if (c === ';') { if (cur.trim()) out.push(cur.trim()) cur = '' diff --git a/cli/test/cli.test.js b/cli/test/cli.test.js index a5f2bfc9..2a406857 100644 --- a/cli/test/cli.test.js +++ b/cli/test/cli.test.js @@ -83,7 +83,7 @@ test('sqlite engine and bridge, end to end', async () => { const file = path.join(tmp(), 'app.db') const db = await openSqlite(file) await db.query(`create table users (id integer primary key, name text not null); insert into users (name) values ('ada'), ('bob')`) - assert.deepEqual(await db.tables(), [{ schema: 'main', name: 'users', kind: 'table' }]) + assert.deepEqual(await db.tables(), [{ schema: 'main', name: 'users', kind: 'table', rows: 2 }]) const cols = await db.columns('main', 'users') assert.deepEqual(cols.map((c) => [c.name, c.primaryKey, c.nullable]), [['id', true, true], ['name', false, false]]) @@ -147,3 +147,38 @@ test('studioUrl keeps the token in the fragment', () => { assert.equal(f.get('bridge'), 'http://127.0.0.1:4321') assert.equal(f.get('token'), 'abc') }) + +test('sqlite: structure, where, and edits by primary key', async () => { + const db = await openSqlite(path.join(tmp(), 'edit.db')) + try { + await db.query(`create table teams (id integer primary key, name text); + create table users (id integer primary key, email text not null default 'x', team integer references teams(id)); + create unique index users_email on users(email); + insert into teams (name) values ('core'); + insert into users (email, team) values ('a@x.io', 1), ('b@x.io', 1), ('c@x.io', null)`) + const t = await db.tables() + assert.equal(t.find((x) => x.name === 'users')?.rows, 3) + + const s = await db.structure('main', 'users') + assert.equal(s.columns.find((c) => c.name === 'email')?.default, "'x'") + assert.ok(s.indexes.some((i) => i.name === 'users_email' && i.unique && i.columns[0] === 'email')) + assert.deepEqual(s.foreignKeys.map((f) => [f.columns[0], f.refTable, f.refColumns[0]]), [['team', 'teams', 'id']]) + + // A key that matches more than one row changes nothing. + await assert.rejects(db.update({ schema: 'main', table: 'users', key: { team: 1 }, set: { team: null } }), /2 rows/) + assert.equal((await db.query('select count(*) from users where team = 1')).rows[0][0], 2) + + const filtered = await db.rows({ schema: 'main', table: 'users', limit: 10, offset: 0, where: 'team is not null' }) + assert.equal(filtered.total, 2) + + assert.equal((await db.update({ schema: 'main', table: 'users', key: { id: 2 }, set: { email: 'bob@x.io', team: null } })).rowCount, 1) + assert.equal((await db.insert({ schema: 'main', table: 'users', values: { email: 'd@x.io' } })).rowCount, 1) + assert.equal((await db.remove({ schema: 'main', table: 'users', keys: [{ id: 1 }, { id: 3 }] })).rowCount, 2) + const left = await db.query('select id, email, team from users order by id') + assert.deepEqual(left.rows, [[2, 'bob@x.io', null], [4, 'd@x.io', null]]) + + await assert.rejects(db.update({ schema: 'main', table: 'users', key: {}, set: { email: 'z' } }), /primary key/) + } finally { + await db.close() + } +})