@saluzi/saluzi-edu 0.2.25 → 0.2.26
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/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Database } from 'bun:sqlite'
|
|
1
|
+
import type { Database } from 'bun:sqlite'
|
|
2
2
|
import { readFileSync, existsSync, unlinkSync } from 'node:fs'
|
|
3
3
|
import { resolve, dirname } from 'node:path'
|
|
4
4
|
import { fileURLToPath } from 'node:url'
|
|
@@ -6,6 +6,84 @@ import { tmpdir } from 'node:os'
|
|
|
6
6
|
|
|
7
7
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
8
8
|
|
|
9
|
+
// Cross-runtime SQLite resolution: bun:sqlite → node:sqlite → libsql.
|
|
10
|
+
// Static `import { Database } from 'bun:sqlite'` is Bun-only and throws
|
|
11
|
+
// ReferenceError under Node.js (slz bin → dist/cli-node.js). RCS requires
|
|
12
|
+
// a working DB — throws a clear error if no backend is available.
|
|
13
|
+
type DatabaseCtor = new (path: string) => Database
|
|
14
|
+
|
|
15
|
+
const ResolvedDatabase: DatabaseCtor = (() => {
|
|
16
|
+
// Tier 1: bun:sqlite (Bun runtime)
|
|
17
|
+
try {
|
|
18
|
+
const mod = require('bun:sqlite')
|
|
19
|
+
if (mod?.Database) return mod.Database as DatabaseCtor
|
|
20
|
+
} catch {
|
|
21
|
+
/* not Bun */
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Tier 2: node:sqlite (Node >= 22.5; --experimental-sqlite on 22.5-22.x)
|
|
25
|
+
try {
|
|
26
|
+
const { DatabaseSync } = require('node:sqlite')
|
|
27
|
+
if (DatabaseSync) {
|
|
28
|
+
// node:sqlite has prepare() but not query() — add bun:sqlite compat.
|
|
29
|
+
return class NodeSqliteCompat extends DatabaseSync {
|
|
30
|
+
query(sql: string) {
|
|
31
|
+
const stmt = this.prepare(sql)
|
|
32
|
+
return {
|
|
33
|
+
get: (...params: unknown[]) => stmt.get(...params),
|
|
34
|
+
all: (...params: unknown[]) => stmt.all(...params),
|
|
35
|
+
run: (...params: unknown[]) => stmt.run(...params),
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
} as unknown as DatabaseCtor
|
|
39
|
+
}
|
|
40
|
+
} catch {
|
|
41
|
+
/* Node < 22.5 or flag not set */
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Tier 3: libsql (optionalDependency — covers Node 20/21)
|
|
45
|
+
try {
|
|
46
|
+
const libsql = require('libsql')
|
|
47
|
+
if (libsql?.Database) {
|
|
48
|
+
return class LibsqlCompat {
|
|
49
|
+
private _db: {
|
|
50
|
+
prepare(sql: string): {
|
|
51
|
+
get(...p: unknown[]): unknown
|
|
52
|
+
all(...p: unknown[]): unknown[]
|
|
53
|
+
run(...p: unknown[]): { changes: number }
|
|
54
|
+
}
|
|
55
|
+
exec(sql: string): void
|
|
56
|
+
close(): void
|
|
57
|
+
}
|
|
58
|
+
constructor(path: string) {
|
|
59
|
+
this._db = new libsql.Database(path)
|
|
60
|
+
}
|
|
61
|
+
exec(sql: string): void {
|
|
62
|
+
this._db.exec(sql)
|
|
63
|
+
}
|
|
64
|
+
query(sql: string) {
|
|
65
|
+
const stmt = this._db.prepare(sql)
|
|
66
|
+
return {
|
|
67
|
+
get: (...params: unknown[]) => stmt.get(...params),
|
|
68
|
+
all: (...params: unknown[]) => stmt.all(...params),
|
|
69
|
+
run: (...params: unknown[]) => stmt.run(...params),
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
close(): void {
|
|
73
|
+
this._db.close()
|
|
74
|
+
}
|
|
75
|
+
} as unknown as DatabaseCtor
|
|
76
|
+
}
|
|
77
|
+
} catch {
|
|
78
|
+
/* libsql not installed */
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
throw new Error(
|
|
82
|
+
'[RCS] No SQLite backend available. ' +
|
|
83
|
+
'Install libsql (npm i libsql), use Node >= 22.5, or run under Bun.',
|
|
84
|
+
)
|
|
85
|
+
})()
|
|
86
|
+
|
|
9
87
|
let _db: Database | null = null
|
|
10
88
|
let _dbPath: string | null = null
|
|
11
89
|
|
|
@@ -94,7 +172,7 @@ function resolveDefaultPath(): string {
|
|
|
94
172
|
*/
|
|
95
173
|
export function initDatabase(path?: string): Database {
|
|
96
174
|
const dbPath = path || resolveDefaultPath()
|
|
97
|
-
const db = new
|
|
175
|
+
const db = new ResolvedDatabase(dbPath)
|
|
98
176
|
db.exec('PRAGMA journal_mode = WAL')
|
|
99
177
|
db.exec('PRAGMA foreign_keys = ON')
|
|
100
178
|
migrateDatabase(db)
|