@bibliotek/sqlite 0.0.1 → 0.0.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.
- package/package.json +1 -1
- package/sqlite.js +4 -4
- package/test/test-sqlite.js +17 -17
package/package.json
CHANGED
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() {
|
|
@@ -84,7 +84,7 @@ export class Sqlite {
|
|
|
84
84
|
}
|
|
85
85
|
}
|
|
86
86
|
|
|
87
|
-
class Statement {
|
|
87
|
+
export class Statement {
|
|
88
88
|
constructor(stmt) {
|
|
89
89
|
this.stmt = stmt;
|
|
90
90
|
}
|
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,7 +49,7 @@ 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);
|
|
@@ -62,7 +62,7 @@ describe("Sqlite", () => {
|
|
|
62
62
|
let sqlite;
|
|
63
63
|
|
|
64
64
|
beforeEach(async () => {
|
|
65
|
-
sqlite =
|
|
65
|
+
sqlite = Database.memory();
|
|
66
66
|
await sqlite.run("create table t (id int, key text)");
|
|
67
67
|
await sqlite.run("insert into t values (1, 'foo')");
|
|
68
68
|
await sqlite.run("insert into t values (2, 'bar')");
|
|
@@ -89,7 +89,7 @@ describe("Sqlite", () => {
|
|
|
89
89
|
let sqlite;
|
|
90
90
|
|
|
91
91
|
beforeEach(async () => {
|
|
92
|
-
sqlite =
|
|
92
|
+
sqlite = Database.memory();
|
|
93
93
|
await sqlite.run("create table t (id int, key text)");
|
|
94
94
|
await sqlite.run("insert into t values (1, 'foo')");
|
|
95
95
|
await sqlite.run("insert into t values (2, 'bar')");
|
|
@@ -106,21 +106,21 @@ describe("Sqlite", () => {
|
|
|
106
106
|
|
|
107
107
|
describe(".prepare(sql)", () => {
|
|
108
108
|
it("should return a prepared statment", async () => {
|
|
109
|
-
const sqlite =
|
|
109
|
+
const sqlite = Database.memory();
|
|
110
110
|
await sqlite.run("create table t (i int, f text)");
|
|
111
111
|
|
|
112
112
|
const stmt = sqlite.prepare("select f from t where i = ?");
|
|
113
113
|
|
|
114
|
-
expect(stmt).to.be.a(
|
|
114
|
+
expect(stmt).to.be.a(Statement);
|
|
115
115
|
});
|
|
116
116
|
});
|
|
117
117
|
});
|
|
118
118
|
|
|
119
|
-
describe("
|
|
119
|
+
describe("Statement", () => {
|
|
120
120
|
let sqlite, insert, select;
|
|
121
121
|
|
|
122
122
|
beforeEach(async () => {
|
|
123
|
-
sqlite =
|
|
123
|
+
sqlite = Database.memory();
|
|
124
124
|
await sqlite.run("create table t (id int, key text)");
|
|
125
125
|
insert = sqlite.prepare("insert into t values (?, ?)");
|
|
126
126
|
select = sqlite.prepare("select key from t where id between ? and ? order by id");
|