@tursodatabase/database-common 0.2.0-pre.9 → 0.2.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
@@ -2,7 +2,7 @@
2
2
 
3
3
  This package is the Turso embedded database common JS library which is shared between final builds for Node and Browser.
4
4
 
5
- Do not use this package directly - instead you must use `@tursodatabase/database` or `@tursodatabase/database-browser`.
5
+ Do not use this package directly - instead you must use `@tursodatabase/database` or `@tursodatabase/database-wasm`.
6
6
 
7
- > **⚠️ Warning:** This software is ALPHA, only use for development, testing, and experimentation. We are working to make it production ready, but do not use it for critical data right now.
7
+ > **⚠️ Warning:** This software is in BETA. It may still contain bugs and unexpected behavior. Use caution with production data and ensure you have backups.
8
8
 
package/dist/promise.d.ts CHANGED
@@ -12,6 +12,7 @@ declare class Database {
12
12
  private db;
13
13
  private execLock;
14
14
  private _inTransaction;
15
+ protected connected: boolean;
15
16
  constructor(db: NativeDatabase);
16
17
  /**
17
18
  * connect database
@@ -59,6 +60,13 @@ declare class Database {
59
60
  */
60
61
  close(): Promise<void>;
61
62
  }
63
+ interface MaybeLazy<T> {
64
+ apply(fn: (value: T) => void): any;
65
+ resolve(): Promise<T>;
66
+ must(): T;
67
+ }
68
+ declare function maybePromise<T>(arg: () => Promise<T>): MaybeLazy<T>;
69
+ declare function maybeValue<T>(value: T): MaybeLazy<T>;
62
70
  /**
63
71
  * Statement represents a prepared SQL statement that can be executed.
64
72
  */
@@ -66,7 +74,7 @@ declare class Statement {
66
74
  private stmt;
67
75
  private db;
68
76
  private execLock;
69
- constructor(stmt: NativeStatement, db: NativeDatabase, execLock: AsyncLock);
77
+ constructor(stmt: MaybeLazy<NativeStatement>, db: NativeDatabase, execLock: AsyncLock);
70
78
  /**
71
79
  * Toggle raw mode.
72
80
  *
@@ -132,5 +140,5 @@ declare class Statement {
132
140
  bind(...bindParameters: any[]): this;
133
141
  close(): void;
134
142
  }
135
- export { Database, Statement };
143
+ export { Database, Statement, maybePromise, maybeValue };
136
144
  //# sourceMappingURL=promise.d.ts.map
@@ -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,CAAC;IAEvB,OAAO,CAAC,EAAE,CAAiB;IAC3B,OAAO,CAAC,QAAQ,CAAY;IAC5B,OAAO,CAAC,cAAc,CAAkB;gBAE5B,EAAE,EAAE,cAAc;IAY9B;;OAEG;IACG,OAAO;IAIb;;;;OAIG;IACH,OAAO,CAAC,GAAG,KAAA;IAYX;;;;OAIG;IACH,WAAW,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,OAAA,KAAK,OAAO,CAAC,GAAG,CAAC;IAmClC,MAAM,CAAC,MAAM,KAAA,EAAE,OAAO,KAAA;IAoB5B,MAAM,CAAC,QAAQ,KAAA,EAAE,OAAO,KAAA;IAIxB,SAAS,CAAC,OAAO,KAAA;IAIjB,QAAQ,CAAC,IAAI,KAAA,EAAE,OAAO,KAAA,EAAE,EAAE,KAAA;IAI1B,SAAS,CAAC,IAAI,KAAA,EAAE,OAAO,KAAA;IAIvB,KAAK,CAAC,IAAI,KAAA,EAAE,OAAO,KAAA;IAInB,aAAa,CAAC,IAAI,KAAA;IAIlB,wBAAwB;IAIxB;;;;;OAKG;IACG,IAAI,CAAC,GAAG,KAAA;IAwBd;;OAEG;IACH,SAAS;IAIT;;;;OAIG;IACH,mBAAmB,CAAC,MAAM,KAAA;IAI1B;;OAEG;IACG,KAAK;CAGZ;AAED;;GAEG;A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,CAAC,QAAQ,CAAY;gBAEhB,IAAI,EAAE,SAAS,CAAC,eAAe,CAAC,EAAE,EAAE,EAAE,cAAc,EAAE,QAAQ,EAAE,SAAS;IAMrF;;;;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,SAET;IAED,IAAI,QAAQ,mBAEX;IAED;;OAEG;IACG,GAAG,CAAC,GAAG,cAAc,OAAA;;;;IAiC3B;;;;OAIG;IACG,GAAG,CAAC,GAAG,cAAc,OAAA;IA0B3B;;;;OAIG;IACI,OAAO,CAAC,GAAG,cAAc,OAAA;IA0BhC;;;;OAIG;IACG,GAAG,CAAC,GAAG,cAAc,OAAA;IA6B3B;;OAEG;IACH,SAAS;IAKT;;;;;OAKG;IACH,IAAI,CAAC,GAAG,cAAc,OAAA;IAStB,KAAK;CAUN;AAED,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,CAAA"}
package/dist/promise.js CHANGED
@@ -29,6 +29,7 @@ class Database {
29
29
  db;
30
30
  execLock;
31
31
  _inTransaction = false;
32
+ connected = false;
32
33
  constructor(db) {
33
34
  this.db = db;
34
35
  this.execLock = new AsyncLock();
@@ -44,7 +45,11 @@ class Database {
44
45
  * connect database
45
46
  */
46
47
  async connect() {
48
+ if (this.connected) {
49
+ return;
50
+ }
47
51
  await this.db.connectAsync();
52
+ this.connected = true;
48
53
  }
49
54
  /**
50
55
  * Prepares a SQL statement for execution.
@@ -56,7 +61,12 @@ class Database {
56
61
  throw new RangeError("The supplied SQL string contains no statements");
57
62
  }
58
63
  try {
59
- return new Statement(this.db.prepare(sql), this.db, this.execLock);
64
+ if (this.connected) {
65
+ return new Statement(maybeValue(this.db.prepare(sql)), this.db, this.execLock);
66
+ }
67
+ else {
68
+ return new Statement(maybePromise(() => this.connect().then(() => this.db.prepare(sql))), this.db, this.execLock);
69
+ }
60
70
  }
61
71
  catch (err) {
62
72
  throw convertError(err);
@@ -190,6 +200,46 @@ class Database {
190
200
  this.db.close();
191
201
  }
192
202
  }
203
+ function maybePromise(arg) {
204
+ let lazy = arg;
205
+ let promise = null;
206
+ let value = null;
207
+ return {
208
+ apply(fn) {
209
+ let previous = lazy;
210
+ lazy = async () => {
211
+ const result = await previous();
212
+ fn(result);
213
+ return result;
214
+ };
215
+ },
216
+ async resolve() {
217
+ if (promise != null) {
218
+ return await promise;
219
+ }
220
+ let valueResolve, valueReject;
221
+ promise = new Promise((resolve, reject) => {
222
+ valueResolve = x => { resolve(x); value = x; };
223
+ valueReject = reject;
224
+ });
225
+ await lazy().then(valueResolve, valueReject);
226
+ return await promise;
227
+ },
228
+ must() {
229
+ if (value == null) {
230
+ throw new Error(`database must be connected before execution the function`);
231
+ }
232
+ return value;
233
+ },
234
+ };
235
+ }
236
+ function maybeValue(value) {
237
+ return {
238
+ apply(fn) { fn(value); },
239
+ resolve() { return Promise.resolve(value); },
240
+ must() { return value; },
241
+ };
242
+ }
193
243
  /**
194
244
  * Statement represents a prepared SQL statement that can be executed.
195
245
  */
@@ -208,7 +258,7 @@ class Statement {
208
258
  * @param raw Enable or disable raw mode. If you don't pass the parameter, raw mode is enabled.
209
259
  */
210
260
  raw(raw) {
211
- this.stmt.raw(raw);
261
+ this.stmt.apply(s => s.raw(raw));
212
262
  return this;
213
263
  }
214
264
  /**
@@ -217,7 +267,7 @@ class Statement {
217
267
  * @param pluckMode Enable or disable pluck mode. If you don't pass the parameter, pluck mode is enabled.
218
268
  */
219
269
  pluck(pluckMode) {
220
- this.stmt.pluck(pluckMode);
270
+ this.stmt.apply(s => s.pluck(pluckMode));
221
271
  return this;
222
272
  }
223
273
  /**
@@ -226,7 +276,7 @@ class Statement {
226
276
  * @param {boolean} [toggle] - Whether to use safe integers.
227
277
  */
228
278
  safeIntegers(toggle) {
229
- this.stmt.safeIntegers(toggle);
279
+ this.stmt.apply(s => s.safeIntegers(toggle));
230
280
  return this;
231
281
  }
232
282
  /**
@@ -235,7 +285,7 @@ class Statement {
235
285
  * @returns {Array} An array of column objects with name, column, table, database, and type properties.
236
286
  */
237
287
  columns() {
238
- return this.stmt.columns();
288
+ return this.stmt.must().columns();
239
289
  }
240
290
  get source() {
241
291
  throw new Error("not implemented");
@@ -250,13 +300,14 @@ class Statement {
250
300
  * Executes the SQL statement and returns an info object.
251
301
  */
252
302
  async run(...bindParameters) {
303
+ let stmt = await this.stmt.resolve();
304
+ stmt.reset();
305
+ bindParams(stmt, bindParameters);
253
306
  const totalChangesBefore = this.db.totalChanges();
254
- this.stmt.reset();
255
- bindParams(this.stmt, bindParameters);
256
307
  await this.execLock.acquire();
257
308
  try {
258
309
  while (true) {
259
- const stepResult = await this.stmt.stepSync();
310
+ const stepResult = await stmt.stepSync();
260
311
  if (stepResult === STEP_IO) {
261
312
  await this.db.ioLoopAsync();
262
313
  continue;
@@ -283,12 +334,13 @@ class Statement {
283
334
  * @param bindParameters - The bind parameters for executing the statement.
284
335
  */
285
336
  async get(...bindParameters) {
286
- this.stmt.reset();
287
- bindParams(this.stmt, bindParameters);
337
+ let stmt = await this.stmt.resolve();
338
+ stmt.reset();
339
+ bindParams(stmt, bindParameters);
288
340
  await this.execLock.acquire();
289
341
  try {
290
342
  while (true) {
291
- const stepResult = await this.stmt.stepSync();
343
+ const stepResult = await stmt.stepSync();
292
344
  if (stepResult === STEP_IO) {
293
345
  await this.db.ioLoopAsync();
294
346
  continue;
@@ -297,7 +349,7 @@ class Statement {
297
349
  return undefined;
298
350
  }
299
351
  if (stepResult === STEP_ROW) {
300
- return this.stmt.row();
352
+ return stmt.row();
301
353
  }
302
354
  }
303
355
  }
@@ -311,12 +363,13 @@ class Statement {
311
363
  * @param bindParameters - The bind parameters for executing the statement.
312
364
  */
313
365
  async *iterate(...bindParameters) {
314
- this.stmt.reset();
315
- bindParams(this.stmt, bindParameters);
366
+ let stmt = await this.stmt.resolve();
367
+ stmt.reset();
368
+ bindParams(stmt, bindParameters);
316
369
  await this.execLock.acquire();
317
370
  try {
318
371
  while (true) {
319
- const stepResult = await this.stmt.stepSync();
372
+ const stepResult = await stmt.stepSync();
320
373
  if (stepResult === STEP_IO) {
321
374
  await this.db.ioLoopAsync();
322
375
  continue;
@@ -325,7 +378,7 @@ class Statement {
325
378
  break;
326
379
  }
327
380
  if (stepResult === STEP_ROW) {
328
- yield this.stmt.row();
381
+ yield stmt.row();
329
382
  }
330
383
  }
331
384
  }
@@ -339,13 +392,14 @@ class Statement {
339
392
  * @param bindParameters - The bind parameters for executing the statement.
340
393
  */
341
394
  async all(...bindParameters) {
342
- this.stmt.reset();
343
- bindParams(this.stmt, bindParameters);
395
+ let stmt = await this.stmt.resolve();
396
+ stmt.reset();
397
+ bindParams(stmt, bindParameters);
344
398
  const rows = [];
345
399
  await this.execLock.acquire();
346
400
  try {
347
401
  while (true) {
348
- const stepResult = await this.stmt.stepSync();
402
+ const stepResult = await stmt.stepSync();
349
403
  if (stepResult === STEP_IO) {
350
404
  await this.db.ioLoopAsync();
351
405
  continue;
@@ -354,7 +408,7 @@ class Statement {
354
408
  break;
355
409
  }
356
410
  if (stepResult === STEP_ROW) {
357
- rows.push(this.stmt.row());
411
+ rows.push(stmt.row());
358
412
  }
359
413
  }
360
414
  return rows;
@@ -385,7 +439,15 @@ class Statement {
385
439
  }
386
440
  }
387
441
  close() {
388
- this.stmt.finalize();
442
+ let stmt;
443
+ try {
444
+ stmt = this.stmt.must();
445
+ }
446
+ catch (e) {
447
+ // ignore error - if stmt wasn't initialized it's fine
448
+ return;
449
+ }
450
+ stmt.finalize();
389
451
  }
390
452
  }
391
- export { Database, Statement };
453
+ export { Database, Statement, maybePromise, maybeValue };
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=promise.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promise.test.d.ts","sourceRoot":"","sources":["../promise.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,14 @@
1
+ import { expect, test } from 'vitest';
2
+ import { maybePromise } from './promise.js';
3
+ test('drizzle-orm', async () => {
4
+ const lazy = maybePromise(() => fetch('http://google.com'));
5
+ let status, headers;
6
+ //@ts-ignore
7
+ lazy.apply(x => { status = x.status; });
8
+ //@ts-ignore
9
+ lazy.apply(x => { headers = x.headers; });
10
+ let response = await lazy.resolve();
11
+ expect(response).not.toBeNull();
12
+ expect(status).toBe(200);
13
+ expect(headers).not.toBeNull();
14
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tursodatabase/database-common",
3
- "version": "0.2.0-pre.9",
3
+ "version": "0.2.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/tursodatabase/turso"
@@ -15,11 +15,12 @@
15
15
  "README.md"
16
16
  ],
17
17
  "devDependencies": {
18
- "typescript": "^5.9.2"
18
+ "typescript": "^5.9.2",
19
+ "vitest": "^3.2.4"
19
20
  },
20
21
  "scripts": {
21
22
  "tsc-build": "npm exec tsc",
22
23
  "build": "npm run tsc-build",
23
- "test": "echo 'no tests'"
24
+ "test": "vitest --run"
24
25
  }
25
26
  }