1 | 'use strict';
|
2 |
|
3 | const EventEmitter = require('events');
|
4 | const sqlite3 = require('sqlite3');
|
5 | const pify = require('pify');
|
6 |
|
7 | class KeyvSqlite extends EventEmitter {
|
8 | constructor(options) {
|
9 | super();
|
10 | this.ttlSupport = false;
|
11 | options = Object.assign({
|
12 | dialect: 'sqlite',
|
13 | uri: 'sqlite://:memory:',
|
14 | }, options);
|
15 | options.db = options.uri.replace(/^sqlite:\/\//, '');
|
16 |
|
17 | options.connect = () => new Promise((resolve, reject) => {
|
18 | const db = new sqlite3.Database(options.db, error => {
|
19 | if (error) {
|
20 | reject(error);
|
21 | } else {
|
22 | if (options.busyTimeout) {
|
23 | db.configure('busyTimeout', options.busyTimeout);
|
24 | }
|
25 |
|
26 | resolve(db);
|
27 | }
|
28 | });
|
29 | })
|
30 | .then(db => pify(db.all).bind(db));
|
31 |
|
32 | this.opts = Object.assign({
|
33 | table: 'keyv',
|
34 | keySize: 255,
|
35 | }, options);
|
36 |
|
37 | const createTable = `CREATE TABLE IF NOT EXISTS ${this.opts.table}(key VARCHAR(${Number(this.opts.keySize)}) PRIMARY KEY, value TEXT )`;
|
38 |
|
39 | const connected = this.opts.connect()
|
40 | .then(query => query(createTable).then(() => query))
|
41 | .catch(error => this.emit('error', error));
|
42 |
|
43 | this.query = sqlString => connected
|
44 | .then(query => query(sqlString));
|
45 | }
|
46 |
|
47 | get(key) {
|
48 | const select = `SELECT * FROM ${this.opts.table} WHERE key = '${key}'`;
|
49 | return this.query(select)
|
50 | .then(rows => {
|
51 | const row = rows[0];
|
52 | if (row === undefined) {
|
53 | return undefined;
|
54 | }
|
55 |
|
56 | return row.value;
|
57 | });
|
58 | }
|
59 |
|
60 | set(key, value) {
|
61 | const upsert = `INSERT INTO ${this.opts.table} (key, value)
|
62 | VALUES('${key}', '${value}')
|
63 | ON CONFLICT(key)
|
64 | DO UPDATE SET value=excluded.value;`;
|
65 | return this.query(upsert);
|
66 | }
|
67 |
|
68 | delete(key) {
|
69 | const select = `SELECT * FROM ${this.opts.table} WHERE key = '${key}'`;
|
70 | const del = `DELETE FROM ${this.opts.table} WHERE key = '${key}'`;
|
71 | return this.query(select)
|
72 | .then(rows => {
|
73 | const row = rows[0];
|
74 | if (row === undefined) {
|
75 | return false;
|
76 | }
|
77 |
|
78 | return this.query(del)
|
79 | .then(() => true);
|
80 | });
|
81 | }
|
82 |
|
83 | clear() {
|
84 | const del = `DELETE FROM ${this.opts.table} WHERE key LIKE '${this.namespace}:%'`;
|
85 | return this.query(del)
|
86 | .then(() => undefined);
|
87 | }
|
88 | }
|
89 |
|
90 | module.exports = KeyvSqlite;
|