@bibliotek/sqlite 0.0.2 → 0.0.5

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@bibliotek/sqlite",
3
- "version": "0.0.2",
3
+ "version": "0.0.5",
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": "^5.0.2"
12
+ "sqlite3": "^4.2.0"
13
13
  },
14
14
  "devDependencies": {
15
15
  "expect.js": "^0.3.1",
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,20 +65,25 @@ 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, err => {
81
- if (err) reject(err); else resolve();
80
+ this.db.run(String(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
  }
@@ -143,4 +148,7 @@ export 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
  }
@@ -56,6 +56,37 @@ describe("Database", () => {
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
+ });
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
+ });
59
90
  });
60
91
 
61
92
  describe(".each(sql)", () => {
@@ -117,13 +148,14 @@ describe("Database", () => {
117
148
  });
118
149
 
119
150
  describe("Statement", () => {
120
- let sqlite, insert, select;
151
+ let sqlite, insert, select, update;
121
152
 
122
153
  beforeEach(async () => {
123
154
  sqlite = Database.memory();
124
- await sqlite.run("create table t (id int, key text)");
155
+ await sqlite.run("create table t (id integer not null primary key autoincrement, key text)");
125
156
  insert = sqlite.prepare("insert into t values (?, ?)");
126
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 > ?");
127
159
  });
128
160
 
129
161
  describe(".run(...params)", () => {
@@ -134,6 +166,22 @@ describe("Statement", () => {
134
166
  const row = await sqlite.get("select count(*) c from t");
135
167
  expect(row.c).to.be(2);
136
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
+ });
137
185
  });
138
186
 
139
187
  describe(".each(...params)", () => {