js-bao 0.5.0 → 0.5.2

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
@@ -29,17 +29,21 @@ npm install js-bao yjs
29
29
  yarn add js-bao yjs
30
30
  # or
31
31
  pnpm add js-bao yjs
32
-
33
- # For SQL.js engine:
34
- npm install sql.js
35
32
  ```
36
33
 
34
+ The default **SQL.js** engine works out of the box in both the browser and
35
+ headless Node — `sql.js` is a direct dependency of `js-bao`, so there's nothing
36
+ extra to install. (In the browser you do need to serve `sql-wasm.wasm`; see
37
+ [SQL.js — the universal default](#sqljs-type-sqljs--the-universal-default).)
38
+
37
39
  ### Optional Dependencies
38
40
 
39
- For Node.js environments, you can install native database engines:
41
+ For native, file-backed SQLite in Node.js (the `node-sqlite` engine), install
42
+ `better-sqlite3`. This is a **one-line opt-in** — `js-bao` never switches engines
43
+ on you automatically:
40
44
 
41
45
  ```bash
42
- # For SQLite support in Node.js
46
+ # Native SQLite in Node.js (then set databaseConfig: { type: "node-sqlite" })
43
47
  npm install better-sqlite3
44
48
  ```
45
49
 
@@ -51,8 +55,10 @@ npm install better-sqlite3
51
55
 
52
56
  ### Node.js
53
57
 
54
- - **better-sqlite3**: Native SQLite with file system support
55
- - **SQL.js**: SQLite WASM (fallback option)
58
+ - **SQL.js (`sqljs`)**: SQLite WASM — the default; works out of the box in
59
+ headless Node (resolves its wasm automatically, no config).
60
+ - **better-sqlite3 (`node-sqlite`)**: native, file-backed SQLite. One-line opt-in
61
+ (`npm install better-sqlite3` + `databaseConfig: { type: "node-sqlite" }`).
56
62
 
57
63
  ## Core Concepts
58
64
 
@@ -1251,22 +1257,106 @@ const tagStats = await Article.aggregate({
1251
1257
 
1252
1258
  ## Database Engine Specifics
1253
1259
 
1254
- ### SQL.js (`type: 'sqljs'`)
1255
-
1256
- - **WASM File**: `sql-wasm.wasm` (from the `sql.js` package) must be publicly accessible in your application.
1257
- - By default, the library expects it at `/sql-wasm.wasm` (root of your public server path).
1258
- - **Vite/Create React App**: Place `sql-wasm.wasm` in your project's `public` directory.
1259
- - **Custom Path**: If the WASM file is located elsewhere, configure it in `DatabaseConfig`:
1260
- ```typescript
1261
- const dbConfig: DatabaseConfig = {
1262
- type: "sqljs",
1263
- options: {
1264
- wasmURL: "/path/to/your/sql-wasm.wasm",
1265
- // or use locateFile for more complex scenarios:
1266
- // locateFile: (file) => `/assets/wasm/${file}`
1267
- },
1268
- };
1269
- ```
1260
+ ### SQL.js (`type: 'sqljs'`) — the universal default
1261
+
1262
+ `sqljs` is SQLite compiled to WebAssembly. It is the **default engine** and now
1263
+ works **out of the box in both the browser and headless Node** — no manual wasm
1264
+ configuration required in either environment.
1265
+
1266
+ #### Browser
1267
+
1268
+ The `sql-wasm.wasm` file (from the `sql.js` package) must be served from your
1269
+ app. By default the library expects it at `/sql-wasm.wasm` (the root of your
1270
+ public server path).
1271
+
1272
+ - **Vite / Create React App**: copy `node_modules/sql.js/dist/sql-wasm.wasm` into
1273
+ your project's `public/` directory so it is served at `/sql-wasm.wasm`.
1274
+ - **Custom path**: if the wasm lives elsewhere, point at it via `DatabaseConfig`:
1275
+
1276
+ ```typescript
1277
+ const dbConfig: DatabaseConfig = {
1278
+ type: "sqljs",
1279
+ options: {
1280
+ wasmURL: "/assets/wasm/sql-wasm.wasm",
1281
+ // or, for more complex layouts:
1282
+ // locateFile: (file) => `/assets/wasm/${file}`,
1283
+ },
1284
+ };
1285
+ ```
1286
+
1287
+ #### Headless Node
1288
+
1289
+ Under Node, `sqljs` automatically resolves sql.js's bundled
1290
+ `sql-wasm.wasm` **relative to the installed js-bao package** (using
1291
+ `createRequire(import.meta.url).resolve("sql.js/dist/sql-wasm.wasm")`, with a
1292
+ `__dirname`-based fallback for the CommonJS build). **No `wasmURL` / `locateFile`
1293
+ override and no extra setup is needed** — `new SqljsEngine()` (or the client's
1294
+ default `{ type: "sqljs" }`) just works:
1295
+
1296
+ ```typescript
1297
+ import { SqljsEngine } from "js-bao/node";
1298
+
1299
+ const engine = new SqljsEngine(); // resolves sql.js's wasm automatically
1300
+ await engine.ensureReady();
1301
+ ```
1302
+
1303
+ An explicit `wasmURL` / `locateFile` still takes precedence if you provide one.
1304
+
1305
+ ##### Graceful, catchable failure
1306
+
1307
+ If the wasm genuinely cannot be located (e.g. you pass a bad `locateFile`),
1308
+ initialization now **rejects with a single, catchable `Error`** carrying
1309
+ remediation guidance:
1310
+
1311
+ ```typescript
1312
+ try {
1313
+ const engine = new SqljsEngine({ locateFile: () => "/bad/path.wasm" });
1314
+ await engine.ensureReady();
1315
+ } catch (err) {
1316
+ // "sql.js wasm not found at "/bad/path.wasm". Pass `locateFile`/`wasmURL` ...,
1317
+ // or install `better-sqlite3` and use the `node-sqlite` engine ..."
1318
+ console.error(err.message);
1319
+ }
1320
+ ```
1321
+
1322
+ Previously a missing/corrupt wasm caused sql.js's emscripten runtime to abort
1323
+ asynchronously with an **uncatchable `RuntimeError: Aborted`** that escaped
1324
+ `try/catch` and crashed the Node process. The engine now preflights the wasm path
1325
+ and installs a defensive `onAbort` handler so the failure surfaces as a normal
1326
+ rejected promise instead.
1327
+
1328
+ ### Native SQLite in Node (`type: 'node-sqlite'`) — easy opt-in
1329
+
1330
+ `sqljs` is fine for most Node usage, but if you want native, file-backed SQLite
1331
+ (via the `better-sqlite3` addon) it is a **one-line opt-in** — `sqljs` is never
1332
+ silently swapped out for you. Install the optional dependency and set the engine
1333
+ type:
1334
+
1335
+ ```bash
1336
+ npm install better-sqlite3
1337
+ ```
1338
+
1339
+ ```typescript
1340
+ // Flip the client (or initJsBao) to native SQLite in one line:
1341
+ const client = new JsBaoClient({
1342
+ // ...other options...
1343
+ databaseConfig: {
1344
+ type: "node-sqlite",
1345
+ options: { filePath: ":memory:" }, // or "/path/to/app.db" for persistence
1346
+ },
1347
+ });
1348
+ ```
1349
+
1350
+ To pick the engine programmatically based on what's installed, use the helper
1351
+ exported from `js-bao/node`:
1352
+
1353
+ ```typescript
1354
+ import { getRecommendedNodeEngine } from "js-bao/node";
1355
+
1356
+ // Returns "node-sqlite" when better-sqlite3 is installed, else "sqljs".
1357
+ const type = await getRecommendedNodeEngine();
1358
+ const databaseConfig = { type, options: { filePath: ":memory:" } };
1359
+ ```
1270
1360
 
1271
1361
  ## Building the Library (for Contributors)
1272
1362
 
@@ -1405,25 +1495,29 @@ Key exports: `connectDoDb`, `DOClientEngine`, and types (`DoDb`, `DocumentFilter
1405
1495
 
1406
1496
  ## Database Configuration
1407
1497
 
1408
- ### SQLite (Node.js)
1498
+ ### SQL.js (Browser / Node.js) — default
1409
1499
 
1410
1500
  ```javascript
1411
1501
  {
1412
- type: 'node-sqlite',
1502
+ type: 'sqljs',
1413
1503
  options: {
1414
- filePath: ':memory:' // or '/path/to/database.db'
1504
+ // Browser: serves /sql-wasm.wasm (place it in public/), or override via
1505
+ // wasmURL / locateFile.
1506
+ // Node.js: resolves sql.js's bundled wasm automatically — no config needed.
1415
1507
  }
1416
1508
  }
1417
1509
  ```
1418
1510
 
1419
- ### SQL.js (Browser/Node.js)
1511
+ ### Native SQLite (Node.js) — `node-sqlite` (better-sqlite3)
1512
+
1513
+ One-line opt-in for native, file-backed SQLite. Requires `better-sqlite3`
1514
+ (`npm install better-sqlite3`):
1420
1515
 
1421
1516
  ```javascript
1422
1517
  {
1423
- type: 'sqljs',
1518
+ type: 'node-sqlite',
1424
1519
  options: {
1425
- // Browser: automatically loads WASM
1426
- // Node.js: fallback option
1520
+ filePath: ':memory:' // or '/path/to/database.db'
1427
1521
  }
1428
1522
  }
1429
1523
  ```