Browse Source

Merge pull request #28 from microlinkhq/options

master
Jytesh 3 years ago
committed by GitHub
parent
commit
74aa78b436
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      package.json
  2. 5
      packages/core/package.json
  3. 99
      packages/core/src/index.js
  4. 2
      packages/mongo/package.json
  5. 2
      packages/mysql/package.json
  6. 2
      packages/postgres/package.json
  7. 4
      packages/redis/src/index.js
  8. 2
      packages/sql/package.json
  9. 2
      packages/sqlite/package.json
  10. 2
      packages/test-suite/package.json

2
package.json

@ -1,7 +1,7 @@
{
"name": "@keyvhq/monorepo",
"description": "Simple key-value storage with support for multiple backends",
"homepage": "https://github.com/microlinkhq/keyv#readme",
"homepage": "https://keyv.js.org",
"version": "",
"author": {
"email": "hello@microlink.io",

5
packages/core/package.json

@ -1,7 +1,7 @@
{
"name": "@keyvhq/core",
"description": "Simple key-value storage with support for multiple backends",
"homepage": "https://github.com/microlinkhq/keyv",
"homepage": "https://keyv.js.org",
"version": "1.0.2",
"main": "src/index.js",
"author": {
@ -40,8 +40,7 @@
"src"
],
"scripts": {
"test": "nyc --temp-dir ../../.nyc_output ava test/keyv.js",
"test:full": "nyc --temp-dir ../../.nyc_output ava --serial"
"test": "nyc --temp-dir ../../.nyc_output ava test/keyv.js"
},
"license": "MIT"
}

99
packages/core/src/index.js

@ -6,18 +6,23 @@ const JSONB = require('json-buffer')
class Keyv extends EventEmitter {
constructor ({ emitErrors = true, ...options } = {}) {
super()
this.options = Object.assign(
const normalizedOptions = Object.assign(
{
namespace: 'keyv',
serialize: JSONB.stringify,
deserialize: JSONB.parse,
emitErrors: true
emitErrors: true,
store: new Map()
},
options
)
this.store = this.options.store || new Map()
this.store.namespace = this.options.namespace
Object.keys(normalizedOptions).forEach(
key => (this[key] = normalizedOptions[key])
)
this.store.namespace = this.namespace
if (typeof this.store.on === 'function' && emitErrors) {
this.store.on('error', error => this.emit('error', error))
@ -28,12 +33,8 @@ class Keyv extends EventEmitter {
for await (const [key, raw] of typeof iterator === 'function'
? iterator()
: iterator) {
const data =
typeof raw === 'string' ? this.options.deserialize(raw) : raw
if (
!key.includes(this.options.namespace) ||
typeof data !== 'object'
) {
const data = typeof raw === 'string' ? this.deserialize(raw) : raw
if (!key.includes(this.namespace) || typeof data !== 'object') {
continue
}
@ -60,11 +61,11 @@ class Keyv extends EventEmitter {
}
_getKeyPrefix (key) {
return this.options.namespace ? `${this.options.namespace}:${key}` : key
return this.namespace ? `${this.namespace}:${key}` : key
}
_getKeyUnprefix (key) {
return this.options.namespace
return this.namespace
? key
.split(':')
.splice(1)
@ -72,75 +73,43 @@ class Keyv extends EventEmitter {
: key
}
get (key, options) {
const keyPrefixed = this._getKeyPrefix(key)
const store = this.store
return Promise.resolve()
.then(() => store.get(keyPrefixed))
.then(data => {
return typeof data === 'string' ? this.options.deserialize(data) : data
})
.then(data => {
if (data === undefined) {
return undefined
}
async get (key, { raw: asRaw = false } = {}) {
const raw = await this.store.get(this._getKeyPrefix(key))
if (raw === undefined) return undefined
const data = typeof raw === 'string' ? await this.deserialize(raw) : raw
if (typeof data.expires === 'number' && Date.now() > data.expires) {
this.delete(key)
return undefined
}
return options && options.raw ? data : data.value
})
return asRaw ? data : data.value
}
has (key) {
const keyPrefixed = this._getKeyPrefix(key)
const store = this.store
if (typeof store.has === 'function') {
return Promise.resolve().then(() => store.has(keyPrefixed))
async has (key) {
return typeof this.store.has === 'function'
? this.store.has(this._getKeyPrefix(key))
: (await this.store.get(this._getKeyPrefix(key))) !== undefined
}
return Promise.resolve()
.then(() => store.get(keyPrefixed))
.then(data => data !== undefined)
}
set (key, value, ttl) {
async set (key, value, ttl) {
if (value === undefined) return false
const keyPrefixed = this._getKeyPrefix(key)
if (typeof ttl === 'undefined') {
ttl = this.options.ttl
}
if (ttl === 0) {
ttl = undefined
}
const store = this.store
return Promise.resolve()
.then(() => {
if (ttl === undefined) ttl = this.ttl
if (ttl === 0) ttl = undefined
const expires = typeof ttl === 'number' ? Date.now() + ttl : null
value = { value, expires }
return this.options.serialize(value)
})
.then(value => store.set(keyPrefixed, value, ttl))
.then(() => true)
}
delete (key) {
const keyPrefixed = this._getKeyPrefix(key)
const store = this.store
return Promise.resolve().then(() => store.delete(keyPrefixed))
const valueSerialized = await this.serialize({ value, expires })
await this.store.set(this._getKeyPrefix(key), valueSerialized, ttl)
return true
}
clear () {
if (!this.options.namespace) {
return Promise.resolve().then(() => undefined)
async delete (key) {
return this.store.delete(this._getKeyPrefix(key))
}
const store = this.store
return Promise.resolve().then(() => store.clear())
async clear () {
if (!this.namespace) return undefined
return this.store.clear()
}
}
module.exports = Keyv

2
packages/mongo/package.json

@ -1,7 +1,7 @@
{
"name": "@keyvhq/mongo",
"description": "MongoDB storage adapter for Keyv",
"homepage": "https://github.com/microlinkhq/keyv",
"homepage": "https://keyv.js.org",
"version": "1.0.2",
"main": "src/index.js",
"author": {

2
packages/mysql/package.json

@ -1,7 +1,7 @@
{
"name": "@keyvhq/mysql",
"description": "MySQL/MariaDB storage adapter for Keyv",
"homepage": "https://github.com/microlinkhq/keyv",
"homepage": "https://keyv.js.org",
"version": "1.0.2",
"main": "src/index.js",
"author": {

2
packages/postgres/package.json

@ -1,7 +1,7 @@
{
"name": "@keyvhq/postgres",
"description": "PostgreSQL storage adapter for Keyv",
"homepage": "https://github.com/microlinkhq/keyv",
"homepage": "https://keyv.js.org",
"version": "1.0.2",
"main": "src/index.js",
"author": {

4
packages/redis/src/index.js

@ -30,10 +30,6 @@ class KeyvRedis extends EventEmitter {
}
async set (key, value, ttl) {
if (typeof value === 'undefined') {
return undefined
}
return typeof ttl === 'number'
? this.redis.set(key, value, 'PX', ttl)
: this.redis.set(key, value)

2
packages/sql/package.json

@ -1,7 +1,7 @@
{
"name": "@keyvhq/sql",
"description": "Parent class for SQL based Keyv storage adapters",
"homepage": "https://github.com/microlinkhq/keyv",
"homepage": "https://keyv.js.org",
"version": "1.0.2",
"main": "src/index.js",
"author": {

2
packages/sqlite/package.json

@ -1,7 +1,7 @@
{
"name": "@keyvhq/sqlite",
"description": "SQLite storage adapter for Keyv",
"homepage": "https://github.com/microlinkhq/keyv",
"homepage": "https://keyv.js.org",
"version": "1.0.2",
"main": "src/index.js",
"author": {

2
packages/test-suite/package.json

@ -1,7 +1,7 @@
{
"name": "@keyvhq/test-suite",
"description": "Test suite for Keyv API compliancy",
"homepage": "https://github.com/microlinkhq/keyv",
"homepage": "https://keyv.js.org",
"version": "1.0.2",
"main": "src/index.js",
"author": {

Loading…
Cancel
Save