evikit 1.0.3 → 2.0.0

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/README.md CHANGED
@@ -78,7 +78,6 @@ node_modules
78
78
  *~
79
79
  *swp
80
80
  .env
81
- *.db
82
81
  ```
83
82
 
84
83
  ### Database schema
@@ -114,16 +113,19 @@ Export the database at `src/lib/db/index.js`:
114
113
  ```js
115
114
  import { sqlite } from "evikit/sqlite";
116
115
 
116
+ import pkg from "../../../package.json" with { type: "json" };
117
117
  import * as schema from "./schema.js";
118
118
 
119
- export const db = sqlite("data/local.db", { schema });
119
+ export const db = sqlite({ pkg, schema });
120
120
  ```
121
121
 
122
122
  - See ChoiceServer below for an example of `upsert` returning a created row.
123
123
  - See QuestionIdServer below for an example of `select` with a join.
124
124
  - See VoteServer below for an example of `update` and `delete`.
125
125
 
126
- Create an empty `data/.gitkeep` file.
126
+ The database is kept under the [OS-specific path](https://github.com/sindresorhus/env-paths) for your application, such as `~/.local/share/mysite-nodejs/db.sqlite3` on GNU/Linux.
127
+
128
+ If you want a temporary, in-memory database, omit `pkg`, pass just `{ schema }`.
127
129
 
128
130
  ### Routing
129
131
 
package/db.d.ts CHANGED
@@ -225,17 +225,17 @@ export function flatten<
225
225
  Schema extends Record<string, ObjectSchema<any, any>>, // eslint-disable-line @typescript-eslint/no-explicit-any
226
226
  >(relations: Relations<Schema>): Flatten<Schema>;
227
227
 
228
- export function primary(): MetadataAction<any, { primary: true }>; // eslint-disable-line @typescript-eslint/no-explicit-any
228
+ export function primary<T>(): MetadataAction<T, { primary: true }>;
229
229
 
230
230
  export function references<
231
231
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
232
232
  R extends { entries: any },
233
233
  K extends keyof R["entries"],
234
+ T,
234
235
  >(
235
236
  references: R,
236
237
  key: K,
237
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
238
- ): MetadataAction<any, { key: R["entries"][K]; references: R }>;
238
+ ): MetadataAction<T, { key: R["entries"][K]; references: R }>;
239
239
 
240
240
  export function relations<
241
241
  Schema extends Record<string, ObjectSchema<any, any>>, // eslint-disable-line @typescript-eslint/no-explicit-any
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": "1.0.3",
4
+ "version": "2.0.0",
5
5
  "type": "module",
6
6
  "exports": {
7
7
  ".": "./index.js",
@@ -24,12 +24,11 @@
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/lodash": "^4.17.24",
28
27
  "@types/node": "^25",
29
28
  "@valibot/to-json-schema": "^1.7.0",
29
+ "env-paths": "^4.0.0",
30
30
  "gettext-parser": "^9.0.2",
31
31
  "gettext.js": "^2.0.3",
32
- "lodash": "^4.18.1",
33
32
  "preact-render-to-string": "^6.7.0"
34
33
  },
35
34
  "peerDependencies": {
package/server.js CHANGED
@@ -2,7 +2,6 @@ import { Busboy } from "@fastify/busboy";
2
2
  import { toJsonSchema } from "@valibot/to-json-schema";
3
3
  import acceptLanguage from "accept-language";
4
4
  import { po } from "gettext-parser";
5
- import groupBy from "lodash/groupBy.js";
6
5
  import { readFile } from "node:fs/promises";
7
6
  import { Fragment, h } from "preact";
8
7
  import { renderToString } from "preact-render-to-string";
@@ -71,7 +70,7 @@ export function endpoint({ input, outputs }, handler) {
71
70
  : undefined),
72
71
  ...Object.fromEntries(
73
72
  Object.entries(
74
- groupBy(
73
+ Object.groupBy(
75
74
  [
76
75
  ...new URL(
77
76
  `http://${req.headers.host}${req.url}`,
@@ -80,7 +79,7 @@ export function endpoint({ input, outputs }, handler) {
80
79
  ([name]) => name || "",
81
80
  ),
82
81
  ).map(([name, group]) => {
83
- const values = group.map((value) => value[1]);
82
+ const values = (group || []).map((value) => value[1]);
84
83
  return [name, values.length == 1 ? values[0] : values];
85
84
  }),
86
85
  ),
package/sqlite.d.ts CHANGED
@@ -5,4 +5,10 @@ import type { Db } from "./db.d.ts";
5
5
  export function sqlite<
6
6
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
7
7
  Schema extends Record<string, ObjectSchema<any, any>>,
8
- >(path: string, { schema }: { schema: Schema }): Db<Schema>;
8
+ >({
9
+ pkg,
10
+ schema,
11
+ }: {
12
+ pkg?: { name: string; version: string };
13
+ schema: Schema;
14
+ }): Db<Schema>;
package/sqlite.js CHANGED
@@ -1,12 +1,23 @@
1
+ import envPaths from "env-paths";
1
2
  import { fail, ok } from "node:assert/strict";
3
+ import { mkdirSync } from "node:fs";
2
4
  import { DatabaseSync } from "node:sqlite";
3
5
  import { getMetadata, parse } from "valibot";
4
6
 
5
7
  import { flatten, relations } from "./db.js";
6
8
 
7
9
  /** @type {typeof import("./sqlite.d.ts").sqlite} */
8
- export function sqlite(path, { schema }) {
9
- const database = new DatabaseSync(path, { timeout: 1000 });
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
+ });
10
21
  const flat = flatten(relations(schema));
11
22
 
12
23
  const haveTables = /** @type {{ name: string }[]} */ (