Comparing version 0.1.13 to 0.2.0
@@ -12,2 +12,7 @@ export interface Data<V> { | ||
}; | ||
export declare function makeField<T = any>(kv: KeyvFile, key: string, defaults?: T): { | ||
get(def?: T | undefined): T | undefined; | ||
set(val?: T | undefined): void; | ||
delete(): any; | ||
}; | ||
export declare class KeyvFile<V = any> { | ||
@@ -14,0 +19,0 @@ ttlSupport: boolean; |
@@ -18,2 +18,16 @@ 'use strict'; | ||
}; | ||
function makeField(kv, key, defaults) { | ||
return { | ||
get(def = defaults) { | ||
return kv.get(key, def); | ||
}, | ||
set(val) { | ||
return kv.set(key, val); | ||
}, | ||
delete() { | ||
return kv.delete(key); | ||
}, | ||
}; | ||
} | ||
exports.makeField = makeField; | ||
class KeyvFile { | ||
@@ -153,3 +167,1 @@ constructor(opts) { | ||
exports.default = KeyvFile; | ||
module.exports = KeyvFile; | ||
module.exports.default = KeyvFile; |
{ | ||
"name": "keyv-file", | ||
"version": "0.1.13", | ||
"version": "0.2.0", | ||
"description": "File storage adapter for Keyv, using msgpack to serialize data fast and small.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -20,5 +20,6 @@ # keyv-file [<img width="100" align="right" src="https://rawgit.com/lukechilds/keyv/master/media/logo.svg" alt="keyv">](https://github.com/lukechilds/keyv) | ||
### Using with keyv | ||
```js | ||
const Keyv = require('keyv') | ||
const KeyvFile = require('keyv-file') | ||
const KeyvFile = require('keyv-file').KeyvFile | ||
@@ -31,7 +32,7 @@ const keyv = new Keyv({ | ||
store: new KeyvFile({ | ||
filename: `${os.tmpdir()}/keyv-file/default-rnd-${Math.random().toString(36).slice(2)}.json` // the file path to store the data | ||
filename: `${os.tmpdir()}/keyv-file/default-rnd-${Math.random().toString(36).slice(2)}.json`, // the file path to store the data | ||
expiredCheckDelay: 24 * 3600 * 1000, // ms, check and remove expired data in each ms | ||
writeDelay: 100, // ms, batch write to disk in a specific duration, enhance write performance. | ||
encode: JSON.stringify, // serialize function | ||
decode: JSON.parse, // deserialize function | ||
decode: JSON.parse // deserialize function | ||
}) | ||
@@ -41,4 +42,26 @@ }) | ||
### Using directly | ||
```ts | ||
import KeyvFile, { makeField } from 'keyv-file' | ||
class Kv extends KeyvFile { | ||
constructor() { | ||
super({ | ||
filename: './db.json' | ||
}) | ||
} | ||
someField = makeField(this, 'field_key') | ||
} | ||
export const kv = new Kv | ||
kv.someField.get(1) // empty return default value 1 | ||
kv.someField.set(2) // set value 2 | ||
kv.someField.get() // return saved value 2 | ||
kv.someField.delete() // delete field | ||
``` | ||
## License | ||
MIT |
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
9740
208
65