@wxn0brp/db 0.7.6 → 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/actions/action.d.ts +18 -18
- package/dist/actions/action.js +49 -45
- package/dist/actions/memory.d.ts +14 -26
- package/dist/actions/memory.js +36 -45
- package/dist/base/actions.d.ts +19 -0
- package/dist/base/actions.js +64 -0
- package/dist/base/db.d.ts +23 -0
- package/dist/base/db.js +64 -0
- package/dist/client/valthera.d.ts +1 -0
- package/dist/client/valthera.js +2 -0
- package/dist/db/valthera.d.ts +2 -1
- package/dist/db/valthera.js +16 -37
- package/dist/file/utils.d.ts +4 -4
- package/dist/file/utils.js +73 -8
- package/dist/helpers/updateOneOrAdd.d.ts +2 -0
- package/dist/helpers/updateOneOrAdd.js +18 -0
- package/dist/index.d.ts +2 -11
- package/dist/types/export.d.ts +11 -0
- package/dist/types/export.js +1 -0
- package/dist/types/query.d.ts +17 -0
- package/dist/types/query.js +1 -0
- package/dist/types/valthera.d.ts +18 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +1 -0
- package/package.json +2 -3
package/dist/actions/action.d.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { Arg, Search, Updater } from "../types/arg.js";
|
|
1
|
+
import dbActionBase from "../base/actions.js";
|
|
3
2
|
import Data from "../types/data.js";
|
|
4
3
|
import FileCpu from "../types/fileCpu.js";
|
|
5
|
-
import { DbOpts
|
|
6
|
-
import {
|
|
7
|
-
import { Transaction } from "../types/transactions.js";
|
|
4
|
+
import { DbOpts } from "../types/options.js";
|
|
5
|
+
import { VQuery } from "../types/query.js";
|
|
8
6
|
/**
|
|
9
7
|
* A class representing database actions on files.
|
|
10
8
|
* @class
|
|
11
9
|
*/
|
|
12
|
-
declare class dbActionC {
|
|
10
|
+
declare class dbActionC extends dbActionBase {
|
|
13
11
|
folder: string;
|
|
14
12
|
options: DbOpts;
|
|
15
13
|
fileCpu: FileCpu;
|
|
@@ -28,47 +26,49 @@ declare class dbActionC {
|
|
|
28
26
|
/**
|
|
29
27
|
* Check and create the specified collection if it doesn't exist.
|
|
30
28
|
*/
|
|
31
|
-
checkCollection(collection:
|
|
29
|
+
checkCollection({ collection }: VQuery): Promise<boolean>;
|
|
32
30
|
/**
|
|
33
31
|
* Check if a collection exists.
|
|
34
32
|
*/
|
|
35
|
-
issetCollection(collection:
|
|
33
|
+
issetCollection({ collection }: VQuery): Promise<boolean>;
|
|
36
34
|
/**
|
|
37
35
|
* Add a new entry to the specified database.
|
|
38
36
|
*/
|
|
39
|
-
add(collection
|
|
37
|
+
add({ collection, data, id_gen }: VQuery): Promise<import("../types/arg.js").Arg>;
|
|
40
38
|
/**
|
|
41
39
|
* Find entries in the specified database based on search criteria.
|
|
42
40
|
*/
|
|
43
|
-
find(collection
|
|
41
|
+
find({ collection, search, context, dbFindOpts, findOpts }: VQuery): Promise<any[]>;
|
|
44
42
|
/**
|
|
45
43
|
* Find the first matching entry in the specified database based on search criteria.
|
|
46
44
|
*/
|
|
47
|
-
findOne(collection
|
|
48
|
-
findStream(collection
|
|
45
|
+
findOne({ collection, search, context, findOpts }: VQuery): Promise<Data>;
|
|
46
|
+
findStream({ collection, search, context, findOpts, limit }: VQuery): AsyncGenerator<any>;
|
|
49
47
|
/**
|
|
50
48
|
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
51
49
|
*/
|
|
52
|
-
update(collection
|
|
50
|
+
update({ collection, search, updater, context }: VQuery): Promise<boolean>;
|
|
53
51
|
/**
|
|
54
52
|
* Update the first matching entry in the specified database based on search criteria and an updater function or object.
|
|
55
53
|
*/
|
|
56
|
-
updateOne(collection
|
|
54
|
+
updateOne({ collection, search, updater, context }: VQuery): Promise<boolean>;
|
|
57
55
|
/**
|
|
58
56
|
* Remove entries from the specified database based on search criteria.
|
|
59
57
|
*/
|
|
60
|
-
remove(collection
|
|
58
|
+
remove({ collection, search, context }: VQuery): Promise<boolean>;
|
|
61
59
|
/**
|
|
62
60
|
* Remove the first matching entry from the specified database based on search criteria.
|
|
63
61
|
*/
|
|
64
|
-
removeOne(collection
|
|
62
|
+
removeOne({ collection, search, context }: VQuery): Promise<boolean>;
|
|
65
63
|
/**
|
|
66
64
|
* Removes a database collection from the file system.
|
|
67
65
|
*/
|
|
68
|
-
removeCollection(collection
|
|
66
|
+
removeCollection({ collection }: {
|
|
67
|
+
collection: any;
|
|
68
|
+
}): Promise<boolean>;
|
|
69
69
|
/**
|
|
70
70
|
* Apply a series of transactions to a database collection.
|
|
71
71
|
*/
|
|
72
|
-
transaction(collection
|
|
72
|
+
transaction({ collection, transaction }: VQuery): Promise<boolean>;
|
|
73
73
|
}
|
|
74
74
|
export default dbActionC;
|
package/dist/actions/action.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { promises } from "fs";
|
|
2
|
-
import
|
|
1
|
+
import { existsSync, mkdirSync, promises, statSync } from "fs";
|
|
2
|
+
import dbActionBase from "../base/actions.js";
|
|
3
3
|
import gen from "../helpers/gen.js";
|
|
4
4
|
/**
|
|
5
5
|
* A class representing database actions on files.
|
|
6
6
|
* @class
|
|
7
7
|
*/
|
|
8
|
-
class dbActionC {
|
|
8
|
+
class dbActionC extends dbActionBase {
|
|
9
9
|
folder;
|
|
10
10
|
options;
|
|
11
11
|
fileCpu;
|
|
@@ -16,6 +16,7 @@ class dbActionC {
|
|
|
16
16
|
* @param options - The options object.
|
|
17
17
|
*/
|
|
18
18
|
constructor(folder, options, fileCpu) {
|
|
19
|
+
super();
|
|
19
20
|
this.folder = folder;
|
|
20
21
|
this.options = {
|
|
21
22
|
maxFileSize: 2 * 1024 * 1024, //2 MB
|
|
@@ -45,16 +46,17 @@ class dbActionC {
|
|
|
45
46
|
/**
|
|
46
47
|
* Check and create the specified collection if it doesn't exist.
|
|
47
48
|
*/
|
|
48
|
-
async checkCollection(collection) {
|
|
49
|
+
async checkCollection({ collection }) {
|
|
49
50
|
if (await this.issetCollection(collection))
|
|
50
51
|
return;
|
|
51
52
|
const cpath = this._getCollectionPath(collection);
|
|
52
53
|
await promises.mkdir(cpath, { recursive: true });
|
|
54
|
+
return true;
|
|
53
55
|
}
|
|
54
56
|
/**
|
|
55
57
|
* Check if a collection exists.
|
|
56
58
|
*/
|
|
57
|
-
async issetCollection(collection) {
|
|
59
|
+
async issetCollection({ collection }) {
|
|
58
60
|
const path = this._getCollectionPath(collection);
|
|
59
61
|
try {
|
|
60
62
|
await promises.access(path);
|
|
@@ -67,44 +69,44 @@ class dbActionC {
|
|
|
67
69
|
/**
|
|
68
70
|
* Add a new entry to the specified database.
|
|
69
71
|
*/
|
|
70
|
-
async add(collection,
|
|
71
|
-
await this.checkCollection(
|
|
72
|
+
async add({ collection, data, id_gen = true }) {
|
|
73
|
+
await this.checkCollection(arguments[0]);
|
|
72
74
|
const cpath = this._getCollectionPath(collection);
|
|
73
75
|
const file = cpath + await getLastFile(cpath, this.options.maxFileSize);
|
|
74
76
|
if (id_gen)
|
|
75
|
-
|
|
76
|
-
await this.fileCpu.add(file,
|
|
77
|
-
return
|
|
77
|
+
data._id = data._id || gen();
|
|
78
|
+
await this.fileCpu.add(file, data);
|
|
79
|
+
return data;
|
|
78
80
|
}
|
|
79
81
|
/**
|
|
80
82
|
* Find entries in the specified database based on search criteria.
|
|
81
83
|
*/
|
|
82
|
-
async find(collection,
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
await this.checkCollection(
|
|
84
|
+
async find({ collection, search, context = {}, dbFindOpts = {}, findOpts = {} }) {
|
|
85
|
+
dbFindOpts.reverse = dbFindOpts.reverse || false;
|
|
86
|
+
dbFindOpts.max = dbFindOpts.max || -1;
|
|
87
|
+
await this.checkCollection(arguments[0]);
|
|
86
88
|
const cpath = this._getCollectionPath(collection);
|
|
87
89
|
const files = await getSortedFiles(cpath);
|
|
88
|
-
if (
|
|
90
|
+
if (dbFindOpts.reverse)
|
|
89
91
|
files.reverse();
|
|
90
92
|
let datas = [];
|
|
91
93
|
let totalEntries = 0;
|
|
92
94
|
for (let f of files) {
|
|
93
|
-
let data = await this.fileCpu.find(cpath + f,
|
|
94
|
-
if (
|
|
95
|
+
let data = await this.fileCpu.find(cpath + f, search, context, findOpts);
|
|
96
|
+
if (dbFindOpts.reverse)
|
|
95
97
|
data.reverse();
|
|
96
|
-
if (
|
|
97
|
-
if (totalEntries + data.length >
|
|
98
|
-
let remainingEntries =
|
|
98
|
+
if (dbFindOpts.max !== -1) {
|
|
99
|
+
if (totalEntries + data.length > dbFindOpts.max) {
|
|
100
|
+
let remainingEntries = dbFindOpts.max - totalEntries;
|
|
99
101
|
data = data.slice(0, remainingEntries);
|
|
100
|
-
totalEntries =
|
|
102
|
+
totalEntries = dbFindOpts.max;
|
|
101
103
|
}
|
|
102
104
|
else {
|
|
103
105
|
totalEntries += data.length;
|
|
104
106
|
}
|
|
105
107
|
}
|
|
106
108
|
datas = datas.concat(data);
|
|
107
|
-
if (
|
|
109
|
+
if (dbFindOpts.max !== -1 && totalEntries >= dbFindOpts.max)
|
|
108
110
|
break;
|
|
109
111
|
}
|
|
110
112
|
return datas;
|
|
@@ -112,24 +114,24 @@ class dbActionC {
|
|
|
112
114
|
/**
|
|
113
115
|
* Find the first matching entry in the specified database based on search criteria.
|
|
114
116
|
*/
|
|
115
|
-
async findOne(collection,
|
|
116
|
-
await this.checkCollection(
|
|
117
|
+
async findOne({ collection, search, context = {}, findOpts = {} }) {
|
|
118
|
+
await this.checkCollection(arguments[0]);
|
|
117
119
|
const cpath = this._getCollectionPath(collection);
|
|
118
120
|
const files = await getSortedFiles(cpath);
|
|
119
121
|
for (let f of files) {
|
|
120
|
-
let data = await this.fileCpu.findOne(cpath + f,
|
|
122
|
+
let data = await this.fileCpu.findOne(cpath + f, search, context, findOpts);
|
|
121
123
|
if (data)
|
|
122
124
|
return data;
|
|
123
125
|
}
|
|
124
126
|
return null;
|
|
125
127
|
}
|
|
126
|
-
async *findStream(collection,
|
|
127
|
-
await this.checkCollection(
|
|
128
|
+
async *findStream({ collection, search, context = {}, findOpts = {}, limit = -1 }) {
|
|
129
|
+
await this.checkCollection(arguments[0]);
|
|
128
130
|
const cpath = this._getCollectionPath(collection);
|
|
129
131
|
const files = await getSortedFiles(cpath);
|
|
130
132
|
let count = 0;
|
|
131
133
|
for (let f of files) {
|
|
132
|
-
for await (const data of this.fileCpu.findStream(cpath + f,
|
|
134
|
+
for await (const data of this.fileCpu.findStream(cpath + f, search, context, findOpts, limit)) {
|
|
133
135
|
yield data;
|
|
134
136
|
count++;
|
|
135
137
|
if (limit !== -1 && count >= limit) {
|
|
@@ -141,53 +143,55 @@ class dbActionC {
|
|
|
141
143
|
/**
|
|
142
144
|
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
143
145
|
*/
|
|
144
|
-
async update(collection,
|
|
145
|
-
await this.checkCollection(
|
|
146
|
-
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), false,
|
|
146
|
+
async update({ collection, search, updater, context = {} }) {
|
|
147
|
+
await this.checkCollection(arguments[0]);
|
|
148
|
+
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), false, search, updater, context);
|
|
147
149
|
}
|
|
148
150
|
/**
|
|
149
151
|
* Update the first matching entry in the specified database based on search criteria and an updater function or object.
|
|
150
152
|
*/
|
|
151
|
-
async updateOne(collection,
|
|
152
|
-
await this.checkCollection(
|
|
153
|
-
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), true,
|
|
153
|
+
async updateOne({ collection, search, updater, context = {} }) {
|
|
154
|
+
await this.checkCollection(arguments[0]);
|
|
155
|
+
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), true, search, updater, context);
|
|
154
156
|
}
|
|
155
157
|
/**
|
|
156
158
|
* Remove entries from the specified database based on search criteria.
|
|
157
159
|
*/
|
|
158
|
-
async remove(collection,
|
|
159
|
-
await this.checkCollection(
|
|
160
|
-
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), false,
|
|
160
|
+
async remove({ collection, search, context = {} }) {
|
|
161
|
+
await this.checkCollection(arguments[0]);
|
|
162
|
+
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), false, search, context);
|
|
161
163
|
}
|
|
162
164
|
/**
|
|
163
165
|
* Remove the first matching entry from the specified database based on search criteria.
|
|
164
166
|
*/
|
|
165
|
-
async removeOne(collection,
|
|
166
|
-
await this.checkCollection(
|
|
167
|
-
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), true,
|
|
167
|
+
async removeOne({ collection, search, context = {} }) {
|
|
168
|
+
await this.checkCollection(arguments[0]);
|
|
169
|
+
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), true, search, context);
|
|
168
170
|
}
|
|
169
171
|
/**
|
|
170
172
|
* Removes a database collection from the file system.
|
|
171
173
|
*/
|
|
172
|
-
async removeCollection(collection) {
|
|
174
|
+
async removeCollection({ collection }) {
|
|
173
175
|
await promises.rm(this.folder + "/" + collection, { recursive: true, force: true });
|
|
176
|
+
return true;
|
|
174
177
|
}
|
|
175
178
|
/**
|
|
176
179
|
* Apply a series of transactions to a database collection.
|
|
177
180
|
*/
|
|
178
|
-
async transaction(collection,
|
|
179
|
-
await this.checkCollection(
|
|
181
|
+
async transaction({ collection, transaction }) {
|
|
182
|
+
await this.checkCollection(arguments[0]);
|
|
180
183
|
const files = await getSortedFiles(this._getCollectionPath(collection));
|
|
181
184
|
if (files.length == 0) {
|
|
182
185
|
await promises.writeFile(this._getCollectionPath(collection) + "1.db", "");
|
|
183
186
|
files.push("1.db");
|
|
184
187
|
}
|
|
185
188
|
for (const file of files) {
|
|
186
|
-
await this.fileCpu.transactions(this._getCollectionPath(collection) + file,
|
|
189
|
+
await this.fileCpu.transactions(this._getCollectionPath(collection) + file, transaction);
|
|
187
190
|
}
|
|
188
191
|
console.log("Transactions applied successfully.");
|
|
189
192
|
console.log("Files:", files);
|
|
190
|
-
console.log("Transactions:",
|
|
193
|
+
console.log("Transactions:", transaction);
|
|
194
|
+
return true;
|
|
191
195
|
}
|
|
192
196
|
}
|
|
193
197
|
/**
|
package/dist/actions/memory.d.ts
CHANGED
|
@@ -1,13 +1,10 @@
|
|
|
1
|
+
import dbActionBase from "../base/actions.js";
|
|
1
2
|
import Valthera from "../db/valthera.js";
|
|
2
|
-
import { Arg, Search, Updater } from "../types/arg.js";
|
|
3
3
|
import Data from "../types/data.js";
|
|
4
4
|
import FileCpu from "../types/fileCpu.js";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
import { Context } from "../types/types.js";
|
|
9
|
-
import dbActionC from "./action.js";
|
|
10
|
-
export declare class MemoryAction implements dbActionC {
|
|
5
|
+
import { DbOpts } from "../types/options.js";
|
|
6
|
+
import { VQuery } from "../types/query.js";
|
|
7
|
+
export declare class MemoryAction extends dbActionBase {
|
|
11
8
|
folder: string;
|
|
12
9
|
options: DbOpts;
|
|
13
10
|
fileCpu: FileCpu;
|
|
@@ -29,52 +26,43 @@ export declare class MemoryAction implements dbActionC {
|
|
|
29
26
|
/**
|
|
30
27
|
* Check and create the specified collection if it doesn't exist.
|
|
31
28
|
*/
|
|
32
|
-
checkCollection(collection:
|
|
29
|
+
checkCollection({ collection }: VQuery): Promise<boolean>;
|
|
33
30
|
/**
|
|
34
31
|
* Check if a collection exists.
|
|
35
32
|
*/
|
|
36
|
-
issetCollection(collection:
|
|
33
|
+
issetCollection({ collection }: VQuery): Promise<boolean>;
|
|
37
34
|
/**
|
|
38
35
|
* Add a new entry to the specified database.
|
|
39
36
|
*/
|
|
40
|
-
add(collection
|
|
37
|
+
add({ collection, data, id_gen }: VQuery): Promise<import("../types/arg.js").Arg>;
|
|
41
38
|
/**
|
|
42
39
|
* Find entries in the specified database based on search criteria.
|
|
43
40
|
*/
|
|
44
|
-
find(collection
|
|
41
|
+
find({ collection, search, context, dbFindOpts, findOpts }: VQuery): Promise<Data[]>;
|
|
45
42
|
/**
|
|
46
43
|
* Find the first matching entry in the specified database based on search criteria.
|
|
47
44
|
*/
|
|
48
|
-
findOne(collection
|
|
49
|
-
/**
|
|
50
|
-
* Find entries in the specified database based on search criteria and return a stream of results.
|
|
51
|
-
*/
|
|
52
|
-
findStream(collection: string, arg: Search, context?: Context, findOpts?: FindOpts, limit?: number): AsyncGenerator<any>;
|
|
45
|
+
findOne({ collection, search, context, findOpts }: VQuery): Promise<Data>;
|
|
53
46
|
/**
|
|
54
47
|
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
55
48
|
*/
|
|
56
|
-
update(collection
|
|
49
|
+
update({ collection, search, updater, context }: VQuery): Promise<boolean>;
|
|
57
50
|
/**
|
|
58
51
|
* Update the first matching entry in the specified database based on search criteria and an updater function or object.
|
|
59
52
|
*/
|
|
60
|
-
updateOne(collection
|
|
53
|
+
updateOne({ collection, search, updater, context }: VQuery): Promise<boolean>;
|
|
61
54
|
/**
|
|
62
55
|
* Remove entries from the specified database based on search criteria.
|
|
63
56
|
*/
|
|
64
|
-
remove(collection
|
|
57
|
+
remove({ collection, search, context }: VQuery): Promise<boolean>;
|
|
65
58
|
/**
|
|
66
59
|
* Remove the first matching entry from the specified database based on search criteria.
|
|
67
60
|
*/
|
|
68
|
-
removeOne(collection
|
|
61
|
+
removeOne({ collection, search, context }: VQuery): Promise<boolean>;
|
|
69
62
|
/**
|
|
70
63
|
* Removes a database collection from the file system.
|
|
71
64
|
*/
|
|
72
|
-
removeCollection(collection:
|
|
73
|
-
/**
|
|
74
|
-
* Executes a list of transactions on the specified database collection.
|
|
75
|
-
* @throws Error - Method not supported in memory.
|
|
76
|
-
*/
|
|
77
|
-
transaction(collection: string, transactions: Transaction[]): Promise<void>;
|
|
65
|
+
removeCollection({ collection }: VQuery): Promise<boolean>;
|
|
78
66
|
}
|
|
79
67
|
export default class ValtheraMemory extends Valthera {
|
|
80
68
|
constructor(...args: any[]);
|
package/dist/actions/memory.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import dbActionBase from "../base/actions.js";
|
|
1
2
|
import Valthera from "../db/valthera.js";
|
|
2
3
|
import CustomFileCpu from "../file/customFileCpu.js";
|
|
3
4
|
import genId from "../helpers/gen.js";
|
|
4
|
-
export class MemoryAction {
|
|
5
|
+
export class MemoryAction extends dbActionBase {
|
|
5
6
|
folder;
|
|
6
7
|
options;
|
|
7
8
|
fileCpu;
|
|
@@ -13,6 +14,7 @@ export class MemoryAction {
|
|
|
13
14
|
* @param options - The options object.
|
|
14
15
|
*/
|
|
15
16
|
constructor() {
|
|
17
|
+
super();
|
|
16
18
|
this.memory = new Map();
|
|
17
19
|
this.fileCpu = new CustomFileCpu(this._readMemory.bind(this), this._writeMemory.bind(this));
|
|
18
20
|
}
|
|
@@ -37,95 +39,84 @@ export class MemoryAction {
|
|
|
37
39
|
/**
|
|
38
40
|
* Check and create the specified collection if it doesn't exist.
|
|
39
41
|
*/
|
|
40
|
-
async checkCollection(collection) {
|
|
42
|
+
async checkCollection({ collection }) {
|
|
41
43
|
if (this.issetCollection(collection))
|
|
42
44
|
return;
|
|
43
45
|
this.memory.set(collection, []);
|
|
46
|
+
return true;
|
|
44
47
|
}
|
|
45
48
|
/**
|
|
46
49
|
* Check if a collection exists.
|
|
47
50
|
*/
|
|
48
|
-
async issetCollection(collection) {
|
|
51
|
+
async issetCollection({ collection }) {
|
|
49
52
|
return this.memory.has(collection);
|
|
50
53
|
}
|
|
51
54
|
/**
|
|
52
55
|
* Add a new entry to the specified database.
|
|
53
56
|
*/
|
|
54
|
-
async add(collection,
|
|
55
|
-
await this.checkCollection(
|
|
57
|
+
async add({ collection, data, id_gen = true }) {
|
|
58
|
+
await this.checkCollection(arguments[0]);
|
|
56
59
|
if (id_gen)
|
|
57
|
-
|
|
58
|
-
await this.fileCpu.add(collection,
|
|
59
|
-
return
|
|
60
|
+
data._id = data._id || genId();
|
|
61
|
+
await this.fileCpu.add(collection, data);
|
|
62
|
+
return data;
|
|
60
63
|
}
|
|
61
64
|
/**
|
|
62
65
|
* Find entries in the specified database based on search criteria.
|
|
63
66
|
*/
|
|
64
|
-
async find(collection,
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
await this.checkCollection(
|
|
68
|
-
let data = await this.fileCpu.find(collection,
|
|
69
|
-
if (
|
|
67
|
+
async find({ collection, search, context = {}, dbFindOpts = {}, findOpts = {} }) {
|
|
68
|
+
dbFindOpts.reverse = dbFindOpts.reverse || false;
|
|
69
|
+
dbFindOpts.max = dbFindOpts.max || -1;
|
|
70
|
+
await this.checkCollection(arguments[0]);
|
|
71
|
+
let data = await this.fileCpu.find(collection, search, context, findOpts);
|
|
72
|
+
if (dbFindOpts.reverse)
|
|
70
73
|
data.reverse();
|
|
71
|
-
if (
|
|
72
|
-
data = data.slice(0,
|
|
74
|
+
if (dbFindOpts.max !== -1 && data.length > dbFindOpts.max)
|
|
75
|
+
data = data.slice(0, dbFindOpts.max);
|
|
73
76
|
return data;
|
|
74
77
|
}
|
|
75
78
|
/**
|
|
76
79
|
* Find the first matching entry in the specified database based on search criteria.
|
|
77
80
|
*/
|
|
78
|
-
async findOne(collection,
|
|
79
|
-
await this.checkCollection(
|
|
80
|
-
let data = await this.fileCpu.findOne(collection,
|
|
81
|
+
async findOne({ collection, search, context = {}, findOpts = {} }) {
|
|
82
|
+
await this.checkCollection(arguments[0]);
|
|
83
|
+
let data = await this.fileCpu.findOne(collection, search, context, findOpts);
|
|
81
84
|
return data || null;
|
|
82
85
|
}
|
|
83
|
-
/**
|
|
84
|
-
* Find entries in the specified database based on search criteria and return a stream of results.
|
|
85
|
-
*/
|
|
86
|
-
async *findStream(collection, arg, context, findOpts, limit) {
|
|
87
|
-
throw new Error("Method not implemented.");
|
|
88
|
-
}
|
|
89
86
|
/**
|
|
90
87
|
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
91
88
|
*/
|
|
92
|
-
async update(collection,
|
|
93
|
-
await this.checkCollection(
|
|
94
|
-
return await this.fileCpu.update(collection, false,
|
|
89
|
+
async update({ collection, search, updater, context = {} }) {
|
|
90
|
+
await this.checkCollection(arguments[0]);
|
|
91
|
+
return await this.fileCpu.update(collection, false, search, updater, context);
|
|
95
92
|
}
|
|
96
93
|
/**
|
|
97
94
|
* Update the first matching entry in the specified database based on search criteria and an updater function or object.
|
|
98
95
|
*/
|
|
99
|
-
async updateOne(collection,
|
|
100
|
-
await this.checkCollection(
|
|
101
|
-
return await this.fileCpu.update(collection, true,
|
|
96
|
+
async updateOne({ collection, search, updater, context = {} }) {
|
|
97
|
+
await this.checkCollection(arguments[0]);
|
|
98
|
+
return await this.fileCpu.update(collection, true, search, updater, context);
|
|
102
99
|
}
|
|
103
100
|
/**
|
|
104
101
|
* Remove entries from the specified database based on search criteria.
|
|
105
102
|
*/
|
|
106
|
-
async remove(collection,
|
|
107
|
-
await this.checkCollection(
|
|
108
|
-
return await this.fileCpu.remove(collection, false,
|
|
103
|
+
async remove({ collection, search, context = {} }) {
|
|
104
|
+
await this.checkCollection(arguments[0]);
|
|
105
|
+
return await this.fileCpu.remove(collection, false, search, context);
|
|
109
106
|
}
|
|
110
107
|
/**
|
|
111
108
|
* Remove the first matching entry from the specified database based on search criteria.
|
|
112
109
|
*/
|
|
113
|
-
async removeOne(collection,
|
|
114
|
-
await this.checkCollection(
|
|
115
|
-
return await this.fileCpu.remove(collection, true,
|
|
110
|
+
async removeOne({ collection, search, context = {} }) {
|
|
111
|
+
await this.checkCollection(arguments[0]);
|
|
112
|
+
return await this.fileCpu.remove(collection, true, search, context);
|
|
116
113
|
}
|
|
117
114
|
/**
|
|
118
115
|
* Removes a database collection from the file system.
|
|
119
116
|
*/
|
|
120
|
-
async removeCollection(collection) {
|
|
117
|
+
async removeCollection({ collection }) {
|
|
121
118
|
this.memory.delete(collection);
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* Executes a list of transactions on the specified database collection.
|
|
125
|
-
* @throws Error - Method not supported in memory.
|
|
126
|
-
*/
|
|
127
|
-
transaction(collection, transactions) {
|
|
128
|
-
throw new Error("Method not supported in memory.");
|
|
119
|
+
return true;
|
|
129
120
|
}
|
|
130
121
|
}
|
|
131
122
|
export default class ValtheraMemory extends Valthera {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import Data from "../types/data.js";
|
|
2
|
+
import { VQuery } from "../types/query.js";
|
|
3
|
+
declare class dbActionBase {
|
|
4
|
+
getCollections(): Promise<string[]>;
|
|
5
|
+
checkCollection(config: VQuery): Promise<boolean>;
|
|
6
|
+
issetCollection(config: VQuery): Promise<boolean>;
|
|
7
|
+
add(config: VQuery): Promise<Data>;
|
|
8
|
+
find(config: VQuery): Promise<Data[]>;
|
|
9
|
+
findOne(config: VQuery): Promise<Data | null>;
|
|
10
|
+
findStream(config: VQuery): AsyncGenerator<any>;
|
|
11
|
+
update(config: VQuery): Promise<boolean>;
|
|
12
|
+
updateOne(config: VQuery): Promise<boolean>;
|
|
13
|
+
remove(config: VQuery): Promise<boolean>;
|
|
14
|
+
removeOne(config: VQuery): Promise<boolean>;
|
|
15
|
+
removeCollection(config: VQuery): Promise<boolean>;
|
|
16
|
+
transaction(config: VQuery): Promise<boolean>;
|
|
17
|
+
updateOneOrAdd(config: VQuery): Promise<boolean>;
|
|
18
|
+
}
|
|
19
|
+
export default dbActionBase;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { setDataUsingUpdateOneOrAdd } from "../helpers/updateOneOrAdd.js";
|
|
2
|
+
class dbActionBase {
|
|
3
|
+
async getCollections() {
|
|
4
|
+
throw new Error("Not implemented");
|
|
5
|
+
return [];
|
|
6
|
+
}
|
|
7
|
+
async checkCollection(config) {
|
|
8
|
+
throw new Error("Not implemented");
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
async issetCollection(config) {
|
|
12
|
+
throw new Error("Not implemented");
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
async add(config) {
|
|
16
|
+
throw new Error("Not implemented");
|
|
17
|
+
return {};
|
|
18
|
+
}
|
|
19
|
+
async find(config) {
|
|
20
|
+
throw new Error("Not implemented");
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
async findOne(config) {
|
|
24
|
+
throw new Error("Not implemented");
|
|
25
|
+
return {};
|
|
26
|
+
}
|
|
27
|
+
async *findStream(config) {
|
|
28
|
+
throw new Error("Not implemented");
|
|
29
|
+
return {};
|
|
30
|
+
}
|
|
31
|
+
async update(config) {
|
|
32
|
+
throw new Error("Not implemented");
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
async updateOne(config) {
|
|
36
|
+
throw new Error("Not implemented");
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
async remove(config) {
|
|
40
|
+
throw new Error("Not implemented");
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
async removeOne(config) {
|
|
44
|
+
throw new Error("Not implemented");
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
async removeCollection(config) {
|
|
48
|
+
throw new Error("Not implemented");
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
async transaction(config) {
|
|
52
|
+
throw new Error("Not implemented");
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
async updateOneOrAdd(config) {
|
|
56
|
+
const res = await this.updateOne(config);
|
|
57
|
+
if (!res) {
|
|
58
|
+
setDataUsingUpdateOneOrAdd(config);
|
|
59
|
+
await this.add(config);
|
|
60
|
+
}
|
|
61
|
+
return res;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export default dbActionBase;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import CollectionManager from "../helpers/CollectionManager.js";
|
|
2
|
+
import Data from "../types/data.js";
|
|
3
|
+
import { VQuery } from "../types/query.js";
|
|
4
|
+
import { ValtheraCompatibleInternal } from "../types/valthera.js";
|
|
5
|
+
declare class ValtheraBase implements ValtheraCompatibleInternal {
|
|
6
|
+
version: string;
|
|
7
|
+
c(config: VQuery): CollectionManager;
|
|
8
|
+
getCollections(): Promise<any[]>;
|
|
9
|
+
checkCollection(config: VQuery): Promise<boolean>;
|
|
10
|
+
issetCollection(config: VQuery): Promise<boolean>;
|
|
11
|
+
add<T = Data>(config: VQuery): Promise<T>;
|
|
12
|
+
find<T = Data>(config: VQuery): Promise<T[]>;
|
|
13
|
+
findOne<T = Data>(config: VQuery): Promise<T>;
|
|
14
|
+
findStream<T = Data>(config: VQuery): Promise<AsyncGenerator<T, any, any>>;
|
|
15
|
+
remove(config: VQuery): Promise<boolean>;
|
|
16
|
+
removeCollection(config: VQuery): Promise<boolean>;
|
|
17
|
+
removeOne(config: VQuery): Promise<boolean>;
|
|
18
|
+
update(config: VQuery): Promise<boolean>;
|
|
19
|
+
updateOne(config: VQuery): Promise<boolean>;
|
|
20
|
+
updateOneOrAdd(config: VQuery): Promise<boolean>;
|
|
21
|
+
transaction(config: VQuery): Promise<boolean>;
|
|
22
|
+
}
|
|
23
|
+
export default ValtheraBase;
|
package/dist/base/db.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { version } from "../version.js";
|
|
2
|
+
class ValtheraBase {
|
|
3
|
+
version = version;
|
|
4
|
+
c(config) {
|
|
5
|
+
throw new Error("Not implemented");
|
|
6
|
+
}
|
|
7
|
+
async getCollections() {
|
|
8
|
+
throw new Error("Not implemented");
|
|
9
|
+
return [];
|
|
10
|
+
}
|
|
11
|
+
async checkCollection(config) {
|
|
12
|
+
throw new Error("Not implemented");
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
async issetCollection(config) {
|
|
16
|
+
throw new Error("Not implemented");
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
async add(config) {
|
|
20
|
+
throw new Error("Not implemented");
|
|
21
|
+
return {};
|
|
22
|
+
}
|
|
23
|
+
async find(config) {
|
|
24
|
+
throw new Error("Not implemented");
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
async findOne(config) {
|
|
28
|
+
throw new Error("Not implemented");
|
|
29
|
+
return {};
|
|
30
|
+
}
|
|
31
|
+
async findStream(config) {
|
|
32
|
+
throw new Error("Not implemented");
|
|
33
|
+
return {};
|
|
34
|
+
}
|
|
35
|
+
async remove(config) {
|
|
36
|
+
throw new Error("Not implemented");
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
async removeCollection(config) {
|
|
40
|
+
throw new Error("Not implemented");
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
async removeOne(config) {
|
|
44
|
+
throw new Error("Not implemented");
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
async update(config) {
|
|
48
|
+
throw new Error("Not implemented");
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
async updateOne(config) {
|
|
52
|
+
throw new Error("Not implemented");
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
async updateOneOrAdd(config) {
|
|
56
|
+
throw new Error("Not implemented");
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
async transaction(config) {
|
|
60
|
+
throw new Error("Not implemented");
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export default ValtheraBase;
|
package/dist/client/valthera.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import ky from "ky";
|
|
2
2
|
import CollectionManager from "../helpers/CollectionManager.js";
|
|
3
3
|
import serializeFunctions from "./function.js";
|
|
4
|
+
import { version } from "../version.js";
|
|
4
5
|
/**
|
|
5
6
|
* Represents a database management class for performing CRUD operations.
|
|
6
7
|
* Uses a remote database.
|
|
@@ -8,6 +9,7 @@ import serializeFunctions from "./function.js";
|
|
|
8
9
|
*/
|
|
9
10
|
class ValtheraRemote {
|
|
10
11
|
remote;
|
|
12
|
+
version = version;
|
|
11
13
|
constructor(remote) {
|
|
12
14
|
if (typeof remote === "string") {
|
|
13
15
|
const urlObj = new URL(remote);
|
package/dist/db/valthera.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ declare class Valthera implements ValtheraCompatible {
|
|
|
17
17
|
dbAction: dbActionC;
|
|
18
18
|
executor: executorC;
|
|
19
19
|
emiter: EventEmitter;
|
|
20
|
+
version: string;
|
|
20
21
|
constructor(folder: string, options?: DbOpts, fileCpu?: FileCpu);
|
|
21
22
|
private execute;
|
|
22
23
|
/**
|
|
@@ -42,7 +43,7 @@ declare class Valthera implements ValtheraCompatible {
|
|
|
42
43
|
/**
|
|
43
44
|
* Find data in a database.
|
|
44
45
|
*/
|
|
45
|
-
find<T = Data>(collection: string, search: Search, context?: Context,
|
|
46
|
+
find<T = Data>(collection: string, search: Search, context?: Context, dbFindOpts?: DbFindOpts, findOpts?: FindOpts): Promise<T[]>;
|
|
46
47
|
/**
|
|
47
48
|
* Find one data entry in a database.
|
|
48
49
|
*/
|
package/dist/db/valthera.js
CHANGED
|
@@ -3,6 +3,7 @@ import executorC from "../helpers/executor.js";
|
|
|
3
3
|
import CollectionManager from "../helpers/CollectionManager.js";
|
|
4
4
|
import vFileCpu from "../file/index.js";
|
|
5
5
|
import { EventEmitter } from "events";
|
|
6
|
+
import { version } from "../version.js";
|
|
6
7
|
/**
|
|
7
8
|
* Represents a database management class for performing CRUD operations.
|
|
8
9
|
* @class
|
|
@@ -11,6 +12,7 @@ class Valthera {
|
|
|
11
12
|
dbAction;
|
|
12
13
|
executor;
|
|
13
14
|
emiter;
|
|
15
|
+
version = version;
|
|
14
16
|
constructor(folder, options = {}, fileCpu) {
|
|
15
17
|
if (!fileCpu)
|
|
16
18
|
fileCpu = vFileCpu;
|
|
@@ -42,102 +44,79 @@ class Valthera {
|
|
|
42
44
|
* Check and create the specified collection if it doesn't exist.
|
|
43
45
|
*/
|
|
44
46
|
async checkCollection(collection) {
|
|
45
|
-
return await this.execute("checkCollection", collection);
|
|
47
|
+
return await this.execute("checkCollection", { collection });
|
|
46
48
|
}
|
|
47
49
|
/**
|
|
48
50
|
* Check if a collection exists.
|
|
49
51
|
*/
|
|
50
52
|
async issetCollection(collection) {
|
|
51
|
-
return await this.execute("issetCollection", collection);
|
|
53
|
+
return await this.execute("issetCollection", { collection });
|
|
52
54
|
}
|
|
53
55
|
/**
|
|
54
56
|
* Add data to a database.
|
|
55
57
|
*/
|
|
56
58
|
async add(collection, data, id_gen = true) {
|
|
57
|
-
return await this.execute("add", collection, data, id_gen);
|
|
59
|
+
return await this.execute("add", { collection, data, id_gen });
|
|
58
60
|
}
|
|
59
61
|
/**
|
|
60
62
|
* Find data in a database.
|
|
61
63
|
*/
|
|
62
|
-
async find(collection, search, context = {},
|
|
63
|
-
return await this.execute("find", collection, search, context,
|
|
64
|
+
async find(collection, search, context = {}, dbFindOpts = {}, findOpts = {}) {
|
|
65
|
+
return await this.execute("find", { collection, search, context, dbFindOpts, findOpts });
|
|
64
66
|
}
|
|
65
67
|
/**
|
|
66
68
|
* Find one data entry in a database.
|
|
67
69
|
*/
|
|
68
70
|
async findOne(collection, search, context = {}, findOpts = {}) {
|
|
69
|
-
return await this.execute("findOne", collection, search, context, findOpts);
|
|
71
|
+
return await this.execute("findOne", { collection, search, context, findOpts });
|
|
70
72
|
}
|
|
71
73
|
/**
|
|
72
74
|
* Find data in a database as a stream.
|
|
73
75
|
*/
|
|
74
76
|
async findStream(collection, search, context = {}, findOpts = {}, limit = -1) {
|
|
75
|
-
return await this.execute("findStream", collection, search, context, findOpts, limit);
|
|
77
|
+
return await this.execute("findStream", { collection, search, context, findOpts, limit });
|
|
76
78
|
}
|
|
77
79
|
/**
|
|
78
80
|
* Update data in a database.
|
|
79
81
|
*/
|
|
80
82
|
async update(collection, search, updater, context = {}) {
|
|
81
|
-
return await this.execute("update", collection, search, updater, context);
|
|
83
|
+
return await this.execute("update", { collection, search, updater, context });
|
|
82
84
|
}
|
|
83
85
|
/**
|
|
84
86
|
* Update one data entry in a database.
|
|
85
87
|
*/
|
|
86
88
|
async updateOne(collection, search, updater, context = {}) {
|
|
87
|
-
return await this.execute("updateOne", collection, search, updater, context);
|
|
89
|
+
return await this.execute("updateOne", { collection, search, updater, context });
|
|
88
90
|
}
|
|
89
91
|
/**
|
|
90
92
|
* Remove data from a database.
|
|
91
93
|
*/
|
|
92
94
|
async remove(collection, search, context = {}) {
|
|
93
|
-
return await this.execute("remove", collection, search, context);
|
|
95
|
+
return await this.execute("remove", { collection, search, context });
|
|
94
96
|
}
|
|
95
97
|
/**
|
|
96
98
|
* Remove one data entry from a database.
|
|
97
99
|
*/
|
|
98
100
|
async removeOne(collection, search, context = {}) {
|
|
99
|
-
return await this.execute("removeOne", collection, search, context);
|
|
101
|
+
return await this.execute("removeOne", { collection, search, context });
|
|
100
102
|
}
|
|
101
103
|
/**
|
|
102
104
|
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
103
105
|
*/
|
|
104
106
|
async updateOneOrAdd(collection, search, updater, add_arg = {}, context = {}, id_gen = true) {
|
|
105
|
-
|
|
106
|
-
if (!res) {
|
|
107
|
-
const assignData = [];
|
|
108
|
-
function assignDataPush(data) {
|
|
109
|
-
if (typeof data !== "object" || Array.isArray(data))
|
|
110
|
-
return;
|
|
111
|
-
const obj = {};
|
|
112
|
-
for (const key of Object.keys(data)) {
|
|
113
|
-
if (key.startsWith("$")) {
|
|
114
|
-
Object.keys(data[key]).forEach((k) => {
|
|
115
|
-
obj[k] = data[key][k];
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
else
|
|
119
|
-
obj[key] = data[key];
|
|
120
|
-
}
|
|
121
|
-
assignData.push(obj);
|
|
122
|
-
}
|
|
123
|
-
assignDataPush(search);
|
|
124
|
-
assignDataPush(updater);
|
|
125
|
-
assignDataPush(add_arg);
|
|
126
|
-
await this.add(collection, Object.assign({}, ...assignData), id_gen);
|
|
127
|
-
}
|
|
128
|
-
return res;
|
|
107
|
+
return await this.execute("updateOneOrAdd", { collection, search, updater, add_arg, context, id_gen });
|
|
129
108
|
}
|
|
130
109
|
/**
|
|
131
110
|
* Removes a database collection from the file system.
|
|
132
111
|
*/
|
|
133
112
|
async removeCollection(collection) {
|
|
134
|
-
return await this.execute("removeCollection", collection);
|
|
113
|
+
return await this.execute("removeCollection", { collection });
|
|
135
114
|
}
|
|
136
115
|
/**
|
|
137
116
|
* Execute a transaction.
|
|
138
117
|
*/
|
|
139
118
|
async transaction(collection, transaction) {
|
|
140
|
-
return await this.execute("transaction", collection, transaction);
|
|
119
|
+
return await this.execute("transaction", { collection, transaction });
|
|
141
120
|
}
|
|
142
121
|
}
|
|
143
122
|
export default Valthera;
|
package/dist/file/utils.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Repairs a file path by replacing double slashes
|
|
3
3
|
*/
|
|
4
4
|
export declare function pathRepair(path: string): string;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
export declare function createRL(file: string):
|
|
5
|
+
export interface LineReader extends AsyncIterable<string> {
|
|
6
|
+
close: () => void;
|
|
7
|
+
}
|
|
8
|
+
export declare function createRL(file: string): LineReader;
|
package/dist/file/utils.js
CHANGED
|
@@ -1,19 +1,84 @@
|
|
|
1
1
|
import { createReadStream } from "fs";
|
|
2
|
-
import { createInterface } from "readline";
|
|
3
2
|
/**
|
|
4
3
|
* Repairs a file path by replacing double slashes
|
|
5
4
|
*/
|
|
6
5
|
export function pathRepair(path) {
|
|
7
6
|
return path.replaceAll("//", "/");
|
|
8
7
|
}
|
|
9
|
-
/**
|
|
10
|
-
* Creates a Readline interface for reading large files with a specified high water mark.
|
|
11
|
-
*/
|
|
12
8
|
export function createRL(file) {
|
|
13
|
-
const
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
9
|
+
const stream = createReadStream(file, { highWaterMark: 64 * 1024 });
|
|
10
|
+
let buffer = "";
|
|
11
|
+
let done = false;
|
|
12
|
+
let error = null;
|
|
13
|
+
const lines = [];
|
|
14
|
+
let waiting = null;
|
|
15
|
+
stream.on("data", (chunk) => {
|
|
16
|
+
buffer += chunk.toString("utf8");
|
|
17
|
+
let index;
|
|
18
|
+
while ((index = buffer.search(/\r?\n/)) >= 0) {
|
|
19
|
+
const line = buffer.slice(0, index);
|
|
20
|
+
lines.push(line);
|
|
21
|
+
buffer = buffer.slice(index + (buffer[index] === "\r" && buffer[index + 1] === "\n" ? 2 : 1));
|
|
22
|
+
}
|
|
23
|
+
feed();
|
|
24
|
+
});
|
|
25
|
+
stream.on("end", () => {
|
|
26
|
+
if (buffer.length > 0) {
|
|
27
|
+
lines.push(buffer);
|
|
28
|
+
buffer = "";
|
|
29
|
+
}
|
|
30
|
+
done = true;
|
|
31
|
+
feed();
|
|
32
|
+
});
|
|
33
|
+
stream.on("error", (err) => {
|
|
34
|
+
error = err;
|
|
35
|
+
done = true;
|
|
36
|
+
feed();
|
|
17
37
|
});
|
|
38
|
+
const feed = () => {
|
|
39
|
+
if (waiting) {
|
|
40
|
+
if (error) {
|
|
41
|
+
waiting(Promise.reject(error));
|
|
42
|
+
}
|
|
43
|
+
else if (lines.length > 0) {
|
|
44
|
+
waiting({ value: lines.shift(), done: false });
|
|
45
|
+
}
|
|
46
|
+
else if (done) {
|
|
47
|
+
waiting({ value: undefined, done: true });
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
waiting = null;
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
const iterator = {
|
|
56
|
+
next() {
|
|
57
|
+
if (error)
|
|
58
|
+
return Promise.reject(error);
|
|
59
|
+
if (lines.length > 0)
|
|
60
|
+
return Promise.resolve({ value: lines.shift(), done: false });
|
|
61
|
+
if (done)
|
|
62
|
+
return Promise.resolve({ value: undefined, done: true });
|
|
63
|
+
return new Promise(res => {
|
|
64
|
+
waiting = res;
|
|
65
|
+
});
|
|
66
|
+
},
|
|
67
|
+
return() {
|
|
68
|
+
rl.close();
|
|
69
|
+
return Promise.resolve({ value: undefined, done: true });
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
const rl = {
|
|
73
|
+
[Symbol.asyncIterator]() {
|
|
74
|
+
return iterator;
|
|
75
|
+
},
|
|
76
|
+
close() {
|
|
77
|
+
if (!done) {
|
|
78
|
+
done = true;
|
|
79
|
+
stream.destroy();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
};
|
|
18
83
|
return rl;
|
|
19
84
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
function assignDataPush(data) {
|
|
2
|
+
if (typeof data !== "object" || Array.isArray(data))
|
|
3
|
+
return;
|
|
4
|
+
const obj = {};
|
|
5
|
+
for (const key of Object.keys(data)) {
|
|
6
|
+
if (key.startsWith("$")) {
|
|
7
|
+
Object.keys(data[key]).forEach((k) => {
|
|
8
|
+
obj[k] = data[key][k];
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
else
|
|
12
|
+
obj[key] = data[key];
|
|
13
|
+
}
|
|
14
|
+
return obj;
|
|
15
|
+
}
|
|
16
|
+
export function setDataUsingUpdateOneOrAdd(query) {
|
|
17
|
+
query.data = Object.assign({}, assignDataPush(query.search), assignDataPush(query.updater), assignDataPush(query.add_arg));
|
|
18
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -9,17 +9,8 @@ import ValtheraMemory, { createMemoryValthera } from "./actions/memory.js";
|
|
|
9
9
|
import { ValtheraAutoCreate } from "./helpers/autoCreate.js";
|
|
10
10
|
import { RelationTypes } from "./types/relation.js";
|
|
11
11
|
import { ValtheraCompatible } from "./types/valthera.js";
|
|
12
|
+
import { ValtheraTypes } from "./types/export.js";
|
|
12
13
|
export { Valthera, Graph, ValtheraRemote, GraphRemote, Relation, genId, CustomFileCpu, ValtheraMemory, createMemoryValthera, ValtheraAutoCreate, };
|
|
13
14
|
type GraphCompatible = Graph | GraphRemote;
|
|
14
|
-
export type { ValtheraCompatible, RelationTypes, GraphCompatible, };
|
|
15
|
+
export type { ValtheraCompatible, RelationTypes, GraphCompatible, ValtheraTypes };
|
|
15
16
|
export type Id = import("./types/Id.js").Id;
|
|
16
|
-
export declare namespace ValtheraTypes {
|
|
17
|
-
type Arg = import("./types/arg.js").Arg;
|
|
18
|
-
type Search = import("./types/arg.js").Search;
|
|
19
|
-
type Updater = import("./types/arg.js").Updater;
|
|
20
|
-
type DbFindOpts = import("./types/options.js").DbFindOpts;
|
|
21
|
-
type FindOpts = import("./types/options.js").FindOpts;
|
|
22
|
-
type DbOpts = import("./types/options.js").DbOpts;
|
|
23
|
-
type Data = import("./types/data.js").Data;
|
|
24
|
-
type SearchOptions = import("./types/searchOpts.js").SearchOptions;
|
|
25
|
-
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare namespace ValtheraTypes {
|
|
2
|
+
type Arg = import("./arg.js").Arg;
|
|
3
|
+
type Search = import("./arg.js").Search;
|
|
4
|
+
type Updater = import("./arg.js").Updater;
|
|
5
|
+
type DbFindOpts = import("./options.js").DbFindOpts;
|
|
6
|
+
type FindOpts = import("./options.js").FindOpts;
|
|
7
|
+
type DbOpts = import("./options.js").DbOpts;
|
|
8
|
+
type Data = import("./data.js").Data;
|
|
9
|
+
type SearchOptions = import("./searchOpts.js").SearchOptions;
|
|
10
|
+
type Id = import("./Id.js").Id;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Context } from "./types.js";
|
|
2
|
+
import { Arg, Search, Updater } from "./arg.js";
|
|
3
|
+
import { DbFindOpts, FindOpts } from "./options.js";
|
|
4
|
+
import { Transaction } from "./transactions.js";
|
|
5
|
+
export interface VQuery {
|
|
6
|
+
collection?: string;
|
|
7
|
+
search?: Search;
|
|
8
|
+
context?: Context;
|
|
9
|
+
dbFindOpts?: DbFindOpts;
|
|
10
|
+
findOpts?: FindOpts;
|
|
11
|
+
data?: Arg;
|
|
12
|
+
id_gen?: boolean;
|
|
13
|
+
limit?: number;
|
|
14
|
+
add_arg?: Arg;
|
|
15
|
+
updater?: Updater;
|
|
16
|
+
transaction?: Transaction[];
|
|
17
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/types/valthera.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import CollectionManager from "../helpers/CollectionManager.js";
|
|
|
2
2
|
import { Arg, Search, Updater } from "./arg.js";
|
|
3
3
|
import Data from "./data.js";
|
|
4
4
|
import { DbFindOpts, FindOpts } from "./options.js";
|
|
5
|
+
import { VQuery } from "./query.js";
|
|
5
6
|
import { Transaction } from "./transactions.js";
|
|
6
7
|
import { Context } from "./types.js";
|
|
7
8
|
export interface ValtheraCompatible {
|
|
@@ -21,3 +22,20 @@ export interface ValtheraCompatible {
|
|
|
21
22
|
transaction(collection: string, transaction: Transaction[]): Promise<boolean>;
|
|
22
23
|
updateOneOrAdd(collection: string, search: Search, updater: Updater, add_arg?: Arg, context?: Context, id_gen?: boolean): Promise<boolean>;
|
|
23
24
|
}
|
|
25
|
+
export interface ValtheraCompatibleInternal {
|
|
26
|
+
c(config: VQuery): CollectionManager;
|
|
27
|
+
getCollections(): Promise<string[]>;
|
|
28
|
+
checkCollection(config: VQuery): Promise<boolean>;
|
|
29
|
+
issetCollection(config: VQuery): Promise<boolean>;
|
|
30
|
+
add<T = Data>(config: VQuery): Promise<T>;
|
|
31
|
+
find<T = Data>(config: VQuery): Promise<T[]>;
|
|
32
|
+
findOne<T = Data>(config: VQuery): Promise<T | null>;
|
|
33
|
+
findStream<T = Data>(config: VQuery): Promise<AsyncGenerator<T>>;
|
|
34
|
+
update(config: VQuery): Promise<boolean>;
|
|
35
|
+
updateOne(config: VQuery): Promise<boolean>;
|
|
36
|
+
remove(config: VQuery): Promise<boolean>;
|
|
37
|
+
removeOne(config: VQuery): Promise<boolean>;
|
|
38
|
+
removeCollection(config: VQuery): Promise<boolean>;
|
|
39
|
+
transaction(config: VQuery): Promise<boolean>;
|
|
40
|
+
updateOneOrAdd(config: VQuery): Promise<boolean>;
|
|
41
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const version = "0.0.1";
|
package/dist/version.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const version = "0.8.1";
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/db",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"description": "A simple file-based database management system with support for CRUD operations, custom queries, and graph structures.",
|
|
@@ -21,8 +21,7 @@
|
|
|
21
21
|
"type": "module",
|
|
22
22
|
"dependencies": {
|
|
23
23
|
"json5": "^2.2.3",
|
|
24
|
-
"ky": "^1.7.4"
|
|
25
|
-
"readline": "^1.3.0"
|
|
24
|
+
"ky": "^1.7.4"
|
|
26
25
|
},
|
|
27
26
|
"devDependencies": {
|
|
28
27
|
"@types/node": "^22.10.2",
|