@furystack/filesystem-store 4.0.0 → 4.0.1
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.
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { promises, watch } from 'fs';
|
|
2
|
+
import { Lock } from 'semaphore-async-await';
|
|
3
|
+
import { InMemoryStore } from '@furystack/core';
|
|
4
|
+
/**
|
|
5
|
+
* Store implementation that stores info in a simple JSON file
|
|
6
|
+
*/
|
|
7
|
+
export class FileSystemStore {
|
|
8
|
+
get cache() {
|
|
9
|
+
return this.inMemoryStore.cache;
|
|
10
|
+
}
|
|
11
|
+
async remove(...keys) {
|
|
12
|
+
await this.fileLock.execute(async () => {
|
|
13
|
+
await this.inMemoryStore.remove(...keys);
|
|
14
|
+
});
|
|
15
|
+
this.hasChanges = true;
|
|
16
|
+
}
|
|
17
|
+
async get(key, select) {
|
|
18
|
+
return await this.fileLock.execute(async () => {
|
|
19
|
+
return await this.inMemoryStore.get(key, select);
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
async add(...entries) {
|
|
23
|
+
const result = await this.fileLock.execute(async () => {
|
|
24
|
+
return await this.inMemoryStore.add(...entries);
|
|
25
|
+
});
|
|
26
|
+
this.hasChanges = true;
|
|
27
|
+
return result;
|
|
28
|
+
}
|
|
29
|
+
async find(filter) {
|
|
30
|
+
return await this.fileLock.execute(async () => {
|
|
31
|
+
return this.inMemoryStore.find(filter);
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
async count(filter) {
|
|
35
|
+
return await this.fileLock.execute(async () => {
|
|
36
|
+
return this.inMemoryStore.count(filter);
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
async saveChanges() {
|
|
40
|
+
if (!this.hasChanges) {
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
await this.fileLock.acquire();
|
|
45
|
+
const values = [];
|
|
46
|
+
for (const key of this.cache.keys()) {
|
|
47
|
+
values.push(this.cache.get(key));
|
|
48
|
+
}
|
|
49
|
+
await this.writeFile(this.options.fileName, JSON.stringify(values));
|
|
50
|
+
this.hasChanges = false;
|
|
51
|
+
}
|
|
52
|
+
finally {
|
|
53
|
+
this.fileLock.release();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
async dispose() {
|
|
57
|
+
await this.saveChanges();
|
|
58
|
+
this.watcher && this.watcher.close();
|
|
59
|
+
clearInterval(this.tick);
|
|
60
|
+
}
|
|
61
|
+
async reloadData() {
|
|
62
|
+
try {
|
|
63
|
+
await this.fileLock.acquire();
|
|
64
|
+
const data = await this.readFile(this.options.fileName);
|
|
65
|
+
const json = (data ? JSON.parse(data.toString()) : []);
|
|
66
|
+
this.cache.clear();
|
|
67
|
+
for (const entity of json) {
|
|
68
|
+
this.cache.set(entity[this.primaryKey], entity);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
catch (err) {
|
|
72
|
+
// ignore if file not exists yet
|
|
73
|
+
if (err instanceof Error && err.code !== 'ENOENT') {
|
|
74
|
+
throw err;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
finally {
|
|
78
|
+
this.fileLock.release();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
async update(id, data) {
|
|
82
|
+
await this.fileLock.execute(async () => {
|
|
83
|
+
return this.inMemoryStore.update(id, data);
|
|
84
|
+
});
|
|
85
|
+
this.hasChanges = true;
|
|
86
|
+
}
|
|
87
|
+
constructor(options) {
|
|
88
|
+
this.options = options;
|
|
89
|
+
this.tick = setInterval(() => this.saveChanges(), this.options.tickMs || 3000);
|
|
90
|
+
this.hasChanges = false;
|
|
91
|
+
this.fileLock = new Lock();
|
|
92
|
+
this.readFile = promises.readFile;
|
|
93
|
+
this.writeFile = promises.writeFile;
|
|
94
|
+
this.primaryKey = options.primaryKey;
|
|
95
|
+
this.model = options.model;
|
|
96
|
+
this.inMemoryStore = new InMemoryStore({ model: this.model, primaryKey: this.primaryKey });
|
|
97
|
+
try {
|
|
98
|
+
this.reloadData();
|
|
99
|
+
this.watcher = watch(this.options.fileName, { encoding: 'buffer' }, () => {
|
|
100
|
+
this.reloadData();
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
// Error registering file watcher for store. External updates won't be updated.
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
//# sourceMappingURL=filesystem-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"filesystem-store.js","sourceRoot":"","sources":["../src/filesystem-store.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,IAAI,CAAA;AAEpC,OAAO,EAAE,IAAI,EAAE,MAAM,uBAAuB,CAAA;AAE5C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAE/C;;GAEG;AACH,MAAM,OAAO,eAAe;IAS1B,IAAY,KAAK;QACf,OAAO,IAAI,CAAC,aAAa,CAAC,KAAK,CAAA;IACjC,CAAC;IAEM,KAAK,CAAC,MAAM,CAAC,GAAG,IAA2B;QAChD,MAAM,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE;YACrC,MAAM,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,GAAG,IAAI,CAAC,CAAA;QAC1C,CAAC,CAAC,CAAA;QACF,IAAI,CAAC,UAAU,GAAG,IAAI,CAAA;IACxB,CAAC;IAIM,KAAK,CAAC,GAAG,CAAC,GAAmB,EAAE,MAAuB;QAC3D,OAAO,MAAM,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE;YAC5C,OAAO,MAAM,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;QAClD,CAAC,CAAC,CAAA;IACJ,CAAC;IAEM,KAAK,CAAC,GAAG,CAAC,GAAG,OAAY;QAC9B,MAAM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QACF,IAAI,CAAC,UAAU,GAAG,IAAI,CAAA;IACxB,CAAC;IAKD,YACmB,OAKhB;QALgB,YAAO,GAAP,OAAO,CAKvB;QAvFI,SAAI,GAAG,WAAW,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,WAAW,EAAE,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM,IAAI,IAAI,CAAC,CAAA;QACzE,eAAU,GAAG,KAAK,CAAA;QA2BjB,aAAQ,GAAG,IAAI,IAAI,EAAE,CAAA;QAkDtB,aAAQ,GAAG,QAAQ,CAAC,QAAQ,CAAA;QAC5B,cAAS,GAAG,QAAQ,CAAC,SAAS,CAAA;QAUnC,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC,UAAU,CAAA;QACpC,IAAI,CAAC,KAAK,GAAG,OAAO,CAAC,KAAK,CAAA;QAC1B,IAAI,CAAC,aAAa,GAAG,IAAI,aAAa,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,UAAU,EAAE,IAAI,CAAC,UAAU,EAAE,CAAC,CAAA;QAE1F,IAAI;YACF,IAAI,CAAC,UAAU,EAAE,CAAA;YACjB,IAAI,CAAC,OAAO,GAAG,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE,GAAG,EAAE;gBACvE,IAAI,CAAC,UAAU,EAAE,CAAA;YACnB,CAAC,CAAC,CAAA;SACH;QAAC,OAAO,KAAK,EAAE;YACd,+EAA+E;SAChF;IACH,CAAC;CACF"}
|
package/esm/index.js
ADDED
package/esm/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAA;AAClC,cAAc,yBAAyB,CAAA"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { addStore } from '@furystack/core';
|
|
2
|
+
import { FileSystemStore } from './filesystem-store';
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param options The Options for store creation
|
|
6
|
+
* @param options.injector The injector to use for creating the store
|
|
7
|
+
* @param options.model The model to use for the store
|
|
8
|
+
* @param options.primaryKey The primary key of the model
|
|
9
|
+
* @param options.fileName The name of the file to use for the store
|
|
10
|
+
* @param options.tickMs The time in ms to wait between each save
|
|
11
|
+
*/
|
|
12
|
+
export const useFileSystemStore = (options) => {
|
|
13
|
+
const store = new FileSystemStore({ ...options });
|
|
14
|
+
addStore(options.injector, store);
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=store-manager-helpers.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"store-manager-helpers.js","sourceRoot":"","sources":["../src/store-manager-helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAA;AAE1C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAA;AAEpD;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAI,OAMrC,EAAE,EAAE;IACH,MAAM,KAAK,GAAG,IAAI,eAAe,CAAC,EAAE,GAAG,OAAO,EAAE,CAAC,CAAA;IACjD,QAAQ,CAAC,OAAO,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAA;AACnC,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@furystack/filesystem-store",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.1",
|
|
4
4
|
"description": "Simple File System store implementation for FuryStack",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
}
|
|
22
22
|
},
|
|
23
23
|
"files": [
|
|
24
|
-
"
|
|
24
|
+
"esm",
|
|
25
25
|
"types",
|
|
26
26
|
"src"
|
|
27
27
|
],
|
|
@@ -46,9 +46,9 @@
|
|
|
46
46
|
},
|
|
47
47
|
"homepage": "https://github.com/furystack/furystack",
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@furystack/core": "^12.0.
|
|
50
|
-
"@furystack/inject": "^8.0.
|
|
51
|
-
"@furystack/utils": "^4.0.
|
|
49
|
+
"@furystack/core": "^12.0.1",
|
|
50
|
+
"@furystack/inject": "^8.0.1",
|
|
51
|
+
"@furystack/utils": "^4.0.1",
|
|
52
52
|
"semaphore-async-await": "^1.5.1"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|