Gathering detailed insights and metrics for key-file-storage
Gathering detailed insights and metrics for key-file-storage
npm install key-file-storage
Typescript
Module System
Node Version
NPM Version
71.5
Supply Chain
96
Quality
73.7
Maintenance
25
Vulnerability
98.6
License
TypeScript (100%)
Total Downloads
55,710
Last Day
61
Last Week
189
Last Month
531
Last Year
13,792
23 Stars
80 Commits
4 Forks
3 Watching
2 Branches
3 Contributors
Minified
Minified + Gzipped
Latest Version
2.3.3
Package Id
key-file-storage@2.3.3
Unpacked Size
78.69 kB
Size
12.43 kB
File Count
26
NPM Version
9.5.1
Node Version
18.16.0
Publised On
02 Nov 2023
Cumulative downloads
Total Downloads
Last day
125.9%
61
Compared to previous day
Last week
39%
189
Compared to previous week
Last month
-82.8%
531
Compared to previous month
Last year
171.4%
13,792
Compared to previous year
4
1const store = require("key-file-storage")('my/storage/path') 2 3// Write something to file 'my/storage/path/myfile' 4store.myfile = { x: 123 } 5 6// Read contents of file 'my/storage/path/myfile' 7const x = store.myfile.x 8 9// Delete file 'my/storage/path/myfile' 10delete store.myfile
A nice alternative for any of these libraries: node-persist, configstore, flat-cache, conf, simple-store, and more...
Installing package on Node.js:
1$ npm install key-file-storage
Initializing a key-file storage:
1// ES Modules import style: 2import kfs from 'key-file-storage' 3 4// CommonJS import style: 5const kfs = require("key-file-storage") 6 7const store = kfs('/storage/directory/path', caching)
The value of caching
can be
true
(By default, if not specified) : Unlimited cache, anything will be cached on memory, good for small data volumes.
false
: No cache, read the files from disk every time, good when other applications can modify the files' contents arbitrarily.
n
(An integer number) : Limited cache, only the n
latest referred key-values will be cached, good for large data volumes where only a fraction of data is being used frequently .
As simple as native javascript objects:
1store['key'] = value // Writes file
1store['key'] // Reads file
1delete store['key'] // Deletes file
1delete store['*'] // Deletes all storage files
1'key' in store // Checks for file existence 2 //=> true or false
You can use store.keyName
instead of store['keyName']
anywhere if the key name allows.
undefined
is not supported as a savable value, but null
is. Saving a key with value undefined
is equivalent to removing it. So, you can use store['key'] = undefined
or even store['*'] = undefined
to delete files.
Synchronous API will throw an exception if any errors happen, so you shall handle it your way.
Every one of the following calls returns a promise:
1store('key', value) // Writes file
1store('key') // Reads file
1new store('key') // Resets/deletes file
1new store('*') /* or */ 2new store() /* or */ 3new store // Deletes all storage files
1('key' in store(), store()) // Checks for file existence 2 // Resolves to true or false
undefined
is not supported as a savable value, but null
is. Saving a key with value undefined
is equivalent to removing it. So, you can use store('key', undefined)
or even store('*', undefined)
to delete files.The same as asynchronous with promises, but with callback function as the last input parameter of store()
:
1store('key', value, cb) // Writes file
1store('key', cb) // Reads file
1new store('key', cb) // Resets/Deletes file
1new store('*', cb) /* or */ 2new store(cb) // Deletes all storage files
1'key' in store(cb) // Checks for file existence 2 // without promise output 3 /* or */ 4('key' in store(), store(cb)) 5 // Checks for file existence 6 // with promise output
These calls still return a promise on their output (except for 'key' in store(callback)
form of existence check).
The first input parameter of all callback functions is err
, so you shall handle it within the callback. Reading and Existence checking callbacks provide the return values as their second input parameter.
Every folder in the storage can be treated as a collection of key-values.
You can query the list of all containing keys (filenames) within a collection (folder) like this (Note that a collection path must end with a forward slash '/'
):
1try { 2 const keys = store['col/path/'] 3 // keys = ['col/path/key1', 'col/path/sub/key2', ... ] 4} catch (error) { 5 // Handle error... 6}
1store('col/path/') 2 .then(keys => { 3 // keys = ['col/path/key1', 'col/path/sub/key2', ... ] 4 }) 5 .catch(error => { 6 // Handle error... 7 })
1store('col/path/', (error, keys) => { 2 if (error) { 3 // Handle error... 4 } 5 // keys = ['col/path/key1', 'col/path/sub/key2', ... ] 6})
NOTE 1 : Each key will map to a separate file (using the key itself as its relative path). Therefore, keys may be relative paths, e.g: 'data.json'
, '/my/key/01'
or 'any/other/relative/path/to/a/file'
. The only exceptions are the strings including '..'
(double dot) which will not be accepted for security reasons.
NOTE 2 : You may have hidden key files by simply add a '.'
before the filename in the key path.
NOTE 3 : If a key's relative path ends with a forward slash '/'
, it will be considered to be a collection (folder) name. So, 'data/set/'
is a collection and 'data/set/key'
is a key in that collection.
NOTE 4 : This module has a built-in implemented cache, so, when activated, accessing a certain key more than once won't require file-system level operations again for that file.
NOTE 5 : When activated, caching will include queries on collections too.
1import kfs from "key-file-storage" 2 3// Locate 'db' folder in the current directory as the storage path, 4// Require 100 latest accessed key-values to be cached: 5const store = kfs('./db', 100) 6 7// Create file './db/users/hessam' containing this user data, synchronously: 8store['users/hessam'] = ({ 9 name: "Hessam", 10 skills: { 11 java: 10, 12 csharp: 15 13 } 14}) 15 16// Read file './db/users/hessam' as a JSON object, asynchronously: 17store('users/hessam').then(hessam => { 18 console.log(`Hessam's java skill is ${hessam.skills.java}.`) 19}) 20 21// Check whether file './db/users/mahdiar' exists or not, asynchronously: 22'users/mahdiar' in store((error, exists) => { 23 if (exists) { 24 console.log("User Mahdiar exists!") 25 } 26}) 27 28// List all the keys in './db/users/', synchronously: 29const allUsers = store['users/'] 30//=> ['users/hessam', 'users/mahdiar', ... ]
It would be very appreciated if you had any suggestions or contribution on this repository or submitted any issue.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
3 existing vulnerabilities detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 2/27 approved changesets -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-01-20
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More