@bibliotek/sqlite 0.0.7 → 0.1.0

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.7",
3
+ "version": "0.1.0",
4
4
  "description": "Sqlite3 database using async/await",
5
5
  "type": "module",
6
6
  "exports": "./sqlite.js",
package/sqlite.js CHANGED
@@ -1,13 +1,26 @@
1
+ import {Console} from "console";
1
2
  import Sqlite3 from "sqlite3";
2
3
  import synch from "@bibliotek/synch";
3
4
 
4
5
  export class Database {
5
- constructor(db) {
6
+ db;
7
+ console;
8
+
9
+ /**
10
+ * @param {Sqlite3.Database} db
11
+ * @param {Console} [console]
12
+ */
13
+ constructor(db, console=undefined) {
6
14
  if (typeof db === "string") {
7
15
  db = new Sqlite3.Database(db);
8
16
  }
9
17
 
18
+ if (!console) {
19
+ console = {log() {}, debug() {}, info() {}, warn() {}, error() {}};
20
+ }
21
+
10
22
  this.db = db;
23
+ this.console = console;
11
24
  }
12
25
 
13
26
  static memory() {
@@ -35,6 +48,7 @@ export class Database {
35
48
  let done = false;
36
49
  let sync = synch();
37
50
 
51
+ this.console.debug(String(sql));
38
52
  this.db.each(String(sql), record, complete);
39
53
 
40
54
  while (!done) {
@@ -57,6 +71,7 @@ export class Database {
57
71
 
58
72
  async exec(sql) {
59
73
  return new Promise((resolve, reject) => {
74
+ this.console.debug(String(sql));
60
75
  this.db.exec(String(sql), err => {
61
76
  if (err) reject(err); else resolve();
62
77
  });
@@ -65,6 +80,7 @@ export class Database {
65
80
 
66
81
  async get(sql, ...params) {
67
82
  return new Promise((resolve, reject) => {
83
+ this.console.debug(String(sql));
68
84
  this.db.get(String(sql), ...params, (err, row) => {
69
85
  if (err) reject(err); else resolve(row);
70
86
  });
@@ -72,11 +88,13 @@ export class Database {
72
88
  }
73
89
 
74
90
  prepare(sql) {
91
+ this.console.debug(String(sql));
75
92
  return new Statement(this.db.prepare(String(sql)));
76
93
  }
77
94
 
78
95
  async run(sql) {
79
96
  return new Promise((resolve, reject) => {
97
+ this.console.debug(String(sql));
80
98
  this.db.run(String(sql), function(err) {
81
99
  if (err) {
82
100
  reject(err);
@@ -145,6 +145,18 @@ describe("Database", () => {
145
145
  expect(stmt).to.be.a(Statement);
146
146
  });
147
147
  });
148
+
149
+ describe("logging", () => {
150
+ it("should log queries to provided console", async () => {
151
+ const console = mockConsole();
152
+ const sqlite = new Database(":memory:", console);
153
+
154
+ await sqlite.run("create table t (i int, f text)");
155
+
156
+ expect(console.debug.calledOnce).to.be(true);
157
+ expect(console.debug.calledWith("create table t (i int, f text)")).to.be(true);
158
+ });
159
+ });
148
160
  });
149
161
 
150
162
  describe("Statement", () => {
@@ -237,3 +249,13 @@ describe("Statement", () => {
237
249
  });
238
250
  });
239
251
  });
252
+
253
+ function mockConsole() {
254
+ return {
255
+ log: sinon.spy(),
256
+ debug: sinon.spy(),
257
+ info: sinon.spy(),
258
+ warn: sinon.spy(),
259
+ error: sinon.spy(),
260
+ };
261
+ }