@tursodatabase/database-common 0.6.0-pre.2 → 0.6.0-pre.20

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/dist/compat.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { NativeDatabase, NativeStatement } from "./types.js";
1
+ import { NativeDatabase, NativeStatement, QueryOptions } from "./types.js";
2
2
  /**
3
3
  * Database represents a connection that can prepare and execute SQL statements.
4
4
  */
@@ -19,6 +19,7 @@ declare class Database {
19
19
  * @param {boolean} [opts.readonly=false] - Open the database in read-only mode.
20
20
  * @param {boolean} [opts.fileMustExist=false] - If true, throws if database file does not exist.
21
21
  * @param {number} [opts.timeout=0] - Timeout duration in milliseconds for database operations. Defaults to 0 (no timeout).
22
+ * @param {number} [opts.defaultQueryTimeout=0] - Default maximum query execution time in milliseconds before interruption.
22
23
  */
23
24
  constructor(db: NativeDatabase);
24
25
  /**
@@ -47,7 +48,7 @@ declare class Database {
47
48
  *
48
49
  * @param {string} sql - The string containing SQL statements to execute
49
50
  */
50
- exec(sql: any): void;
51
+ exec(sql: any, queryOptions?: QueryOptions): void;
51
52
  /**
52
53
  * Interrupts the database connection.
53
54
  */
@@ -1 +1 @@
1
- {"version":3,"file":"compat.d.ts","sourceRoot":"","sources":["../compat.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,eAAe,EAAgC,MAAM,YAAY,CAAC;AAyB3F;;GAEG;AACH,cAAM,QAAQ;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,OAAO,CAAC;IAClB,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,EAAE,OAAO,CAAC;IAChB,aAAa,EAAE,OAAO,CAAC;IAEvB,OAAO,CAAC,EAAE,CAAiB;IAC3B,OAAO,CAAC,cAAc,CAAkB;IAExC;;;;;;;;;OASG;gBACS,EAAE,EAAE,cAAc;IAa9B;;;;OAIG;IACH,OAAO,CAAC,GAAG,KAAA;IAeX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AKrD;;;;OAIG;IACH,GAAG,CAAC,GAAG,KAAA;IAKP;;;;OAIG;IACH,KAAK,CAAC,SAAS,KAAA;IAKf;;;;OAIG;IACH,YAAY,CAAC,MAAM,KAAA;IAKnB;;;;OAIG;IACH,OAAO;IAIP,IAAI,MAAM,SAET;IAED,IAAI,MAAM,IAAI,OAAO,CAEpB;IAED,IAAI,QAAQ,mBAEX;IAED;;OAEG;IACH,GAAG,CAAC,GAAG,cAAc,OAAA;;;;IA4BrB;;;;OAIG;IACH,GAAG,CAAC,GAAG,cAAc,OAAA;IAsBrB;;;;OAIG;IACF,OAAO,CAAC,GAAG,cAAc,OAAA;IAqB1B;;;;OAIG;IACH,GAAG,CAAC,GAAG,cAAc,OAAA;IAsBrB;;OAEG;IACH,SAAS;IAKT;;;;;OAKG;IACH,IAAI,CAAC,GAAG,cAAc,OAAA;IAStB,KAAK;CAGN;AAED,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,CAAA"}
package/dist/compat.js CHANGED
@@ -16,6 +16,30 @@ function createErrorByName(name, message) {
16
16
  }
17
17
  return new ErrorConstructor(message);
18
18
  }
19
+ function isQueryOptions(value) {
20
+ return value != null
21
+ && typeof value === "object"
22
+ && !Array.isArray(value)
23
+ && Object.prototype.hasOwnProperty.call(value, "queryTimeout");
24
+ }
25
+ function splitBindParameters(bindParameters) {
26
+ if (bindParameters.length === 0) {
27
+ return { params: undefined, queryOptions: undefined };
28
+ }
29
+ if (bindParameters.length > 1 && isQueryOptions(bindParameters[bindParameters.length - 1])) {
30
+ return {
31
+ params: bindParameters.length === 2 ? bindParameters[0] : bindParameters.slice(0, -1),
32
+ queryOptions: bindParameters[bindParameters.length - 1],
33
+ };
34
+ }
35
+ return { params: bindParameters.length === 1 ? bindParameters[0] : bindParameters, queryOptions: undefined };
36
+ }
37
+ function toBindArgs(params) {
38
+ if (params === undefined) {
39
+ return [];
40
+ }
41
+ return [params];
42
+ }
19
43
  /**
20
44
  * Database represents a connection that can prepare and execute SQL statements.
21
45
  */
@@ -36,6 +60,7 @@ class Database {
36
60
  * @param {boolean} [opts.readonly=false] - Open the database in read-only mode.
37
61
  * @param {boolean} [opts.fileMustExist=false] - If true, throws if database file does not exist.
38
62
  * @param {number} [opts.timeout=0] - Timeout duration in milliseconds for database operations. Defaults to 0 (no timeout).
63
+ * @param {number} [opts.defaultQueryTimeout=0] - Default maximum query execution time in milliseconds before interruption.
39
64
  */
40
65
  constructor(db) {
41
66
  this.db = db;
@@ -150,11 +175,11 @@ class Database {
150
175
  *
151
176
  * @param {string} sql - The string containing SQL statements to execute
152
177
  */
153
- exec(sql) {
178
+ exec(sql, queryOptions) {
154
179
  if (!this.open) {
155
180
  throw new TypeError("The database connection is not open");
156
181
  }
157
- const exec = this.db.executor(sql);
182
+ const exec = this.db.executor(sql, queryOptions);
158
183
  try {
159
184
  while (true) {
160
185
  const stepResult = exec.stepSync();
@@ -255,8 +280,10 @@ class Statement {
255
280
  */
256
281
  run(...bindParameters) {
257
282
  const totalChangesBefore = this.db.totalChanges();
283
+ const { params, queryOptions } = splitBindParameters(bindParameters);
258
284
  this.stmt.reset();
259
- bindParams(this.stmt, bindParameters);
285
+ this.stmt.setQueryTimeout(queryOptions);
286
+ bindParams(this.stmt, toBindArgs(params));
260
287
  for (;;) {
261
288
  const stepResult = this.stmt.stepSync();
262
289
  if (stepResult === STEP_IO) {
@@ -281,8 +308,10 @@ class Statement {
281
308
  * @param bindParameters - The bind parameters for executing the statement.
282
309
  */
283
310
  get(...bindParameters) {
311
+ const { params, queryOptions } = splitBindParameters(bindParameters);
284
312
  this.stmt.reset();
285
- bindParams(this.stmt, bindParameters);
313
+ this.stmt.setQueryTimeout(queryOptions);
314
+ bindParams(this.stmt, toBindArgs(params));
286
315
  let row = undefined;
287
316
  for (;;) {
288
317
  const stepResult = this.stmt.stepSync();
@@ -305,8 +334,10 @@ class Statement {
305
334
  * @param bindParameters - The bind parameters for executing the statement.
306
335
  */
307
336
  *iterate(...bindParameters) {
337
+ const { params, queryOptions } = splitBindParameters(bindParameters);
308
338
  this.stmt.reset();
309
- bindParams(this.stmt, bindParameters);
339
+ this.stmt.setQueryTimeout(queryOptions);
340
+ bindParams(this.stmt, toBindArgs(params));
310
341
  while (true) {
311
342
  const stepResult = this.stmt.stepSync();
312
343
  if (stepResult === STEP_IO) {
@@ -327,8 +358,10 @@ class Statement {
327
358
  * @param bindParameters - The bind parameters for executing the statement.
328
359
  */
329
360
  all(...bindParameters) {
361
+ const { params, queryOptions } = splitBindParameters(bindParameters);
330
362
  this.stmt.reset();
331
- bindParams(this.stmt, bindParameters);
363
+ this.stmt.setQueryTimeout(queryOptions);
364
+ bindParams(this.stmt, toBindArgs(params));
332
365
  const rows = [];
333
366
  for (;;) {
334
367
  const stepResult = this.stmt.stepSync();
package/dist/promise.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AsyncLock } from "./async-lock.js";
2
- import { NativeDatabase, NativeStatement } from "./types.js";
2
+ import { NativeDatabase, NativeStatement, QueryOptions } from "./types.js";
3
3
  /**
4
4
  * Database represents a connection that can prepare and execute SQL statements.
5
5
  */
@@ -45,7 +45,7 @@ declare class Database {
45
45
  *
46
46
  * @param {string} sql - The string containing SQL statements to execute
47
47
  */
48
- exec(sql: any): Promise<void>;
48
+ exec(sql: any, queryOptions?: QueryOptions): Promise<void>;
49
49
  /**
50
50
  * Interrupts the database connection.
51
51
  */
@@ -1 +1 @@
1
- {"version":3,"file":"promise.d.ts","sourceRoot":"","sources":["../promise.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,OAAO,EAAE,cAAc,EAAE,eAAe,EAA8C,MAAM,YAAY,CAAC;AAyBzG;;GAEG;AACH,cAAM,QAAQ;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,OAAO,CAAC;IAClB,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,EAAE,OAAO,CAAC;IAChB,aAAa,EAAE,OAAO,C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ist/promise.js CHANGED
@@ -17,6 +17,30 @@ function createErrorByName(name, message) {
17
17
  }
18
18
  return new ErrorConstructor(message);
19
19
  }
20
+ function isQueryOptions(value) {
21
+ return value != null
22
+ && typeof value === "object"
23
+ && !Array.isArray(value)
24
+ && Object.prototype.hasOwnProperty.call(value, "queryTimeout");
25
+ }
26
+ function splitBindParameters(bindParameters) {
27
+ if (bindParameters.length === 0) {
28
+ return { params: undefined, queryOptions: undefined };
29
+ }
30
+ if (bindParameters.length > 1 && isQueryOptions(bindParameters[bindParameters.length - 1])) {
31
+ return {
32
+ params: bindParameters.length === 2 ? bindParameters[0] : bindParameters.slice(0, -1),
33
+ queryOptions: bindParameters[bindParameters.length - 1],
34
+ };
35
+ }
36
+ return { params: bindParameters.length === 1 ? bindParameters[0] : bindParameters, queryOptions: undefined };
37
+ }
38
+ function toBindArgs(params) {
39
+ if (params === undefined) {
40
+ return [];
41
+ }
42
+ return [params];
43
+ }
20
44
  /**
21
45
  * Database represents a connection that can prepare and execute SQL statements.
22
46
  */
@@ -162,12 +186,12 @@ class Database {
162
186
  *
163
187
  * @param {string} sql - The string containing SQL statements to execute
164
188
  */
165
- async exec(sql) {
189
+ async exec(sql, queryOptions) {
166
190
  if (!this.open) {
167
191
  throw new TypeError("The database connection is not open");
168
192
  }
169
193
  await this.execLock.acquire();
170
- const exec = this.db.executor(sql);
194
+ const exec = this.db.executor(sql, queryOptions);
171
195
  try {
172
196
  while (true) {
173
197
  const stepResult = exec.stepSync();
@@ -319,7 +343,9 @@ class Statement {
319
343
  */
320
344
  async run(...bindParameters) {
321
345
  let stmt = await this.stmt.resolve();
322
- bindParams(stmt, bindParameters);
346
+ const { params, queryOptions } = splitBindParameters(bindParameters);
347
+ stmt.setQueryTimeout(queryOptions);
348
+ bindParams(stmt, toBindArgs(params));
323
349
  const totalChangesBefore = this.db.totalChanges();
324
350
  await this.execLock.acquire();
325
351
  try {
@@ -353,7 +379,9 @@ class Statement {
353
379
  */
354
380
  async get(...bindParameters) {
355
381
  let stmt = await this.stmt.resolve();
356
- bindParams(stmt, bindParameters);
382
+ const { params, queryOptions } = splitBindParameters(bindParameters);
383
+ stmt.setQueryTimeout(queryOptions);
384
+ bindParams(stmt, toBindArgs(params));
357
385
  await this.execLock.acquire();
358
386
  let row = undefined;
359
387
  try {
@@ -385,7 +413,9 @@ class Statement {
385
413
  */
386
414
  async *iterate(...bindParameters) {
387
415
  let stmt = await this.stmt.resolve();
388
- bindParams(stmt, bindParameters);
416
+ const { params, queryOptions } = splitBindParameters(bindParameters);
417
+ stmt.setQueryTimeout(queryOptions);
418
+ bindParams(stmt, toBindArgs(params));
389
419
  await this.execLock.acquire();
390
420
  try {
391
421
  while (true) {
@@ -414,7 +444,9 @@ class Statement {
414
444
  */
415
445
  async all(...bindParameters) {
416
446
  let stmt = await this.stmt.resolve();
417
- bindParams(stmt, bindParameters);
447
+ const { params, queryOptions } = splitBindParameters(bindParameters);
448
+ stmt.setQueryTimeout(queryOptions);
449
+ bindParams(stmt, toBindArgs(params));
418
450
  const rows = [];
419
451
  await this.execLock.acquire();
420
452
  try {
package/dist/types.d.ts CHANGED
@@ -11,12 +11,18 @@ export interface DatabaseOpts {
11
11
  readonly?: boolean;
12
12
  fileMustExist?: boolean;
13
13
  timeout?: number;
14
+ /** Default maximum query execution time in milliseconds before interruption. */
15
+ defaultQueryTimeout?: number;
14
16
  tracing?: 'info' | 'debug' | 'trace';
15
17
  /** Experimental features to enable */
16
18
  experimental?: ExperimentalFeature[];
17
19
  /** Optional local encryption configuration */
18
20
  encryption?: EncryptionOpts;
19
21
  }
22
+ export interface QueryOptions {
23
+ /** Per-query timeout in milliseconds. Overrides defaultQueryTimeout for this call. */
24
+ queryTimeout?: number;
25
+ }
20
26
  export interface NativeDatabase {
21
27
  memory: boolean;
22
28
  path: string;
@@ -28,7 +34,7 @@ export interface NativeDatabase {
28
34
  ioLoopSync(): any;
29
35
  ioLoopAsync(): Promise<void>;
30
36
  prepare(sql: string): NativeStatement;
31
- executor(sql: string): NativeExecutor;
37
+ executor(sql: string, queryOptions?: QueryOptions): NativeExecutor;
32
38
  defaultSafeIntegers(toggle: boolean): any;
33
39
  totalChanges(): number;
34
40
  changes(): number;
@@ -47,6 +53,7 @@ export interface NativeExecutor {
47
53
  reset(): any;
48
54
  }
49
55
  export interface NativeStatement {
56
+ setQueryTimeout(queryOptions?: QueryOptions): void;
50
57
  stepAsync(): Promise<number>;
51
58
  stepSync(): number;
52
59
  pluck(pluckMode: boolean): any;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,mBAAmB,GAAG,OAAO,GAAG,QAAQ,GAAG,YAAY,GAAG,cAAc,GAAG,YAAY,GAAG,UAAU,GAAG,QAAQ,CAAC;AAE5H,kEAAkE;AAClE,MAAM,MAAM,gBAAgB,GAAG,WAAW,GAAG,WAAW,GAAG,UAAU,GAAG,YAAY,GAAG,WAAW,GAAG,YAAY,GAAG,YAAY,CAAA;AAEhI,qDAAqD;AACrD,MAAM,WAAW,cAAc;IAC3B,MAAM,EAAE,gBAAgB,CAAA;IACxB,qCAAqC;IACrC,MAAM,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,YAAY;IACzB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,CAAA;IACpC,sCAAsC;IACtC,YAAY,CAAC,EAAE,mBAAmB,EAAE,CAAA;IACpC,8CAA8C;IAC9C,UAAU,CAAC,EAAE,cAAc,CAAA;CAC9B;AAED,MAAM,WAAW,cAAc;IAC3B,MAAM,EAAE,OAAO,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,OAAO,CAAC;IAClB,IAAI,EAAE,OAAO,CAAC;IACd,KAAI,IAAI,EAA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CAAC;IACvB,OAAO,IAAI,MAAM,CAAC;IAClB,eAAe,IAAI,MAAM,CAAC;IAC1B,KAAK,QAAG;CACX;AAID,eAAO,MAAM,QAAQ,IAAI,CAAC;AAC1B,eAAO,MAAM,SAAS,IAAI,CAAC;AAC3B,eAAO,MAAM,OAAO,IAAI,CAAC;AAEzB,MAAM,WAAW,WAAW;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAA;CACf;AAED,MAAM,WAAW,cAAc;IAC3B,QAAQ,IAAI,MAAM,CAAC;IACnB,KAAK,QAAG;CACX;AACD,MAAM,WAAW,eAAe;IAC5B,eAAe,CAAC,YAAY,CAAC,EAAE,YAAY,GAAG,IAAI,CAAC;IACnD,SAAS,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAC7B,QAAQ,IAAI,MAAM,CAAC;IAEnB,KAAK,CAAC,SAAS,EAAE,OAAO,OAAE;IAC1B,YAAY,CAAC,MAAM,EAAE,OAAO,OAAE;IAC9B,GAAG,CAAC,MAAM,EAAE,OAAO,OAAE;IACrB,OAAO,IAAI,WAAW,EAAE,CAAC;IACzB,GAAG,IAAI,GAAG,CAAC;IACX,KAAK,QAAG;IACR,QAAQ,QAAG;CACd"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tursodatabase/database-common",
3
- "version": "0.6.0-pre.2",
3
+ "version": "0.6.0-pre.20",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/tursodatabase/turso"