fs-key-value 0.1.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +181 -0
- package/fs-key-value.js +326 -36
- package/package.json +30 -14
- package/.npmignore +0 -1
- package/Readme.md +0 -40
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013-present Andrew Shell
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
# fs-key-value
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/fs-key-value)
|
|
4
|
+
[](https://github.com/andrewshell/node-fs-key-value/actions/workflows/ci.yml)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
|
|
7
|
+
A simple key-value data store using only the filesystem. Uses POSIX file locking for safe operation in multi-process environments without the overhead of a separate database server.
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- Simple key-value API (`get`, `put`, `delete`)
|
|
12
|
+
- Both callback and async/await interfaces
|
|
13
|
+
- File-based storage (values stored as JSON)
|
|
14
|
+
- POSIX file locking for safe concurrent access
|
|
15
|
+
- Works across multiple processes (via cluster, child_process, etc.)
|
|
16
|
+
- No external database required
|
|
17
|
+
|
|
18
|
+
## Requirements
|
|
19
|
+
|
|
20
|
+
- Node.js >= 18.0.0
|
|
21
|
+
- POSIX-compliant filesystem (Linux, macOS, etc.)
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install fs-key-value
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Quick Start
|
|
30
|
+
|
|
31
|
+
### Using async/await
|
|
32
|
+
|
|
33
|
+
```js
|
|
34
|
+
const FsKeyValue = require('fs-key-value');
|
|
35
|
+
|
|
36
|
+
const db = new FsKeyValue();
|
|
37
|
+
await db.openAsync('./mydb');
|
|
38
|
+
|
|
39
|
+
// Store a value
|
|
40
|
+
await db.putAsync('user:1', { name: 'Alice', age: 30 });
|
|
41
|
+
|
|
42
|
+
// Retrieve the value
|
|
43
|
+
const data = await db.getAsync('user:1');
|
|
44
|
+
console.log(data); // { name: 'Alice', age: 30 }
|
|
45
|
+
|
|
46
|
+
// Delete the value
|
|
47
|
+
await db.deleteAsync('user:1');
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Using callbacks
|
|
51
|
+
|
|
52
|
+
```js
|
|
53
|
+
var FsKeyValue = require('fs-key-value');
|
|
54
|
+
|
|
55
|
+
var db = new FsKeyValue('./mydb', function (err, db) {
|
|
56
|
+
if (err) throw err;
|
|
57
|
+
|
|
58
|
+
// Store a value
|
|
59
|
+
db.put('user:1', { name: 'Alice', age: 30 }, function (err) {
|
|
60
|
+
if (err) throw err;
|
|
61
|
+
|
|
62
|
+
// Retrieve the value
|
|
63
|
+
db.get('user:1', function (err, data) {
|
|
64
|
+
if (err) throw err;
|
|
65
|
+
console.log(data); // { name: 'Alice', age: 30 }
|
|
66
|
+
|
|
67
|
+
// Delete the value
|
|
68
|
+
db.delete('user:1', function (err) {
|
|
69
|
+
if (err) throw err;
|
|
70
|
+
console.log('Deleted!');
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## API
|
|
78
|
+
|
|
79
|
+
### `new FsKeyValue([directory], [callback])`
|
|
80
|
+
|
|
81
|
+
Creates a new key-value store instance.
|
|
82
|
+
|
|
83
|
+
- `directory` (string, optional): Path to the storage directory
|
|
84
|
+
- `callback` (function, optional): Called with `(err, db)` when initialization completes
|
|
85
|
+
|
|
86
|
+
### `db.open(directory, callback)`
|
|
87
|
+
|
|
88
|
+
Initialize or reinitialize the store with a directory. Creates the directory if it doesn't exist.
|
|
89
|
+
|
|
90
|
+
- `directory` (string): Path to the storage directory
|
|
91
|
+
- `callback` (function): Called with `(err, db)` when complete
|
|
92
|
+
|
|
93
|
+
### `db.get(key, callback)`
|
|
94
|
+
|
|
95
|
+
Retrieve a value by key.
|
|
96
|
+
|
|
97
|
+
- `key` (string): The key to retrieve
|
|
98
|
+
- `callback` (function): Called with `(err, value)`. Value is `undefined` if key doesn't exist.
|
|
99
|
+
|
|
100
|
+
### `db.put(key, value, callback)`
|
|
101
|
+
|
|
102
|
+
Store a value. The value is serialized to JSON.
|
|
103
|
+
|
|
104
|
+
- `key` (string): The key to store
|
|
105
|
+
- `value` (any): Any JSON-serializable value
|
|
106
|
+
- `callback` (function): Called with `(err)` when complete
|
|
107
|
+
|
|
108
|
+
### `db.delete(key, callback)`
|
|
109
|
+
|
|
110
|
+
Delete a key from the store.
|
|
111
|
+
|
|
112
|
+
- `key` (string): The key to delete
|
|
113
|
+
- `callback` (function): Called with `(err)` when complete
|
|
114
|
+
|
|
115
|
+
### Async Methods
|
|
116
|
+
|
|
117
|
+
All methods are also available as async/Promise versions:
|
|
118
|
+
|
|
119
|
+
- `db.openAsync(directory)` → `Promise<void>`
|
|
120
|
+
- `db.getAsync(key)` → `Promise<value | undefined>`
|
|
121
|
+
- `db.putAsync(key, value)` → `Promise<void>`
|
|
122
|
+
- `db.deleteAsync(key)` → `Promise<void>`
|
|
123
|
+
|
|
124
|
+
## Multi-Process Example
|
|
125
|
+
|
|
126
|
+
This example demonstrates safe concurrent access from multiple worker processes:
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
var cluster = require('cluster');
|
|
130
|
+
var FsKeyValue = require('fs-key-value');
|
|
131
|
+
|
|
132
|
+
if (cluster.isMaster) {
|
|
133
|
+
// Fork 8 worker processes
|
|
134
|
+
for (var i = 0; i < 8; i++) {
|
|
135
|
+
cluster.fork();
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
var id = cluster.worker.id % 2;
|
|
139
|
+
|
|
140
|
+
var mydb = new FsKeyValue('./mydb', function (err, db) {
|
|
141
|
+
if (err) {
|
|
142
|
+
return console.log(err);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
db.put(
|
|
146
|
+
'hoopla' + id,
|
|
147
|
+
{ msg: 'ballyhoo ' + cluster.worker.id },
|
|
148
|
+
function (err) {
|
|
149
|
+
if (err) {
|
|
150
|
+
return console.log(cluster.worker.id + ' err ' + err);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
db.get('hoopla' + id, function (err, data) {
|
|
154
|
+
if (err) {
|
|
155
|
+
return console.log(cluster.worker.id + ' err ' + err);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
if (data != undefined) {
|
|
159
|
+
console.log(data.msg);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
db.delete('hoopla' + id);
|
|
163
|
+
cluster.worker.kill();
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
## How It Works
|
|
172
|
+
|
|
173
|
+
- Each key is stored as a separate file in the specified directory
|
|
174
|
+
- Values are serialized as JSON
|
|
175
|
+
- Uses `fs-ext` for POSIX file locking (`flock`)
|
|
176
|
+
- Shared locks for reads, exclusive locks for writes
|
|
177
|
+
- Directory-level lock file (`.lock`) coordinates operations
|
|
178
|
+
|
|
179
|
+
## License
|
|
180
|
+
|
|
181
|
+
MIT
|
package/fs-key-value.js
CHANGED
|
@@ -1,46 +1,336 @@
|
|
|
1
|
-
|
|
2
|
-
var path = require('path')
|
|
1
|
+
'use strict';
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const fsp = require('fs/promises');
|
|
5
|
+
const fsExt = require('fs-ext');
|
|
6
|
+
const path = require('path');
|
|
7
|
+
const { promisify } = require('util');
|
|
8
|
+
|
|
9
|
+
const flock = promisify(fsExt.flock);
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @callback ErrorCallback
|
|
13
|
+
* @param {Error|null} err - Error if one occurred
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @callback GetCallback
|
|
18
|
+
* @param {Error|null} err - Error if one occurred
|
|
19
|
+
* @param {*} [value] - The retrieved value, or undefined if key doesn't exist
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @callback OpenCallback
|
|
24
|
+
* @param {Error|null} err - Error if one occurred
|
|
25
|
+
* @param {FsKeyValue} [instance] - The initialized FsKeyValue instance
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Filesystem-based key-value store with file locking.
|
|
30
|
+
*
|
|
31
|
+
* Stores values as JSON files in a directory, using POSIX file locks
|
|
32
|
+
* for safe concurrent access.
|
|
33
|
+
*
|
|
34
|
+
* @constructor
|
|
35
|
+
* @param {string} [directory] - Path to storage directory
|
|
36
|
+
* @param {OpenCallback} [callback] - Called when initialization completes
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* var FsKeyValue = require('fs-key-value');
|
|
40
|
+
* var store = new FsKeyValue('/tmp/mydb', function(err, db) {
|
|
41
|
+
* if (err) throw err;
|
|
42
|
+
* db.put('mykey', { foo: 'bar' }, function(err) {
|
|
43
|
+
* if (err) throw err;
|
|
44
|
+
* console.log('Value stored');
|
|
45
|
+
* });
|
|
46
|
+
* });
|
|
47
|
+
*/
|
|
48
|
+
function FsKeyValue(directory, callback) {
|
|
49
|
+
if (directory) {
|
|
50
|
+
this.open(directory, callback);
|
|
8
51
|
}
|
|
9
|
-
var filename = path.join(this.directory, '.lock')
|
|
10
|
-
this.lock = fs.openSync(filename, 'a')
|
|
11
52
|
}
|
|
12
53
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
54
|
+
/**
|
|
55
|
+
* The storage directory path.
|
|
56
|
+
* @type {string}
|
|
57
|
+
*/
|
|
58
|
+
FsKeyValue.prototype.directory = undefined;
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Path to the directory lock file.
|
|
62
|
+
* @type {string}
|
|
63
|
+
*/
|
|
64
|
+
FsKeyValue.prototype.directoryLock = undefined;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Initialize the store with a directory.
|
|
68
|
+
*
|
|
69
|
+
* Creates the directory if it doesn't exist.
|
|
70
|
+
*
|
|
71
|
+
* @param {string} directory - Path to storage directory
|
|
72
|
+
* @param {OpenCallback} [callback] - Called when initialization completes
|
|
73
|
+
*/
|
|
74
|
+
FsKeyValue.prototype.open = function (directory, callback) {
|
|
75
|
+
if (typeof callback !== 'function') {
|
|
76
|
+
callback = function (err) {
|
|
77
|
+
if (err) {
|
|
78
|
+
throw err;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
this.openAsync(directory)
|
|
84
|
+
.then(() => callback(null, this))
|
|
85
|
+
.catch((err) => callback(err));
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Initialize the store with a directory (async version).
|
|
90
|
+
*
|
|
91
|
+
* Creates the directory if it doesn't exist.
|
|
92
|
+
*
|
|
93
|
+
* @param {string} directory - Path to storage directory
|
|
94
|
+
* @returns {Promise<void>}
|
|
95
|
+
*/
|
|
96
|
+
FsKeyValue.prototype.openAsync = async function (directory) {
|
|
97
|
+
const exists = fs.existsSync(directory);
|
|
98
|
+
if (!exists) {
|
|
99
|
+
await fsp.mkdir(directory, { mode: 0o777 });
|
|
100
|
+
}
|
|
101
|
+
this.directory = directory;
|
|
102
|
+
this.directoryLock = path.join(directory, '.lock');
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Retrieve a value by key.
|
|
107
|
+
*
|
|
108
|
+
* Uses a shared lock on the key file for safe concurrent reads.
|
|
109
|
+
*
|
|
110
|
+
* @param {string} key - The key to retrieve
|
|
111
|
+
* @param {GetCallback} [callback] - Called with the value or undefined if not found
|
|
112
|
+
*/
|
|
113
|
+
FsKeyValue.prototype.get = function (key, callback) {
|
|
114
|
+
if (typeof callback !== 'function') {
|
|
115
|
+
callback = function (err) {
|
|
116
|
+
if (err) {
|
|
117
|
+
throw err;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
26
121
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
122
|
+
this.getAsync(key)
|
|
123
|
+
.then((value) => {
|
|
124
|
+
if (value === undefined) {
|
|
125
|
+
// Preserve original behavior: callback() with no args for missing keys
|
|
126
|
+
callback();
|
|
127
|
+
} else {
|
|
128
|
+
callback(null, value);
|
|
129
|
+
}
|
|
130
|
+
})
|
|
131
|
+
.catch((err) => callback(err));
|
|
35
132
|
};
|
|
36
133
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
134
|
+
/**
|
|
135
|
+
* Retrieve a value by key (async version).
|
|
136
|
+
*
|
|
137
|
+
* Uses a shared lock on the key file for safe concurrent reads.
|
|
138
|
+
*
|
|
139
|
+
* @param {string} key - The key to retrieve
|
|
140
|
+
* @returns {Promise<*>} The value, or undefined if key doesn't exist
|
|
141
|
+
*/
|
|
142
|
+
FsKeyValue.prototype.getAsync = async function (key) {
|
|
143
|
+
const filename = path.join(this.directory, key);
|
|
144
|
+
let dirlock = null;
|
|
145
|
+
let keyfile = null;
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
// Open and lock directory
|
|
149
|
+
dirlock = fs.openSync(this.directoryLock, 'a', 0o666);
|
|
150
|
+
await flock(dirlock, 'sh');
|
|
151
|
+
|
|
152
|
+
// Check if key exists
|
|
153
|
+
const exists = fs.existsSync(filename);
|
|
154
|
+
if (!exists) {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Open and lock key file
|
|
159
|
+
keyfile = fs.openSync(filename, 'a+', 0o666);
|
|
160
|
+
await flock(keyfile, 'sh');
|
|
161
|
+
|
|
162
|
+
// Read value
|
|
163
|
+
const data = await fsp.readFile(filename, { encoding: 'utf8' });
|
|
164
|
+
return JSON.parse(data);
|
|
165
|
+
} finally {
|
|
166
|
+
// Release locks and close files
|
|
167
|
+
if (keyfile !== null) {
|
|
168
|
+
try {
|
|
169
|
+
await flock(keyfile, 'un');
|
|
170
|
+
} catch {
|
|
171
|
+
/* ignore */
|
|
172
|
+
}
|
|
173
|
+
try {
|
|
174
|
+
fs.closeSync(keyfile);
|
|
175
|
+
} catch {
|
|
176
|
+
/* ignore */
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (dirlock !== null) {
|
|
180
|
+
try {
|
|
181
|
+
await flock(dirlock, 'un');
|
|
182
|
+
} catch {
|
|
183
|
+
/* ignore */
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
fs.closeSync(dirlock);
|
|
187
|
+
} catch {
|
|
188
|
+
/* ignore */
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Store a value by key.
|
|
196
|
+
*
|
|
197
|
+
* Uses an exclusive lock on the key file for safe writes.
|
|
198
|
+
* The value is serialized to JSON.
|
|
199
|
+
*
|
|
200
|
+
* @param {string} key - The key to store
|
|
201
|
+
* @param {*} value - The value to store (must be JSON-serializable)
|
|
202
|
+
* @param {ErrorCallback} [callback] - Called when write completes
|
|
203
|
+
*/
|
|
204
|
+
FsKeyValue.prototype.put = function (key, value, callback) {
|
|
205
|
+
if (typeof callback !== 'function') {
|
|
206
|
+
callback = function (err) {
|
|
207
|
+
if (err) {
|
|
208
|
+
throw err;
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
this.putAsync(key, value)
|
|
214
|
+
.then(() => callback(null))
|
|
215
|
+
.catch((err) => callback(err));
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Store a value by key (async version).
|
|
220
|
+
*
|
|
221
|
+
* Uses an exclusive lock on the key file for safe writes.
|
|
222
|
+
* The value is serialized to JSON.
|
|
223
|
+
*
|
|
224
|
+
* @param {string} key - The key to store
|
|
225
|
+
* @param {*} value - The value to store (must be JSON-serializable)
|
|
226
|
+
* @returns {Promise<void>}
|
|
227
|
+
*/
|
|
228
|
+
FsKeyValue.prototype.putAsync = async function (key, value) {
|
|
229
|
+
const filename = path.join(this.directory, key);
|
|
230
|
+
let dirlock = null;
|
|
231
|
+
let keyfile = null;
|
|
232
|
+
|
|
233
|
+
try {
|
|
234
|
+
// Open and lock directory (shared lock for puts)
|
|
235
|
+
dirlock = fs.openSync(this.directoryLock, 'a', 0o666);
|
|
236
|
+
await flock(dirlock, 'sh');
|
|
237
|
+
|
|
238
|
+
// Open and lock key file (exclusive lock for write)
|
|
239
|
+
keyfile = fs.openSync(filename, 'a', 0o666);
|
|
240
|
+
await flock(keyfile, 'ex');
|
|
241
|
+
|
|
242
|
+
// Write value
|
|
243
|
+
await fsp.writeFile(filename, JSON.stringify(value), { encoding: 'utf8' });
|
|
244
|
+
} finally {
|
|
245
|
+
// Release locks and close files
|
|
246
|
+
if (keyfile !== null) {
|
|
247
|
+
try {
|
|
248
|
+
await flock(keyfile, 'un');
|
|
249
|
+
} catch {
|
|
250
|
+
/* ignore */
|
|
251
|
+
}
|
|
252
|
+
try {
|
|
253
|
+
fs.closeSync(keyfile);
|
|
254
|
+
} catch {
|
|
255
|
+
/* ignore */
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (dirlock !== null) {
|
|
259
|
+
try {
|
|
260
|
+
await flock(dirlock, 'un');
|
|
261
|
+
} catch {
|
|
262
|
+
/* ignore */
|
|
263
|
+
}
|
|
264
|
+
try {
|
|
265
|
+
fs.closeSync(dirlock);
|
|
266
|
+
} catch {
|
|
267
|
+
/* ignore */
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Delete a key from the store.
|
|
275
|
+
*
|
|
276
|
+
* Uses an exclusive lock on the directory for safe deletion.
|
|
277
|
+
* Does nothing if the key doesn't exist.
|
|
278
|
+
*
|
|
279
|
+
* @param {string} key - The key to delete
|
|
280
|
+
* @param {ErrorCallback} [callback] - Called when deletion completes
|
|
281
|
+
*/
|
|
282
|
+
FsKeyValue.prototype.delete = function (key, callback) {
|
|
283
|
+
if (typeof callback !== 'function') {
|
|
284
|
+
callback = function (err) {
|
|
285
|
+
if (err) {
|
|
286
|
+
throw err;
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
this.deleteAsync(key)
|
|
292
|
+
.then(() => callback(null))
|
|
293
|
+
.catch((err) => callback(err));
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Delete a key from the store (async version).
|
|
298
|
+
*
|
|
299
|
+
* Uses an exclusive lock on the directory for safe deletion.
|
|
300
|
+
* Does nothing if the key doesn't exist.
|
|
301
|
+
*
|
|
302
|
+
* @param {string} key - The key to delete
|
|
303
|
+
* @returns {Promise<void>}
|
|
304
|
+
*/
|
|
305
|
+
FsKeyValue.prototype.deleteAsync = async function (key) {
|
|
306
|
+
const filename = path.join(this.directory, key);
|
|
307
|
+
let dirlock = null;
|
|
308
|
+
|
|
309
|
+
try {
|
|
310
|
+
// Open and lock directory (exclusive lock for delete)
|
|
311
|
+
dirlock = fs.openSync(this.directoryLock, 'a', 0o666);
|
|
312
|
+
await flock(dirlock, 'ex');
|
|
313
|
+
|
|
314
|
+
// Delete if exists
|
|
315
|
+
const exists = fs.existsSync(filename);
|
|
316
|
+
if (exists) {
|
|
317
|
+
await fsp.unlink(filename);
|
|
318
|
+
}
|
|
319
|
+
} finally {
|
|
320
|
+
// Release lock and close file
|
|
321
|
+
if (dirlock !== null) {
|
|
322
|
+
try {
|
|
323
|
+
await flock(dirlock, 'un');
|
|
324
|
+
} catch {
|
|
325
|
+
/* ignore */
|
|
326
|
+
}
|
|
327
|
+
try {
|
|
328
|
+
fs.closeSync(dirlock);
|
|
329
|
+
} catch {
|
|
330
|
+
/* ignore */
|
|
331
|
+
}
|
|
332
|
+
}
|
|
42
333
|
}
|
|
43
|
-
fs.flockSync(this.lock, 'un')
|
|
44
334
|
};
|
|
45
335
|
|
|
46
|
-
module.exports = FsKeyValue
|
|
336
|
+
module.exports = FsKeyValue;
|
package/package.json
CHANGED
|
@@ -1,21 +1,37 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "fs-key-value",
|
|
3
|
+
"version": "1.2.0",
|
|
3
4
|
"description": "Key value data store using the filesystem",
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"author": "Andrew Shell <andrew@andrewshell.org> (http://blog.andrewshell.org/)",
|
|
9
|
-
"main": "./fs-key-value.js",
|
|
10
|
-
"dependencies": {
|
|
11
|
-
"fs-ext": ">= 0.3.x",
|
|
12
|
-
"path": ">= 0.4.x"
|
|
5
|
+
"homepage": "https://github.com/andrewshell/node-fs-key-value",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/andrewshell/node-fs-key-value.git"
|
|
13
9
|
},
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/andrewshell/node-fs-key-value/issues"
|
|
12
|
+
},
|
|
13
|
+
"author": "Andrew Shell <andrew@andrewshell.org> (https://blog.andrewshell.org/)",
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"main": "./fs-key-value.js",
|
|
16
|
+
"files": [
|
|
17
|
+
"fs-key-value.js",
|
|
18
|
+
"README.md",
|
|
19
|
+
"LICENSE"
|
|
20
|
+
],
|
|
14
21
|
"engines": {
|
|
15
|
-
"node": ">=
|
|
22
|
+
"node": ">=18.0.0"
|
|
16
23
|
},
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
24
|
+
"scripts": {
|
|
25
|
+
"test": "node --test 'test/**/*.test.js'",
|
|
26
|
+
"lint": "eslint .",
|
|
27
|
+
"format": "prettier --write .",
|
|
28
|
+
"format:check": "prettier --check ."
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"fs-ext": "^2.0.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"eslint": "^9.17.0",
|
|
35
|
+
"prettier": "^3.4.2"
|
|
20
36
|
}
|
|
21
|
-
}
|
|
37
|
+
}
|
package/.npmignore
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
.DS_Store
|
package/Readme.md
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
# fs-key-value
|
|
2
|
-
|
|
3
|
-
This module provides a simple key value data store using only the file system. It makes use of file locking to allow safe operation in a multiple process environment without the overhead of a separate database server.
|
|
4
|
-
|
|
5
|
-
[](https://nodei.co/npm/fs-key-value/)
|
|
6
|
-
|
|
7
|
-
## Installation
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
$ npm install fs-key-value
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
## Example
|
|
14
|
-
|
|
15
|
-
```js
|
|
16
|
-
var FsKeyValue = new require('fs-key-value')
|
|
17
|
-
|
|
18
|
-
var db = new FsKeyValue('./mydb')
|
|
19
|
-
|
|
20
|
-
var cluster = require('cluster')
|
|
21
|
-
|
|
22
|
-
if (cluster.isMaster) {
|
|
23
|
-
for (var i = 0; i < 8; i++) {
|
|
24
|
-
cluster.fork()
|
|
25
|
-
}
|
|
26
|
-
} else {
|
|
27
|
-
var id = cluster.worker.id % 2
|
|
28
|
-
|
|
29
|
-
db.put('hoopla' + id, {'msg': 'ballyhoo ' + cluster.worker.id})
|
|
30
|
-
|
|
31
|
-
var data = db.get('hoopla' + id)
|
|
32
|
-
if (data != undefined) {
|
|
33
|
-
console.log(data.msg)
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
db.del('hoopla' + id)
|
|
37
|
-
|
|
38
|
-
cluster.worker.kill()
|
|
39
|
-
}
|
|
40
|
-
```
|