@wxn0brp/db 0.1.1 → 0.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/dist/cjs/action.d.ts +4 -4
- package/dist/cjs/action.js +53 -35
- package/dist/cjs/client/database.js +1 -1
- package/dist/cjs/database.js +4 -4
- package/dist/cjs/file/customFileCpu.d.ts +2 -2
- package/dist/cjs/file/customFileCpu.js +4 -4
- package/dist/cjs/file/remove.d.ts +3 -3
- package/dist/cjs/file/remove.js +2 -17
- package/dist/cjs/file/update.d.ts +3 -3
- package/dist/cjs/file/update.js +2 -17
- package/dist/cjs/types/fileCpu.d.ts +4 -4
- package/dist/cjs/types/types.d.ts +0 -4
- package/dist/esm/action.d.ts +4 -4
- package/dist/esm/action.js +54 -36
- package/dist/esm/client/database.js +1 -1
- package/dist/esm/database.js +4 -4
- package/dist/esm/file/customFileCpu.d.ts +2 -2
- package/dist/esm/file/customFileCpu.js +4 -4
- package/dist/esm/file/remove.d.ts +3 -3
- package/dist/esm/file/remove.js +3 -18
- package/dist/esm/file/update.d.ts +3 -3
- package/dist/esm/file/update.js +3 -18
- package/dist/esm/types/fileCpu.d.ts +4 -4
- package/dist/esm/types/types.d.ts +0 -4
- package/package.json +5 -5
package/dist/cjs/action.d.ts
CHANGED
|
@@ -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;
|
package/dist/cjs/action.js
CHANGED
|
@@ -35,8 +35,9 @@ class dbActionC {
|
|
|
35
35
|
/**
|
|
36
36
|
* Get a list of available databases in the specified folder.
|
|
37
37
|
*/
|
|
38
|
-
getCollections() {
|
|
39
|
-
const
|
|
38
|
+
async getCollections() {
|
|
39
|
+
const allCollections = await fs_1.promises.readdir(this.folder, { recursive: true, withFileTypes: true });
|
|
40
|
+
const collections = allCollections
|
|
40
41
|
.filter(dirent => dirent.isDirectory())
|
|
41
42
|
.map(dirent => {
|
|
42
43
|
if (dirent.parentPath === this.folder)
|
|
@@ -48,17 +49,24 @@ class dbActionC {
|
|
|
48
49
|
/**
|
|
49
50
|
* Check and create the specified collection if it doesn't exist.
|
|
50
51
|
*/
|
|
51
|
-
checkCollection(collection) {
|
|
52
|
+
async checkCollection(collection) {
|
|
53
|
+
if (await this.issetCollection(collection))
|
|
54
|
+
return;
|
|
52
55
|
const cpath = this._getCollectionPath(collection);
|
|
53
|
-
|
|
54
|
-
(0, fs_1.mkdirSync)(cpath, { recursive: true });
|
|
56
|
+
await fs_1.promises.mkdir(cpath, { recursive: true });
|
|
55
57
|
}
|
|
56
58
|
/**
|
|
57
59
|
* Check if a collection exists.
|
|
58
60
|
*/
|
|
59
|
-
issetCollection(collection) {
|
|
60
|
-
const path = this.
|
|
61
|
-
|
|
61
|
+
async issetCollection(collection) {
|
|
62
|
+
const path = this._getCollectionPath(collection);
|
|
63
|
+
try {
|
|
64
|
+
await fs_1.promises.access(path);
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
62
70
|
}
|
|
63
71
|
/**
|
|
64
72
|
* Add a new entry to the specified database.
|
|
@@ -66,7 +74,7 @@ class dbActionC {
|
|
|
66
74
|
async add(collection, arg, id_gen = true) {
|
|
67
75
|
this.checkCollection(collection);
|
|
68
76
|
const cpath = this._getCollectionPath(collection);
|
|
69
|
-
const file = cpath + getLastFile(cpath, this.options.maxFileSize);
|
|
77
|
+
const file = cpath + await getLastFile(cpath, this.options.maxFileSize);
|
|
70
78
|
if (id_gen)
|
|
71
79
|
arg._id = arg._id || (0, gen_1.default)();
|
|
72
80
|
await this.fileCpu.add(file, arg);
|
|
@@ -80,7 +88,7 @@ class dbActionC {
|
|
|
80
88
|
options.max = options.max || -1;
|
|
81
89
|
this.checkCollection(collection);
|
|
82
90
|
const cpath = this._getCollectionPath(collection);
|
|
83
|
-
const files = getSortedFiles(cpath)
|
|
91
|
+
const files = await getSortedFiles(cpath);
|
|
84
92
|
if (options.reverse)
|
|
85
93
|
files.reverse();
|
|
86
94
|
let datas = [];
|
|
@@ -111,7 +119,7 @@ class dbActionC {
|
|
|
111
119
|
async findOne(collection, arg, context = {}, findOpts = {}) {
|
|
112
120
|
this.checkCollection(collection);
|
|
113
121
|
const cpath = this._getCollectionPath(collection);
|
|
114
|
-
const files = getSortedFiles(cpath)
|
|
122
|
+
const files = await getSortedFiles(cpath);
|
|
115
123
|
for (let f of files) {
|
|
116
124
|
let data = await this.fileCpu.findOne(cpath + f, arg, context, findOpts);
|
|
117
125
|
if (data)
|
|
@@ -124,69 +132,79 @@ class dbActionC {
|
|
|
124
132
|
*/
|
|
125
133
|
async update(collection, arg, updater, context = {}) {
|
|
126
134
|
this.checkCollection(collection);
|
|
127
|
-
return await this.fileCpu.update(this.
|
|
135
|
+
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), false, arg, updater, context);
|
|
128
136
|
}
|
|
129
137
|
/**
|
|
130
138
|
* Update the first matching entry in the specified database based on search criteria and an updater function or object.
|
|
131
139
|
*/
|
|
132
140
|
async updateOne(collection, arg, updater, context = {}) {
|
|
133
141
|
this.checkCollection(collection);
|
|
134
|
-
return await this.fileCpu.update(this.
|
|
142
|
+
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), true, arg, updater, context);
|
|
135
143
|
}
|
|
136
144
|
/**
|
|
137
145
|
* Remove entries from the specified database based on search criteria.
|
|
138
146
|
*/
|
|
139
147
|
async remove(collection, arg, context = {}) {
|
|
140
148
|
this.checkCollection(collection);
|
|
141
|
-
return await this.fileCpu.remove(this.
|
|
149
|
+
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), false, arg, context);
|
|
142
150
|
}
|
|
143
151
|
/**
|
|
144
152
|
* Remove the first matching entry from the specified database based on search criteria.
|
|
145
153
|
*/
|
|
146
154
|
async removeOne(collection, arg, context = {}) {
|
|
147
155
|
this.checkCollection(collection);
|
|
148
|
-
return await this.fileCpu.remove(this.
|
|
156
|
+
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), true, arg, context);
|
|
149
157
|
}
|
|
150
158
|
/**
|
|
151
159
|
* Removes a database collection from the file system.
|
|
152
160
|
*/
|
|
153
|
-
removeCollection(collection) {
|
|
154
|
-
|
|
161
|
+
async removeCollection(collection) {
|
|
162
|
+
await fs_1.promises.rm(this.folder + "/" + collection, { recursive: true, force: true });
|
|
155
163
|
}
|
|
156
164
|
}
|
|
157
165
|
/**
|
|
158
166
|
* Get the last file in the specified directory.
|
|
159
167
|
*/
|
|
160
|
-
function getLastFile(path, maxFileSize = 1024 * 1024) {
|
|
168
|
+
async function getLastFile(path, maxFileSize = 1024 * 1024) {
|
|
161
169
|
if (!(0, fs_1.existsSync)(path))
|
|
162
170
|
(0, fs_1.mkdirSync)(path, { recursive: true });
|
|
163
|
-
const files = getSortedFiles(path);
|
|
171
|
+
const files = await getSortedFiles(path);
|
|
164
172
|
if (files.length == 0) {
|
|
165
|
-
|
|
173
|
+
await fs_1.promises.writeFile(path + "/1.db", "");
|
|
166
174
|
return "1.db";
|
|
167
175
|
}
|
|
168
176
|
const last = files[files.length - 1];
|
|
169
|
-
const info = path + "/" + last
|
|
177
|
+
const info = path + "/" + last;
|
|
170
178
|
if ((0, fs_1.statSync)(info).size < maxFileSize)
|
|
171
|
-
return last
|
|
172
|
-
const num = last.
|
|
173
|
-
|
|
179
|
+
return last;
|
|
180
|
+
const num = parseInt(last.replace(".db", ""), 10) + 1;
|
|
181
|
+
await fs_1.promises.writeFile(path + "/" + num + ".db", "");
|
|
174
182
|
return num + ".db";
|
|
175
183
|
}
|
|
176
184
|
/**
|
|
177
185
|
* Get all files in a directory sorted by name.
|
|
178
186
|
*/
|
|
179
|
-
function getSortedFiles(
|
|
180
|
-
const files =
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
};
|
|
187
|
+
async function getSortedFiles(folder) {
|
|
188
|
+
const files = await fs_1.promises.readdir(folder, { withFileTypes: true });
|
|
189
|
+
return files
|
|
190
|
+
.filter(file => file.isFile() && !file.name.endsWith(".tmp"))
|
|
191
|
+
.map(file => file.name)
|
|
192
|
+
.filter(name => /^\d+\.db$/.test(name))
|
|
193
|
+
.sort((a, b) => {
|
|
194
|
+
const numA = parseInt(a, 10);
|
|
195
|
+
const numB = parseInt(b, 10);
|
|
196
|
+
return numA - numB;
|
|
190
197
|
});
|
|
191
198
|
}
|
|
199
|
+
async function operationUpdater(cpath, worker, one, ...args) {
|
|
200
|
+
const files = await getSortedFiles(cpath);
|
|
201
|
+
let update = false;
|
|
202
|
+
for (const file of files) {
|
|
203
|
+
const updated = await worker(cpath + file, one, ...args);
|
|
204
|
+
if (one && updated)
|
|
205
|
+
break;
|
|
206
|
+
update = update || updated;
|
|
207
|
+
}
|
|
208
|
+
return update;
|
|
209
|
+
}
|
|
192
210
|
exports.default = dbActionC;
|
|
@@ -107,7 +107,7 @@ class DataBaseRemote {
|
|
|
107
107
|
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
108
108
|
*/
|
|
109
109
|
async updateOneOrAdd(collection, search, arg, add_arg = {}, context = {}, id_gen = true) {
|
|
110
|
-
return await this._request("updateOneOrAdd", [collection, search, arg, add_arg,
|
|
110
|
+
return await this._request("updateOneOrAdd", [collection, search, arg, add_arg, context, id_gen]);
|
|
111
111
|
}
|
|
112
112
|
/**
|
|
113
113
|
* Removes a database collection from the file system.
|
package/dist/cjs/database.js
CHANGED
|
@@ -30,19 +30,19 @@ class DataBase {
|
|
|
30
30
|
* Get the names of all available databases.
|
|
31
31
|
*/
|
|
32
32
|
async getCollections() {
|
|
33
|
-
return this.dbAction.getCollections();
|
|
33
|
+
return await this.dbAction.getCollections();
|
|
34
34
|
}
|
|
35
35
|
/**
|
|
36
36
|
* Check and create the specified collection if it doesn't exist.
|
|
37
37
|
*/
|
|
38
38
|
async checkCollection(collection) {
|
|
39
|
-
this.dbAction.checkCollection(collection);
|
|
39
|
+
await this.dbAction.checkCollection(collection);
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* Check if a collection exists.
|
|
43
43
|
*/
|
|
44
44
|
async issetCollection(collection) {
|
|
45
|
-
return this.dbAction.issetCollection(collection);
|
|
45
|
+
return await this.dbAction.issetCollection(collection);
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
48
|
* Add data to a database.
|
|
@@ -107,7 +107,7 @@ class DataBase {
|
|
|
107
107
|
* Removes a database collection from the file system.
|
|
108
108
|
*/
|
|
109
109
|
async removeCollection(collection) {
|
|
110
|
-
this.dbAction.removeCollection(collection);
|
|
110
|
+
await this.dbAction.removeCollection(collection);
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
exports.default = DataBase;
|
|
@@ -13,7 +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
|
-
remove(
|
|
17
|
-
update(
|
|
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>;
|
|
18
18
|
}
|
|
19
19
|
export default CustomFileCpu;
|
|
@@ -37,8 +37,8 @@ class CustomFileCpu {
|
|
|
37
37
|
const result = entries.find(entry => typeof arg === "function" ? arg(entry, context) : (0, hasFieldsAdvanced_1.default)(entry, arg));
|
|
38
38
|
return result ? (0, updateFindObject_1.default)(result, findOpts) : false;
|
|
39
39
|
}
|
|
40
|
-
async remove(
|
|
41
|
-
|
|
40
|
+
async remove(file, one, arg, context = {}) {
|
|
41
|
+
file = (0, utils_1.pathRepair)(file);
|
|
42
42
|
let entries = await this._readFile(file);
|
|
43
43
|
let removed = false;
|
|
44
44
|
entries = entries.filter(entry => {
|
|
@@ -56,8 +56,8 @@ class CustomFileCpu {
|
|
|
56
56
|
await this._writeFile(file, entries);
|
|
57
57
|
return true;
|
|
58
58
|
}
|
|
59
|
-
async update(
|
|
60
|
-
|
|
59
|
+
async update(file, one, arg, updater, context = {}) {
|
|
60
|
+
file = (0, utils_1.pathRepair)(file);
|
|
61
61
|
let entries = await this._readFile(file);
|
|
62
62
|
let updated = false;
|
|
63
63
|
entries = entries.map(entry => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Search } from "../types/arg.js";
|
|
2
2
|
import { Context } from "../types/types.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Removes entries from a file based on search criteria.
|
|
5
5
|
*/
|
|
6
|
-
declare function
|
|
7
|
-
export default
|
|
6
|
+
declare function removeWorker(file: string, one: boolean, search: Search, context?: Context): Promise<boolean>;
|
|
7
|
+
export default removeWorker;
|
package/dist/cjs/file/remove.js
CHANGED
|
@@ -10,7 +10,7 @@ const hasFieldsAdvanced_1 = __importDefault(require("../utils/hasFieldsAdvanced.
|
|
|
10
10
|
/**
|
|
11
11
|
* Removes entries from a file based on search criteria.
|
|
12
12
|
*/
|
|
13
|
-
async function removeWorker(file, search, context = {}
|
|
13
|
+
async function removeWorker(file, one, search, context = {}) {
|
|
14
14
|
file = (0, utils_1.pathRepair)(file);
|
|
15
15
|
if (!(0, fs_1.existsSync)(file)) {
|
|
16
16
|
await fs_1.promises.writeFile(file, "");
|
|
@@ -43,19 +43,4 @@ async function removeWorker(file, search, context = {}, one = false) {
|
|
|
43
43
|
await fs_1.promises.writeFile(file + ".tmp", "");
|
|
44
44
|
return removed;
|
|
45
45
|
}
|
|
46
|
-
|
|
47
|
-
* Asynchronously removes entries from a file based on search criteria.
|
|
48
|
-
*/
|
|
49
|
-
async function remove(cpath, arg, context = {}, one = false) {
|
|
50
|
-
let files = (0, fs_1.readdirSync)(cpath).filter(file => !/\.tmp$/.test(file));
|
|
51
|
-
files.reverse();
|
|
52
|
-
let remove = false;
|
|
53
|
-
for (const file of files) {
|
|
54
|
-
const removed = await removeWorker(cpath + file, arg, context, one);
|
|
55
|
-
if (one && removed)
|
|
56
|
-
break;
|
|
57
|
-
remove = remove || removed;
|
|
58
|
-
}
|
|
59
|
-
return remove;
|
|
60
|
-
}
|
|
61
|
-
exports.default = remove;
|
|
46
|
+
exports.default = removeWorker;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Context } from "../types/types.js";
|
|
2
2
|
import { Search, Updater } from "../types/arg.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Updates a file based on search criteria and an updater function or object.
|
|
5
5
|
*/
|
|
6
|
-
declare function
|
|
7
|
-
export default
|
|
6
|
+
declare function updateWorker(file: string, one: boolean, search: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
7
|
+
export default updateWorker;
|
package/dist/cjs/file/update.js
CHANGED
|
@@ -11,7 +11,7 @@ const updateObject_1 = __importDefault(require("../utils/updateObject.js"));
|
|
|
11
11
|
/**
|
|
12
12
|
* Updates a file based on search criteria and an updater function or object.
|
|
13
13
|
*/
|
|
14
|
-
async function updateWorker(file, search, updater, context = {}
|
|
14
|
+
async function updateWorker(file, one, search, updater, context = {}) {
|
|
15
15
|
file = (0, utils_1.pathRepair)(file);
|
|
16
16
|
if (!(0, fs_1.existsSync)(file)) {
|
|
17
17
|
await fs_1.promises.writeFile(file, "");
|
|
@@ -52,19 +52,4 @@ async function updateWorker(file, search, updater, context = {}, one = false) {
|
|
|
52
52
|
await fs_1.promises.writeFile(file + ".tmp", "");
|
|
53
53
|
return updated;
|
|
54
54
|
}
|
|
55
|
-
|
|
56
|
-
* Asynchronously updates entries in a file based on search criteria and an updater function or object.
|
|
57
|
-
*/
|
|
58
|
-
async function update(cpath, arg, updater, context = {}, one = false) {
|
|
59
|
-
let files = (0, fs_1.readdirSync)(cpath).filter(file => !/\.tmp$/.test(file));
|
|
60
|
-
files.reverse();
|
|
61
|
-
let update = false;
|
|
62
|
-
for (const file of files) {
|
|
63
|
-
const updated = await updateWorker(cpath + file, arg, updater, context, one);
|
|
64
|
-
if (one && updated)
|
|
65
|
-
return true;
|
|
66
|
-
update = update || updated;
|
|
67
|
-
}
|
|
68
|
-
return update;
|
|
69
|
-
}
|
|
70
|
-
exports.default = update;
|
|
55
|
+
exports.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/dist/esm/action.d.ts
CHANGED
|
@@ -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;
|
package/dist/esm/action.js
CHANGED
|
@@ -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.
|
package/dist/esm/database.js
CHANGED
|
@@ -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,7 +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
|
-
remove(
|
|
17
|
-
update(
|
|
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>;
|
|
18
18
|
}
|
|
19
19
|
export default CustomFileCpu;
|
|
@@ -32,8 +32,8 @@ class CustomFileCpu {
|
|
|
32
32
|
const result = entries.find(entry => typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg));
|
|
33
33
|
return result ? updateFindObject(result, findOpts) : false;
|
|
34
34
|
}
|
|
35
|
-
async remove(
|
|
36
|
-
|
|
35
|
+
async remove(file, one, arg, context = {}) {
|
|
36
|
+
file = pathRepair(file);
|
|
37
37
|
let entries = await this._readFile(file);
|
|
38
38
|
let removed = false;
|
|
39
39
|
entries = entries.filter(entry => {
|
|
@@ -51,8 +51,8 @@ class CustomFileCpu {
|
|
|
51
51
|
await this._writeFile(file, entries);
|
|
52
52
|
return true;
|
|
53
53
|
}
|
|
54
|
-
async update(
|
|
55
|
-
|
|
54
|
+
async update(file, one, arg, updater, context = {}) {
|
|
55
|
+
file = pathRepair(file);
|
|
56
56
|
let entries = await this._readFile(file);
|
|
57
57
|
let updated = false;
|
|
58
58
|
entries = entries.map(entry => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Search } from "../types/arg.js";
|
|
2
2
|
import { Context } from "../types/types.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Removes entries from a file based on search criteria.
|
|
5
5
|
*/
|
|
6
|
-
declare function
|
|
7
|
-
export default
|
|
6
|
+
declare function removeWorker(file: string, one: boolean, search: Search, context?: Context): Promise<boolean>;
|
|
7
|
+
export default removeWorker;
|
package/dist/esm/file/remove.js
CHANGED
|
@@ -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;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Context } from "../types/types.js";
|
|
2
2
|
import { Search, Updater } from "../types/arg.js";
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Updates a file based on search criteria and an updater function or object.
|
|
5
5
|
*/
|
|
6
|
-
declare function
|
|
7
|
-
export default
|
|
6
|
+
declare function updateWorker(file: string, one: boolean, search: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
7
|
+
export default updateWorker;
|
package/dist/esm/file/update.js
CHANGED
|
@@ -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.2.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.",
|
|
@@ -41,14 +41,14 @@
|
|
|
41
41
|
],
|
|
42
42
|
"exports": {
|
|
43
43
|
".": {
|
|
44
|
+
"types": "./dist/esm/index.d.ts",
|
|
44
45
|
"import": "./dist/esm/index.js",
|
|
45
|
-
"require": "./dist/cjs/index.js"
|
|
46
|
-
"types": "./dist/esm/index.d.ts"
|
|
46
|
+
"require": "./dist/cjs/index.js"
|
|
47
47
|
},
|
|
48
48
|
"./*": {
|
|
49
|
+
"types": "./dist/esm/*",
|
|
49
50
|
"import": "./dist/esm/*",
|
|
50
|
-
"require": "./dist/cjs/*"
|
|
51
|
-
"types": "./dist/esm/*"
|
|
51
|
+
"require": "./dist/cjs/*"
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
}
|