Skip to content

Commit cb4207e

Browse files
feat(ext/node): add sqlite-type symbol for DatabaseSync (#30511)
Related: nodejs/node@d1eabcb Related: nodejs/node#59405 Related: better-auth/better-auth#3869 Related: oven-sh/bun#22109 Nowadays, there are tons of database packages, like sqlite3, sqlite, better-sqlite, bun:sqlite... Checking the difference from them is pretty hard. instanceof is not good, since the developer will still need to import the module, which is costly. I think we should provide a symbol to distinguish different SQLite classes, at least nodejs could make the first step. --------- Signed-off-by: Alex Yang <himself65@outlook.com> Co-authored-by: Bartek Iwańczuk <biwanczuk@gmail.com>
1 parent a434ac2 commit cb4207e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

ext/node/polyfills/sqlite.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
// Copyright 2018-2025 the Deno authors. MIT license.
22

3+
import { primordials } from "ext:core/mod.js";
34
import { DatabaseSync, StatementSync } from "ext:core/ops";
45

6+
const {
7+
ObjectDefineProperty,
8+
SymbolFor,
9+
} = primordials;
10+
511
export const constants = {
612
SQLITE_CHANGESET_OMIT: 0,
713
SQLITE_CHANGESET_REPLACE: 1,
@@ -14,6 +20,14 @@ export const constants = {
1420
SQLITE_CHANGESET_FOREIGN_KEY: 5,
1521
};
1622

23+
const sqliteTypeSymbol = SymbolFor("sqlite-type");
24+
ObjectDefineProperty(DatabaseSync.prototype, sqliteTypeSymbol, {
25+
__proto__: null,
26+
value: "node:sqlite",
27+
enumerable: false,
28+
configurable: true,
29+
});
30+
1731
export { DatabaseSync, StatementSync };
1832

1933
export default {

tests/unit_node/sqlite_test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ import { assert, assertEquals, assertThrows } from "@std/assert";
44

55
const tempDir = Deno.makeTempDirSync();
66

7+
Deno.test("[node/sqlite] sqlite-type symbol", () => {
8+
const db = new DatabaseSync(":memory:");
9+
const sqliteTypeSymbol = Symbol.for("sqlite-type");
10+
11+
// @ts-ignore `sqliteTypeSymbol` is not available in `@types:node` for version 24.3
12+
assertEquals(db[sqliteTypeSymbol], "node:sqlite");
13+
14+
db.close();
15+
});
16+
717
Deno.test("[node/sqlite] in-memory databases", () => {
818
const db1 = new DatabaseSync(":memory:");
919
const db2 = new DatabaseSync(":memory:");

0 commit comments

Comments
 (0)