@keyv/sqlite
Advanced tools
Comparing version 3.0.0 to 3.0.1
{ | ||
"name": "@keyv/sqlite", | ||
"version": "3.0.0", | ||
"version": "3.0.1", | ||
"description": "SQLite storage adapter for Keyv", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "xo && nyc ava", | ||
"test": "xo --fix && nyc ava", | ||
"coverage": "nyc report --reporter=text-lcov > coverage.lcov", | ||
@@ -12,5 +12,5 @@ "clean": "rm -rf node_modules && rm -rf .nyc_output && rm -rf coverage.lcov && rm -rf ./test/testdb.sqlite" | ||
"xo": { | ||
"extends": "xo-lukechilds", | ||
"rules": { | ||
"unicorn/prefer-module": 0 | ||
"unicorn/prefer-module": 0, | ||
"unicorn/prefer-node-protocol": 0 | ||
} | ||
@@ -50,4 +50,3 @@ }, | ||
"@keyv/test-suite": "*", | ||
"ava": "^3.15.0", | ||
"eslint-config-xo-lukechilds": "^1.0.1", | ||
"ava": "^4.0.1", | ||
"keyv": "*", | ||
@@ -57,4 +56,16 @@ "nyc": "^15.1.0", | ||
"this": "^1.1.0", | ||
"xo": "^0.47.0" | ||
} | ||
"tsd": "^0.19.1", | ||
"typescript": "^4.6.2", | ||
"xo": "^0.48.0" | ||
}, | ||
"tsd" : { | ||
"directory" : "test" | ||
}, | ||
"types": "./src/index.d.ts", | ||
"engines": { | ||
"node": ">= 12" | ||
}, | ||
"files": [ | ||
"src" | ||
] | ||
} |
@@ -10,12 +10,8 @@ 'use strict'; | ||
this.ttlSupport = false; | ||
options = Object.assign({ | ||
dialect: 'sqlite', | ||
uri: 'sqlite://:memory:', | ||
}, options); | ||
options = {dialect: 'sqlite', | ||
uri: 'sqlite://:memory:', ...options}; | ||
options.db = options.uri.replace(/^sqlite:\/\//, ''); | ||
this.opts = Object.assign({ | ||
table: 'keyv', | ||
keySize: 255, | ||
}, options); | ||
this.opts = {table: 'keyv', | ||
keySize: 255, ...options}; | ||
@@ -47,2 +43,26 @@ const createTable = `CREATE TABLE IF NOT EXISTS ${this.opts.table}(key VARCHAR(${Number(this.opts.keySize)}) PRIMARY KEY, value TEXT )`; | ||
getMany(keys) { | ||
const getMany = `SELECT * FROM ${this.opts.table} WHERE key IN (SELECT value FROM json_each(?))`; | ||
const rows = this.db.prepare(getMany).all(JSON.stringify(keys)); | ||
if (rows.length === 0) { | ||
return []; | ||
} | ||
const results = [...keys]; | ||
let i = 0; | ||
for (const key of keys) { | ||
const rowIndex = rows.findIndex(row => row.key === key); | ||
if (rowIndex > -1) { | ||
results[i] = rows[rowIndex].value; | ||
} else { | ||
results[i] = undefined; | ||
} | ||
i++; | ||
} | ||
return results; | ||
} | ||
set(key, value) { | ||
@@ -69,9 +89,43 @@ const upsert = `INSERT INTO ${this.opts.table} (key, value) | ||
deleteMany(keys) { | ||
const del = `DELETE FROM ${this.opts.table} WHERE key IN (SELECT value FROM json_each(?))`; | ||
const result = this.db.prepare(del).run(JSON.stringify(keys)); | ||
return result.changes !== 0; | ||
} | ||
clear() { | ||
const del = `DELETE FROM ${this.opts.table} WHERE key LIKE ?`; | ||
this.db.prepare(del).run(`${this.namespace}:%`); | ||
this.db.prepare(del).run(`${this.namespace ? this.namespace + ':' : ''}%`); | ||
return undefined; | ||
} | ||
async * iterator(namespace) { | ||
const limit = Number.parseInt(this.opts.iterationLimit, 10) || 10; | ||
async function * iterate(offset, options, db) { | ||
const select = `SELECT * FROM ${options.table} WHERE key LIKE ? LIMIT ? OFFSET ?`; | ||
const iterator = db.prepare(select).iterate([`${namespace ? namespace + ':' : ''}%`, limit, offset]); | ||
const enteries = [...iterator]; | ||
if (enteries.length === 0) { | ||
return; | ||
} | ||
for (const entry of enteries) { | ||
offset += 1; | ||
yield [entry.key, entry.value]; | ||
} | ||
if (offset !== 0) { | ||
yield * iterate(offset, options, db); | ||
} | ||
} | ||
yield * iterate(0, this.opts, this.db); | ||
} | ||
has(key) { | ||
const exists = `SELECT EXISTS ( SELECT * FROM ${this.opts.table} WHERE key = ? )`; | ||
return Object.values(this.db.prepare(exists).get(key))[0] === 1; | ||
} | ||
} | ||
module.exports = KeyvSqlite; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
129
0
6543
9
4