@bibliotek/sqlite 0.0.1 → 0.0.4
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 +2 -2
- package/sqlite.js +14 -6
- package/test/test-sqlite.js +57 -19
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bibliotek/sqlite",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.4",
|
|
4
4
|
"description": "Sqlite3 database using async/await",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": "./sqlite.js",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@bibliotek/synch": "^0.0.1",
|
|
12
|
-
"sqlite3": "^
|
|
12
|
+
"sqlite3": "^4.2.0"
|
|
13
13
|
},
|
|
14
14
|
"devDependencies": {
|
|
15
15
|
"expect.js": "^0.3.1",
|
package/sqlite.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import Sqlite3 from "sqlite3";
|
|
2
2
|
import synch from "@bibliotek/synch";
|
|
3
3
|
|
|
4
|
-
export class
|
|
4
|
+
export class Database {
|
|
5
5
|
constructor(db) {
|
|
6
6
|
if (typeof db === "string") {
|
|
7
7
|
db = new Sqlite3.Database(db);
|
|
@@ -11,11 +11,11 @@ export class Sqlite {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
static memory() {
|
|
14
|
-
return new
|
|
14
|
+
return new Database(":memory:");
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
static disk() {
|
|
18
|
-
return new
|
|
18
|
+
return new Database("");
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
static get Statement() {
|
|
@@ -77,14 +77,19 @@ export class Sqlite {
|
|
|
77
77
|
|
|
78
78
|
async run(sql) {
|
|
79
79
|
return new Promise((resolve, reject) => {
|
|
80
|
-
this.db.run(sql, err
|
|
81
|
-
if (err)
|
|
80
|
+
this.db.run(sql, function(err) {
|
|
81
|
+
if (err) {
|
|
82
|
+
reject(err);
|
|
83
|
+
} else {
|
|
84
|
+
const {lastID, changes} = this || {};
|
|
85
|
+
resolve({lastID, changes});
|
|
86
|
+
}
|
|
82
87
|
});
|
|
83
88
|
});
|
|
84
89
|
}
|
|
85
90
|
}
|
|
86
91
|
|
|
87
|
-
class Statement {
|
|
92
|
+
export class Statement {
|
|
88
93
|
constructor(stmt) {
|
|
89
94
|
this.stmt = stmt;
|
|
90
95
|
}
|
|
@@ -143,4 +148,7 @@ class Statement {
|
|
|
143
148
|
});
|
|
144
149
|
});
|
|
145
150
|
}
|
|
151
|
+
|
|
152
|
+
get lastID() { return this.stmt.lastID; }
|
|
153
|
+
get changes() { return this.stmt.changes; }
|
|
146
154
|
}
|
package/test/test-sqlite.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import expect from "expect.js";
|
|
2
2
|
import sinon from "sinon";
|
|
3
3
|
import Sqlite3 from "sqlite3";
|
|
4
|
-
import {
|
|
4
|
+
import {Database, Statement} from "@bibliotek/sqlite";
|
|
5
5
|
|
|
6
|
-
describe("
|
|
6
|
+
describe("Database", () => {
|
|
7
7
|
describe("constructor(db)", () => {
|
|
8
8
|
it("should initialize db from file path", async () => {
|
|
9
|
-
const db = new
|
|
10
|
-
expect(db).to.be.a(
|
|
9
|
+
const db = new Database(":memory:");
|
|
10
|
+
expect(db).to.be.a(Database);
|
|
11
11
|
expect(db.db).to.be.a(Sqlite3.Database);
|
|
12
12
|
expect(db.db.filename).to.be(":memory:");
|
|
13
13
|
await db.close();
|
|
@@ -15,22 +15,22 @@ describe("Sqlite", () => {
|
|
|
15
15
|
|
|
16
16
|
it("should use database if provided", async () => {
|
|
17
17
|
const sqlite = {};
|
|
18
|
-
const db = new
|
|
18
|
+
const db = new Database(sqlite);
|
|
19
19
|
expect(db.db).to.be(sqlite);
|
|
20
20
|
});
|
|
21
21
|
});
|
|
22
22
|
|
|
23
|
-
describe("
|
|
23
|
+
describe("Database.memory()", () => {
|
|
24
24
|
it("should return in-memory DB", async () => {
|
|
25
|
-
const db =
|
|
25
|
+
const db = Database.memory();
|
|
26
26
|
expect(db.db.filename).to.be(":memory:");
|
|
27
27
|
await db.close();
|
|
28
28
|
});
|
|
29
29
|
});
|
|
30
30
|
|
|
31
|
-
describe("
|
|
31
|
+
describe("Database.disk()", () => {
|
|
32
32
|
it("should return on-disk temp DB", async () => {
|
|
33
|
-
const db =
|
|
33
|
+
const db = Database.disk();
|
|
34
34
|
expect(db.db.filename).to.be("");
|
|
35
35
|
await db.close();
|
|
36
36
|
});
|
|
@@ -39,7 +39,7 @@ describe("Sqlite", () => {
|
|
|
39
39
|
describe(".close()", () => {
|
|
40
40
|
it("should close the DB", async () => {
|
|
41
41
|
const db = {close: sinon.spy(fn => fn())};
|
|
42
|
-
const sqlite = new
|
|
42
|
+
const sqlite = new Database(db);
|
|
43
43
|
|
|
44
44
|
await sqlite.close();
|
|
45
45
|
expect(db.close.calledOnce).to.be(true);
|
|
@@ -49,20 +49,41 @@ describe("Sqlite", () => {
|
|
|
49
49
|
describe(".run(sql)", () => {
|
|
50
50
|
it("should run query on the DB", async () => {
|
|
51
51
|
const db = {run: sinon.spy((_, fn) => fn())};
|
|
52
|
-
const sqlite = new
|
|
52
|
+
const sqlite = new Database(db);
|
|
53
53
|
const sql = "select 1";
|
|
54
54
|
|
|
55
55
|
await sqlite.run(sql);
|
|
56
56
|
expect(db.run.calledOnce).to.be(true);
|
|
57
57
|
expect(db.run.calledWith(sql)).to.be(true);
|
|
58
58
|
});
|
|
59
|
+
|
|
60
|
+
it("should resolve with .lastID", async () => {
|
|
61
|
+
const db = Database.memory();
|
|
62
|
+
await db.run("create table t (id integer not null primary key autoincrement, key text)");
|
|
63
|
+
await db.run("insert into t values (null, 'foo')");
|
|
64
|
+
const result = await db.run("insert into t values (null, 'bar')");
|
|
65
|
+
|
|
66
|
+
expect(result).to.be.an("object");
|
|
67
|
+
expect(result.lastID).to.be(2);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("should resolve with .changes", async () => {
|
|
71
|
+
const db = Database.memory();
|
|
72
|
+
await db.run("create table t (id int, key text)");
|
|
73
|
+
await db.run("insert into t values (1, 'foo')");
|
|
74
|
+
await db.run("insert into t values (3, 'bar')");
|
|
75
|
+
const result = await db.run("update t set key = 'new'");
|
|
76
|
+
|
|
77
|
+
expect(result).to.be.an("object");
|
|
78
|
+
expect(result.changes).to.be(2);
|
|
79
|
+
});
|
|
59
80
|
});
|
|
60
81
|
|
|
61
82
|
describe(".each(sql)", () => {
|
|
62
83
|
let sqlite;
|
|
63
84
|
|
|
64
85
|
beforeEach(async () => {
|
|
65
|
-
sqlite =
|
|
86
|
+
sqlite = Database.memory();
|
|
66
87
|
await sqlite.run("create table t (id int, key text)");
|
|
67
88
|
await sqlite.run("insert into t values (1, 'foo')");
|
|
68
89
|
await sqlite.run("insert into t values (2, 'bar')");
|
|
@@ -89,7 +110,7 @@ describe("Sqlite", () => {
|
|
|
89
110
|
let sqlite;
|
|
90
111
|
|
|
91
112
|
beforeEach(async () => {
|
|
92
|
-
sqlite =
|
|
113
|
+
sqlite = Database.memory();
|
|
93
114
|
await sqlite.run("create table t (id int, key text)");
|
|
94
115
|
await sqlite.run("insert into t values (1, 'foo')");
|
|
95
116
|
await sqlite.run("insert into t values (2, 'bar')");
|
|
@@ -106,24 +127,25 @@ describe("Sqlite", () => {
|
|
|
106
127
|
|
|
107
128
|
describe(".prepare(sql)", () => {
|
|
108
129
|
it("should return a prepared statment", async () => {
|
|
109
|
-
const sqlite =
|
|
130
|
+
const sqlite = Database.memory();
|
|
110
131
|
await sqlite.run("create table t (i int, f text)");
|
|
111
132
|
|
|
112
133
|
const stmt = sqlite.prepare("select f from t where i = ?");
|
|
113
134
|
|
|
114
|
-
expect(stmt).to.be.a(
|
|
135
|
+
expect(stmt).to.be.a(Statement);
|
|
115
136
|
});
|
|
116
137
|
});
|
|
117
138
|
});
|
|
118
139
|
|
|
119
|
-
describe("
|
|
120
|
-
let sqlite, insert, select;
|
|
140
|
+
describe("Statement", () => {
|
|
141
|
+
let sqlite, insert, select, update;
|
|
121
142
|
|
|
122
143
|
beforeEach(async () => {
|
|
123
|
-
sqlite =
|
|
124
|
-
await sqlite.run("create table t (id
|
|
144
|
+
sqlite = Database.memory();
|
|
145
|
+
await sqlite.run("create table t (id integer not null primary key autoincrement, key text)");
|
|
125
146
|
insert = sqlite.prepare("insert into t values (?, ?)");
|
|
126
147
|
select = sqlite.prepare("select key from t where id between ? and ? order by id");
|
|
148
|
+
update = sqlite.prepare("update t set key = 'none' where id > ?");
|
|
127
149
|
});
|
|
128
150
|
|
|
129
151
|
describe(".run(...params)", () => {
|
|
@@ -134,6 +156,22 @@ describe("Sqlite.Statement", () => {
|
|
|
134
156
|
const row = await sqlite.get("select count(*) c from t");
|
|
135
157
|
expect(row.c).to.be(2);
|
|
136
158
|
});
|
|
159
|
+
|
|
160
|
+
it("should set .lastID on statement", async () => {
|
|
161
|
+
await insert.run(null, "foo");
|
|
162
|
+
await insert.run(null, "bar");
|
|
163
|
+
|
|
164
|
+
expect(insert.lastID).to.be(2);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("should set .changes on statement", async () => {
|
|
168
|
+
await insert.run(null, "foo");
|
|
169
|
+
await insert.run(null, "bar");
|
|
170
|
+
await insert.run(null, "baz");
|
|
171
|
+
await update.run(0);
|
|
172
|
+
|
|
173
|
+
expect(update.changes).to.be(3);
|
|
174
|
+
});
|
|
137
175
|
});
|
|
138
176
|
|
|
139
177
|
describe(".each(...params)", () => {
|