@tursodatabase/database 0.9.0-pre.1 → 0.9.0-pre.3

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.
@@ -1,6 +1,6 @@
1
- import { unlinkSync } from "node:fs";
1
+ import { unlinkSync, writeFileSync } from "node:fs";
2
2
  import { expect, test } from 'vitest';
3
- import { Database } from './compat.js';
3
+ import { Database, SqliteError } from './compat.js';
4
4
  import { Database as NativeDatabase } from '#index';
5
5
  test('classifySql treats reindex as write', () => {
6
6
  const db = new NativeDatabase(':memory:');
@@ -18,6 +18,53 @@ test('classifySql treats reindex as write', () => {
18
18
  db.close();
19
19
  }
20
20
  });
21
+ test('errors carry the SQLite message and result code name', () => {
22
+ const db = new Database(':memory:');
23
+ db.exec('PRAGMA foreign_keys = ON');
24
+ db.exec('CREATE TABLE t(a UNIQUE, b NOT NULL DEFAULT 1, c CHECK(c < 5))');
25
+ db.exec('CREATE TABLE p(id INTEGER PRIMARY KEY)');
26
+ db.exec('CREATE TABLE ch(pid REFERENCES p(id))');
27
+ db.exec('INSERT INTO t(a) VALUES (1)');
28
+ db.exec('INSERT INTO p VALUES (1)');
29
+ db.exec('CREATE TABLE k(id TEXT PRIMARY KEY)');
30
+ db.exec('CREATE TABLE pair(a, b, PRIMARY KEY(a, b))');
31
+ db.exec("INSERT INTO k VALUES ('x')");
32
+ db.exec('INSERT INTO pair VALUES (1, 1), (1, 2)');
33
+ db.exec('CREATE TABLE s(a INTEGER) STRICT');
34
+ db.exec('CREATE TABLE tr(a)');
35
+ db.exec("CREATE TRIGGER trg BEFORE INSERT ON tr BEGIN SELECT RAISE(ABORT, 'nope'); END");
36
+ const notADatabase = `test-${(Math.random() * 10000) | 0}.db`;
37
+ writeFileSync(notADatabase, Buffer.alloc(8192, 7));
38
+ const cases = [
39
+ [() => db.prepare('INSERT INTO t(a) VALUES (1)').run(), 'SQLITE_CONSTRAINT_UNIQUE', 'UNIQUE constraint failed: t.a'],
40
+ [() => db.exec('INSERT INTO t(a) VALUES (1)'), 'SQLITE_CONSTRAINT_UNIQUE', 'UNIQUE constraint failed: t.a'],
41
+ [() => db.prepare('INSERT INTO t(a, b) VALUES (2, NULL)').get(), 'SQLITE_CONSTRAINT_NOTNULL', 'NOT NULL constraint failed: t.b'],
42
+ [() => db.prepare('INSERT INTO t(a, c) VALUES (3, 9) RETURNING a').all(), 'SQLITE_CONSTRAINT_CHECK', 'CHECK constraint failed: c < 5'],
43
+ [() => db.prepare('INSERT INTO p VALUES (1)').run(), 'SQLITE_CONSTRAINT_PRIMARYKEY', 'UNIQUE constraint failed: p.id'],
44
+ [() => db.prepare("INSERT INTO k VALUES ('x')").run(), 'SQLITE_CONSTRAINT_PRIMARYKEY', 'UNIQUE constraint failed: k.id'],
45
+ [() => db.prepare('INSERT INTO pair VALUES (1, 1)').run(), 'SQLITE_CONSTRAINT_PRIMARYKEY', 'UNIQUE constraint failed: pair.a, pair.b'],
46
+ [() => db.prepare('UPDATE pair SET b = 1 WHERE b = 2').run(), 'SQLITE_CONSTRAINT_PRIMARYKEY', 'UNIQUE constraint failed: pair.a, pair.b'],
47
+ [() => db.prepare('INSERT INTO ch VALUES (9)').run(), 'SQLITE_CONSTRAINT_FOREIGNKEY', 'FOREIGN KEY constraint failed'],
48
+ [() => db.prepare("INSERT INTO s VALUES ('x')").run(), 'SQLITE_CONSTRAINT_DATATYPE', 'cannot store TEXT value in INTEGER column s.a'],
49
+ [() => db.prepare('INSERT INTO tr VALUES (1)').run(), 'SQLITE_CONSTRAINT_TRIGGER', 'nope'],
50
+ [() => db.prepare('SYNTAX ERROR'), 'SQLITE_ERROR', 'near "SYNTAX": syntax error'],
51
+ [() => db.prepare('SELECT * FROM missing'), 'SQLITE_ERROR', 'no such table: missing'],
52
+ [() => db.prepare("SELECT json_extract('{', '$')").get(), 'SQLITE_ERROR', 'malformed JSON'],
53
+ [() => new Database(notADatabase), 'SQLITE_NOTADB', 'file is not a database'],
54
+ ];
55
+ for (const [run, code, message] of cases) {
56
+ let error;
57
+ try {
58
+ run();
59
+ }
60
+ catch (e) {
61
+ error = e;
62
+ }
63
+ expect(error).toBeInstanceOf(SqliteError);
64
+ expect({ code: error.code, message: error.message }).toEqual({ code, message });
65
+ }
66
+ unlinkSync(notADatabase);
67
+ });
21
68
  test('insert returning test', () => {
22
69
  const db = new Database(':memory:');
23
70
  db.prepare(`create table t (x);`).run();
@@ -79,7 +126,16 @@ test('readonly-db', () => {
79
126
  });
80
127
  test('file-must-exist', () => {
81
128
  const path = `test-${(Math.random() * 10000) | 0}.db`;
82
- expect(() => new Database(path, { fileMustExist: true })).toThrowError(/failed to open database/);
129
+ let error;
130
+ try {
131
+ new Database(path, { fileMustExist: true });
132
+ }
133
+ catch (e) {
134
+ error = e;
135
+ }
136
+ expect(error).toBeInstanceOf(SqliteError);
137
+ expect(error.code).toBe('SQLITE_CANTOPEN');
138
+ expect(error.message).toContain('I/O error (open)');
83
139
  });
84
140
  test('on-disk db', () => {
85
141
  const path = `test-${(Math.random() * 10000) | 0}.db`;
@@ -1,6 +1,6 @@
1
- import { unlinkSync } from "node:fs";
1
+ import { unlinkSync, writeFileSync } from "node:fs";
2
2
  import { expect, test } from 'vitest';
3
- import { Database, connect, Transaction } from './promise.js';
3
+ import { Database, connect, SqliteError, Transaction } from './promise.js';
4
4
  import { sql } from 'drizzle-orm';
5
5
  import { drizzle } from 'drizzle-orm/better-sqlite3';
6
6
  test('drizzle-orm', async () => {
@@ -29,6 +29,30 @@ test('in-memory-db-async', async () => {
29
29
  const rows = await stmt.all([1]);
30
30
  expect(rows).toEqual([{ x: 1 }, { x: 3 }]);
31
31
  });
32
+ test('concurrent calls on one prepared statement use their own parameters', async () => {
33
+ const db = await connect(":memory:");
34
+ await db.exec("CREATE TABLE items (key TEXT PRIMARY KEY, value TEXT NOT NULL)");
35
+ await db.exec("INSERT INTO items VALUES ('first', 'first value'), ('second', 'second value')");
36
+ const select = await db.prepare("SELECT value FROM items WHERE key = ?");
37
+ const rows = await Promise.all([select.get('first'), select.get('second')]);
38
+ expect(rows).toEqual([{ value: 'first value' }, { value: 'second value' }]);
39
+ const all = await Promise.all([select.all('first'), select.all('second')]);
40
+ expect(all).toEqual([[{ value: 'first value' }], [{ value: 'second value' }]]);
41
+ const collect = async (key) => {
42
+ const out = [];
43
+ for await (const row of select.iterate(key)) {
44
+ out.push(row);
45
+ }
46
+ return out;
47
+ };
48
+ const iterated = await Promise.all([collect('first'), collect('second')]);
49
+ expect(iterated).toEqual([[{ value: 'first value' }], [{ value: 'second value' }]]);
50
+ await db.exec("CREATE TABLE w (k TEXT)");
51
+ const insert = await db.prepare("INSERT INTO w VALUES (?)");
52
+ const results = await Promise.all([insert.run('a'), insert.run('b')]);
53
+ expect(results.map(r => r.changes)).toEqual([1, 1]);
54
+ expect(await db.all("SELECT k FROM w ORDER BY k")).toEqual([{ k: 'a' }, { k: 'b' }]);
55
+ });
32
56
  test('busy timeout gives up on time when the write lock is never released', async () => {
33
57
  const path = `test-${(Math.random() * 10000) | 0}.db`;
34
58
  try {
@@ -119,7 +143,10 @@ test('readonly-db', async () => {
119
143
  });
120
144
  test('file-must-exist', async () => {
121
145
  const path = `test-${(Math.random() * 10000) | 0}.db`;
122
- await expect(async () => await connect(path, { fileMustExist: true })).rejects.toThrowError(/failed to open database/);
146
+ const error = await connect(path, { fileMustExist: true }).then(() => undefined, (e) => e);
147
+ expect(error).toBeInstanceOf(SqliteError);
148
+ expect(error.code).toBe('SQLITE_CANTOPEN');
149
+ expect(error.message).toContain('I/O error (open)');
123
150
  });
124
151
  test('implicit connect', async () => {
125
152
  const db = new Database(':memory:');
@@ -128,6 +155,49 @@ test('implicit connect', async () => {
128
155
  await expect(async () => await db.prepare("SELECT * FROM t")).rejects.toThrowError(/no such table: t/);
129
156
  expect(await (await db.prepare("SELECT 1 as x")).all()).toEqual([{ x: 1 }]);
130
157
  });
158
+ test('errors carry the SQLite message and result code name', async () => {
159
+ const notADatabase = `test-${(Math.random() * 10000) | 0}.db`;
160
+ writeFileSync(notADatabase, Buffer.alloc(8192, 7));
161
+ const db = await connect(':memory:');
162
+ await db.exec('PRAGMA foreign_keys = ON');
163
+ await db.exec('CREATE TABLE t(a UNIQUE, b NOT NULL DEFAULT 1, c CHECK(c < 5))');
164
+ await db.exec('CREATE TABLE p(id INTEGER PRIMARY KEY)');
165
+ await db.exec('CREATE TABLE ch(pid REFERENCES p(id))');
166
+ await db.exec('INSERT INTO t(a) VALUES (1)');
167
+ await db.exec('INSERT INTO p VALUES (1)');
168
+ await db.exec('CREATE TABLE k(id TEXT PRIMARY KEY)');
169
+ await db.exec('CREATE TABLE pair(a, b, PRIMARY KEY(a, b))');
170
+ await db.exec("INSERT INTO k VALUES ('x')");
171
+ await db.exec('INSERT INTO pair VALUES (1, 1), (1, 2)');
172
+ await db.exec('CREATE TABLE s(a INTEGER) STRICT');
173
+ await db.exec('CREATE TABLE tr(a)');
174
+ await db.exec("CREATE TRIGGER trg BEFORE INSERT ON tr BEGIN SELECT RAISE(ABORT, 'nope'); END");
175
+ const cases = [
176
+ [async () => (await db.prepare('INSERT INTO t(a) VALUES (1)')).run(), 'SQLITE_CONSTRAINT_UNIQUE', 'UNIQUE constraint failed: t.a'],
177
+ [() => db.exec('INSERT INTO t(a) VALUES (1)'), 'SQLITE_CONSTRAINT_UNIQUE', 'UNIQUE constraint failed: t.a'],
178
+ [async () => (await db.prepare('INSERT INTO t(a, b) VALUES (2, NULL)')).get(), 'SQLITE_CONSTRAINT_NOTNULL', 'NOT NULL constraint failed: t.b'],
179
+ [async () => (await db.prepare('INSERT INTO t(a, c) VALUES (3, 9) RETURNING a')).all(), 'SQLITE_CONSTRAINT_CHECK', 'CHECK constraint failed: c < 5'],
180
+ [() => db.batch(['INSERT INTO t(a) VALUES (1)']), 'SQLITE_CONSTRAINT_UNIQUE', 'UNIQUE constraint failed: t.a'],
181
+ [async () => (await db.prepare('INSERT INTO p VALUES (1)')).run(), 'SQLITE_CONSTRAINT_PRIMARYKEY', 'UNIQUE constraint failed: p.id'],
182
+ [async () => (await db.prepare("INSERT INTO k VALUES ('x')")).run(), 'SQLITE_CONSTRAINT_PRIMARYKEY', 'UNIQUE constraint failed: k.id'],
183
+ [async () => (await db.prepare('INSERT INTO pair VALUES (1, 1)')).run(), 'SQLITE_CONSTRAINT_PRIMARYKEY', 'UNIQUE constraint failed: pair.a, pair.b'],
184
+ [async () => (await db.prepare('UPDATE pair SET b = 1 WHERE b = 2')).run(), 'SQLITE_CONSTRAINT_PRIMARYKEY', 'UNIQUE constraint failed: pair.a, pair.b'],
185
+ [async () => (await db.prepare('INSERT INTO ch VALUES (9)')).run(), 'SQLITE_CONSTRAINT_FOREIGNKEY', 'FOREIGN KEY constraint failed'],
186
+ [async () => (await db.prepare("INSERT INTO s VALUES ('x')")).run(), 'SQLITE_CONSTRAINT_DATATYPE', 'cannot store TEXT value in INTEGER column s.a'],
187
+ [async () => (await db.prepare('INSERT INTO tr VALUES (1)')).run(), 'SQLITE_CONSTRAINT_TRIGGER', 'nope'],
188
+ [async () => db.prepare('SYNTAX ERROR'), 'SQLITE_ERROR', 'near "SYNTAX": syntax error'],
189
+ [async () => db.prepare('SELECT * FROM missing'), 'SQLITE_ERROR', 'no such table: missing'],
190
+ [async () => (await new Database(':memory:').prepare('SELECT * FROM missing')).all(), 'SQLITE_ERROR', 'no such table: missing'],
191
+ [async () => (await db.prepare("SELECT json_extract('{', '$')")).get(), 'SQLITE_ERROR', 'malformed JSON'],
192
+ [() => connect(notADatabase), 'SQLITE_NOTADB', 'file is not a database'],
193
+ ];
194
+ for (const [run, code, message] of cases) {
195
+ const error = await run().then(() => undefined, (e) => e);
196
+ expect(error).toBeInstanceOf(SqliteError);
197
+ expect({ code: error.code, message: error.message }).toEqual({ code, message });
198
+ }
199
+ unlinkSync(notADatabase);
200
+ });
131
201
  test('zero-limit-bug', async () => {
132
202
  const db = await connect(':memory:');
133
203
  const create = await db.prepare(`CREATE TABLE users (name TEXT NOT NULL);`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tursodatabase/database",
3
- "version": "0.9.0-pre.1",
3
+ "version": "0.9.0-pre.3",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/tursodatabase/turso"
@@ -49,15 +49,15 @@
49
49
  ]
50
50
  },
51
51
  "dependencies": {
52
- "@tursodatabase/database-common": "^0.9.0-pre.1"
52
+ "@tursodatabase/database-common": "^0.9.0-pre.3"
53
53
  },
54
54
  "imports": {
55
55
  "#index": "./index.js"
56
56
  },
57
57
  "optionalDependencies": {
58
- "@tursodatabase/database-linux-x64-gnu": "0.9.0-pre.1",
59
- "@tursodatabase/database-win32-x64-msvc": "0.9.0-pre.1",
60
- "@tursodatabase/database-darwin-arm64": "0.9.0-pre.1",
61
- "@tursodatabase/database-linux-arm64-gnu": "0.9.0-pre.1"
58
+ "@tursodatabase/database-linux-x64-gnu": "0.9.0-pre.3",
59
+ "@tursodatabase/database-win32-x64-msvc": "0.9.0-pre.3",
60
+ "@tursodatabase/database-darwin-arm64": "0.9.0-pre.3",
61
+ "@tursodatabase/database-linux-arm64-gnu": "0.9.0-pre.3"
62
62
  }
63
63
  }