@wxn0brp/db 0.0.5 → 0.0.7
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/README.md +4 -1
- package/dist/cjs/CollectionManager.d.ts +44 -0
- package/dist/cjs/CollectionManager.js +59 -0
- package/dist/cjs/action.d.ts +66 -0
- package/dist/cjs/action.js +193 -0
- package/dist/cjs/client/database.d.ts +72 -0
- package/dist/cjs/client/database.js +145 -0
- package/dist/cjs/client/function.d.ts +5 -0
- package/dist/cjs/client/function.js +32 -0
- package/dist/cjs/client/graph.d.ts +54 -0
- package/dist/cjs/client/graph.js +93 -0
- package/dist/cjs/client/remote.d.ts +16 -0
- package/dist/cjs/client/remote.js +2 -0
- package/dist/cjs/database.d.ts +69 -0
- package/dist/cjs/database.js +110 -0
- package/dist/cjs/executor.d.ts +28 -0
- package/{executor.js → dist/cjs/executor.js} +47 -54
- package/dist/cjs/file/find.d.ts +11 -0
- package/dist/cjs/file/find.js +80 -0
- package/dist/cjs/file/index.d.ts +3 -0
- package/dist/cjs/file/index.js +25 -0
- package/dist/cjs/file/remove.d.ts +7 -0
- package/dist/cjs/file/remove.js +61 -0
- package/dist/cjs/file/update.d.ts +7 -0
- package/dist/cjs/file/update.js +70 -0
- package/dist/cjs/file/utils.d.ts +8 -0
- package/dist/cjs/file/utils.js +23 -0
- package/dist/cjs/format.d.ts +18 -0
- package/dist/cjs/format.js +36 -0
- package/dist/cjs/gen.d.ts +5 -0
- package/dist/cjs/gen.js +78 -0
- package/dist/cjs/graph.d.ts +47 -0
- package/dist/cjs/graph.js +90 -0
- package/dist/cjs/index.d.ts +13 -0
- package/dist/cjs/index.js +18 -0
- package/dist/cjs/relation.d.ts +64 -0
- package/{relation.js → dist/cjs/relation.js} +26 -45
- package/dist/cjs/types/Id.d.ts +3 -0
- package/dist/cjs/types/Id.js +2 -0
- package/dist/cjs/types/arg.d.ts +12 -0
- package/dist/cjs/types/arg.js +2 -0
- package/dist/cjs/types/data.d.ts +4 -0
- package/dist/cjs/types/data.js +2 -0
- package/dist/cjs/types/options.d.ts +12 -0
- package/dist/cjs/types/options.js +2 -0
- package/dist/cjs/types/searchOpts.d.ts +61 -0
- package/dist/cjs/types/searchOpts.js +8 -0
- package/dist/cjs/types/types.d.ts +6 -0
- package/dist/cjs/types/types.js +2 -0
- package/dist/cjs/types/updater.d.ts +26 -0
- package/dist/cjs/types/updater.js +5 -0
- package/dist/cjs/utils/hasFields.d.ts +8 -0
- package/dist/cjs/utils/hasFields.js +22 -0
- package/dist/cjs/utils/hasFieldsAdvanced.d.ts +5 -0
- package/dist/cjs/utils/hasFieldsAdvanced.js +182 -0
- package/dist/cjs/utils/updateFindObject.d.ts +11 -0
- package/dist/cjs/utils/updateFindObject.js +32 -0
- package/dist/cjs/utils/updateObject.d.ts +7 -0
- package/dist/cjs/utils/updateObject.js +148 -0
- package/dist/esm/CollectionManager.d.ts +44 -0
- package/dist/esm/CollectionManager.js +57 -0
- package/dist/esm/action.d.ts +66 -0
- package/dist/esm/action.js +188 -0
- package/dist/esm/client/database.d.ts +72 -0
- package/dist/esm/client/database.js +140 -0
- package/dist/esm/client/function.d.ts +5 -0
- package/dist/esm/client/function.js +30 -0
- package/dist/esm/client/graph.d.ts +54 -0
- package/dist/esm/client/graph.js +88 -0
- package/dist/esm/client/remote.d.ts +16 -0
- package/dist/esm/client/remote.js +1 -0
- package/dist/esm/database.d.ts +69 -0
- package/dist/esm/database.js +105 -0
- package/dist/esm/executor.d.ts +28 -0
- package/dist/esm/executor.js +45 -0
- package/dist/esm/file/find.d.ts +11 -0
- package/dist/esm/file/find.js +73 -0
- package/dist/esm/file/index.d.ts +3 -0
- package/{file → dist/esm/file}/index.js +1 -1
- package/dist/esm/file/remove.d.ts +7 -0
- package/dist/esm/file/remove.js +56 -0
- package/dist/esm/file/update.d.ts +7 -0
- package/dist/esm/file/update.js +65 -0
- package/dist/esm/file/utils.d.ts +8 -0
- package/{file → dist/esm/file}/utils.js +3 -11
- package/dist/esm/format.d.ts +18 -0
- package/{format.js → dist/esm/format.js} +29 -29
- package/dist/esm/gen.d.ts +5 -0
- package/dist/esm/gen.js +75 -0
- package/dist/esm/graph.d.ts +47 -0
- package/dist/esm/graph.js +85 -0
- package/dist/esm/index.d.ts +13 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/relation.d.ts +64 -0
- package/dist/esm/relation.js +65 -0
- package/dist/esm/types/Id.d.ts +3 -0
- package/dist/esm/types/Id.js +1 -0
- package/dist/esm/types/arg.d.ts +12 -0
- package/dist/esm/types/arg.js +1 -0
- package/dist/esm/types/data.d.ts +4 -0
- package/dist/esm/types/data.js +1 -0
- package/dist/esm/types/options.d.ts +12 -0
- package/dist/esm/types/options.js +1 -0
- package/dist/esm/types/searchOpts.d.ts +61 -0
- package/dist/esm/types/searchOpts.js +7 -0
- package/dist/esm/types/types.d.ts +6 -0
- package/dist/esm/types/types.js +1 -0
- package/dist/esm/types/updater.d.ts +26 -0
- package/dist/esm/types/updater.js +4 -0
- package/dist/esm/utils/hasFields.d.ts +8 -0
- package/{utils → dist/esm/utils}/hasFields.js +4 -4
- package/dist/esm/utils/hasFieldsAdvanced.d.ts +5 -0
- package/dist/esm/utils/hasFieldsAdvanced.js +176 -0
- package/dist/esm/utils/updateFindObject.d.ts +11 -0
- package/{utils → dist/esm/utils}/updateFindObject.js +11 -16
- package/dist/esm/utils/updateObject.d.ts +7 -0
- package/dist/esm/utils/updateObject.js +145 -0
- package/package.json +52 -36
- package/CollectionManager.js +0 -119
- package/action.js +0 -258
- package/database.d.ts +0 -44
- package/database.js +0 -203
- package/docs/database.md +0 -140
- package/docs/graph.md +0 -86
- package/docs/relation.md +0 -51
- package/docs/remote.md +0 -30
- package/docs/remote_server.md +0 -35
- package/docs/search_opts.md +0 -227
- package/file/find.js +0 -89
- package/file/remove.js +0 -74
- package/file/update.js +0 -83
- package/gen.d.ts +0 -1
- package/gen.js +0 -97
- package/graph.d.ts +0 -27
- package/graph.js +0 -140
- package/index.d.ts +0 -7
- package/index.js +0 -15
- package/relation.d.ts +0 -23
- package/remote/client/database.d.ts +0 -41
- package/remote/client/database.js +0 -228
- package/remote/client/graph.d.ts +0 -31
- package/remote/client/graph.js +0 -148
- package/remote/server/auth.js +0 -100
- package/remote/server/db.js +0 -197
- package/remote/server/function.js +0 -43
- package/remote/server/graph.js +0 -136
- package/remote/server/gui/css/main.css +0 -130
- package/remote/server/gui/css/scrool.css +0 -81
- package/remote/server/gui/css/style.css +0 -61
- package/remote/server/gui/favicon.svg +0 -12
- package/remote/server/gui/html/data.html +0 -15
- package/remote/server/gui/html/main.html +0 -46
- package/remote/server/gui/html/nav.html +0 -25
- package/remote/server/gui/html/popup.html +0 -51
- package/remote/server/gui/index.html +0 -49
- package/remote/server/gui/js/api.js +0 -166
- package/remote/server/gui/js/index.js +0 -17
- package/remote/server/gui/js/loadHTML.js +0 -16
- package/remote/server/gui/js/popUp.js +0 -72
- package/remote/server/gui/js/queryApi.js +0 -51
- package/remote/server/gui/js/queryDb.js +0 -79
- package/remote/server/gui/js/queryGraph.js +0 -144
- package/remote/server/gui/js/render.js +0 -64
- package/remote/server/gui/js/templates.js +0 -31
- package/remote/server/gui/js/utils.js +0 -36
- package/remote/server/gui/js/vars.js +0 -9
- package/remote/server/gui/libs/core.js +0 -176
- package/remote/server/gui/libs/d3.v7.min.js +0 -2
- package/remote/server/gui/libs/handlebars.min.js +0 -29
- package/remote/server/gui/libs/json5.min.js +0 -1
- package/remote/server/index.js +0 -63
- package/remote/server/initDataBases.js +0 -20
- package/remote/server/pathUtils.js +0 -7
- package/remote/server/secret.js +0 -23
- package/remote/serverMgmt/index.js +0 -86
- package/test/hasFieldsAdvanced.test.js +0 -70
- package/utils/hasFieldsAdvanced.js +0 -184
- package/utils/updateObject.js +0 -15
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import dbActionC from "./action.js";
|
|
2
|
+
import executorC from "./executor.js";
|
|
3
|
+
import CollectionManager from "./CollectionManager.js";
|
|
4
|
+
import { DbFindOpts, DbOpts, FindOpts } from "./types/options.js";
|
|
5
|
+
import { Arg, Search, Updater } from "./types/arg.js";
|
|
6
|
+
import Data from "./types/data.js";
|
|
7
|
+
import { Context } from "./types/types.js";
|
|
8
|
+
/**
|
|
9
|
+
* Represents a database management class for performing CRUD operations.
|
|
10
|
+
* @class
|
|
11
|
+
*/
|
|
12
|
+
declare class DataBase {
|
|
13
|
+
dbAction: dbActionC;
|
|
14
|
+
executor: executorC;
|
|
15
|
+
constructor(folder: string, options?: DbOpts);
|
|
16
|
+
/**
|
|
17
|
+
* Create a new instance of a CollectionManager class.
|
|
18
|
+
*/
|
|
19
|
+
c(collection: string): CollectionManager;
|
|
20
|
+
/**
|
|
21
|
+
* Get the names of all available databases.
|
|
22
|
+
*/
|
|
23
|
+
getCollections(): Promise<string[]>;
|
|
24
|
+
/**
|
|
25
|
+
* Check and create the specified collection if it doesn't exist.
|
|
26
|
+
*/
|
|
27
|
+
checkCollection(collection: string): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Check if a collection exists.
|
|
30
|
+
*/
|
|
31
|
+
issetCollection(collection: string): Promise<boolean>;
|
|
32
|
+
/**
|
|
33
|
+
* Add data to a database.
|
|
34
|
+
*/
|
|
35
|
+
add<T = Data>(collection: string, data: Arg, id_gen?: boolean): Promise<T>;
|
|
36
|
+
/**
|
|
37
|
+
* Find data in a database.
|
|
38
|
+
*/
|
|
39
|
+
find<T = Data>(collection: string, search: Search, context?: Context, options?: DbFindOpts, findOpts?: FindOpts): Promise<T[]>;
|
|
40
|
+
/**
|
|
41
|
+
* Find one data entry in a database.
|
|
42
|
+
*/
|
|
43
|
+
findOne<T = Data>(collection: string, search: Search, context?: Context, findOpts?: FindOpts): Promise<T>;
|
|
44
|
+
/**
|
|
45
|
+
* Update data in a database.
|
|
46
|
+
*/
|
|
47
|
+
update(collection: string, search: Search, updater: Updater, context?: {}): Promise<boolean>;
|
|
48
|
+
/**
|
|
49
|
+
* Update one data entry in a database.
|
|
50
|
+
*/
|
|
51
|
+
updateOne(collection: string, search: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
52
|
+
/**
|
|
53
|
+
* Remove data from a database.
|
|
54
|
+
*/
|
|
55
|
+
remove(collection: string, search: Search, context?: Context): Promise<boolean>;
|
|
56
|
+
/**
|
|
57
|
+
* Remove one data entry from a database.
|
|
58
|
+
*/
|
|
59
|
+
removeOne(collection: string, search: Search, context?: Context): Promise<boolean>;
|
|
60
|
+
/**
|
|
61
|
+
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
62
|
+
*/
|
|
63
|
+
updateOneOrAdd(collection: string, search: Search, updater: Updater, add_arg?: Arg, context?: Context, id_gen?: boolean): Promise<boolean>;
|
|
64
|
+
/**
|
|
65
|
+
* Removes a database collection from the file system.
|
|
66
|
+
*/
|
|
67
|
+
removeCollection(collection: string): Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
export default DataBase;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import dbActionC from "./action.js";
|
|
2
|
+
import executorC from "./executor.js";
|
|
3
|
+
import CollectionManager from "./CollectionManager.js";
|
|
4
|
+
/**
|
|
5
|
+
* Represents a database management class for performing CRUD operations.
|
|
6
|
+
* @class
|
|
7
|
+
*/
|
|
8
|
+
class DataBase {
|
|
9
|
+
dbAction;
|
|
10
|
+
executor;
|
|
11
|
+
constructor(folder, options = {}) {
|
|
12
|
+
this.dbAction = new dbActionC(folder, options);
|
|
13
|
+
this.executor = new executorC();
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Create a new instance of a CollectionManager class.
|
|
17
|
+
*/
|
|
18
|
+
c(collection) {
|
|
19
|
+
return new CollectionManager(this, collection);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Get the names of all available databases.
|
|
23
|
+
*/
|
|
24
|
+
async getCollections() {
|
|
25
|
+
return this.dbAction.getCollections();
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Check and create the specified collection if it doesn't exist.
|
|
29
|
+
*/
|
|
30
|
+
async checkCollection(collection) {
|
|
31
|
+
this.dbAction.checkCollection(collection);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Check if a collection exists.
|
|
35
|
+
*/
|
|
36
|
+
async issetCollection(collection) {
|
|
37
|
+
return this.dbAction.issetCollection(collection);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Add data to a database.
|
|
41
|
+
*/
|
|
42
|
+
async add(collection, data, id_gen = true) {
|
|
43
|
+
return await this.executor.addOp(this.dbAction.add.bind(this.dbAction), collection, data, id_gen);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Find data in a database.
|
|
47
|
+
*/
|
|
48
|
+
async find(collection, search, context = {}, options = {}, findOpts = {}) {
|
|
49
|
+
return await this.executor.addOp(this.dbAction.find.bind(this.dbAction), collection, search, context, options, findOpts);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Find one data entry in a database.
|
|
53
|
+
*/
|
|
54
|
+
async findOne(collection, search, context = {}, findOpts = {}) {
|
|
55
|
+
return await this.executor.addOp(this.dbAction.findOne.bind(this.dbAction), collection, search, context, findOpts);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Update data in a database.
|
|
59
|
+
*/
|
|
60
|
+
async update(collection, search, updater, context = {}) {
|
|
61
|
+
return await this.executor.addOp(this.dbAction.update.bind(this.dbAction), collection, search, updater, context);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Update one data entry in a database.
|
|
65
|
+
*/
|
|
66
|
+
async updateOne(collection, search, updater, context = {}) {
|
|
67
|
+
return await this.executor.addOp(this.dbAction.updateOne.bind(this.dbAction), collection, search, updater, context);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Remove data from a database.
|
|
71
|
+
*/
|
|
72
|
+
async remove(collection, search, context = {}) {
|
|
73
|
+
return await this.executor.addOp(this.dbAction.remove.bind(this.dbAction), collection, search, context);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Remove one data entry from a database.
|
|
77
|
+
*/
|
|
78
|
+
async removeOne(collection, search, context = {}) {
|
|
79
|
+
return await this.executor.addOp(this.dbAction.removeOne.bind(this.dbAction), collection, search, context);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
83
|
+
*/
|
|
84
|
+
async updateOneOrAdd(collection, search, updater, add_arg = {}, context = {}, id_gen = true) {
|
|
85
|
+
const res = await this.updateOne(collection, search, updater, context);
|
|
86
|
+
if (!res) {
|
|
87
|
+
const assignData = [];
|
|
88
|
+
if (typeof search === "object" && !Array.isArray(search))
|
|
89
|
+
assignData.push(search);
|
|
90
|
+
if (typeof updater === "object" && !Array.isArray(updater))
|
|
91
|
+
assignData.push(updater);
|
|
92
|
+
if (typeof add_arg === "object" && !Array.isArray(add_arg))
|
|
93
|
+
assignData.push(add_arg);
|
|
94
|
+
await this.add(collection, Object.assign({}, ...assignData), id_gen);
|
|
95
|
+
}
|
|
96
|
+
return res;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Removes a database collection from the file system.
|
|
100
|
+
*/
|
|
101
|
+
async removeCollection(collection) {
|
|
102
|
+
this.dbAction.removeCollection(collection);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
export default DataBase;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
interface Task {
|
|
2
|
+
func: Function;
|
|
3
|
+
param: any[];
|
|
4
|
+
resolve: Function;
|
|
5
|
+
reject: Function;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* A simple executor for queuing and executing asynchronous operations sequentially.
|
|
9
|
+
* @class
|
|
10
|
+
*/
|
|
11
|
+
declare class executorC {
|
|
12
|
+
quote: Task[];
|
|
13
|
+
isExecuting: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Create a new executor instance.
|
|
16
|
+
* @constructor
|
|
17
|
+
*/
|
|
18
|
+
constructor();
|
|
19
|
+
/**
|
|
20
|
+
* Add an asynchronous operation to the execution queue.
|
|
21
|
+
*/
|
|
22
|
+
addOp(func: Function, ...param: any[]): Promise<unknown>;
|
|
23
|
+
/**
|
|
24
|
+
* Execute the queued asynchronous operations sequentially.
|
|
25
|
+
*/
|
|
26
|
+
execute(): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
export default executorC;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A simple executor for queuing and executing asynchronous operations sequentially.
|
|
3
|
+
* @class
|
|
4
|
+
*/
|
|
5
|
+
class executorC {
|
|
6
|
+
quote;
|
|
7
|
+
isExecuting;
|
|
8
|
+
/**
|
|
9
|
+
* Create a new executor instance.
|
|
10
|
+
* @constructor
|
|
11
|
+
*/
|
|
12
|
+
constructor() {
|
|
13
|
+
this.quote = [];
|
|
14
|
+
this.isExecuting = false;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Add an asynchronous operation to the execution queue.
|
|
18
|
+
*/
|
|
19
|
+
async addOp(func, ...param) {
|
|
20
|
+
return await new Promise((resolve, reject) => {
|
|
21
|
+
this.quote.push({
|
|
22
|
+
func,
|
|
23
|
+
param,
|
|
24
|
+
resolve,
|
|
25
|
+
reject
|
|
26
|
+
});
|
|
27
|
+
this.execute();
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Execute the queued asynchronous operations sequentially.
|
|
32
|
+
*/
|
|
33
|
+
async execute() {
|
|
34
|
+
if (this.isExecuting)
|
|
35
|
+
return;
|
|
36
|
+
this.isExecuting = true;
|
|
37
|
+
while (this.quote.length > 0) {
|
|
38
|
+
let q = this.quote.shift();
|
|
39
|
+
let res = await q.func(...q.param);
|
|
40
|
+
q.resolve(res);
|
|
41
|
+
}
|
|
42
|
+
this.isExecuting = false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export default executorC;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Search } from "../types/arg.js";
|
|
2
|
+
import { Context } from "../types/types.js";
|
|
3
|
+
import { FindOpts } from "../types/options.js";
|
|
4
|
+
/**
|
|
5
|
+
* Asynchronously finds entries in a file based on search criteria.
|
|
6
|
+
*/
|
|
7
|
+
export declare function find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<unknown>;
|
|
8
|
+
/**
|
|
9
|
+
* Asynchronously finds one entry in a file based on search criteria.
|
|
10
|
+
*/
|
|
11
|
+
export declare function findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<unknown>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { existsSync, promises } from "fs";
|
|
2
|
+
import { pathRepair, createRL } from "./utils.js";
|
|
3
|
+
import { parse } from "../format.js";
|
|
4
|
+
import hasFieldsAdvanced from "../utils/hasFieldsAdvanced.js";
|
|
5
|
+
import updateFindObject from "../utils/updateFindObject.js";
|
|
6
|
+
/**
|
|
7
|
+
* Processes a line of text from a file and checks if it matches the search criteria.
|
|
8
|
+
*/
|
|
9
|
+
async function findProcesLine(arg, line, context = {}, findOpts = {}) {
|
|
10
|
+
const ob = parse(line);
|
|
11
|
+
let res = false;
|
|
12
|
+
if (typeof arg === "function") {
|
|
13
|
+
if (arg(ob, context))
|
|
14
|
+
res = true;
|
|
15
|
+
}
|
|
16
|
+
else if (typeof arg === "object" && !Array.isArray(arg)) {
|
|
17
|
+
if (hasFieldsAdvanced(ob, arg))
|
|
18
|
+
res = true;
|
|
19
|
+
}
|
|
20
|
+
if (res)
|
|
21
|
+
return updateFindObject(ob, findOpts);
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Asynchronously finds entries in a file based on search criteria.
|
|
26
|
+
*/
|
|
27
|
+
export async function find(file, arg, context = {}, findOpts = {}) {
|
|
28
|
+
file = pathRepair(file);
|
|
29
|
+
return await new Promise(async (resolve) => {
|
|
30
|
+
if (!existsSync(file)) {
|
|
31
|
+
await promises.writeFile(file, "");
|
|
32
|
+
resolve(false);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
const rl = createRL(file);
|
|
36
|
+
const resF = [];
|
|
37
|
+
for await (const line of rl) {
|
|
38
|
+
if (line == "" || !line)
|
|
39
|
+
continue;
|
|
40
|
+
const res = await findProcesLine(arg, line, context, findOpts);
|
|
41
|
+
if (res)
|
|
42
|
+
resF.push(res);
|
|
43
|
+
}
|
|
44
|
+
;
|
|
45
|
+
resolve(resF);
|
|
46
|
+
rl.close();
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Asynchronously finds one entry in a file based on search criteria.
|
|
51
|
+
*/
|
|
52
|
+
export async function findOne(file, arg, context = {}, findOpts = {}) {
|
|
53
|
+
file = pathRepair(file);
|
|
54
|
+
return await new Promise(async (resolve) => {
|
|
55
|
+
if (!existsSync(file)) {
|
|
56
|
+
await promises.writeFile(file, "");
|
|
57
|
+
resolve(false);
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const rl = createRL(file);
|
|
61
|
+
for await (const line of rl) {
|
|
62
|
+
if (line == "" || !line)
|
|
63
|
+
continue;
|
|
64
|
+
const res = await findProcesLine(arg, line, context, findOpts);
|
|
65
|
+
if (res) {
|
|
66
|
+
resolve(res);
|
|
67
|
+
rl.close();
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
;
|
|
71
|
+
resolve(false);
|
|
72
|
+
});
|
|
73
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Search } from "../types/arg.js";
|
|
2
|
+
import { Context } from "../types/types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Asynchronously removes entries from a file based on search criteria.
|
|
5
|
+
*/
|
|
6
|
+
declare function remove(cpath: string, arg: Search, context?: Context, one?: boolean): Promise<boolean>;
|
|
7
|
+
export default remove;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { existsSync, promises, appendFileSync, readdirSync } from "fs";
|
|
2
|
+
import { pathRepair, createRL } from "./utils.js";
|
|
3
|
+
import { parse } from "../format.js";
|
|
4
|
+
import hasFieldsAdvanced from "../utils/hasFieldsAdvanced.js";
|
|
5
|
+
/**
|
|
6
|
+
* Removes entries from a file based on search criteria.
|
|
7
|
+
*/
|
|
8
|
+
async function removeWorker(file, search, context = {}, one = false) {
|
|
9
|
+
file = pathRepair(file);
|
|
10
|
+
if (!existsSync(file)) {
|
|
11
|
+
await promises.writeFile(file, "");
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
await promises.copyFile(file, file + ".tmp");
|
|
15
|
+
await promises.writeFile(file, "");
|
|
16
|
+
const rl = createRL(file + ".tmp");
|
|
17
|
+
let removed = false;
|
|
18
|
+
for await (let line of rl) {
|
|
19
|
+
if (one && removed) {
|
|
20
|
+
appendFileSync(file, line + "\n");
|
|
21
|
+
continue;
|
|
22
|
+
}
|
|
23
|
+
const data = parse(line);
|
|
24
|
+
if (typeof search === "function") {
|
|
25
|
+
if (search(data, context)) {
|
|
26
|
+
removed = true;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
else if (typeof search === "object" && !Array.isArray(search)) {
|
|
31
|
+
if (hasFieldsAdvanced(data, search)) {
|
|
32
|
+
removed = true;
|
|
33
|
+
continue;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
appendFileSync(file, line + "\n");
|
|
37
|
+
}
|
|
38
|
+
await promises.writeFile(file + ".tmp", "");
|
|
39
|
+
return removed;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Asynchronously removes entries from a file based on search criteria.
|
|
43
|
+
*/
|
|
44
|
+
async function remove(cpath, arg, context = {}, one = false) {
|
|
45
|
+
let files = readdirSync(cpath).filter(file => !/\.tmp$/.test(file));
|
|
46
|
+
files.reverse();
|
|
47
|
+
let remove = false;
|
|
48
|
+
for (const file of files) {
|
|
49
|
+
const removed = await removeWorker(cpath + file, arg, context, one);
|
|
50
|
+
if (one && removed)
|
|
51
|
+
break;
|
|
52
|
+
remove = remove || removed;
|
|
53
|
+
}
|
|
54
|
+
return remove;
|
|
55
|
+
}
|
|
56
|
+
export default remove;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Context } from "../types/types.js";
|
|
2
|
+
import { Search, Updater } from "../types/arg.js";
|
|
3
|
+
/**
|
|
4
|
+
* Asynchronously updates entries in a file based on search criteria and an updater function or object.
|
|
5
|
+
*/
|
|
6
|
+
declare function update(cpath: string, arg: Search, updater: Updater, context?: Context, one?: boolean): Promise<boolean>;
|
|
7
|
+
export default update;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { existsSync, promises, readdirSync } from "fs";
|
|
2
|
+
import { pathRepair, createRL } from "./utils.js";
|
|
3
|
+
import { parse, stringify } from "../format.js";
|
|
4
|
+
import hasFieldsAdvanced from "../utils/hasFieldsAdvanced.js";
|
|
5
|
+
import updateObjectAdvanced from "../utils/updateObject.js";
|
|
6
|
+
/**
|
|
7
|
+
* Updates a file based on search criteria and an updater function or object.
|
|
8
|
+
*/
|
|
9
|
+
async function updateWorker(file, search, updater, context = {}, one = false) {
|
|
10
|
+
file = pathRepair(file);
|
|
11
|
+
if (!existsSync(file)) {
|
|
12
|
+
await promises.writeFile(file, "");
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
await promises.copyFile(file, file + ".tmp");
|
|
16
|
+
await promises.writeFile(file, "");
|
|
17
|
+
const rl = createRL(file + ".tmp");
|
|
18
|
+
let updated = false;
|
|
19
|
+
for await (let line of rl) {
|
|
20
|
+
if (one && updated) {
|
|
21
|
+
await promises.appendFile(file, line + "\n");
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
const data = parse(line);
|
|
25
|
+
let ob = false;
|
|
26
|
+
if (typeof search === "function") {
|
|
27
|
+
ob = search(data, context) || false;
|
|
28
|
+
}
|
|
29
|
+
else if (typeof search === "object" && !Array.isArray(search)) {
|
|
30
|
+
ob = hasFieldsAdvanced(data, search);
|
|
31
|
+
}
|
|
32
|
+
if (ob) {
|
|
33
|
+
let updateObj = data;
|
|
34
|
+
if (typeof updater === "function") {
|
|
35
|
+
const updateObjValue = updater(data, context);
|
|
36
|
+
if (updateObjValue)
|
|
37
|
+
updateObj = updateObjValue;
|
|
38
|
+
}
|
|
39
|
+
else if (typeof updater === "object" && !Array.isArray(updater)) {
|
|
40
|
+
updateObj = updateObjectAdvanced(data, updater);
|
|
41
|
+
}
|
|
42
|
+
line = await stringify(updateObj);
|
|
43
|
+
updated = true;
|
|
44
|
+
}
|
|
45
|
+
await promises.appendFile(file, line + "\n");
|
|
46
|
+
}
|
|
47
|
+
await promises.writeFile(file + ".tmp", "");
|
|
48
|
+
return updated;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Asynchronously updates entries in a file based on search criteria and an updater function or object.
|
|
52
|
+
*/
|
|
53
|
+
async function update(cpath, arg, updater, context = {}, one = false) {
|
|
54
|
+
let files = readdirSync(cpath).filter(file => !/\.tmp$/.test(file));
|
|
55
|
+
files.reverse();
|
|
56
|
+
let update = false;
|
|
57
|
+
for (const file of files) {
|
|
58
|
+
const updated = await updateWorker(cpath + file, arg, updater, context, one);
|
|
59
|
+
if (one && updated)
|
|
60
|
+
return true;
|
|
61
|
+
update = update || updated;
|
|
62
|
+
}
|
|
63
|
+
return update;
|
|
64
|
+
}
|
|
65
|
+
export default update;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Repairs a file path by replacing double slashes
|
|
3
|
+
*/
|
|
4
|
+
export declare function pathRepair(path: string): string;
|
|
5
|
+
/**
|
|
6
|
+
* Creates a Readline interface for reading large files with a specified high water mark.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createRL(file: string): import("readline").Interface;
|
|
@@ -1,27 +1,19 @@
|
|
|
1
1
|
import { createReadStream } from "fs";
|
|
2
2
|
import { createInterface } from "readline";
|
|
3
|
-
|
|
4
3
|
/**
|
|
5
4
|
* Repairs a file path by replacing double slashes
|
|
6
|
-
* @private
|
|
7
|
-
* @param {string} path - The file path to repair.
|
|
8
|
-
* @returns {string} The repaired file path.
|
|
9
5
|
*/
|
|
10
|
-
export function pathRepair(path){
|
|
6
|
+
export function pathRepair(path) {
|
|
11
7
|
return path.replaceAll("//", "/");
|
|
12
8
|
}
|
|
13
|
-
|
|
14
9
|
/**
|
|
15
10
|
* Creates a Readline interface for reading large files with a specified high water mark.
|
|
16
|
-
* @private
|
|
17
|
-
* @param {string} file - The file path to create a Readline interface for.
|
|
18
|
-
* @returns {readline.Interface} The Readline interface.
|
|
19
11
|
*/
|
|
20
|
-
export function createRL(file){
|
|
12
|
+
export function createRL(file) {
|
|
21
13
|
const read_stream = createReadStream(file, { highWaterMark: 10 * 1024 * 1024 }); //10MB
|
|
22
14
|
const rl = createInterface({
|
|
23
15
|
input: read_stream,
|
|
24
16
|
crlfDelay: Infinity
|
|
25
17
|
});
|
|
26
18
|
return rl;
|
|
27
|
-
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses given string into a JSON object. If the string does not start with
|
|
3
|
+
* a {, it is wrapped in one. This allows for a shorthand when
|
|
4
|
+
* storing/reading data from a file.
|
|
5
|
+
*
|
|
6
|
+
* @param {string} data
|
|
7
|
+
* @returns {Object}
|
|
8
|
+
*/
|
|
9
|
+
export declare function parse(data: any): any;
|
|
10
|
+
/**
|
|
11
|
+
* Converts given object to a string. If the string is a valid json5, it is
|
|
12
|
+
* returned as is. If it is a valid json5 wrapped in {}, the curly brackets
|
|
13
|
+
* are removed. Otherwise the string is wrapped in {}.
|
|
14
|
+
*
|
|
15
|
+
* @param {Object} data
|
|
16
|
+
* @return {String}
|
|
17
|
+
*/
|
|
18
|
+
export declare function stringify(data: any): any;
|
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import json5 from "json5";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* @
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return json5.parse(data);
|
|
14
|
-
}
|
|
15
|
-
/**
|
|
16
|
-
* Converts given object to a string. If the string is a valid json5, it is
|
|
17
|
-
* returned as is. If it is a valid json5 wrapped in {}, the curly brackets
|
|
18
|
-
* are removed. Otherwise the string is wrapped in {}.
|
|
19
|
-
*
|
|
20
|
-
* @param {Object} data
|
|
21
|
-
* @return {String}
|
|
22
|
-
*/
|
|
23
|
-
export function stringify(data){
|
|
24
|
-
data = json5.stringify(data);
|
|
25
|
-
if(data.startsWith("{")){
|
|
26
|
-
data = data.slice(1, -1);
|
|
27
|
-
}
|
|
28
|
-
return data;
|
|
29
|
-
}
|
|
1
|
+
import json5 from "json5";
|
|
2
|
+
/**
|
|
3
|
+
* Parses given string into a JSON object. If the string does not start with
|
|
4
|
+
* a {, it is wrapped in one. This allows for a shorthand when
|
|
5
|
+
* storing/reading data from a file.
|
|
6
|
+
*
|
|
7
|
+
* @param {string} data
|
|
8
|
+
* @returns {Object}
|
|
9
|
+
*/
|
|
10
|
+
export function parse(data) {
|
|
11
|
+
if (!data.startsWith("{"))
|
|
12
|
+
data = "{" + data + "}";
|
|
13
|
+
return json5.parse(data);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Converts given object to a string. If the string is a valid json5, it is
|
|
17
|
+
* returned as is. If it is a valid json5 wrapped in {}, the curly brackets
|
|
18
|
+
* are removed. Otherwise the string is wrapped in {}.
|
|
19
|
+
*
|
|
20
|
+
* @param {Object} data
|
|
21
|
+
* @return {String}
|
|
22
|
+
*/
|
|
23
|
+
export function stringify(data) {
|
|
24
|
+
data = json5.stringify(data);
|
|
25
|
+
if (data.startsWith("{")) {
|
|
26
|
+
data = data.slice(1, -1);
|
|
27
|
+
}
|
|
28
|
+
return data;
|
|
29
|
+
}
|