@bibliotek/sqlite 0.0.3 → 0.0.6
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/LICENSE +21 -0
- package/README.md +40 -0
- package/package.json +2 -1
- package/sqlite.js +8 -5
- package/test/test-sqlite.js +30 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright © 2022 Richard Remer
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
The **@bibliotek/sqlite** library is a light async/await wrapper around the
|
|
2
|
+
**sqlite3** library.
|
|
3
|
+
|
|
4
|
+
@bibliotek/sqlite
|
|
5
|
+
=================
|
|
6
|
+
For more detailed information, check the unit tests in the **./test**
|
|
7
|
+
directory.
|
|
8
|
+
|
|
9
|
+
Examples
|
|
10
|
+
--------
|
|
11
|
+
```js
|
|
12
|
+
import {Database, Statement} from "@bibliotek/sqlite";
|
|
13
|
+
|
|
14
|
+
let result = null;
|
|
15
|
+
let stmt = null;
|
|
16
|
+
|
|
17
|
+
const db = Database.memory(); // in-memory DB
|
|
18
|
+
// new Database("./my.db"); // ...or load from file
|
|
19
|
+
|
|
20
|
+
// use .run to execute DML statements
|
|
21
|
+
await db.run(`create table thing (id integer primary key autoincrement, value text)`);
|
|
22
|
+
result = await db.run(`insert into thing (value) values ('foo')`);
|
|
23
|
+
result = await db.run(`insert into thing (value) values ('bar')`);
|
|
24
|
+
console.log("insert id", result.lastID);
|
|
25
|
+
console.log("inserted", result.changes, "rows");
|
|
26
|
+
|
|
27
|
+
// use .each to iterate over results
|
|
28
|
+
for await (const row of db.each(`select * from thing`)) {
|
|
29
|
+
console.log(row);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// use .get to fetch a single row
|
|
33
|
+
console.log(await db.get("select * from thing where id = 1"));
|
|
34
|
+
|
|
35
|
+
// use .prepare for prepared statements
|
|
36
|
+
stmt = db.prepare("select * from thing where id = ?");
|
|
37
|
+
|
|
38
|
+
// pass params to statement methods to bind data to statement
|
|
39
|
+
result = await stmt.get(1); // bind 1 to id in prepared statement
|
|
40
|
+
```
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bibliotek/sqlite",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "Sqlite3 database using async/await",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": "./sqlite.js",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "mocha"
|
|
9
9
|
},
|
|
10
|
+
"license": "MIT",
|
|
10
11
|
"dependencies": {
|
|
11
12
|
"@bibliotek/synch": "^0.0.1",
|
|
12
13
|
"sqlite3": "^4.2.0"
|
package/sqlite.js
CHANGED
|
@@ -35,7 +35,7 @@ export class Database {
|
|
|
35
35
|
let done = false;
|
|
36
36
|
let sync = synch();
|
|
37
37
|
|
|
38
|
-
this.db.each(sql, record, complete);
|
|
38
|
+
this.db.each(String(sql), record, complete);
|
|
39
39
|
|
|
40
40
|
while (!done) {
|
|
41
41
|
await sync;
|
|
@@ -57,7 +57,7 @@ export class Database {
|
|
|
57
57
|
|
|
58
58
|
async exec(sql) {
|
|
59
59
|
return new Promise((resolve, reject) => {
|
|
60
|
-
this.db.exec(sql, err => {
|
|
60
|
+
this.db.exec(String(sql), err => {
|
|
61
61
|
if (err) reject(err); else resolve();
|
|
62
62
|
});
|
|
63
63
|
});
|
|
@@ -65,19 +65,19 @@ export class Database {
|
|
|
65
65
|
|
|
66
66
|
async get(sql, ...params) {
|
|
67
67
|
return new Promise((resolve, reject) => {
|
|
68
|
-
this.db.get(sql, ...params, (err, row) => {
|
|
68
|
+
this.db.get(String(sql), ...params, (err, row) => {
|
|
69
69
|
if (err) reject(err); else resolve(row);
|
|
70
70
|
});
|
|
71
71
|
});
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
prepare(sql) {
|
|
75
|
-
return new Statement(this.db.prepare(sql));
|
|
75
|
+
return new Statement(this.db.prepare(String(sql)));
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
async run(sql) {
|
|
79
79
|
return new Promise((resolve, reject) => {
|
|
80
|
-
this.db.run(sql, function(err) {
|
|
80
|
+
this.db.run(String(sql), function(err) {
|
|
81
81
|
if (err) {
|
|
82
82
|
reject(err);
|
|
83
83
|
} else {
|
|
@@ -148,4 +148,7 @@ export class Statement {
|
|
|
148
148
|
});
|
|
149
149
|
});
|
|
150
150
|
}
|
|
151
|
+
|
|
152
|
+
get lastID() { return this.stmt.lastID; }
|
|
153
|
+
get changes() { return this.stmt.changes; }
|
|
151
154
|
}
|
package/test/test-sqlite.js
CHANGED
|
@@ -59,7 +59,7 @@ describe("Database", () => {
|
|
|
59
59
|
|
|
60
60
|
it("should resolve with .lastID", async () => {
|
|
61
61
|
const db = Database.memory();
|
|
62
|
-
await db.run("create table t (id
|
|
62
|
+
await db.run("create table t (id integer not null primary key autoincrement, key text)");
|
|
63
63
|
await db.run("insert into t values (null, 'foo')");
|
|
64
64
|
const result = await db.run("insert into t values (null, 'bar')");
|
|
65
65
|
|
|
@@ -77,6 +77,16 @@ describe("Database", () => {
|
|
|
77
77
|
expect(result).to.be.an("object");
|
|
78
78
|
expect(result.changes).to.be(2);
|
|
79
79
|
});
|
|
80
|
+
|
|
81
|
+
it("should handle String instances", async () => {
|
|
82
|
+
const db = {run: sinon.spy((_, fn) => fn())};
|
|
83
|
+
const sqlite = new Database(db);
|
|
84
|
+
const sql = new String("select 1");
|
|
85
|
+
|
|
86
|
+
await sqlite.run(sql);
|
|
87
|
+
expect(db.run.calledOnce).to.be(true);
|
|
88
|
+
expect(db.run.calledWith("select 1")).to.be(true);
|
|
89
|
+
});
|
|
80
90
|
});
|
|
81
91
|
|
|
82
92
|
describe(".each(sql)", () => {
|
|
@@ -138,13 +148,14 @@ describe("Database", () => {
|
|
|
138
148
|
});
|
|
139
149
|
|
|
140
150
|
describe("Statement", () => {
|
|
141
|
-
let sqlite, insert, select;
|
|
151
|
+
let sqlite, insert, select, update;
|
|
142
152
|
|
|
143
153
|
beforeEach(async () => {
|
|
144
154
|
sqlite = Database.memory();
|
|
145
|
-
await sqlite.run("create table t (id
|
|
155
|
+
await sqlite.run("create table t (id integer not null primary key autoincrement, key text)");
|
|
146
156
|
insert = sqlite.prepare("insert into t values (?, ?)");
|
|
147
157
|
select = sqlite.prepare("select key from t where id between ? and ? order by id");
|
|
158
|
+
update = sqlite.prepare("update t set key = 'none' where id > ?");
|
|
148
159
|
});
|
|
149
160
|
|
|
150
161
|
describe(".run(...params)", () => {
|
|
@@ -155,6 +166,22 @@ describe("Statement", () => {
|
|
|
155
166
|
const row = await sqlite.get("select count(*) c from t");
|
|
156
167
|
expect(row.c).to.be(2);
|
|
157
168
|
});
|
|
169
|
+
|
|
170
|
+
it("should set .lastID on statement", async () => {
|
|
171
|
+
await insert.run(null, "foo");
|
|
172
|
+
await insert.run(null, "bar");
|
|
173
|
+
|
|
174
|
+
expect(insert.lastID).to.be(2);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("should set .changes on statement", async () => {
|
|
178
|
+
await insert.run(null, "foo");
|
|
179
|
+
await insert.run(null, "bar");
|
|
180
|
+
await insert.run(null, "baz");
|
|
181
|
+
await update.run(0);
|
|
182
|
+
|
|
183
|
+
expect(update.changes).to.be(3);
|
|
184
|
+
});
|
|
158
185
|
});
|
|
159
186
|
|
|
160
187
|
describe(".each(...params)", () => {
|