@reddb-io/browser 0.2.0 → 0.3.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
@@ -12,7 +12,7 @@ RedDB server, account, CDN or external API. Applications bundle the JS, worker
12
12
  and WASM assets and make them available locally or through their own offline
13
13
  application cache. Sync is optional and must never gate local reads or writes.
14
14
 
15
- Version `0.2.0` exposes local table SELECT, CREATE TABLE, INSERT, UPDATE, DELETE
15
+ Version `0.3.0` exposes local table SELECT, CREATE TABLE, INSERT, UPDATE, DELETE
16
16
  and explicit BEGIN/COMMIT/ROLLBACK transactions through `openDatabase`.
17
17
  Other models, synchronization, conflict
18
18
  resolution and forwarding between tabs remain pending. The browser package
@@ -88,7 +88,7 @@ COALESCE, ABS, JSON_PARSE, JSON_EXTRACT/JSON_EXTRACT_TEXT and CONTAINS. Unknown
88
88
  fields/functions are rejected even on empty tables. Functions and operators use
89
89
  the canonical Rust evaluators; exact values retain `reddb-json-v1` tags.
90
90
 
91
- Parameters, DISTINCT, grouping/aggregates, windows, joins/subqueries,
91
+ DISTINCT, grouping/aggregates, windows, joins/subqueries,
92
92
  universal/system sources, other collection models, TTL and retention are rejected
93
93
  explicitly. Materialization is capped at 100,000 visible source rows, independently
94
94
  of LIMIT. Superseded and moderated rows use the shared committed-scan visibility;
@@ -100,6 +100,31 @@ pending row actions. DML commits use the native WAL batch; catalog changes use
100
100
  one complete native snapshot publication. BEGIN copies the committed resident
101
101
  image, so transaction startup costs memory proportional to the local database.
102
102
 
103
+ ### Positional parameters
104
+
105
+ Use `query(sql, params)` to bind values separately from the SQL text:
106
+
107
+ ```ts
108
+ const body = "O'Reilly's offline note"
109
+ await database.query('INSERT INTO notes (id, body) VALUES ($1, $2)', [1, body])
110
+ const found = await database.query('SELECT id, body FROM notes WHERE id = $1', [1])
111
+ ```
112
+
113
+ The canonical native Rust binder replaces AST parameter nodes with typed literals.
114
+ Values never become SQL syntax. `$1`, `$2`, … and numbered `?1`, `?2`, … use
115
+ one-based, contiguous indexes; repeated indexes reuse the same value. Missing,
116
+ extra or skipped positions return `INVALID_PARAMS` and abort an active SQL
117
+ transaction until ROLLBACK. Invalid JavaScript argument types are rejected before
118
+ statement execution.
119
+
120
+ Parameters accept `null`, boolean, string, finite fractional numbers, safe integer
121
+ numbers and `bigint` in the signed i64 or unsigned u64 range. Unsafe integer
122
+ numbers must use `bigint`; exact results retain the existing `$int`/`$uint` tags.
123
+ Objects, arrays, dates and binary values are not parameter values in this release.
124
+ At most 65,536 values are admitted. Bind parameters in supported SELECT/INSERT/
125
+ UPDATE/DELETE expressions, including LIMIT (> 0) and OFFSET (>= 0); table/column
126
+ names, operators and DDL are not parameterized.
127
+
103
128
  The embedding application owns this local file and its origin. This API does not
104
129
  establish a server user/tenant session or replay server RLS policies: export only
105
130
  the authorized subset into an application-owned file. Import a native `.rdb` into
@@ -124,9 +149,9 @@ try {
124
149
 
125
150
  The versioned Worker/WASM contract reports `abiVersion: 1`,
126
151
  `valueEncoding: 'reddb-json-v1'` and capabilities. Currently `queryValidation`
127
- and `scalarQueries` are true. Version `0.2.0` also reports `storedQueries: true`
152
+ and `scalarQueries` are true. Version `0.3.0` also reports `storedQueries: true`
128
153
  with `storedQueryMode: 'read-write-tables'`, `sqlWrites: true` and
129
- `transactions: true`; `sync` remains false.
154
+ `transactions: true` and `queryParameters: true`; `sync` remains false.
130
155
  Capabilities describe the engine, not the operations exposed by a particular handle. Mismatched SDK/Worker/WASM versions fail with `VERSION_MISMATCH` before
131
156
  opening storage. Cache the JS, Worker and WASM as one versioned unit.
132
157
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reddb-io/browser",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "description": "RedDB local browser table SQL, transactions and offline OPFS persistence through a shared Rust/WASM engine.",
package/src/index.d.ts CHANGED
@@ -14,6 +14,7 @@ export interface RuntimeInfo {
14
14
  readonly storedQueryMode: 'read-write-tables'
15
15
  readonly sqlWrites: boolean
16
16
  readonly transactions: boolean
17
+ readonly queryParameters?: boolean
17
18
  readonly sync: boolean
18
19
  }
19
20
  }
@@ -64,9 +65,12 @@ export function openArtifact(options: {
64
65
  worker?: Worker
65
66
  }): Promise<Artifact>
66
67
 
68
+ /** Scalars bind as AST literals; integer numbers must be safe and bigints fit i64/u64. */
69
+ export type QueryParameter = null | boolean | number | string | bigint
70
+
67
71
  export interface Database extends Runtime {
68
72
  /** SELECT and table SQL mutations; mutations return an affectedRows column. */
69
- query(sql: string): Promise<{ columns: string[]; rows: unknown[][] }>
73
+ query(sql: string, params?: readonly QueryParameter[]): Promise<{ columns: string[]; rows: unknown[][] }>
70
74
  /** Release exclusive ownership of the local file and terminate its worker. */
71
75
  close(): Promise<void>
72
76
  }
package/src/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /** Shared Rust/WASM runtime and artifact storage. */
2
- import { BrowserError, RUNTIME_ABI_VERSION, VALUE_ENCODING } from './protocol.js'
2
+ import { BrowserError, RUNTIME_ABI_VERSION, VALUE_ENCODING, MAX_QUERY_PARAMETERS } from './protocol.js'
3
3
  export { BrowserError } from './protocol.js'
4
4
 
5
5
  function createClient(suppliedWorker) {
@@ -138,8 +138,9 @@ async function openFile({ name, worker, create = false } = {}, method = 'open')
138
138
  try {
139
139
  const info = await client.ready
140
140
  if (method === 'openDatabase' && (info.capabilities.storedQueries !== true
141
- || info.capabilities.storedQueryMode !== 'read-write-tables' || !info.capabilities.sqlWrites || !info.capabilities.transactions)) {
142
- throw new BrowserError('VERSION_MISMATCH', 'WASM runtime does not expose local table SQL and transactions')
141
+ || info.capabilities.storedQueryMode !== 'read-write-tables' || !info.capabilities.sqlWrites || !info.capabilities.transactions
142
+ || info.capabilities.queryParameters !== true)) {
143
+ throw new BrowserError('VERSION_MISMATCH', 'WASM runtime does not expose local table SQL, transactions and parameters')
143
144
  }
144
145
  await client.call(method, method === 'openDatabase' ? { name, create } : { name })
145
146
  }
@@ -167,9 +168,14 @@ export async function openDatabase(options) {
167
168
  const client = await openFile(options, 'openDatabase')
168
169
  return {
169
170
  ...runtimeMethods(client),
170
- query: sql => typeof sql === 'string'
171
- ? client.call('query', { sql })
172
- : Promise.reject(new BrowserError('INVALID_ARGUMENT', 'Expected an RQL string')),
171
+ query(sql, params) {
172
+ if (typeof sql !== 'string') return Promise.reject(new BrowserError('INVALID_ARGUMENT', 'Expected an RQL string'))
173
+ if (params === undefined) return client.call('query', { sql })
174
+ if (!Array.isArray(params) || params.length > MAX_QUERY_PARAMETERS) {
175
+ return Promise.reject(new BrowserError('INVALID_ARGUMENT', 'Expected an array of at most 65536 positional parameters'))
176
+ }
177
+ return client.call('queryWithParams', { sql, params })
178
+ },
173
179
  close: client.closeFile,
174
180
  }
175
181
  }
package/src/protocol.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export const RUNTIME_ABI_VERSION = 1
2
2
  export const VALUE_ENCODING = 'reddb-json-v1'
3
+ export const MAX_QUERY_PARAMETERS = 65_536
3
4
 
4
5
  export class BrowserError extends Error {
5
6
  constructor(code, message) {
@@ -32,6 +32,7 @@ export class BrowserDatabase {
32
32
  static create(handle: any, name: string, now_ms: number): BrowserDatabase;
33
33
  static open(handle: any, name: string): BrowserDatabase;
34
34
  query(sql: string, now_ms: number): any;
35
+ queryWithParams(sql: string, params: Array<any>, now_ms: number): any;
35
36
  }
36
37
 
37
38
  /**
@@ -62,6 +63,7 @@ export interface InitOutput {
62
63
  readonly browserdatabase_create: (a: any, b: number, c: number, d: number) => [number, number, number];
63
64
  readonly browserdatabase_open: (a: any, b: number, c: number) => [number, number, number];
64
65
  readonly browserdatabase_query: (a: number, b: number, c: number, d: number) => [number, number, number];
66
+ readonly browserdatabase_queryWithParams: (a: number, b: number, c: number, d: any, e: number) => [number, number, number];
65
67
  readonly executeScalar: (a: number, b: number) => [number, number, number];
66
68
  readonly runtimeInfo: () => [number, number, number];
67
69
  readonly validateQuery: (a: number, b: number) => [number, number];
@@ -185,6 +185,21 @@ export class BrowserDatabase {
185
185
  }
186
186
  return takeFromExternrefTable0(ret[0]);
187
187
  }
188
+ /**
189
+ * @param {string} sql
190
+ * @param {Array<any>} params
191
+ * @param {number} now_ms
192
+ * @returns {any}
193
+ */
194
+ queryWithParams(sql, params, now_ms) {
195
+ const ptr0 = passStringToWasm0(sql, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
196
+ const len0 = WASM_VECTOR_LEN;
197
+ const ret = wasm.browserdatabase_queryWithParams(this.__wbg_ptr, ptr0, len0, params, now_ms);
198
+ if (ret[2]) {
199
+ throw takeFromExternrefTable0(ret[1]);
200
+ }
201
+ return takeFromExternrefTable0(ret[0]);
202
+ }
188
203
  }
189
204
  if (Symbol.dispose) BrowserDatabase.prototype[Symbol.dispose] = BrowserDatabase.prototype.free;
190
205
 
@@ -229,6 +244,25 @@ export function validateQuery(sql) {
229
244
  function __wbg_get_imports() {
230
245
  const import0 = {
231
246
  __proto__: null,
247
+ __wbg___wbindgen_boolean_get_c3dd5c39f1b5a12b: function(arg0) {
248
+ const v = arg0;
249
+ const ret = typeof(v) === 'boolean' ? v : undefined;
250
+ return isLikeNone(ret) ? 0xFFFFFF : ret ? 1 : 0;
251
+ },
252
+ __wbg___wbindgen_is_bigint_d6a8167cac401b95: function(arg0) {
253
+ const ret = typeof(arg0) === 'bigint';
254
+ return ret;
255
+ },
256
+ __wbg___wbindgen_is_null_066086be3abe9bb3: function(arg0) {
257
+ const ret = arg0 === null;
258
+ return ret;
259
+ },
260
+ __wbg___wbindgen_number_get_dd6d69a6079f26f1: function(arg0, arg1) {
261
+ const obj = arg1;
262
+ const ret = typeof(obj) === 'number' ? obj : undefined;
263
+ getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
264
+ getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
265
+ },
232
266
  __wbg___wbindgen_string_get_965592073e5d848c: function(arg0, arg1) {
233
267
  const obj = arg1;
234
268
  const ret = typeof(obj) === 'string' ? obj : undefined;
@@ -318,6 +352,10 @@ function __wbg_get_imports() {
318
352
  const ret = Reflect.set(arg0, arg1, arg2);
319
353
  return ret;
320
354
  }, arguments); },
355
+ __wbg_toString_7fbc0abf5c3327f5: function() { return handleError(function (arg0, arg1) {
356
+ const ret = arg0.toString(arg1);
357
+ return ret;
358
+ }, arguments); },
321
359
  __wbg_truncate_d9e55b46e14f9493: function() { return handleError(function (arg0, arg1) {
322
360
  arg0.truncate(arg1);
323
361
  }, arguments); },
Binary file
@@ -13,6 +13,7 @@ export const browserartifact_readWal: (a: number) => [number, number, number];
13
13
  export const browserdatabase_create: (a: any, b: number, c: number, d: number) => [number, number, number];
14
14
  export const browserdatabase_open: (a: any, b: number, c: number) => [number, number, number];
15
15
  export const browserdatabase_query: (a: number, b: number, c: number, d: number) => [number, number, number];
16
+ export const browserdatabase_queryWithParams: (a: number, b: number, c: number, d: any, e: number) => [number, number, number];
16
17
  export const executeScalar: (a: number, b: number) => [number, number, number];
17
18
  export const runtimeInfo: () => [number, number, number];
18
19
  export const validateQuery: (a: number, b: number) => [number, number];
package/src/worker.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import init, * as wasm from './wasm/reddb_wasm.js'
2
- import { RUNTIME_ABI_VERSION, VALUE_ENCODING } from './protocol.js'
2
+ import { RUNTIME_ABI_VERSION, VALUE_ENCODING, MAX_QUERY_PARAMETERS } from './protocol.js'
3
3
 
4
4
  let artifact
5
5
  let database
@@ -115,8 +115,15 @@ async function execute(method, params) {
115
115
  return
116
116
  }
117
117
  if (database) {
118
- if (method !== 'query') throw error('QUERY_UNSUPPORTED', 'This database handle exposes local SQL')
118
+ if (method !== 'query' && method !== 'queryWithParams') throw error('QUERY_UNSUPPORTED', 'This database handle exposes local SQL')
119
119
  if (typeof params.sql !== 'string') throw error('INVALID_ARGUMENT', 'Expected an RQL string')
120
+ if (method === 'queryWithParams') {
121
+ if (!Array.isArray(params.params) || params.params.length > MAX_QUERY_PARAMETERS) {
122
+ throw error('INVALID_ARGUMENT', 'Expected an array of at most 65536 positional parameters')
123
+ }
124
+ if (typeof database.queryWithParams !== 'function') throw error('VERSION_MISMATCH', 'WASM parameter binding is missing')
125
+ return database.queryWithParams(params.sql, params.params, Date.now())
126
+ }
120
127
  return database.query(params.sql, Date.now())
121
128
  }
122
129
  switch (method) {