ras-stack 0.9.0 → 0.10.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
@@ -16,6 +16,7 @@ The package contains independent helpers for common application infrastructure:
16
16
  - **Server requests:** same-origin mutation guards, error-normalizing RPC wrappers, canonical-host redirects, and health responses.
17
17
  - **Realtime:** Centrifugo client lifecycle, token signing, presence synchronization, and a bounded publisher with retries and graceful shutdown.
18
18
  - **Email:** SMTP environment parsing and a small Nodemailer delivery interface.
19
+ - **Database:** standard Better SQLite lifecycle and Drizzle migration mechanics that preserve the native typed database.
19
20
  - **Uploads:** a promise-based wrapper around resumable `tus-js-client` uploads.
20
21
  - **Project configuration:** shared TypeScript and Oxlint bases.
21
22
 
@@ -35,12 +36,13 @@ The libraries underneath remain available normally. Applications still configure
35
36
  pnpm add ras-stack
36
37
  ```
37
38
 
38
- Nodemailer, Centrifuge, and `tus-js-client` are optional peer dependencies. Install one only when using its integration:
39
+ Nodemailer, Centrifuge, Better SQLite/Drizzle, and `tus-js-client` are optional peer dependencies. Install them only when using their integrations:
39
40
 
40
41
  ```sh
41
42
  pnpm add nodemailer
42
43
  pnpm add centrifuge
43
44
  pnpm add tus-js-client
45
+ pnpm add better-sqlite3 drizzle-orm
44
46
  ```
45
47
 
46
48
  ## Authentication and request security
@@ -88,6 +90,27 @@ Applications still own their auth clients, authorization, routes, router, loggin
88
90
 
89
91
  Only enable `trustForwardedHeaders` behind a proxy that replaces incoming forwarded headers. Otherwise a client could choose the origin used by the check.
90
92
 
93
+ ## Database lifecycle
94
+
95
+ The SQLite entrypoint owns the native client lifecycle, standard safety PRAGMAs, and optional Drizzle migrations while returning the upstream typed database:
96
+
97
+ ```ts
98
+ import { bundledDirectory } from 'ras-stack/database'
99
+ import { openDrizzleSqlite } from 'ras-stack/database/sqlite'
100
+
101
+ const database = openDrizzleSqlite({
102
+ file,
103
+ schema,
104
+ migrationsFolder: bundledDirectory({
105
+ developmentUrl: new URL('../../drizzle', import.meta.url),
106
+ production: import.meta.env.PROD,
107
+ name: 'drizzle',
108
+ }),
109
+ })
110
+ ```
111
+
112
+ Applications retain their schema, migrations, database path, repositories, transactions, and driver selection. Dual-database applications can use `openSqliteClient()` and `configureSqlite()` beneath their own wrapper without forcing PostgreSQL through a shared database interface.
113
+
91
114
  ## Realtime updates
92
115
 
93
116
  Applications choose their channel names, authorize subscriptions, and define payloads. `ras-stack` handles Centrifugo's HTTP publication, signed tokens, and repeated browser lifecycle mechanics:
@@ -236,14 +259,14 @@ The JavaScript setup action reads the Node version from `engines.node` and the p
236
259
  ```yaml
237
260
  steps:
238
261
  - uses: actions/checkout@v7
239
- - uses: richardsolomou/ras-stack/actions/setup-js@v0.3.0
262
+ - uses: richardsolomou/ras-stack/actions/setup-js@v0.9.0
240
263
  - run: pnpm check
241
264
  ```
242
265
 
243
266
  Just is independent of the application language and is installed separately when a repository uses it:
244
267
 
245
268
  ```yaml
246
- - uses: richardsolomou/ras-stack/actions/setup-just@v0.2.0
269
+ - uses: richardsolomou/ras-stack/actions/setup-just@v0.9.0
247
270
  with:
248
271
  version: '1.58.0'
249
272
  ```
@@ -256,7 +279,7 @@ release:
256
279
  needs: [check]
257
280
  permissions:
258
281
  contents: write
259
- uses: richardsolomou/ras-stack/.github/workflows/release-changesets.yml@v0.3.0
282
+ uses: richardsolomou/ras-stack/.github/workflows/release-changesets.yml@v0.9.0
260
283
  secrets: inherit
261
284
  ```
262
285
 
@@ -0,0 +1,8 @@
1
+ import { type URL } from 'node:url';
2
+ export type BundledDirectoryOptions = {
3
+ developmentUrl: URL;
4
+ production: boolean;
5
+ productionEntry?: string;
6
+ name: string;
7
+ };
8
+ export declare function bundledDirectory(options: BundledDirectoryOptions): string;
@@ -0,0 +1,11 @@
1
+ import path from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
3
+ export function bundledDirectory(options) {
4
+ if (!options.production)
5
+ return fileURLToPath(options.developmentUrl);
6
+ const entry = options.productionEntry ?? process.argv[1];
7
+ if (!entry)
8
+ throw new Error('production entrypoint is required to resolve a bundled directory');
9
+ return path.join(path.dirname(entry), options.name);
10
+ }
11
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/database/index.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,WAAW,CAAA;AAC5B,OAAO,EAAE,aAAa,EAAY,MAAM,UAAU,CAAA;AASlD,MAAM,UAAU,gBAAgB,CAAC,OAAgC;IAC/D,IAAI,CAAC,OAAO,CAAC,UAAU;QAAE,OAAO,aAAa,CAAC,OAAO,CAAC,cAAc,CAAC,CAAA;IACrE,MAAM,KAAK,GAAG,OAAO,CAAC,eAAe,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAA;IACxD,IAAI,CAAC,KAAK;QAAE,MAAM,IAAI,KAAK,CAAC,kEAAkE,CAAC,CAAA;IAC/F,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,OAAO,CAAC,IAAI,CAAC,CAAA;AACrD,CAAC","sourcesContent":["import path from 'node:path'\nimport { fileURLToPath, type URL } from 'node:url'\n\nexport type BundledDirectoryOptions = {\n developmentUrl: URL\n production: boolean\n productionEntry?: string\n name: string\n}\n\nexport function bundledDirectory(options: BundledDirectoryOptions) {\n if (!options.production) return fileURLToPath(options.developmentUrl)\n const entry = options.productionEntry ?? process.argv[1]\n if (!entry) throw new Error('production entrypoint is required to resolve a bundled directory')\n return path.join(path.dirname(entry), options.name)\n}\n"]}
@@ -0,0 +1,25 @@
1
+ import Database from 'better-sqlite3';
2
+ import { type BetterSQLite3Database } from 'drizzle-orm/better-sqlite3';
3
+ export type SqlitePragmas = {
4
+ journalMode?: 'DELETE' | 'TRUNCATE' | 'PERSIST' | 'MEMORY' | 'WAL' | 'OFF';
5
+ synchronous?: 'OFF' | 'NORMAL' | 'FULL' | 'EXTRA';
6
+ busyTimeout?: number;
7
+ foreignKeys?: boolean;
8
+ };
9
+ export type OpenSqliteOptions = {
10
+ database?: Database.Options;
11
+ pragmas?: SqlitePragmas | false;
12
+ };
13
+ export declare function openSqliteClient(file: string, options?: OpenSqliteOptions): Database.Database;
14
+ export declare function configureSqlite(client: Database.Database, pragmas?: SqlitePragmas): Database.Database;
15
+ export type OpenDrizzleSqliteOptions<TSchema extends Record<string, unknown>> = OpenSqliteOptions & {
16
+ file: string;
17
+ schema: TSchema;
18
+ migrationsFolder?: string;
19
+ };
20
+ export declare function openDrizzleSqlite<TSchema extends Record<string, unknown>>(options: OpenDrizzleSqliteOptions<TSchema>): BetterSQLite3Database<TSchema> & {
21
+ $client: Database.Database;
22
+ };
23
+ export declare function closeDrizzleSqlite(database: {
24
+ $client: Database.Database;
25
+ }): void;
@@ -0,0 +1,49 @@
1
+ import Database from 'better-sqlite3';
2
+ import { drizzle } from 'drizzle-orm/better-sqlite3';
3
+ import { migrate } from 'drizzle-orm/better-sqlite3/migrator';
4
+ import { mkdirSync } from 'node:fs';
5
+ import path from 'node:path';
6
+ export function openSqliteClient(file, options = {}) {
7
+ if (file !== ':memory:')
8
+ mkdirSync(path.dirname(path.resolve(file)), { recursive: true });
9
+ const client = new Database(file, options.database);
10
+ try {
11
+ if (options.pragmas !== false)
12
+ configureSqlite(client, options.pragmas);
13
+ return client;
14
+ }
15
+ catch (error) {
16
+ client.close();
17
+ throw error;
18
+ }
19
+ }
20
+ export function configureSqlite(client, pragmas = {}) {
21
+ const journalMode = pragmas.journalMode ?? 'WAL';
22
+ const synchronous = pragmas.synchronous ?? 'FULL';
23
+ const busyTimeout = pragmas.busyTimeout ?? 5000;
24
+ const foreignKeys = pragmas.foreignKeys ?? true;
25
+ if (!Number.isInteger(busyTimeout) || busyTimeout < 0)
26
+ throw new Error('busyTimeout must be a non-negative integer');
27
+ client.pragma(`journal_mode = ${journalMode}`);
28
+ client.pragma(`synchronous = ${synchronous}`);
29
+ client.pragma(`busy_timeout = ${busyTimeout}`);
30
+ client.pragma(`foreign_keys = ${foreignKeys ? 'ON' : 'OFF'}`);
31
+ return client;
32
+ }
33
+ export function openDrizzleSqlite(options) {
34
+ const client = openSqliteClient(options.file, options);
35
+ try {
36
+ const database = drizzle({ client, schema: options.schema });
37
+ if (options.migrationsFolder)
38
+ migrate(database, { migrationsFolder: options.migrationsFolder });
39
+ return database;
40
+ }
41
+ catch (error) {
42
+ client.close();
43
+ throw error;
44
+ }
45
+ }
46
+ export function closeDrizzleSqlite(database) {
47
+ database.$client.close();
48
+ }
49
+ //# sourceMappingURL=sqlite.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqlite.js","sourceRoot":"","sources":["../../src/database/sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,MAAM,gBAAgB,CAAA;AACrC,OAAO,EAAE,OAAO,EAA8B,MAAM,4BAA4B,CAAA;AAChF,OAAO,EAAE,OAAO,EAAE,MAAM,qCAAqC,CAAA;AAC7D,OAAO,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AACnC,OAAO,IAAI,MAAM,WAAW,CAAA;AAc5B,MAAM,UAAU,gBAAgB,CAAC,IAAY,EAAE,OAAO,GAAsB,EAAE;IAC5E,IAAI,IAAI,KAAK,UAAU;QAAE,SAAS,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAA;IACzF,MAAM,MAAM,GAAG,IAAI,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAA;IACnD,IAAI,CAAC;QACH,IAAI,OAAO,CAAC,OAAO,KAAK,KAAK;YAAE,eAAe,CAAC,MAAM,EAAE,OAAO,CAAC,OAAO,CAAC,CAAA;QACvE,OAAO,MAAM,CAAA;IACf,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,MAAM,CAAC,KAAK,EAAE,CAAA;QACd,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K,EAAE,CAAC;QACf,MAAM,CAAC,KAAK,EAAE,CAAA;QACd,MAAM,KAAK,CAAA;IACb,CAAC;AACH,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,QAAwC;IACzE,QAAQ,CAAC,OAAO,CAAC,KAAK,EAAE,CAAA;AAC1B,CAAC","sourcesContent":["import Database from 'better-sqlite3'\nimport { drizzle, type BetterSQLite3Database } from 'drizzle-orm/better-sqlite3'\nimport { migrate } from 'drizzle-orm/better-sqlite3/migrator'\nimport { mkdirSync } from 'node:fs'\nimport path from 'node:path'\n\nexport type SqlitePragmas = {\n journalMode?: 'DELETE' | 'TRUNCATE' | 'PERSIST' | 'MEMORY' | 'WAL' | 'OFF'\n synchronous?: 'OFF' | 'NORMAL' | 'FULL' | 'EXTRA'\n busyTimeout?: number\n foreignKeys?: boolean\n}\n\nexport type OpenSqliteOptions = {\n database?: Database.Options\n pragmas?: SqlitePragmas | false\n}\n\nexport function openSqliteClient(file: string, options: OpenSqliteOptions = {}): Database.Database {\n if (file !== ':memory:') mkdirSync(path.dirname(path.resolve(file)), { recursive: true })\n const client = new Database(file, options.database)\n try {\n if (options.pragmas !== false) configureSqlite(client, options.pragmas)\n return client\n } catch (error) {\n client.close()\n throw error\n }\n}\n\nexport function configureSqlite(client: Database.Database, pragmas: SqlitePragmas = {}): Database.Database {\n const journalMode = pragmas.journalMode ?? 'WAL'\n const synchronous = pragmas.synchronous ?? 'FULL'\n const busyTimeout = pragmas.busyTimeout ?? 5000\n const foreignKeys = pragmas.foreignKeys ?? true\n if (!Number.isInteger(busyTimeout) || busyTimeout < 0) throw new Error('busyTimeout must be a non-negative integer')\n client.pragma(`journal_mode = ${journalMode}`)\n client.pragma(`synchronous = ${synchronous}`)\n client.pragma(`busy_timeout = ${busyTimeout}`)\n client.pragma(`foreign_keys = ${foreignKeys ? 'ON' : 'OFF'}`)\n return client\n}\n\nexport type OpenDrizzleSqliteOptions<TSchema extends Record<string, unknown>> = OpenSqliteOptions & {\n file: string\n schema: TSchema\n migrationsFolder?: string\n}\n\nexport function openDrizzleSqlite<TSchema extends Record<string, unknown>>(\n options: OpenDrizzleSqliteOptions<TSchema>,\n): BetterSQLite3Database<TSchema> & { $client: Database.Database } {\n const client = openSqliteClient(options.file, options)\n try {\n const database = drizzle({ client, schema: options.schema })\n if (options.migrationsFolder) migrate(database, { migrationsFolder: options.migrationsFolder })\n return database\n } catch (error) {\n client.close()\n throw error\n }\n}\n\nexport function closeDrizzleSqlite(database: { $client: Database.Database }) {\n database.$client.close()\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ras-stack",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "Composable full-stack primitives shared across Richard Solomou's applications.",
5
5
  "keywords": [
6
6
  "authentication",
@@ -34,6 +34,14 @@
34
34
  "types": "./dist/email/index.d.ts",
35
35
  "default": "./dist/email/index.js"
36
36
  },
37
+ "./database": {
38
+ "types": "./dist/database/index.d.ts",
39
+ "default": "./dist/database/index.js"
40
+ },
41
+ "./database/sqlite": {
42
+ "types": "./dist/database/sqlite.d.ts",
43
+ "default": "./dist/database/sqlite.js"
44
+ },
37
45
  "./policy": {
38
46
  "types": "./dist/policy/index.d.ts",
39
47
  "default": "./dist/policy/index.js"
@@ -94,9 +102,12 @@
94
102
  "@changesets/cli": "^2.31.1",
95
103
  "@tanstack/react-query": "^5.101.4",
96
104
  "@tanstack/react-start": "^1.168.32",
105
+ "@types/better-sqlite3": "9.6.0",
97
106
  "@types/node": "^26.0.0",
98
107
  "@types/nodemailer": "^8.0.1",
108
+ "better-sqlite3": "12.11.1",
99
109
  "centrifuge": "5.7.0",
110
+ "drizzle-orm": "0.45.2",
100
111
  "nodemailer": "^9.0.3",
101
112
  "oxfmt": "^0.61.0",
102
113
  "oxlint": "^1.74.0",
@@ -109,7 +120,9 @@
109
120
  "peerDependencies": {
110
121
  "@tanstack/react-query": ">=5 <6",
111
122
  "@tanstack/react-start": ">=1 <2",
123
+ "better-sqlite3": ">=12 <13",
112
124
  "centrifuge": ">=5 <6",
125
+ "drizzle-orm": ">=0.45 <1",
113
126
  "nodemailer": ">=9 <10",
114
127
  "tus-js-client": ">=4 <5"
115
128
  },
@@ -120,9 +133,15 @@
120
133
  "@tanstack/react-start": {
121
134
  "optional": true
122
135
  },
136
+ "better-sqlite3": {
137
+ "optional": true
138
+ },
123
139
  "centrifuge": {
124
140
  "optional": true
125
141
  },
142
+ "drizzle-orm": {
143
+ "optional": true
144
+ },
126
145
  "nodemailer": {
127
146
  "optional": true
128
147
  },