@smart-db/sqlite-node 0.1.2

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.
@@ -0,0 +1,2 @@
1
+ import type { AsyncDatabase } from "@smart-db/core";
2
+ export declare function createAsync(dbPath: string): AsyncDatabase;
package/dist/async.js ADDED
@@ -0,0 +1,48 @@
1
+ import NodeDatabase from "better-sqlite3";
2
+ import { getArgs } from "./utils.js";
3
+ export function createAsync(dbPath) {
4
+ const db = new NodeDatabase(dbPath);
5
+ async function run(sql, params) {
6
+ const info = db.prepare(sql).run(...getArgs(params));
7
+ return {
8
+ changes: info.changes,
9
+ lastInsertRowid: Number(info.lastInsertRowid)
10
+ };
11
+ }
12
+ async function all(sql, params) {
13
+ return db.prepare(sql).all(...getArgs(params));
14
+ }
15
+ async function get(sql, params) {
16
+ const result = db.prepare(sql).get(...getArgs(params));
17
+ return result ?? undefined;
18
+ }
19
+ async function first(sql, params) {
20
+ return get(sql, params);
21
+ }
22
+ async function value(sql, params) {
23
+ const result = db.prepare(sql).pluck(true).get(...getArgs(params));
24
+ return result ?? undefined;
25
+ }
26
+ async function pluck(sql, params) {
27
+ return db.prepare(sql).pluck(true).all(...getArgs(params));
28
+ }
29
+ function transaction(callback) {
30
+ const wrappedFunction = async (...args) => {
31
+ db.exec("BEGIN TRANSACTION");
32
+ try {
33
+ const result = await callback(...args);
34
+ db.exec("COMMIT");
35
+ return result;
36
+ }
37
+ catch (error) {
38
+ db.exec("ROLLBACK");
39
+ throw error;
40
+ }
41
+ };
42
+ return wrappedFunction;
43
+ }
44
+ async function close() {
45
+ db.close();
46
+ }
47
+ return { run, all, get, first, value, pluck, transaction, close };
48
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./sync.js";
2
+ export * from "./async.js";
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./sync.js";
2
+ export * from "./async.js";
package/dist/sync.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { SyncDatabase } from "@smart-db/core";
2
+ export declare function createSync(dbPath: string): SyncDatabase;
package/dist/sync.js ADDED
@@ -0,0 +1,36 @@
1
+ import NodeDatabase from "better-sqlite3";
2
+ import { getArgs } from "./utils.js";
3
+ export function createSync(dbPath) {
4
+ const db = new NodeDatabase(dbPath);
5
+ function run(sql, params) {
6
+ const info = db.prepare(sql).run(...getArgs(params));
7
+ return {
8
+ changes: info.changes,
9
+ lastInsertRowid: Number(info.lastInsertRowid)
10
+ };
11
+ }
12
+ function all(sql, params) {
13
+ return db.prepare(sql).all(...getArgs(params));
14
+ }
15
+ function get(sql, params) {
16
+ const result = db.prepare(sql).get(...getArgs(params));
17
+ return result ?? undefined;
18
+ }
19
+ function first(sql, params) {
20
+ return get(sql, params);
21
+ }
22
+ function value(sql, params) {
23
+ const result = db.prepare(sql).pluck(true).get(...getArgs(params));
24
+ return result ?? undefined;
25
+ }
26
+ function pluck(sql, params) {
27
+ return db.prepare(sql).pluck(true).all(...getArgs(params));
28
+ }
29
+ function transaction(callback) {
30
+ return db.transaction(callback);
31
+ }
32
+ function close() {
33
+ db.close();
34
+ }
35
+ return { run, all, get, first, value, pluck, transaction, close };
36
+ }
@@ -0,0 +1,2 @@
1
+ import type { SQLiteParams } from "@smart-db/core";
2
+ export declare function getArgs(params?: SQLiteParams): any[];
package/dist/utils.js ADDED
@@ -0,0 +1,7 @@
1
+ export function getArgs(params) {
2
+ if (!params)
3
+ return [];
4
+ if (Array.isArray(params))
5
+ return params;
6
+ return [params];
7
+ }
package/package.json ADDED
@@ -0,0 +1,44 @@
1
+ {
2
+ "name": "@smart-db/sqlite-node",
3
+ "version": "0.1.2",
4
+ "description": "Node.js better-sqlite3 adapter for the smart-db ecosystem",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/smart-db/smart-db.git",
8
+ "directory": "packages/sqlite-node"
9
+ },
10
+ "bugs": {
11
+ "url": "https://github.com/smart-db/smart-db/issues"
12
+ },
13
+ "homepage": "https://github.com/smart-db/smart-db#readme",
14
+ "author": "Martin Winkler",
15
+ "keywords": [],
16
+ "main": "dist/index.js",
17
+ "types": "dist/index.d.ts",
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "type": "module",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.js"
26
+ }
27
+ },
28
+ "scripts": {
29
+ "build": "tsc",
30
+ "prepublishOnly": "bun run build",
31
+ "test": "vitest run"
32
+ },
33
+ "publishConfig": {
34
+ "access": "public"
35
+ },
36
+ "dependencies": {
37
+ "@smart-db/core": "^0.1.1",
38
+ "better-sqlite3": "^12.11.1"
39
+ },
40
+ "peerDependencies": {},
41
+ "devDependencies": {
42
+ "@types/better-sqlite3": "^7.6.10"
43
+ }
44
+ }