@wxn0brp/db 0.1.2 → 0.3.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/dist/{esm/action.d.ts → action.d.ts} +4 -4
- package/dist/{esm/action.js → action.js} +54 -36
- package/dist/{esm/client → client}/database.js +1 -1
- package/dist/{esm/database.js → database.js} +4 -4
- package/dist/{esm/file → file}/customFileCpu.d.ts +2 -4
- package/dist/{esm/file → file}/customFileCpu.js +2 -27
- package/dist/file/remove.d.ts +7 -0
- package/dist/{esm/file → file}/remove.js +3 -18
- package/dist/file/update.d.ts +7 -0
- package/dist/{esm/file → file}/update.js +3 -18
- package/dist/{esm/types → types}/fileCpu.d.ts +4 -4
- package/dist/types/types.d.ts +3 -0
- package/package.json +8 -11
- package/dist/cjs/CollectionManager.js +0 -59
- package/dist/cjs/action.d.ts +0 -68
- package/dist/cjs/action.js +0 -192
- package/dist/cjs/client/database.js +0 -119
- package/dist/cjs/client/function.js +0 -32
- package/dist/cjs/client/graph.js +0 -94
- package/dist/cjs/client/remote.js +0 -2
- package/dist/cjs/database.js +0 -113
- package/dist/cjs/executor.js +0 -47
- package/dist/cjs/file/customFileCpu.d.ts +0 -21
- package/dist/cjs/file/customFileCpu.js +0 -104
- package/dist/cjs/file/find.js +0 -80
- package/dist/cjs/file/index.js +0 -21
- package/dist/cjs/file/remove.d.ts +0 -7
- package/dist/cjs/file/remove.js +0 -61
- package/dist/cjs/file/update.d.ts +0 -7
- package/dist/cjs/file/update.js +0 -70
- package/dist/cjs/file/utils.js +0 -23
- package/dist/cjs/format.js +0 -30
- package/dist/cjs/gen.js +0 -137
- package/dist/cjs/graph.js +0 -90
- package/dist/cjs/index.js +0 -20
- package/dist/cjs/relation.js +0 -64
- package/dist/cjs/types/Id.js +0 -2
- package/dist/cjs/types/arg.js +0 -2
- package/dist/cjs/types/data.js +0 -2
- package/dist/cjs/types/fileCpu.d.ts +0 -51
- package/dist/cjs/types/fileCpu.js +0 -2
- package/dist/cjs/types/options.js +0 -2
- package/dist/cjs/types/searchOpts.js +0 -8
- package/dist/cjs/types/types.d.ts +0 -7
- package/dist/cjs/types/types.js +0 -2
- package/dist/cjs/types/updater.js +0 -5
- package/dist/cjs/utils/hasFields.js +0 -21
- package/dist/cjs/utils/hasFieldsAdvanced.js +0 -182
- package/dist/cjs/utils/updateFindObject.js +0 -31
- package/dist/cjs/utils/updateObject.js +0 -148
- package/dist/esm/CollectionManager.d.ts +0 -44
- package/dist/esm/client/database.d.ts +0 -72
- package/dist/esm/client/function.d.ts +0 -5
- package/dist/esm/client/graph.d.ts +0 -54
- package/dist/esm/client/remote.d.ts +0 -15
- package/dist/esm/database.d.ts +0 -70
- package/dist/esm/executor.d.ts +0 -28
- package/dist/esm/file/find.d.ts +0 -11
- package/dist/esm/file/index.d.ts +0 -3
- package/dist/esm/file/remove.d.ts +0 -7
- package/dist/esm/file/update.d.ts +0 -7
- package/dist/esm/file/utils.d.ts +0 -8
- package/dist/esm/format.d.ts +0 -12
- package/dist/esm/gen.d.ts +0 -8
- package/dist/esm/graph.d.ts +0 -47
- package/dist/esm/index.d.ts +0 -14
- package/dist/esm/relation.d.ts +0 -34
- package/dist/esm/types/Id.d.ts +0 -3
- package/dist/esm/types/arg.d.ts +0 -12
- package/dist/esm/types/data.d.ts +0 -4
- package/dist/esm/types/options.d.ts +0 -12
- package/dist/esm/types/searchOpts.d.ts +0 -62
- package/dist/esm/types/types.d.ts +0 -7
- package/dist/esm/types/updater.d.ts +0 -26
- package/dist/esm/utils/hasFields.d.ts +0 -7
- package/dist/esm/utils/hasFieldsAdvanced.d.ts +0 -5
- package/dist/esm/utils/updateFindObject.d.ts +0 -11
- package/dist/esm/utils/updateObject.d.ts +0 -7
- /package/dist/{cjs/CollectionManager.d.ts → CollectionManager.d.ts} +0 -0
- /package/dist/{esm/CollectionManager.js → CollectionManager.js} +0 -0
- /package/dist/{cjs/client → client}/database.d.ts +0 -0
- /package/dist/{cjs/client → client}/function.d.ts +0 -0
- /package/dist/{esm/client → client}/function.js +0 -0
- /package/dist/{cjs/client → client}/graph.d.ts +0 -0
- /package/dist/{esm/client → client}/graph.js +0 -0
- /package/dist/{cjs/client → client}/remote.d.ts +0 -0
- /package/dist/{esm/client → client}/remote.js +0 -0
- /package/dist/{cjs/database.d.ts → database.d.ts} +0 -0
- /package/dist/{cjs/executor.d.ts → executor.d.ts} +0 -0
- /package/dist/{esm/executor.js → executor.js} +0 -0
- /package/dist/{cjs/file → file}/find.d.ts +0 -0
- /package/dist/{esm/file → file}/find.js +0 -0
- /package/dist/{cjs/file → file}/index.d.ts +0 -0
- /package/dist/{esm/file → file}/index.js +0 -0
- /package/dist/{cjs/file → file}/utils.d.ts +0 -0
- /package/dist/{esm/file → file}/utils.js +0 -0
- /package/dist/{cjs/format.d.ts → format.d.ts} +0 -0
- /package/dist/{esm/format.js → format.js} +0 -0
- /package/dist/{cjs/gen.d.ts → gen.d.ts} +0 -0
- /package/dist/{esm/gen.js → gen.js} +0 -0
- /package/dist/{cjs/graph.d.ts → graph.d.ts} +0 -0
- /package/dist/{esm/graph.js → graph.js} +0 -0
- /package/dist/{cjs/index.d.ts → index.d.ts} +0 -0
- /package/dist/{esm/index.js → index.js} +0 -0
- /package/dist/{cjs/relation.d.ts → relation.d.ts} +0 -0
- /package/dist/{esm/relation.js → relation.js} +0 -0
- /package/dist/{cjs/types → types}/Id.d.ts +0 -0
- /package/dist/{esm/types → types}/Id.js +0 -0
- /package/dist/{cjs/types → types}/arg.d.ts +0 -0
- /package/dist/{esm/types → types}/arg.js +0 -0
- /package/dist/{cjs/types → types}/data.d.ts +0 -0
- /package/dist/{esm/types → types}/data.js +0 -0
- /package/dist/{esm/types → types}/fileCpu.js +0 -0
- /package/dist/{cjs/types → types}/options.d.ts +0 -0
- /package/dist/{esm/types → types}/options.js +0 -0
- /package/dist/{cjs/types → types}/searchOpts.d.ts +0 -0
- /package/dist/{esm/types → types}/searchOpts.js +0 -0
- /package/dist/{esm/types → types}/types.js +0 -0
- /package/dist/{cjs/types → types}/updater.d.ts +0 -0
- /package/dist/{esm/types → types}/updater.js +0 -0
- /package/dist/{cjs/utils → utils}/hasFields.d.ts +0 -0
- /package/dist/{esm/utils → utils}/hasFields.js +0 -0
- /package/dist/{cjs/utils → utils}/hasFieldsAdvanced.d.ts +0 -0
- /package/dist/{esm/utils → utils}/hasFieldsAdvanced.js +0 -0
- /package/dist/{cjs/utils → utils}/updateFindObject.d.ts +0 -0
- /package/dist/{esm/utils → utils}/updateFindObject.js +0 -0
- /package/dist/{cjs/utils → utils}/updateObject.d.ts +0 -0
- /package/dist/{esm/utils → utils}/updateObject.js +0 -0
|
@@ -23,15 +23,15 @@ declare class dbActionC {
|
|
|
23
23
|
/**
|
|
24
24
|
* Get a list of available databases in the specified folder.
|
|
25
25
|
*/
|
|
26
|
-
getCollections(): string[]
|
|
26
|
+
getCollections(): Promise<string[]>;
|
|
27
27
|
/**
|
|
28
28
|
* Check and create the specified collection if it doesn't exist.
|
|
29
29
|
*/
|
|
30
|
-
checkCollection(collection: string): void
|
|
30
|
+
checkCollection(collection: string): Promise<void>;
|
|
31
31
|
/**
|
|
32
32
|
* Check if a collection exists.
|
|
33
33
|
*/
|
|
34
|
-
issetCollection(collection: string): boolean
|
|
34
|
+
issetCollection(collection: string): Promise<boolean>;
|
|
35
35
|
/**
|
|
36
36
|
* Add a new entry to the specified database.
|
|
37
37
|
*/
|
|
@@ -63,6 +63,6 @@ declare class dbActionC {
|
|
|
63
63
|
/**
|
|
64
64
|
* Removes a database collection from the file system.
|
|
65
65
|
*/
|
|
66
|
-
removeCollection(collection: string): void
|
|
66
|
+
removeCollection(collection: string): Promise<void>;
|
|
67
67
|
}
|
|
68
68
|
export default dbActionC;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import gen from "./gen.js";
|
|
2
|
-
import { existsSync, mkdirSync,
|
|
2
|
+
import { existsSync, mkdirSync, statSync, promises } from "fs";
|
|
3
3
|
/**
|
|
4
4
|
* A class representing database actions on files.
|
|
5
5
|
* @class
|
|
@@ -30,8 +30,9 @@ class dbActionC {
|
|
|
30
30
|
/**
|
|
31
31
|
* Get a list of available databases in the specified folder.
|
|
32
32
|
*/
|
|
33
|
-
getCollections() {
|
|
34
|
-
const
|
|
33
|
+
async getCollections() {
|
|
34
|
+
const allCollections = await promises.readdir(this.folder, { recursive: true, withFileTypes: true });
|
|
35
|
+
const collections = allCollections
|
|
35
36
|
.filter(dirent => dirent.isDirectory())
|
|
36
37
|
.map(dirent => {
|
|
37
38
|
if (dirent.parentPath === this.folder)
|
|
@@ -43,17 +44,24 @@ class dbActionC {
|
|
|
43
44
|
/**
|
|
44
45
|
* Check and create the specified collection if it doesn't exist.
|
|
45
46
|
*/
|
|
46
|
-
checkCollection(collection) {
|
|
47
|
+
async checkCollection(collection) {
|
|
48
|
+
if (await this.issetCollection(collection))
|
|
49
|
+
return;
|
|
47
50
|
const cpath = this._getCollectionPath(collection);
|
|
48
|
-
|
|
49
|
-
mkdirSync(cpath, { recursive: true });
|
|
51
|
+
await promises.mkdir(cpath, { recursive: true });
|
|
50
52
|
}
|
|
51
53
|
/**
|
|
52
54
|
* Check if a collection exists.
|
|
53
55
|
*/
|
|
54
|
-
issetCollection(collection) {
|
|
55
|
-
const path = this.
|
|
56
|
-
|
|
56
|
+
async issetCollection(collection) {
|
|
57
|
+
const path = this._getCollectionPath(collection);
|
|
58
|
+
try {
|
|
59
|
+
await promises.access(path);
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
57
65
|
}
|
|
58
66
|
/**
|
|
59
67
|
* Add a new entry to the specified database.
|
|
@@ -61,7 +69,7 @@ class dbActionC {
|
|
|
61
69
|
async add(collection, arg, id_gen = true) {
|
|
62
70
|
this.checkCollection(collection);
|
|
63
71
|
const cpath = this._getCollectionPath(collection);
|
|
64
|
-
const file = cpath + getLastFile(cpath, this.options.maxFileSize);
|
|
72
|
+
const file = cpath + await getLastFile(cpath, this.options.maxFileSize);
|
|
65
73
|
if (id_gen)
|
|
66
74
|
arg._id = arg._id || gen();
|
|
67
75
|
await this.fileCpu.add(file, arg);
|
|
@@ -75,7 +83,7 @@ class dbActionC {
|
|
|
75
83
|
options.max = options.max || -1;
|
|
76
84
|
this.checkCollection(collection);
|
|
77
85
|
const cpath = this._getCollectionPath(collection);
|
|
78
|
-
const files = getSortedFiles(cpath)
|
|
86
|
+
const files = await getSortedFiles(cpath);
|
|
79
87
|
if (options.reverse)
|
|
80
88
|
files.reverse();
|
|
81
89
|
let datas = [];
|
|
@@ -106,7 +114,7 @@ class dbActionC {
|
|
|
106
114
|
async findOne(collection, arg, context = {}, findOpts = {}) {
|
|
107
115
|
this.checkCollection(collection);
|
|
108
116
|
const cpath = this._getCollectionPath(collection);
|
|
109
|
-
const files = getSortedFiles(cpath)
|
|
117
|
+
const files = await getSortedFiles(cpath);
|
|
110
118
|
for (let f of files) {
|
|
111
119
|
let data = await this.fileCpu.findOne(cpath + f, arg, context, findOpts);
|
|
112
120
|
if (data)
|
|
@@ -119,69 +127,79 @@ class dbActionC {
|
|
|
119
127
|
*/
|
|
120
128
|
async update(collection, arg, updater, context = {}) {
|
|
121
129
|
this.checkCollection(collection);
|
|
122
|
-
return await this.fileCpu.update(this.
|
|
130
|
+
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), false, arg, updater, context);
|
|
123
131
|
}
|
|
124
132
|
/**
|
|
125
133
|
* Update the first matching entry in the specified database based on search criteria and an updater function or object.
|
|
126
134
|
*/
|
|
127
135
|
async updateOne(collection, arg, updater, context = {}) {
|
|
128
136
|
this.checkCollection(collection);
|
|
129
|
-
return await this.fileCpu.update(this.
|
|
137
|
+
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), true, arg, updater, context);
|
|
130
138
|
}
|
|
131
139
|
/**
|
|
132
140
|
* Remove entries from the specified database based on search criteria.
|
|
133
141
|
*/
|
|
134
142
|
async remove(collection, arg, context = {}) {
|
|
135
143
|
this.checkCollection(collection);
|
|
136
|
-
return await this.fileCpu.remove(this.
|
|
144
|
+
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), false, arg, context);
|
|
137
145
|
}
|
|
138
146
|
/**
|
|
139
147
|
* Remove the first matching entry from the specified database based on search criteria.
|
|
140
148
|
*/
|
|
141
149
|
async removeOne(collection, arg, context = {}) {
|
|
142
150
|
this.checkCollection(collection);
|
|
143
|
-
return await this.fileCpu.remove(this.
|
|
151
|
+
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), true, arg, context);
|
|
144
152
|
}
|
|
145
153
|
/**
|
|
146
154
|
* Removes a database collection from the file system.
|
|
147
155
|
*/
|
|
148
|
-
removeCollection(collection) {
|
|
149
|
-
|
|
156
|
+
async removeCollection(collection) {
|
|
157
|
+
await promises.rm(this.folder + "/" + collection, { recursive: true, force: true });
|
|
150
158
|
}
|
|
151
159
|
}
|
|
152
160
|
/**
|
|
153
161
|
* Get the last file in the specified directory.
|
|
154
162
|
*/
|
|
155
|
-
function getLastFile(path, maxFileSize = 1024 * 1024) {
|
|
163
|
+
async function getLastFile(path, maxFileSize = 1024 * 1024) {
|
|
156
164
|
if (!existsSync(path))
|
|
157
165
|
mkdirSync(path, { recursive: true });
|
|
158
|
-
const files = getSortedFiles(path);
|
|
166
|
+
const files = await getSortedFiles(path);
|
|
159
167
|
if (files.length == 0) {
|
|
160
|
-
|
|
168
|
+
await promises.writeFile(path + "/1.db", "");
|
|
161
169
|
return "1.db";
|
|
162
170
|
}
|
|
163
171
|
const last = files[files.length - 1];
|
|
164
|
-
const info = path + "/" + last
|
|
172
|
+
const info = path + "/" + last;
|
|
165
173
|
if (statSync(info).size < maxFileSize)
|
|
166
|
-
return last
|
|
167
|
-
const num = last.
|
|
168
|
-
|
|
174
|
+
return last;
|
|
175
|
+
const num = parseInt(last.replace(".db", ""), 10) + 1;
|
|
176
|
+
await promises.writeFile(path + "/" + num + ".db", "");
|
|
169
177
|
return num + ".db";
|
|
170
178
|
}
|
|
171
179
|
/**
|
|
172
180
|
* Get all files in a directory sorted by name.
|
|
173
181
|
*/
|
|
174
|
-
function getSortedFiles(
|
|
175
|
-
const files =
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
};
|
|
182
|
+
async function getSortedFiles(folder) {
|
|
183
|
+
const files = await promises.readdir(folder, { withFileTypes: true });
|
|
184
|
+
return files
|
|
185
|
+
.filter(file => file.isFile() && !file.name.endsWith(".tmp"))
|
|
186
|
+
.map(file => file.name)
|
|
187
|
+
.filter(name => /^\d+\.db$/.test(name))
|
|
188
|
+
.sort((a, b) => {
|
|
189
|
+
const numA = parseInt(a, 10);
|
|
190
|
+
const numB = parseInt(b, 10);
|
|
191
|
+
return numA - numB;
|
|
185
192
|
});
|
|
186
193
|
}
|
|
194
|
+
async function operationUpdater(cpath, worker, one, ...args) {
|
|
195
|
+
const files = await getSortedFiles(cpath);
|
|
196
|
+
let update = false;
|
|
197
|
+
for (const file of files) {
|
|
198
|
+
const updated = await worker(cpath + file, one, ...args);
|
|
199
|
+
if (one && updated)
|
|
200
|
+
break;
|
|
201
|
+
update = update || updated;
|
|
202
|
+
}
|
|
203
|
+
return update;
|
|
204
|
+
}
|
|
187
205
|
export default dbActionC;
|
|
@@ -102,7 +102,7 @@ class DataBaseRemote {
|
|
|
102
102
|
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
103
103
|
*/
|
|
104
104
|
async updateOneOrAdd(collection, search, arg, add_arg = {}, context = {}, id_gen = true) {
|
|
105
|
-
return await this._request("updateOneOrAdd", [collection, search, arg, add_arg,
|
|
105
|
+
return await this._request("updateOneOrAdd", [collection, search, arg, add_arg, context, id_gen]);
|
|
106
106
|
}
|
|
107
107
|
/**
|
|
108
108
|
* Removes a database collection from the file system.
|
|
@@ -25,19 +25,19 @@ class DataBase {
|
|
|
25
25
|
* Get the names of all available databases.
|
|
26
26
|
*/
|
|
27
27
|
async getCollections() {
|
|
28
|
-
return this.dbAction.getCollections();
|
|
28
|
+
return await this.dbAction.getCollections();
|
|
29
29
|
}
|
|
30
30
|
/**
|
|
31
31
|
* Check and create the specified collection if it doesn't exist.
|
|
32
32
|
*/
|
|
33
33
|
async checkCollection(collection) {
|
|
34
|
-
this.dbAction.checkCollection(collection);
|
|
34
|
+
await this.dbAction.checkCollection(collection);
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
37
37
|
* Check if a collection exists.
|
|
38
38
|
*/
|
|
39
39
|
async issetCollection(collection) {
|
|
40
|
-
return this.dbAction.issetCollection(collection);
|
|
40
|
+
return await this.dbAction.issetCollection(collection);
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
43
|
* Add data to a database.
|
|
@@ -102,7 +102,7 @@ class DataBase {
|
|
|
102
102
|
* Removes a database collection from the file system.
|
|
103
103
|
*/
|
|
104
104
|
async removeCollection(collection) {
|
|
105
|
-
this.dbAction.removeCollection(collection);
|
|
105
|
+
await this.dbAction.removeCollection(collection);
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
export default DataBase;
|
|
@@ -13,9 +13,7 @@ declare class CustomFileCpu implements FileCpu {
|
|
|
13
13
|
addMany(file: string, data: Data[]): Promise<void>;
|
|
14
14
|
find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any[] | false>;
|
|
15
15
|
findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
updateWorker(file: string, arg: Search, updater: Updater, context?: Context, one?: boolean): Promise<boolean>;
|
|
19
|
-
update(cpath: string, arg: Search, updater: Updater, context?: Context, one?: boolean): Promise<boolean>;
|
|
16
|
+
remove(file: string, one: boolean, arg: Search, context?: Context): Promise<boolean>;
|
|
17
|
+
update(file: string, one: boolean, arg: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
20
18
|
}
|
|
21
19
|
export default CustomFileCpu;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import hasFieldsAdvanced from "../utils/hasFieldsAdvanced.js";
|
|
2
2
|
import updateFindObject from "../utils/updateFindObject.js";
|
|
3
3
|
import { pathRepair } from "./utils.js";
|
|
4
|
-
import { readdirSync } from "fs";
|
|
5
4
|
class CustomFileCpu {
|
|
6
5
|
_readFile;
|
|
7
6
|
_writeFile;
|
|
@@ -33,7 +32,7 @@ class CustomFileCpu {
|
|
|
33
32
|
const result = entries.find(entry => typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg));
|
|
34
33
|
return result ? updateFindObject(result, findOpts) : false;
|
|
35
34
|
}
|
|
36
|
-
async
|
|
35
|
+
async remove(file, one, arg, context = {}) {
|
|
37
36
|
file = pathRepair(file);
|
|
38
37
|
let entries = await this._readFile(file);
|
|
39
38
|
let removed = false;
|
|
@@ -52,19 +51,7 @@ class CustomFileCpu {
|
|
|
52
51
|
await this._writeFile(file, entries);
|
|
53
52
|
return true;
|
|
54
53
|
}
|
|
55
|
-
async
|
|
56
|
-
let files = readdirSync(cpath).filter(file => !/\.tmp$/.test(file));
|
|
57
|
-
files.reverse();
|
|
58
|
-
let remove = false;
|
|
59
|
-
for (const file of files) {
|
|
60
|
-
const removed = await this.removeWorker(cpath + file, arg, context, one);
|
|
61
|
-
if (one && removed)
|
|
62
|
-
break;
|
|
63
|
-
remove = remove || removed;
|
|
64
|
-
}
|
|
65
|
-
return remove;
|
|
66
|
-
}
|
|
67
|
-
async updateWorker(file, arg, updater, context = {}, one = false) {
|
|
54
|
+
async update(file, one, arg, updater, context = {}) {
|
|
68
55
|
file = pathRepair(file);
|
|
69
56
|
let entries = await this._readFile(file);
|
|
70
57
|
let updated = false;
|
|
@@ -83,17 +70,5 @@ class CustomFileCpu {
|
|
|
83
70
|
await this._writeFile(file, entries);
|
|
84
71
|
return true;
|
|
85
72
|
}
|
|
86
|
-
async update(cpath, arg, updater, context = {}, one = false) {
|
|
87
|
-
let files = readdirSync(cpath).filter(file => !/\.tmp$/.test(file));
|
|
88
|
-
files.reverse();
|
|
89
|
-
let update = false;
|
|
90
|
-
for (const file of files) {
|
|
91
|
-
const updated = await this.updateWorker(cpath + file, arg, updater, context, one);
|
|
92
|
-
if (one && updated)
|
|
93
|
-
return true;
|
|
94
|
-
update = update || updated;
|
|
95
|
-
}
|
|
96
|
-
return update;
|
|
97
|
-
}
|
|
98
73
|
}
|
|
99
74
|
export default CustomFileCpu;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Search } from "../types/arg.js";
|
|
2
|
+
import { Context } from "../types/types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Removes entries from a file based on search criteria.
|
|
5
|
+
*/
|
|
6
|
+
declare function removeWorker(file: string, one: boolean, search: Search, context?: Context): Promise<boolean>;
|
|
7
|
+
export default removeWorker;
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { existsSync, promises, appendFileSync
|
|
1
|
+
import { existsSync, promises, appendFileSync } from "fs";
|
|
2
2
|
import { pathRepair, createRL } from "./utils.js";
|
|
3
3
|
import { parse } from "../format.js";
|
|
4
4
|
import hasFieldsAdvanced from "../utils/hasFieldsAdvanced.js";
|
|
5
5
|
/**
|
|
6
6
|
* Removes entries from a file based on search criteria.
|
|
7
7
|
*/
|
|
8
|
-
async function removeWorker(file, search, context = {}
|
|
8
|
+
async function removeWorker(file, one, search, context = {}) {
|
|
9
9
|
file = pathRepair(file);
|
|
10
10
|
if (!existsSync(file)) {
|
|
11
11
|
await promises.writeFile(file, "");
|
|
@@ -38,19 +38,4 @@ async function removeWorker(file, search, context = {}, one = false) {
|
|
|
38
38
|
await promises.writeFile(file + ".tmp", "");
|
|
39
39
|
return removed;
|
|
40
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;
|
|
41
|
+
export default removeWorker;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Context } from "../types/types.js";
|
|
2
|
+
import { Search, Updater } from "../types/arg.js";
|
|
3
|
+
/**
|
|
4
|
+
* Updates a file based on search criteria and an updater function or object.
|
|
5
|
+
*/
|
|
6
|
+
declare function updateWorker(file: string, one: boolean, search: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
7
|
+
export default updateWorker;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { existsSync, promises
|
|
1
|
+
import { existsSync, promises } from "fs";
|
|
2
2
|
import { pathRepair, createRL } from "./utils.js";
|
|
3
3
|
import { parse, stringify } from "../format.js";
|
|
4
4
|
import hasFieldsAdvanced from "../utils/hasFieldsAdvanced.js";
|
|
@@ -6,7 +6,7 @@ import updateObjectAdvanced from "../utils/updateObject.js";
|
|
|
6
6
|
/**
|
|
7
7
|
* Updates a file based on search criteria and an updater function or object.
|
|
8
8
|
*/
|
|
9
|
-
async function updateWorker(file, search, updater, context = {}
|
|
9
|
+
async function updateWorker(file, one, search, updater, context = {}) {
|
|
10
10
|
file = pathRepair(file);
|
|
11
11
|
if (!existsSync(file)) {
|
|
12
12
|
await promises.writeFile(file, "");
|
|
@@ -47,19 +47,4 @@ async function updateWorker(file, search, updater, context = {}, one = false) {
|
|
|
47
47
|
await promises.writeFile(file + ".tmp", "");
|
|
48
48
|
return updated;
|
|
49
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;
|
|
50
|
+
export default updateWorker;
|
|
@@ -30,22 +30,22 @@ interface FileCpu {
|
|
|
30
30
|
findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
|
|
31
31
|
/**
|
|
32
32
|
* Asynchronously removes entries from a file based on search criteria.
|
|
33
|
-
* @param
|
|
33
|
+
* @param file The path to the file.
|
|
34
34
|
* @param arg The search criteria.
|
|
35
35
|
* @param context Additional context for the operation.
|
|
36
36
|
* @param one If `true`, removes only the first matching entry.
|
|
37
37
|
* @returns A promise resolving to `true` if at least one entry was removed, otherwise `false`.
|
|
38
38
|
*/
|
|
39
|
-
remove(
|
|
39
|
+
remove(file: string, one: boolean, arg: Search, context?: Context): Promise<boolean>;
|
|
40
40
|
/**
|
|
41
41
|
* Asynchronously updates entries in a file based on search criteria and an updater function or object.
|
|
42
|
-
* @param
|
|
42
|
+
* @param file The path to the file.
|
|
43
43
|
* @param arg The search criteria.
|
|
44
44
|
* @param updater The updater function or object.
|
|
45
45
|
* @param context Additional context for the operation.
|
|
46
46
|
* @param one If `true`, updates only the first matching entry.
|
|
47
47
|
* @returns A promise resolving to `true` if at least one entry was updated, otherwise `false`.
|
|
48
48
|
*/
|
|
49
|
-
update(
|
|
49
|
+
update(file: string, one: boolean, arg: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
50
50
|
}
|
|
51
51
|
export default FileCpu;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/db",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"main": "dist/esm/index.js",
|
|
5
5
|
"types": "dist/esm/index.d.ts",
|
|
6
6
|
"description": "A simple file-based database management system with support for CRUD operations, custom queries, and graph structures.",
|
|
@@ -30,25 +30,22 @@
|
|
|
30
30
|
"typescript": "^5.7.2"
|
|
31
31
|
},
|
|
32
32
|
"scripts": {
|
|
33
|
-
"build
|
|
34
|
-
"build:cjs": "tsc -p tsconfig.cjs.json && tsc-alias -p tsconfig.cjs.json",
|
|
35
|
-
"build": "npm run build:esm && npm run build:cjs",
|
|
33
|
+
"build": "tsc && tsc-alias",
|
|
36
34
|
"prepare": "npm run build",
|
|
37
|
-
"postversion": "git push && git push --tags"
|
|
35
|
+
"postversion": "git push && git push --tags",
|
|
36
|
+
"build:cdn": "npm run build && cd cdn && node index.js"
|
|
38
37
|
},
|
|
39
38
|
"files": [
|
|
40
39
|
"dist"
|
|
41
40
|
],
|
|
42
41
|
"exports": {
|
|
43
42
|
".": {
|
|
44
|
-
"types": "./dist/
|
|
45
|
-
"import": "./dist/
|
|
46
|
-
"require": "./dist/cjs/index.js"
|
|
43
|
+
"types": "./dist/index.d.ts",
|
|
44
|
+
"import": "./dist/index.js"
|
|
47
45
|
},
|
|
48
46
|
"./*": {
|
|
49
|
-
"types": "./dist
|
|
50
|
-
"import": "./dist
|
|
51
|
-
"require": "./dist/cjs/*"
|
|
47
|
+
"types": "./dist/*",
|
|
48
|
+
"import": "./dist/*"
|
|
52
49
|
}
|
|
53
50
|
}
|
|
54
51
|
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
class CollectionManager {
|
|
4
|
-
db;
|
|
5
|
-
collection;
|
|
6
|
-
constructor(db, collection) {
|
|
7
|
-
this.db = db;
|
|
8
|
-
this.collection = collection;
|
|
9
|
-
}
|
|
10
|
-
/**
|
|
11
|
-
* Add data to a database.
|
|
12
|
-
*/
|
|
13
|
-
async add(data, id_gen = true) {
|
|
14
|
-
return await this.db.add(this.collection, data, id_gen);
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Find data in a database.
|
|
18
|
-
*/
|
|
19
|
-
async find(search, context = {}, options = {}, findOpts = {}) {
|
|
20
|
-
return await this.db.find(this.collection, search, context, options, findOpts);
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Find one data entry in a database.
|
|
24
|
-
*/
|
|
25
|
-
async findOne(search, context = {}, findOpts = {}) {
|
|
26
|
-
return await this.db.findOne(this.collection, search, context, findOpts);
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Update data in a database.
|
|
30
|
-
*/
|
|
31
|
-
async update(search, updater, context = {}) {
|
|
32
|
-
return await this.db.update(this.collection, search, updater, context);
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Update one data entry in a database.
|
|
36
|
-
*/
|
|
37
|
-
async updateOne(search, updater, context = {}) {
|
|
38
|
-
return await this.db.updateOne(this.collection, search, updater, context);
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Remove data from a database.
|
|
42
|
-
*/
|
|
43
|
-
async remove(search, context = {}) {
|
|
44
|
-
return await this.db.remove(this.collection, search, context);
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Remove one data entry from a database.
|
|
48
|
-
*/
|
|
49
|
-
async removeOne(search, context = {}) {
|
|
50
|
-
return await this.db.removeOne(this.collection, search, context);
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
54
|
-
*/
|
|
55
|
-
async updateOneOrAdd(search, updater, add_arg = {}, context = {}, id_gen = true) {
|
|
56
|
-
return await this.db.updateOneOrAdd(this.collection, search, updater, add_arg, context, id_gen);
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
exports.default = CollectionManager;
|
package/dist/cjs/action.d.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { Arg, Search, Updater } from "./types/arg.js";
|
|
2
|
-
import { DbFindOpts, DbOpts, FindOpts } from "./types/options.js";
|
|
3
|
-
import { Context } from "./types/types.js";
|
|
4
|
-
import { SearchOptions } from "./types/searchOpts.js";
|
|
5
|
-
import Data from "./types/data.js";
|
|
6
|
-
import FileCpu from "./types/fileCpu.js";
|
|
7
|
-
/**
|
|
8
|
-
* A class representing database actions on files.
|
|
9
|
-
* @class
|
|
10
|
-
*/
|
|
11
|
-
declare class dbActionC {
|
|
12
|
-
folder: string;
|
|
13
|
-
options: DbOpts;
|
|
14
|
-
fileCpu: FileCpu;
|
|
15
|
-
/**
|
|
16
|
-
* Creates a new instance of dbActionC.
|
|
17
|
-
* @constructor
|
|
18
|
-
* @param folder - The folder where database files are stored.
|
|
19
|
-
* @param options - The options object.
|
|
20
|
-
*/
|
|
21
|
-
constructor(folder: string, options: DbOpts, fileCpu: FileCpu);
|
|
22
|
-
_getCollectionPath(collection: string): string;
|
|
23
|
-
/**
|
|
24
|
-
* Get a list of available databases in the specified folder.
|
|
25
|
-
*/
|
|
26
|
-
getCollections(): string[];
|
|
27
|
-
/**
|
|
28
|
-
* Check and create the specified collection if it doesn't exist.
|
|
29
|
-
*/
|
|
30
|
-
checkCollection(collection: string): void;
|
|
31
|
-
/**
|
|
32
|
-
* Check if a collection exists.
|
|
33
|
-
*/
|
|
34
|
-
issetCollection(collection: string): boolean;
|
|
35
|
-
/**
|
|
36
|
-
* Add a new entry to the specified database.
|
|
37
|
-
*/
|
|
38
|
-
add(collection: string, arg: Arg, id_gen?: boolean): Promise<Arg>;
|
|
39
|
-
/**
|
|
40
|
-
* Find entries in the specified database based on search criteria.
|
|
41
|
-
*/
|
|
42
|
-
find(collection: string, arg: Search, context?: Context, options?: DbFindOpts, findOpts?: FindOpts): Promise<any[]>;
|
|
43
|
-
/**
|
|
44
|
-
* Find the first matching entry in the specified database based on search criteria.
|
|
45
|
-
*/
|
|
46
|
-
findOne(collection: string, arg: SearchOptions, context?: Context, findOpts?: FindOpts): Promise<Data>;
|
|
47
|
-
/**
|
|
48
|
-
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
49
|
-
*/
|
|
50
|
-
update(collection: string, arg: Search, updater: Updater, context?: {}): Promise<boolean>;
|
|
51
|
-
/**
|
|
52
|
-
* Update the first matching entry in the specified database based on search criteria and an updater function or object.
|
|
53
|
-
*/
|
|
54
|
-
updateOne(collection: string, arg: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
55
|
-
/**
|
|
56
|
-
* Remove entries from the specified database based on search criteria.
|
|
57
|
-
*/
|
|
58
|
-
remove(collection: string, arg: Search, context?: Context): Promise<boolean>;
|
|
59
|
-
/**
|
|
60
|
-
* Remove the first matching entry from the specified database based on search criteria.
|
|
61
|
-
*/
|
|
62
|
-
removeOne(collection: string, arg: Search, context?: Context): Promise<boolean>;
|
|
63
|
-
/**
|
|
64
|
-
* Removes a database collection from the file system.
|
|
65
|
-
*/
|
|
66
|
-
removeCollection(collection: string): void;
|
|
67
|
-
}
|
|
68
|
-
export default dbActionC;
|