Installations
npm install key-file-storage
Developer Guide
Typescript
Yes
Module System
CommonJS
Node Version
18.16.0
NPM Version
9.5.1
Score
71.5
Supply Chain
96
Quality
73.7
Maintenance
25
Vulnerability
98.6
License
Releases
Unable to fetch releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (100%)
Developer
ahs502
Download Statistics
Total Downloads
55,710
Last Day
61
Last Week
189
Last Month
531
Last Year
13,792
GitHub Statistics
23 Stars
80 Commits
4 Forks
3 Watching
2 Branches
3 Contributors
Bundle Size
86.31 kB
Minified
26.77 kB
Minified + Gzipped
Package Meta Information
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
Total Downloads
Cumulative downloads
Total Downloads
55,710
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
4
key-file-storage
Simple key-value storage (a persistent data structure) directly on file system, maps each key to a separate file.
- Simple key-value storage model
- Very easy to learn and use
- Both Synchronous and Asynchronous APIs
- One JSON containing file per each key
- Built-in configurable cache
- Both Promise and Callback support
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...
Installation
Installing package on Node.js:
1$ npm install key-file-storage
Initialization
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 then
latest referred key-values will be cached, good for large data volumes where only a fraction of data is being used frequently .
Usage
Synchronous API
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 ofstore['keyName']
anywhere if the key name allows. -
undefined
is not supported as a savable value, butnull
is. Saving a key with valueundefined
is equivalent to removing it. So, you can usestore['key'] = undefined
or evenstore['*'] = undefined
to delete files. -
Synchronous API will throw an exception if any errors happen, so you shall handle it your way.
Asynchronous API with Promises
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
- Once again,
undefined
is not supported as a savable value, butnull
is. Saving a key with valueundefined
is equivalent to removing it. So, you can usestore('key', undefined)
or evenstore('*', undefined)
to delete files.
Asynchronous API with Callbacks
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.
Folders as Collections
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 '/'
):
Synchronous API
1try { 2 const keys = store['col/path/'] 3 // keys = ['col/path/key1', 'col/path/sub/key2', ... ] 4} catch (error) { 5 // Handle error... 6}
Asynchronous API with Promises
1store('col/path/') 2 .then(keys => { 3 // keys = ['col/path/key1', 'col/path/sub/key2', ... ] 4 }) 5 .catch(error => { 6 // Handle error... 7 })
Asynchronous API with Callbacks
1store('col/path/', (error, keys) => { 2 if (error) { 3 // Handle error... 4 } 5 // keys = ['col/path/key1', 'col/path/sub/key2', ... ] 6})
Notes
-
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.
Example
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', ... ]
Contribute
It would be very appreciated if you had any suggestions or contribution on this repository or submitted any issue.
- See the code on GitHub
- Contact me by my gmail address (Hessamoddin A Shokravi)
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: ISC License: LICENSE:0
Reason
3 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-f8q6-p94x-37v3
- Warn: Project is vulnerable to: GHSA-vh95-rmgr-6w4m
- Warn: Project is vulnerable to: GHSA-xvch-5gv4-984h
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
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 5 are checked with a SAST tool
Score
2.7
/10
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