evikit 2.5.1 → 2.5.3

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/index.d.ts CHANGED
@@ -98,6 +98,7 @@ export const del: H<"del">;
98
98
  export const details: H<"details">;
99
99
  export const dialog: H<"dialog">;
100
100
  export const div: H<"div">;
101
+ export const ellipse: H<"ellipse">;
101
102
  export const em: H<"em">;
102
103
  export const fieldset: H<"fieldset">;
103
104
  export const figcaption: H<"figcaption">;
@@ -128,7 +129,9 @@ export const option: H<"option">;
128
129
  export const p: H<"p">;
129
130
  export const path: H<"path">;
130
131
  export const picture: H<"picture">;
132
+ export const polyline: H<"polyline">;
131
133
  export const pre: H<"pre">;
134
+ export const rect: H<"rect">;
132
135
  export const section: H<"section">;
133
136
  export const select: H<"select">;
134
137
  export const small: H<"small">;
@@ -142,7 +145,9 @@ export const svg: H<"svg">;
142
145
  export const table: H<"table">;
143
146
  export const tbody: H<"tbody">;
144
147
  export const td: H<"td">;
148
+ export const text: H<"text">;
145
149
  export const textarea: H<"textarea">;
150
+ export const th: H<"th">;
146
151
  export const thead: H<"thead">;
147
152
  export const title: H<"title">;
148
153
  export const tr: H<"tr">;
package/index.js CHANGED
@@ -116,13 +116,22 @@ export async function ssr(App, data) {
116
116
  const { html, links } = await prerender(h(/** @type {any} */ (App), {}), {
117
117
  props: { dehydratedState, i18n },
118
118
  });
119
- const { metas, title } = toStatic();
119
+ const { links: hoofdLinks, metas, title } = toStatic();
120
120
 
121
121
  return {
122
122
  data: { ...data, dehydratedState },
123
123
  head: {
124
- elements: new Set(
125
- metas.map((props) => ({
124
+ elements: new Set([
125
+ ...hoofdLinks.map((props) => ({
126
+ props: Object.fromEntries(
127
+ Object.entries(props).map(([name, content]) => [
128
+ name,
129
+ String(content),
130
+ ]),
131
+ ),
132
+ type: "link",
133
+ })),
134
+ ...metas.map((props) => ({
126
135
  props: Object.fromEntries(
127
136
  Object.entries(props).map(([name, content]) => [
128
137
  name,
@@ -131,7 +140,7 @@ export async function ssr(App, data) {
131
140
  ),
132
141
  type: "meta",
133
142
  })),
134
- ),
143
+ ]),
135
144
  lang: i18n.getLocale(),
136
145
  title,
137
146
  },
@@ -156,6 +165,7 @@ export const del = HTMLElement("del");
156
165
  export const details = HTMLElement("details");
157
166
  export const dialog = HTMLElement("dialog");
158
167
  export const div = HTMLElement("div");
168
+ export const ellipse = HTMLElement("ellipse");
159
169
  export const em = HTMLElement("em");
160
170
  export const fieldset = HTMLElement("fieldset");
161
171
  export const figcaption = HTMLElement("figcaption");
@@ -186,7 +196,9 @@ export const option = HTMLElement("option");
186
196
  export const p = HTMLElement("p");
187
197
  export const path = HTMLElement("path");
188
198
  export const picture = HTMLElement("picture");
199
+ export const polyline = HTMLElement("polyline");
189
200
  export const pre = HTMLElement("pre");
201
+ export const rect = HTMLElement("rect");
190
202
  export const section = HTMLElement("section");
191
203
  export const select = HTMLElement("select");
192
204
  export const small = HTMLElement("small");
@@ -200,7 +212,9 @@ export const svg = HTMLElement("svg");
200
212
  export const table = HTMLElement("table");
201
213
  export const tbody = HTMLElement("tbody");
202
214
  export const td = HTMLElement("td");
215
+ export const text = HTMLElement("text");
203
216
  export const textarea = HTMLElement("textarea");
217
+ export const th = HTMLElement("th");
204
218
  export const thead = HTMLElement("thead");
205
219
  export const title = HTMLElement("title");
206
220
  export const tr = HTMLElement("tr");
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "evikit",
3
3
  "description": "Preact/SQLite SSR framework for small web apps",
4
- "version": "2.5.1",
4
+ "version": "2.5.3",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": "./index.js",
@@ -24,7 +24,6 @@
24
24
  "@fastify/busboy": "^3.2.0",
25
25
  "@types/gettext-parser": "^9.0.0",
26
26
  "@types/gettext.js": "^2.0.0",
27
- "@types/node": "^25",
28
27
  "@valibot/to-json-schema": "^1.7.0",
29
28
  "env-paths": "^4.0.0",
30
29
  "gettext-parser": "^9.0.2",
@@ -41,6 +40,7 @@
41
40
  "devDependencies": {
42
41
  "@eslint/compat": "^2.1.0",
43
42
  "@eslint/js": "^10.0.1",
43
+ "@types/node": "^24.13.3",
44
44
  "eslint": "^10.4.0",
45
45
  "eslint-config-prettier": "^10.1.8",
46
46
  "eslint-plugin-perfectionist": "^5.9.0",
package/server.js CHANGED
@@ -260,7 +260,11 @@ export function production({ html, prerender }) {
260
260
  renderToString(h("title", null, rendered.head.title)),
261
261
  )
262
262
  .replace(/<!--app-head-->[\s\S]+?<body/, (match) =>
263
- match.replace(/<meta[^>]+>/g, ""),
263
+ match
264
+ .replace(/<link[^>]+>/g, (link) =>
265
+ /data-hoofd/.test(link) ? "" : link,
266
+ )
267
+ .replace(/<meta[^>]+>/g, ""),
264
268
  )
265
269
  .replace(
266
270
  `<!--app-head-->`,
package/sqlite.js CHANGED
@@ -1,489 +1,496 @@
1
- import envPaths from "env-paths";
2
- import { fail, ok } from "node:assert/strict";
3
- import { mkdirSync } from "node:fs";
4
- import { DatabaseSync } from "node:sqlite";
5
- import { getMetadata, parse } from "valibot";
6
-
7
- import { flatten, relations } from "./db.js";
8
-
9
- /** @type {typeof import("./sqlite.d.ts").sqlite} */
10
- export function sqlite({ pkg, schema }) {
11
- const data = pkg
12
- ? (() => {
13
- const { data } = envPaths(pkg.name);
14
- mkdirSync(data, { recursive: true });
15
- return data;
16
- })()
17
- : undefined;
18
- const database = new DatabaseSync(data ? `${data}/db.sqlite3` : ":memory:", {
19
- timeout: 1000,
20
- });
21
- const flat = flatten(relations(schema));
22
-
23
- const haveTables = /** @type {{ name: string }[]} */ (
24
- database
25
- .prepare(
26
- "select * from sqlite_schema where type='table' and name not like 'sqlite_%'",
27
- )
28
- .all()
29
- ).map((table) => {
30
- const columns =
31
- /** @type {{ name: string, notnull: 0 | 1, pk: 0 | 1, type: "INTEGER" | "TEXT", }[]} */ (
32
- database.prepare(`pragma table_info(${table.name})`).all()
33
- );
34
- const foreignKeys =
35
- /** @type {{ from: string, on_delete: "CASCADE" | "SET NULL", on_update: "CASCADE" | "SET NULL", table: string, to: string, }[]} */ (
36
- database.prepare(`pragma foreign_key_list(${table.name})`).all()
37
- );
38
- return {
39
- columns,
40
- foreignKeys,
41
- indices: /** @type {{ name: string }[]} */ (
42
- database.prepare(`pragma index_list(${table.name})`).all()
43
- )
44
- .filter(({ name }) => !/^sqlite_/.test(name))
45
- .map((index) => ({
46
- ...index,
47
- info: /** @type {{ name: string; }} */ (
48
- database.prepare(`pragma index_info(${index.name})`).get()
49
- ),
50
- })),
51
- table,
52
- };
53
- });
54
-
55
- const wantTables = Object.entries(schema).map(([name, { entries }]) => ({
56
- columns: Object.entries(entries).map(([name, maybeEntry]) => {
57
- const entry =
58
- /** @type {import("valibot").BaseSchema<undefined, undefined, any>} */ (
59
- maybeEntry
60
- );
61
- const pk = /** @type {{ primary?: true }} */ (getMetadata(entry)).primary
62
- ? 1
63
- : 0;
64
- return {
65
- name,
66
- notnull: !pk && entry.expects.includes("undefined") ? 0 : 1,
67
- pk,
68
- type:
69
- entry.expects == "string" || entry.expects == "(string | undefined)"
70
- ? "TEXT"
71
- : entry.expects == "number" ||
72
- entry.expects == "(number | undefined)"
73
- ? "INTEGER"
74
- : entry.expects == "Uint8Array" ||
75
- entry.expects == "(Uint8Array | undefined)"
76
- ? "BLOB"
77
- : fail(name),
78
- };
79
- }),
80
- foreignKeys: Object.entries(entries)
81
- .map(([name, maybeEntry]) => {
82
- const entry =
83
- /** @type {import("valibot").BaseSchema<undefined, undefined, any>} */ (
84
- maybeEntry
85
- );
86
-
87
- const metadata =
88
- /** @type {{ key?: string, references?: any } | undefined} */ (
89
- getMetadata(entry)
90
- );
91
- const table = metadata
92
- ? Object.entries(schema)
93
- .filter(([, value]) => value == metadata.references)
94
- .find(Boolean)
95
- : undefined;
96
- if (!metadata?.references) {
97
- return;
98
- }
99
- ok(table);
100
- const to = Object.entries(table[1].entries).find(
101
- ([, value]) => value == metadata.key,
102
- );
103
- ok(to);
104
- return {
105
- from: name,
106
- on_delete: /undefined/.test(entry.expects) ? "SET NULL" : "CASCADE",
107
- on_update: /undefined/.test(entry.expects) ? "SET NULL" : "CASCADE",
108
- table: table[0],
109
- to: to[0],
110
- };
111
- })
112
- .filter(Boolean)
113
- .map((x) => {
114
- ok(x);
115
- return x;
116
- }),
117
- table: { name },
118
- }));
119
-
120
- const migration = [];
121
-
122
- for (const wantTable of wantTables) {
123
- const haveTable = haveTables.find(
124
- ({ table }) => table.name == wantTable.table.name,
125
- );
126
- if (haveTable) {
127
- for (const { info, name } of haveTable.indices) {
128
- if (!wantTable.foreignKeys.find(({ from }) => from == info.name)) {
129
- migration.push(`drop index ${name}`);
130
- }
131
- }
132
- const haveColumns = haveTable.columns;
133
- const columnsNowMissing = haveColumns.filter(
134
- (haveColumn) =>
135
- !wantTable.columns.find(({ name }) => name == haveColumn.name),
136
- );
137
- for (const { name, pk } of columnsNowMissing) {
138
- if (pk) {
139
- fail(`${haveTable.table.name}.${name}: Can't drop primary key`);
140
- }
141
- }
142
- for (const wantColumn of wantTable.columns) {
143
- const haveColumn = haveColumns.find(
144
- ({ name }) => name == wantColumn.name,
145
- );
146
-
147
- /** @param {typeof haveColumn} column */
148
- function checkConstraint(column) {
149
- ok(column);
150
- ok(haveTable);
151
- if (column.pk != wantColumn.pk) {
152
- fail(
153
- `${haveTable.table.name}.${column.name}: Can't toggle primary key`,
154
- );
155
- }
156
- if (
157
- !wantColumn.pk && // Without this condition I get an error with real database created by drizzle-kit.
158
- column.notnull != wantColumn.notnull
159
- ) {
160
- fail(
161
- `${haveTable.table.name}.${column.name}: Can't toggle optional`,
162
- );
163
- }
164
- }
165
-
166
- if (haveColumn) {
167
- checkConstraint(haveColumn);
168
- } else {
169
- const renamedColumn = columnsNowMissing.find(
170
- ({ notnull, type }) =>
171
- wantColumn.type == type && wantColumn.notnull == notnull,
172
- );
173
- if (renamedColumn) {
174
- checkConstraint(renamedColumn);
175
- columnsNowMissing.splice(
176
- columnsNowMissing.indexOf(renamedColumn),
177
- 1,
178
- );
179
- migration.push(
180
- `alter table ${wantTable.table.name} rename column ${renamedColumn.name} to ${wantColumn.name}`,
181
- );
182
- } else {
183
- if (wantColumn.pk) {
184
- fail(
185
- `${wantTable.table.name}.${wantColumn.name}: Can't add primary key`,
186
- );
187
- }
188
- migration.push(
189
- [
190
- `alter table ${wantTable.table.name} add column`,
191
- wantColumn.name,
192
- wantColumn.type,
193
- wantColumn.notnull ? "not null" : "",
194
- ...wantTable.foreignKeys
195
- .map(({ from, on_delete, on_update, table, to }) =>
196
- from == wantColumn.name
197
- ? `references ${table}(${to}) on delete ${on_delete} on update ${on_update}`
198
- : "",
199
- )
200
- .filter(Boolean),
201
- ]
202
- .filter(Boolean)
203
- .join(" "),
204
- );
205
- }
206
- }
207
-
208
- for (const { from } of wantTable.foreignKeys) {
209
- if (
210
- from == wantColumn.name &&
211
- !haveTable.indices.find(({ info }) => info.name == from)
212
- ) {
213
- migration.push(
214
- `create index ${wantTable.table.name}_${from} on ${wantTable.table.name}(${from})`,
215
- );
216
- }
217
- }
218
- }
219
- for (const missingColumn of columnsNowMissing) {
220
- migration.push(
221
- `alter table ${wantTable.table.name} drop column ${missingColumn.name}`,
222
- );
223
- }
224
- } else {
225
- if (!wantTable.columns.some(({ pk }) => pk)) {
226
- fail(`${wantTable.table.name} has no primary key`);
227
- }
228
- migration.push(
229
- `create table ${wantTable.table.name} (\n` +
230
- wantTable.columns
231
- .map(({ name, notnull, pk, type }) =>
232
- [
233
- name,
234
- type,
235
- pk ? "primary key" : "",
236
- !pk && notnull ? "not null" : "",
237
- ...wantTable.foreignKeys
238
- .map(({ from, on_delete, on_update, table, to }) =>
239
- from == name
240
- ? `references ${table}(${to}) on delete ${on_delete} on update ${on_update}`
241
- : "",
242
- )
243
- .filter(Boolean),
244
- ]
245
- .filter(Boolean)
246
- .join(" "),
247
- )
248
- .join(",\n") +
249
- `)`,
250
- );
251
- for (const { from } of wantTable.foreignKeys) {
252
- migration.push(
253
- `create index ${wantTable.table.name}_${from} on ${wantTable.table.name}(${from})`,
254
- );
255
- }
256
- }
257
- }
258
-
259
- for (const haveTable of haveTables) {
260
- const wantTable = wantTables.find(
261
- ({ table }) => table.name == haveTable.table.name,
262
- );
263
- if (!wantTable) {
264
- migration.push(`drop table ${haveTable.table.name}`);
265
- }
266
- }
267
-
268
- for (const sql of migration) {
269
- database.exec(sql);
270
- }
271
-
272
- return /** @type {any} */ (
273
- Object.fromEntries(
274
- Object.entries(schema).map(([maybeTable]) => {
275
- const table = /** @type {keyof typeof schema & string} */ (maybeTable);
276
-
277
- /** @param {{ [K in keyof (typeof schema)[typeof table]["entries"]]?: import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]> | import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]>[]; }} where */
278
- function ensureArrayRecord(where) {
279
- for (const key of Object.keys(where)) {
280
- if (!Array.isArray(where[key])) {
281
- Object.assign(where, { [key]: [where[key]] });
282
- }
283
- }
284
- }
285
-
286
- return [
287
- table,
288
- {
289
- /** @param {{ [K in keyof (typeof schema)[typeof table]["entries"]]?: import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]> | import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]>[]; }} where */
290
- delete(where) {
291
- ensureArrayRecord(where);
292
- database
293
- .prepare(
294
- `delete from ${table} where true\n` +
295
- Object.entries(where)
296
- .map(
297
- ([name, values]) =>
298
- `and ${name} in (${values.map(() => "?").join(", ")})`,
299
- )
300
- .join("\n"),
301
- )
302
- .run(
303
- ...Object.values(where)
304
- .flatMap((values) => values)
305
- .map((value) => value ?? null),
306
- );
307
- },
308
-
309
- /** @param {any} join */
310
- select(join) {
311
- return select({ database, flat, join, schema, table });
312
- },
313
-
314
- /** @param {{ set: Partial<import("valibot").InferOutput<(typeof schema)[typeof table]>>, where: { [K in keyof (typeof schema)[typeof table]["entries"]]?: import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]> | import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]>[]; } }} props */
315
- update({ set, where }) {
316
- ensureArrayRecord(where);
317
- database
318
- .prepare(
319
- `update ${table} set\n` +
320
- Object.keys(set)
321
- .map((name) => `${name} = ?`)
322
- .join(",\n") +
323
- `\nwhere true\n` +
324
- Object.entries(where)
325
- .map(
326
- ([name, values]) =>
327
- `and ${name} in (${values.map(() => "?").join(", ")})`,
328
- )
329
- .join("\n"),
330
- )
331
- .run(
332
- ...[
333
- ...Object.values(set).map((value) => value ?? null),
334
- ...Object.values(where)
335
- .flatMap((values) => values)
336
- .map((value) => value ?? null),
337
- ],
338
- );
339
- },
340
-
341
- /** @param {any[]} values */
342
- upsert(...values) {
343
- ok(schema[table]);
344
- const columns = Object.entries(schema[table].entries);
345
- const primaryKey = columns.find(
346
- ([, column]) =>
347
- /** @type {{ primary?: true }} */ (getMetadata(column))
348
- .primary,
349
- );
350
- ok(primaryKey);
351
- const query = database.prepare(
352
- `insert into ${table} (${columns.map(([name]) => name).join(", ")}) values\n` +
353
- `(${columns.map(() => "?").join(", ")})\n` +
354
- `on conflict (${primaryKey[0]}) do update set\n` +
355
- columns.map(([name]) => `${name} = ?`).join(",\n"),
356
- );
357
- const outputs = [];
358
- for (const row of values) {
359
- const output = parse(schema[table], row);
360
- const values = columns.map(([name]) => output[name] ?? null);
361
- query.run(...values, ...values);
362
- outputs.push(output);
363
- }
364
- return outputs;
365
- },
366
- },
367
- ];
368
- }),
369
- )
370
- );
371
- }
372
-
373
- /** @type {<Schema extends Record<string, import("valibot").ObjectSchema<any, any>>, R extends import("./db.d.ts").Relations<Schema> = import("./db.d.ts").Relations<Schema>, F extends import("./db.d.ts").Flatten<Schema> = import("./db.d.ts").Flatten<Schema>, Table extends keyof F & keyof Schema = keyof F & keyof Schema >(props: { database: DatabaseSync; flat: F; join?: import("./db.d.ts").Join<R, Table>; schema: Schema; table: Table; }) => any[]} */
374
- function select(props) {
375
- ok(typeof props.table == "string");
376
- let alias = `${props.table}_0`;
377
- let sql = `from ${props.table} ${alias}`;
378
-
379
- let columns = Object.keys(
380
- /** @type {any} */ (props.schema)[props.table].entries,
381
- ).map((name) => `${alias}.${name} ${alias}_${name}`);
382
-
383
- /** @type {Record<string, { entities: any[]; table: string }>} */
384
- const aliases = {};
385
- /**
386
- * @param {number} unique
387
- * @param {string} table
388
- * @param {string} alias
389
- * @param {{ with: any }} ownJoin // eslint-disable-line @typescript-eslint/no-explicit-any
390
- */
391
- function join(unique, table, alias, ownJoin) {
392
- if (!aliases[alias]) {
393
- aliases[alias] = { entities: [], table };
394
- }
395
- for (const [name, itsJoin] of ownJoin ? Object.entries(ownJoin.with) : []) {
396
- const otherFlat = props.flat[table];
397
- ok(otherFlat);
398
- const relation = otherFlat[/** @type {keyof typeof otherFlat} */ (name)];
399
- ok(relation);
400
-
401
- const { Its, Other, Own } =
402
- /** @type {{ Its: string, Other: string, Own: string }} */ (
403
- /** @type {any} */ (relation)
404
- );
405
- const otherAlias = `${name}_${unique + 1}`;
406
- if (!aliases[otherAlias]) {
407
- aliases[otherAlias] = { entities: [], table: Other };
408
- }
409
- columns = [
410
- ...columns,
411
-
412
- ...Object.keys(/** @type {any} */ (props.schema)[Other].entries).map(
413
- (name) => `${otherAlias}.${name} ${otherAlias}_${name}`,
414
- ),
415
- ];
416
- sql += `\n left join ${Other} ${otherAlias} on ${otherAlias}.${Its} = ${alias}.${Own}`;
417
- if (typeof itsJoin == "object") {
418
- ok(itsJoin);
419
- join(unique + 1, Other, otherAlias, itsJoin);
420
- }
421
- }
422
- }
423
-
424
- join(0, props.table, alias, /** @type {any} */ (props.join));
425
- sql = `select ${columns.join(",\n")} ${sql}`;
426
-
427
- const rows = props.database.prepare(sql).all();
428
- for (const [alias, state] of Object.entries(aliases)) {
429
- /** @type {Record<string, (typeof state.entities)[0]>} */
430
- const unique = {};
431
- for (const row of rows) {
432
- /** @type {Record<string, any>} */
433
- const unwrapped = {};
434
-
435
- let empty = true;
436
- for (const name of Object.keys(
437
- /** @type {any} */ (props.schema)[state.table].entries,
438
- )) {
439
- unwrapped[name] = row[`${alias}_${name}`] ?? undefined;
440
- if (unwrapped[name] !== undefined) {
441
- empty = false;
442
- }
443
- }
444
- if (!empty) {
445
- unique[JSON.stringify(unwrapped)] = unwrapped;
446
- }
447
- }
448
- state.entities = Object.values(unique);
449
- }
450
-
451
- alias = `${props.table}_0`;
452
- /**
453
- * @param {number} unique
454
- * @param {string} table
455
- * @param {string} alias
456
- * @param {{ with: any }} ownJoin // eslint-disable-line @typescript-eslint/no-explicit-any
457
- */
458
- function merge(unique, table, alias, ownJoin) {
459
- ok(aliases[alias]);
460
- for (const [name, itsJoin] of ownJoin ? Object.entries(ownJoin.with) : []) {
461
- const otherFlat = props.flat[table];
462
- ok(otherFlat);
463
- const relation = otherFlat[/** @type {keyof typeof otherFlat} */ (name)];
464
- ok(relation);
465
-
466
- const { Its, Other, Own, type } =
467
- /** @type {{ Its: string, Other: string, Own: string, type: "referencedBy" | "references" }} */ (
468
- /** @type {any} */ (relation)
469
- );
470
- const otherAlias = `${name}_${unique + 1}`;
471
- ok(aliases[otherAlias]?.entities);
472
- for (const self of aliases[alias].entities) {
473
- const matchingOthers = aliases[otherAlias].entities.filter(
474
- (other) => other[Its] == self[Own],
475
- );
476
- self[name] = type == "references" ? matchingOthers[0] : matchingOthers;
477
- }
478
- if (typeof itsJoin == "object") {
479
- ok(itsJoin);
480
- merge(unique + 1, Other, otherAlias, itsJoin);
481
- }
482
- }
483
- }
484
-
485
- merge(0, props.table, alias, /** @type {any} */ (props.join));
486
- const state = aliases[alias];
487
- ok(state);
488
- return state.entities;
489
- }
1
+ import envPaths from "env-paths";
2
+ import { fail, ok } from "node:assert/strict";
3
+ import { mkdirSync } from "node:fs";
4
+ import { DatabaseSync } from "node:sqlite";
5
+ import { getMetadata, parse } from "valibot";
6
+
7
+ import { flatten, relations } from "./db.js";
8
+
9
+ /**
10
+ * @typedef Column
11
+ * @property {string} name
12
+ * @property {0 | 1} notnull
13
+ * @property {0 | 1} pk
14
+ * @property {"INTEGER" | "TEXT"} type
15
+ */
16
+
17
+ /** @type {typeof import("./sqlite.d.ts").sqlite} */
18
+ export function sqlite({ pkg, schema }) {
19
+ const data = pkg
20
+ ? (() => {
21
+ const { data } = envPaths(pkg.name);
22
+ mkdirSync(data, { recursive: true });
23
+ return data;
24
+ })()
25
+ : undefined;
26
+ const database = new DatabaseSync(data ? `${data}/db.sqlite3` : ":memory:", {
27
+ timeout: 1000,
28
+ });
29
+ const flat = flatten(relations(schema));
30
+
31
+ const haveTables = /** @type {{ name: string }[]} */ (
32
+ database
33
+ .prepare(
34
+ "select * from sqlite_schema where type='table' and name not like 'sqlite_%'",
35
+ )
36
+ .all()
37
+ ).map((table) => {
38
+ const columns = /** @type {Column[]} */ (
39
+ database.prepare(`pragma table_info(${table.name})`).all()
40
+ );
41
+ const foreignKeys =
42
+ /** @type {{ from: string, on_delete: "CASCADE" | "SET NULL", on_update: "CASCADE" | "SET NULL", table: string, to: string, }[]} */ (
43
+ database.prepare(`pragma foreign_key_list(${table.name})`).all()
44
+ );
45
+ return {
46
+ columns,
47
+ foreignKeys,
48
+ indices: /** @type {{ name: string }[]} */ (
49
+ database.prepare(`pragma index_list(${table.name})`).all()
50
+ )
51
+ .filter(({ name }) => !/^sqlite_/.test(name))
52
+ .map((index) => ({
53
+ ...index,
54
+ info: /** @type {{ name: string; }} */ (
55
+ database.prepare(`pragma index_info(${index.name})`).get()
56
+ ),
57
+ })),
58
+ table,
59
+ };
60
+ });
61
+
62
+ const wantTables = Object.entries(schema).map(([name, { entries }]) => ({
63
+ columns: Object.entries(entries).map(([name, maybeEntry]) => {
64
+ const entry =
65
+ /** @type {import("valibot").BaseSchema<undefined, undefined, any>} */ (
66
+ maybeEntry
67
+ );
68
+ const pk = /** @type {{ primary?: true }} */ (getMetadata(entry)).primary
69
+ ? 1
70
+ : 0;
71
+ return {
72
+ name,
73
+ notnull: !pk && entry.expects.includes("undefined") ? 0 : 1,
74
+ pk,
75
+ type:
76
+ entry.expects == "string" || entry.expects == "(string | undefined)"
77
+ ? "TEXT"
78
+ : entry.expects == "number" ||
79
+ entry.expects == "(number | undefined)"
80
+ ? "INTEGER"
81
+ : entry.expects == "Uint8Array" ||
82
+ entry.expects == "(Uint8Array | undefined)"
83
+ ? "BLOB"
84
+ : fail(name),
85
+ };
86
+ }),
87
+ foreignKeys: Object.entries(entries)
88
+ .map(([name, maybeEntry]) => {
89
+ const entry =
90
+ /** @type {import("valibot").BaseSchema<undefined, undefined, any>} */ (
91
+ maybeEntry
92
+ );
93
+
94
+ const metadata =
95
+ /** @type {{ key?: string, references?: any } | undefined} */ (
96
+ getMetadata(entry)
97
+ );
98
+ const table = metadata
99
+ ? Object.entries(schema)
100
+ .filter(([, value]) => value == metadata.references)
101
+ .find(Boolean)
102
+ : undefined;
103
+ if (!metadata?.references) {
104
+ return;
105
+ }
106
+ ok(table);
107
+ const to = Object.entries(table[1].entries).find(
108
+ ([, value]) => value == metadata.key,
109
+ );
110
+ ok(to);
111
+ return {
112
+ from: name,
113
+ on_delete: /undefined/.test(entry.expects) ? "SET NULL" : "CASCADE",
114
+ on_update: /undefined/.test(entry.expects) ? "SET NULL" : "CASCADE",
115
+ table: table[0],
116
+ to: to[0],
117
+ };
118
+ })
119
+ .filter(Boolean)
120
+ .map((x) => {
121
+ ok(x);
122
+ return x;
123
+ }),
124
+ table: { name },
125
+ }));
126
+
127
+ const migration = [];
128
+
129
+ for (const wantTable of wantTables) {
130
+ const haveTable = haveTables.find(
131
+ ({ table }) => table.name == wantTable.table.name,
132
+ );
133
+ if (haveTable) {
134
+ for (const { info, name } of haveTable.indices) {
135
+ if (!wantTable.foreignKeys.find(({ from }) => from == info.name)) {
136
+ migration.push(`drop index ${name}`);
137
+ }
138
+ }
139
+ const haveColumns = haveTable.columns;
140
+ const columnsNowMissing = haveColumns.filter(
141
+ (haveColumn) =>
142
+ !wantTable.columns.find(({ name }) => name == haveColumn.name),
143
+ );
144
+ for (const { name, pk } of columnsNowMissing) {
145
+ if (pk) {
146
+ fail(`${haveTable.table.name}.${name}: Can't drop primary key`);
147
+ }
148
+ }
149
+ for (const wantColumn of wantTable.columns) {
150
+ const haveColumn = haveColumns.find(
151
+ ({ name }) => name == wantColumn.name,
152
+ );
153
+
154
+ /** @param {Column} column */
155
+ function checkConstraint(column) {
156
+ ok(column);
157
+ ok(haveTable);
158
+ if (column.pk != wantColumn.pk) {
159
+ fail(
160
+ `${haveTable.table.name}.${column.name}: Can't toggle primary key`,
161
+ );
162
+ }
163
+ if (
164
+ !wantColumn.pk && // Without this condition I get an error with real database created by drizzle-kit.
165
+ column.notnull != wantColumn.notnull
166
+ ) {
167
+ fail(
168
+ `${haveTable.table.name}.${column.name}: Can't toggle optional`,
169
+ );
170
+ }
171
+ }
172
+
173
+ if (haveColumn) {
174
+ checkConstraint(haveColumn);
175
+ } else {
176
+ const renamedColumn = columnsNowMissing.find(
177
+ ({ notnull, type }) =>
178
+ wantColumn.type == type && wantColumn.notnull == notnull,
179
+ );
180
+ if (renamedColumn) {
181
+ checkConstraint(renamedColumn);
182
+ columnsNowMissing.splice(
183
+ columnsNowMissing.indexOf(renamedColumn),
184
+ 1,
185
+ );
186
+ migration.push(
187
+ `alter table ${wantTable.table.name} rename column ${renamedColumn.name} to ${wantColumn.name}`,
188
+ );
189
+ } else {
190
+ if (wantColumn.pk) {
191
+ fail(
192
+ `${wantTable.table.name}.${wantColumn.name}: Can't add primary key`,
193
+ );
194
+ }
195
+ migration.push(
196
+ [
197
+ `alter table ${wantTable.table.name} add column`,
198
+ wantColumn.name,
199
+ wantColumn.type,
200
+ wantColumn.notnull ? "not null" : "",
201
+ ...wantTable.foreignKeys
202
+ .map(({ from, on_delete, on_update, table, to }) =>
203
+ from == wantColumn.name
204
+ ? `references ${table}(${to}) on delete ${on_delete} on update ${on_update}`
205
+ : "",
206
+ )
207
+ .filter(Boolean),
208
+ ]
209
+ .filter(Boolean)
210
+ .join(" "),
211
+ );
212
+ }
213
+ }
214
+
215
+ for (const { from } of wantTable.foreignKeys) {
216
+ if (
217
+ from == wantColumn.name &&
218
+ !haveTable.indices.find(({ info }) => info.name == from)
219
+ ) {
220
+ migration.push(
221
+ `create index ${wantTable.table.name}_${from} on ${wantTable.table.name}(${from})`,
222
+ );
223
+ }
224
+ }
225
+ }
226
+ for (const missingColumn of columnsNowMissing) {
227
+ migration.push(
228
+ `alter table ${wantTable.table.name} drop column ${missingColumn.name}`,
229
+ );
230
+ }
231
+ } else {
232
+ if (!wantTable.columns.some(({ pk }) => pk)) {
233
+ fail(`${wantTable.table.name} has no primary key`);
234
+ }
235
+ migration.push(
236
+ `create table ${wantTable.table.name} (\n` +
237
+ wantTable.columns
238
+ .map(({ name, notnull, pk, type }) =>
239
+ [
240
+ name,
241
+ type,
242
+ pk ? "primary key" : "",
243
+ !pk && notnull ? "not null" : "",
244
+ ...wantTable.foreignKeys
245
+ .map(({ from, on_delete, on_update, table, to }) =>
246
+ from == name
247
+ ? `references ${table}(${to}) on delete ${on_delete} on update ${on_update}`
248
+ : "",
249
+ )
250
+ .filter(Boolean),
251
+ ]
252
+ .filter(Boolean)
253
+ .join(" "),
254
+ )
255
+ .join(",\n") +
256
+ `)`,
257
+ );
258
+ for (const { from } of wantTable.foreignKeys) {
259
+ migration.push(
260
+ `create index ${wantTable.table.name}_${from} on ${wantTable.table.name}(${from})`,
261
+ );
262
+ }
263
+ }
264
+ }
265
+
266
+ for (const haveTable of haveTables) {
267
+ const wantTable = wantTables.find(
268
+ ({ table }) => table.name == haveTable.table.name,
269
+ );
270
+ if (!wantTable) {
271
+ migration.push(`drop table ${haveTable.table.name}`);
272
+ }
273
+ }
274
+
275
+ for (const sql of migration) {
276
+ database.exec(sql);
277
+ }
278
+
279
+ return /** @type {any} */ (
280
+ Object.fromEntries(
281
+ Object.entries(schema).map(([maybeTable]) => {
282
+ const table = /** @type {keyof typeof schema & string} */ (maybeTable);
283
+
284
+ /** @param {{ [K in keyof (typeof schema)[typeof table]["entries"]]?: import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]> | import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]>[]; }} where */
285
+ function ensureArrayRecord(where) {
286
+ for (const key of Object.keys(where)) {
287
+ if (!Array.isArray(where[key])) {
288
+ Object.assign(where, { [key]: [where[key]] });
289
+ }
290
+ }
291
+ }
292
+
293
+ return [
294
+ table,
295
+ {
296
+ /** @param {{ [K in keyof (typeof schema)[typeof table]["entries"]]?: import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]> | import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]>[]; }} where */
297
+ delete(where) {
298
+ ensureArrayRecord(where);
299
+ database
300
+ .prepare(
301
+ `delete from ${table} where true\n` +
302
+ Object.entries(where)
303
+ .map(
304
+ ([name, values]) =>
305
+ `and ${name} in (${values.map(() => "?").join(", ")})`,
306
+ )
307
+ .join("\n"),
308
+ )
309
+ .run(
310
+ ...Object.values(where)
311
+ .flatMap((values) => values)
312
+ .map((value) => value ?? null),
313
+ );
314
+ },
315
+
316
+ /** @param {any} join */
317
+ select(join) {
318
+ return select({ database, flat, join, schema, table });
319
+ },
320
+
321
+ /** @param {{ set: Partial<import("valibot").InferOutput<(typeof schema)[typeof table]>>, where: { [K in keyof (typeof schema)[typeof table]["entries"]]?: import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]> | import("valibot").InferOutput<(typeof schema)[typeof table]["entries"][K]>[]; } }} props */
322
+ update({ set, where }) {
323
+ ensureArrayRecord(where);
324
+ database
325
+ .prepare(
326
+ `update ${table} set\n` +
327
+ Object.keys(set)
328
+ .map((name) => `${name} = ?`)
329
+ .join(",\n") +
330
+ `\nwhere true\n` +
331
+ Object.entries(where)
332
+ .map(
333
+ ([name, values]) =>
334
+ `and ${name} in (${values.map(() => "?").join(", ")})`,
335
+ )
336
+ .join("\n"),
337
+ )
338
+ .run(
339
+ ...[
340
+ ...Object.values(set).map((value) => value ?? null),
341
+ ...Object.values(where)
342
+ .flatMap((values) => values)
343
+ .map((value) => value ?? null),
344
+ ],
345
+ );
346
+ },
347
+
348
+ /** @param {any[]} values */
349
+ upsert(...values) {
350
+ ok(schema[table]);
351
+ const columns = Object.entries(schema[table].entries);
352
+ const primaryKey = columns.find(
353
+ ([, column]) =>
354
+ /** @type {{ primary?: true }} */ (getMetadata(column))
355
+ .primary,
356
+ );
357
+ ok(primaryKey);
358
+ const query = database.prepare(
359
+ `insert into ${table} (${columns.map(([name]) => name).join(", ")}) values\n` +
360
+ `(${columns.map(() => "?").join(", ")})\n` +
361
+ `on conflict (${primaryKey[0]}) do update set\n` +
362
+ columns.map(([name]) => `${name} = ?`).join(",\n"),
363
+ );
364
+ const outputs = [];
365
+ for (const row of values) {
366
+ const output = parse(schema[table], row);
367
+ const values = columns.map(([name]) => output[name] ?? null);
368
+ query.run(...values, ...values);
369
+ outputs.push(output);
370
+ }
371
+ return outputs;
372
+ },
373
+ },
374
+ ];
375
+ }),
376
+ )
377
+ );
378
+ }
379
+
380
+ /** @type {<Schema extends Record<string, import("valibot").ObjectSchema<any, any>>, R extends import("./db.d.ts").Relations<Schema> = import("./db.d.ts").Relations<Schema>, F extends import("./db.d.ts").Flatten<Schema> = import("./db.d.ts").Flatten<Schema>, Table extends keyof F & keyof Schema = keyof F & keyof Schema >(props: { database: DatabaseSync; flat: F; join?: import("./db.d.ts").Join<R, Table>; schema: Schema; table: Table; }) => any[]} */
381
+ function select(props) {
382
+ ok(typeof props.table == "string");
383
+ let alias = `${props.table}_0`;
384
+ let sql = `from ${props.table} ${alias}`;
385
+
386
+ let columns = Object.keys(
387
+ /** @type {any} */ (props.schema)[props.table].entries,
388
+ ).map((name) => `${alias}.${name} ${alias}_${name}`);
389
+
390
+ /** @type {Record<string, { entities: any[]; table: string }>} */
391
+ const aliases = {};
392
+ /**
393
+ * @param {number} unique
394
+ * @param {string} table
395
+ * @param {string} alias
396
+ * @param {{ with: any }} ownJoin // eslint-disable-line @typescript-eslint/no-explicit-any
397
+ */
398
+ function join(unique, table, alias, ownJoin) {
399
+ if (!aliases[alias]) {
400
+ aliases[alias] = { entities: [], table };
401
+ }
402
+ for (const [name, itsJoin] of ownJoin ? Object.entries(ownJoin.with) : []) {
403
+ const otherFlat = props.flat[table];
404
+ ok(otherFlat);
405
+ const relation = otherFlat[/** @type {keyof typeof otherFlat} */ (name)];
406
+ ok(relation);
407
+
408
+ const { Its, Other, Own } =
409
+ /** @type {{ Its: string, Other: string, Own: string }} */ (
410
+ /** @type {any} */ (relation)
411
+ );
412
+ const otherAlias = `${name}_${unique + 1}`;
413
+ if (!aliases[otherAlias]) {
414
+ aliases[otherAlias] = { entities: [], table: Other };
415
+ }
416
+ columns = [
417
+ ...columns,
418
+
419
+ ...Object.keys(/** @type {any} */ (props.schema)[Other].entries).map(
420
+ (name) => `${otherAlias}.${name} ${otherAlias}_${name}`,
421
+ ),
422
+ ];
423
+ sql += `\n left join ${Other} ${otherAlias} on ${otherAlias}.${Its} = ${alias}.${Own}`;
424
+ if (typeof itsJoin == "object") {
425
+ ok(itsJoin);
426
+ join(unique + 1, Other, otherAlias, itsJoin);
427
+ }
428
+ }
429
+ }
430
+
431
+ join(0, props.table, alias, /** @type {any} */ (props.join));
432
+ sql = `select ${columns.join(",\n")} ${sql}`;
433
+
434
+ const rows = props.database.prepare(sql).all();
435
+ for (const [alias, state] of Object.entries(aliases)) {
436
+ /** @type {Record<string, (typeof state.entities)[0]>} */
437
+ const unique = {};
438
+ for (const row of rows) {
439
+ /** @type {Record<string, any>} */
440
+ const unwrapped = {};
441
+
442
+ let empty = true;
443
+ for (const name of Object.keys(
444
+ /** @type {any} */ (props.schema)[state.table].entries,
445
+ )) {
446
+ unwrapped[name] = row[`${alias}_${name}`] ?? undefined;
447
+ if (unwrapped[name] !== undefined) {
448
+ empty = false;
449
+ }
450
+ }
451
+ if (!empty) {
452
+ unique[JSON.stringify(unwrapped)] = unwrapped;
453
+ }
454
+ }
455
+ state.entities = Object.values(unique);
456
+ }
457
+
458
+ alias = `${props.table}_0`;
459
+ /**
460
+ * @param {number} unique
461
+ * @param {string} table
462
+ * @param {string} alias
463
+ * @param {{ with: any }} ownJoin // eslint-disable-line @typescript-eslint/no-explicit-any
464
+ */
465
+ function merge(unique, table, alias, ownJoin) {
466
+ ok(aliases[alias]);
467
+ for (const [name, itsJoin] of ownJoin ? Object.entries(ownJoin.with) : []) {
468
+ const otherFlat = props.flat[table];
469
+ ok(otherFlat);
470
+ const relation = otherFlat[/** @type {keyof typeof otherFlat} */ (name)];
471
+ ok(relation);
472
+
473
+ const { Its, Other, Own, type } =
474
+ /** @type {{ Its: string, Other: string, Own: string, type: "referencedBy" | "references" }} */ (
475
+ /** @type {any} */ (relation)
476
+ );
477
+ const otherAlias = `${name}_${unique + 1}`;
478
+ ok(aliases[otherAlias]?.entities);
479
+ for (const self of aliases[alias].entities) {
480
+ const matchingOthers = aliases[otherAlias].entities.filter(
481
+ (other) => other[Its] == self[Own],
482
+ );
483
+ self[name] = type == "references" ? matchingOthers[0] : matchingOthers;
484
+ }
485
+ if (typeof itsJoin == "object") {
486
+ ok(itsJoin);
487
+ merge(unique + 1, Other, otherAlias, itsJoin);
488
+ }
489
+ }
490
+ }
491
+
492
+ merge(0, props.table, alias, /** @type {any} */ (props.join));
493
+ const state = aliases[alias];
494
+ ok(state);
495
+ return state.entities;
496
+ }