@wxn0brp/db 0.9.1 → 0.10.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/actions/action.d.ts +1 -6
- package/dist/actions/action.js +40 -51
- package/dist/actions/memory.js +16 -6
- package/dist/base/actions.d.ts +0 -2
- package/dist/base/actions.js +0 -8
- package/dist/base/db.d.ts +0 -2
- package/dist/base/db.js +0 -8
- package/dist/client/valthera.d.ts +0 -9
- package/dist/client/valthera.js +0 -13
- package/dist/db/valthera.d.ts +4 -13
- package/dist/db/valthera.js +3 -15
- package/dist/file/customFileCpu.d.ts +1 -4
- package/dist/file/customFileCpu.js +0 -6
- package/dist/file/find.d.ts +1 -2
- package/dist/file/find.js +1 -28
- package/dist/file/index.js +1 -4
- package/dist/helpers/CollectionManager.d.ts +0 -4
- package/dist/helpers/CollectionManager.js +0 -6
- package/dist/types/fileCpu.d.ts +0 -18
- package/dist/types/options.d.ts +3 -0
- package/dist/types/query.d.ts +0 -2
- package/dist/types/valthera.d.ts +0 -5
- package/dist/utils/sort.d.ts +1 -0
- package/dist/utils/sort.js +23 -0
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/dist/file/transactions.d.ts +0 -3
- package/dist/file/transactions.js +0 -119
- package/dist/types/transactions.d.ts +0 -10
- package/dist/types/transactions.js +0 -1
package/dist/actions/action.d.ts
CHANGED
|
@@ -38,12 +38,11 @@ declare class dbActionC extends dbActionBase {
|
|
|
38
38
|
/**
|
|
39
39
|
* Find entries in the specified database based on search criteria.
|
|
40
40
|
*/
|
|
41
|
-
find({ collection, search, context, dbFindOpts, findOpts }: VQuery): Promise<
|
|
41
|
+
find({ collection, search, context, dbFindOpts, findOpts }: VQuery): Promise<Data[]>;
|
|
42
42
|
/**
|
|
43
43
|
* Find the first matching entry in the specified database based on search criteria.
|
|
44
44
|
*/
|
|
45
45
|
findOne({ collection, search, context, findOpts }: VQuery): Promise<Data>;
|
|
46
|
-
findStream({ collection, search, context, findOpts, limit }: VQuery): AsyncGenerator<any>;
|
|
47
46
|
/**
|
|
48
47
|
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
49
48
|
*/
|
|
@@ -66,9 +65,5 @@ declare class dbActionC extends dbActionBase {
|
|
|
66
65
|
removeCollection({ collection }: {
|
|
67
66
|
collection: any;
|
|
68
67
|
}): Promise<boolean>;
|
|
69
|
-
/**
|
|
70
|
-
* Apply a series of transactions to a database collection.
|
|
71
|
-
*/
|
|
72
|
-
transaction({ collection, transaction }: VQuery): Promise<boolean>;
|
|
73
68
|
}
|
|
74
69
|
export default dbActionC;
|
package/dist/actions/action.js
CHANGED
|
@@ -2,6 +2,7 @@ import { existsSync, mkdirSync, promises, statSync } from "fs";
|
|
|
2
2
|
import dbActionBase from "../base/actions.js";
|
|
3
3
|
import gen from "../helpers/gen.js";
|
|
4
4
|
import { resolve, sep } from "path";
|
|
5
|
+
import { compareSafe } from "../utils/sort.js";
|
|
5
6
|
/**
|
|
6
7
|
* A class representing database actions on files.
|
|
7
8
|
* @class
|
|
@@ -85,32 +86,53 @@ class dbActionC extends dbActionBase {
|
|
|
85
86
|
* Find entries in the specified database based on search criteria.
|
|
86
87
|
*/
|
|
87
88
|
async find({ collection, search, context = {}, dbFindOpts = {}, findOpts = {} }) {
|
|
88
|
-
|
|
89
|
-
dbFindOpts.max = dbFindOpts.max || -1;
|
|
89
|
+
const { reverse = false, max = -1, offset = 0, sortBy, sortAsc = true } = dbFindOpts;
|
|
90
90
|
await this.checkCollection(arguments[0]);
|
|
91
91
|
const cpath = this._getCollectionPath(collection);
|
|
92
|
-
|
|
93
|
-
if (
|
|
92
|
+
let files = await getSortedFiles(cpath);
|
|
93
|
+
if (reverse && !sortBy)
|
|
94
94
|
files.reverse();
|
|
95
95
|
let datas = [];
|
|
96
96
|
let totalEntries = 0;
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
97
|
+
let skippedEntries = 0;
|
|
98
|
+
for (const f of files) {
|
|
99
|
+
let entries = await this.fileCpu.find(cpath + f, search, context, findOpts);
|
|
100
|
+
if (reverse && !sortBy)
|
|
101
|
+
entries.reverse();
|
|
102
|
+
if (!sortBy) {
|
|
103
|
+
if (offset > skippedEntries) {
|
|
104
|
+
const remainingSkip = offset - skippedEntries;
|
|
105
|
+
if (entries.length <= remainingSkip) {
|
|
106
|
+
skippedEntries += entries.length;
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
entries = entries.slice(remainingSkip);
|
|
110
|
+
skippedEntries = offset;
|
|
106
111
|
}
|
|
107
|
-
|
|
108
|
-
totalEntries
|
|
112
|
+
if (max !== -1) {
|
|
113
|
+
if (totalEntries + entries.length > max) {
|
|
114
|
+
const remaining = max - totalEntries;
|
|
115
|
+
entries = entries.slice(0, remaining);
|
|
116
|
+
totalEntries = max;
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
totalEntries += entries.length;
|
|
120
|
+
}
|
|
109
121
|
}
|
|
122
|
+
datas.push(...entries);
|
|
123
|
+
if (max !== -1 && totalEntries >= max)
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
datas.push(...entries);
|
|
110
128
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
129
|
+
}
|
|
130
|
+
if (sortBy) {
|
|
131
|
+
const dir = sortAsc ? 1 : -1;
|
|
132
|
+
datas.sort((a, b) => compareSafe(a[sortBy], b[sortBy]) * dir);
|
|
133
|
+
const start = offset;
|
|
134
|
+
const end = max !== -1 ? offset + max : undefined;
|
|
135
|
+
datas = datas.slice(start, end);
|
|
114
136
|
}
|
|
115
137
|
return datas;
|
|
116
138
|
}
|
|
@@ -128,21 +150,6 @@ class dbActionC extends dbActionBase {
|
|
|
128
150
|
}
|
|
129
151
|
return null;
|
|
130
152
|
}
|
|
131
|
-
async *findStream({ collection, search, context = {}, findOpts = {}, limit = -1 }) {
|
|
132
|
-
await this.checkCollection(arguments[0]);
|
|
133
|
-
const cpath = this._getCollectionPath(collection);
|
|
134
|
-
const files = await getSortedFiles(cpath);
|
|
135
|
-
let count = 0;
|
|
136
|
-
for (let f of files) {
|
|
137
|
-
for await (const data of this.fileCpu.findStream(cpath + f, search, context, findOpts, limit)) {
|
|
138
|
-
yield data;
|
|
139
|
-
count++;
|
|
140
|
-
if (limit !== -1 && count >= limit) {
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
153
|
/**
|
|
147
154
|
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
148
155
|
*/
|
|
@@ -178,24 +185,6 @@ class dbActionC extends dbActionBase {
|
|
|
178
185
|
await promises.rm(this.folder + "/" + collection, { recursive: true, force: true });
|
|
179
186
|
return true;
|
|
180
187
|
}
|
|
181
|
-
/**
|
|
182
|
-
* Apply a series of transactions to a database collection.
|
|
183
|
-
*/
|
|
184
|
-
async transaction({ collection, transaction }) {
|
|
185
|
-
await this.checkCollection(arguments[0]);
|
|
186
|
-
const files = await getSortedFiles(this._getCollectionPath(collection));
|
|
187
|
-
if (files.length == 0) {
|
|
188
|
-
await promises.writeFile(this._getCollectionPath(collection) + "1.db", "");
|
|
189
|
-
files.push("1.db");
|
|
190
|
-
}
|
|
191
|
-
for (const file of files) {
|
|
192
|
-
await this.fileCpu.transactions(this._getCollectionPath(collection) + file, transaction);
|
|
193
|
-
}
|
|
194
|
-
console.log("Transactions applied successfully.");
|
|
195
|
-
console.log("Files:", files);
|
|
196
|
-
console.log("Transactions:", transaction);
|
|
197
|
-
return true;
|
|
198
|
-
}
|
|
199
188
|
}
|
|
200
189
|
/**
|
|
201
190
|
* Get the last file in the specified directory.
|
package/dist/actions/memory.js
CHANGED
|
@@ -2,6 +2,7 @@ import dbActionBase from "../base/actions.js";
|
|
|
2
2
|
import Valthera from "../db/valthera.js";
|
|
3
3
|
import CustomFileCpu from "../file/customFileCpu.js";
|
|
4
4
|
import genId from "../helpers/gen.js";
|
|
5
|
+
import { compareSafe } from "../utils/sort.js";
|
|
5
6
|
export class MemoryAction extends dbActionBase {
|
|
6
7
|
folder;
|
|
7
8
|
options;
|
|
@@ -65,14 +66,23 @@ export class MemoryAction extends dbActionBase {
|
|
|
65
66
|
* Find entries in the specified database based on search criteria.
|
|
66
67
|
*/
|
|
67
68
|
async find({ collection, search, context = {}, dbFindOpts = {}, findOpts = {} }) {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
await this.checkCollection(arguments[0]);
|
|
69
|
+
const { reverse = false, max = -1, offset = 0, sortBy, sortAsc = true } = dbFindOpts;
|
|
70
|
+
await this.checkCollection(collection);
|
|
71
71
|
let data = await this.fileCpu.find(collection, search, context, findOpts);
|
|
72
|
-
if (
|
|
72
|
+
if (reverse)
|
|
73
73
|
data.reverse();
|
|
74
|
-
if (
|
|
75
|
-
|
|
74
|
+
if (sortBy) {
|
|
75
|
+
const dir = sortAsc ? 1 : -1;
|
|
76
|
+
data.sort((a, b) => compareSafe(a[sortBy], b[sortBy]) * dir);
|
|
77
|
+
}
|
|
78
|
+
if (offset > 0) {
|
|
79
|
+
if (data.length <= offset)
|
|
80
|
+
return [];
|
|
81
|
+
data = data.slice(offset);
|
|
82
|
+
}
|
|
83
|
+
if (max !== -1 && data.length > max) {
|
|
84
|
+
data = data.slice(0, max);
|
|
85
|
+
}
|
|
76
86
|
return data;
|
|
77
87
|
}
|
|
78
88
|
/**
|
package/dist/base/actions.d.ts
CHANGED
|
@@ -7,13 +7,11 @@ declare class dbActionBase {
|
|
|
7
7
|
add(config: VQuery): Promise<Data>;
|
|
8
8
|
find(config: VQuery): Promise<Data[]>;
|
|
9
9
|
findOne(config: VQuery): Promise<Data | null>;
|
|
10
|
-
findStream(config: VQuery): AsyncGenerator<any>;
|
|
11
10
|
update(config: VQuery): Promise<boolean>;
|
|
12
11
|
updateOne(config: VQuery): Promise<boolean>;
|
|
13
12
|
remove(config: VQuery): Promise<boolean>;
|
|
14
13
|
removeOne(config: VQuery): Promise<boolean>;
|
|
15
14
|
removeCollection(config: VQuery): Promise<boolean>;
|
|
16
|
-
transaction(config: VQuery): Promise<boolean>;
|
|
17
15
|
updateOneOrAdd(config: VQuery): Promise<boolean>;
|
|
18
16
|
}
|
|
19
17
|
export default dbActionBase;
|
package/dist/base/actions.js
CHANGED
|
@@ -24,10 +24,6 @@ class dbActionBase {
|
|
|
24
24
|
throw new Error("Not implemented");
|
|
25
25
|
return {};
|
|
26
26
|
}
|
|
27
|
-
async *findStream(config) {
|
|
28
|
-
throw new Error("Not implemented");
|
|
29
|
-
return {};
|
|
30
|
-
}
|
|
31
27
|
async update(config) {
|
|
32
28
|
throw new Error("Not implemented");
|
|
33
29
|
return false;
|
|
@@ -48,10 +44,6 @@ class dbActionBase {
|
|
|
48
44
|
throw new Error("Not implemented");
|
|
49
45
|
return false;
|
|
50
46
|
}
|
|
51
|
-
async transaction(config) {
|
|
52
|
-
throw new Error("Not implemented");
|
|
53
|
-
return false;
|
|
54
|
-
}
|
|
55
47
|
async updateOneOrAdd(config) {
|
|
56
48
|
const res = await this.updateOne(config);
|
|
57
49
|
if (!res) {
|
package/dist/base/db.d.ts
CHANGED
|
@@ -11,13 +11,11 @@ declare class ValtheraBase implements ValtheraCompatibleInternal {
|
|
|
11
11
|
add<T = Data>(config: VQuery): Promise<T>;
|
|
12
12
|
find<T = Data>(config: VQuery): Promise<T[]>;
|
|
13
13
|
findOne<T = Data>(config: VQuery): Promise<T>;
|
|
14
|
-
findStream<T = Data>(config: VQuery): Promise<AsyncGenerator<T, any, any>>;
|
|
15
14
|
remove(config: VQuery): Promise<boolean>;
|
|
16
15
|
removeCollection(config: VQuery): Promise<boolean>;
|
|
17
16
|
removeOne(config: VQuery): Promise<boolean>;
|
|
18
17
|
update(config: VQuery): Promise<boolean>;
|
|
19
18
|
updateOne(config: VQuery): Promise<boolean>;
|
|
20
19
|
updateOneOrAdd(config: VQuery): Promise<boolean>;
|
|
21
|
-
transaction(config: VQuery): Promise<boolean>;
|
|
22
20
|
}
|
|
23
21
|
export default ValtheraBase;
|
package/dist/base/db.js
CHANGED
|
@@ -28,10 +28,6 @@ class ValtheraBase {
|
|
|
28
28
|
throw new Error("Not implemented");
|
|
29
29
|
return {};
|
|
30
30
|
}
|
|
31
|
-
async findStream(config) {
|
|
32
|
-
throw new Error("Not implemented");
|
|
33
|
-
return {};
|
|
34
|
-
}
|
|
35
31
|
async remove(config) {
|
|
36
32
|
throw new Error("Not implemented");
|
|
37
33
|
return false;
|
|
@@ -56,9 +52,5 @@ class ValtheraBase {
|
|
|
56
52
|
throw new Error("Not implemented");
|
|
57
53
|
return false;
|
|
58
54
|
}
|
|
59
|
-
async transaction(config) {
|
|
60
|
-
throw new Error("Not implemented");
|
|
61
|
-
return false;
|
|
62
|
-
}
|
|
63
55
|
}
|
|
64
56
|
export default ValtheraBase;
|
|
@@ -4,7 +4,6 @@ import { Arg, Search, Updater } from "../types/arg.js";
|
|
|
4
4
|
import { DbFindOpts, FindOpts } from "../types/options.js";
|
|
5
5
|
import { Context } from "../types/types.js";
|
|
6
6
|
import Data from "../types/data.js";
|
|
7
|
-
import { Transaction } from "../types/transactions.js";
|
|
8
7
|
import { ValtheraCompatible } from "../types/valthera.js";
|
|
9
8
|
/**
|
|
10
9
|
* Represents a database management class for performing CRUD operations.
|
|
@@ -47,10 +46,6 @@ declare class ValtheraRemote implements ValtheraCompatible {
|
|
|
47
46
|
* Find one data entry in a database.
|
|
48
47
|
*/
|
|
49
48
|
findOne<T = Data>(collection: string, search: Search, context?: Context, findOpts?: FindOpts): Promise<T>;
|
|
50
|
-
/**
|
|
51
|
-
* Find data in a database as a stream.
|
|
52
|
-
*/
|
|
53
|
-
findStream<T = Data>(collection: string, search: Search, context?: Context, findOpts?: FindOpts, limit?: number): Promise<AsyncGenerator<T, any, any>>;
|
|
54
49
|
/**
|
|
55
50
|
* Update data in a database.
|
|
56
51
|
*/
|
|
@@ -75,9 +70,5 @@ declare class ValtheraRemote implements ValtheraCompatible {
|
|
|
75
70
|
* Removes a database collection from the file system.
|
|
76
71
|
*/
|
|
77
72
|
removeCollection(name: string): Promise<boolean>;
|
|
78
|
-
/**
|
|
79
|
-
* Execute a transaction.
|
|
80
|
-
*/
|
|
81
|
-
transaction(collection: string, transaction: Transaction[]): Promise<boolean>;
|
|
82
73
|
}
|
|
83
74
|
export default ValtheraRemote;
|
package/dist/client/valthera.js
CHANGED
|
@@ -95,13 +95,6 @@ class ValtheraRemote {
|
|
|
95
95
|
async findOne(collection, search, context = {}, findOpts = {}) {
|
|
96
96
|
return await this._request("findOne", [collection, search, context, findOpts]);
|
|
97
97
|
}
|
|
98
|
-
/**
|
|
99
|
-
* Find data in a database as a stream.
|
|
100
|
-
*/
|
|
101
|
-
async findStream(collection, search, context = {}, findOpts = {}, limit = -1) {
|
|
102
|
-
throw new Error("Method not implemented.");
|
|
103
|
-
return await this._request("findStream", [collection, search, context, findOpts, limit]);
|
|
104
|
-
}
|
|
105
98
|
/**
|
|
106
99
|
* Update data in a database.
|
|
107
100
|
*/
|
|
@@ -138,11 +131,5 @@ class ValtheraRemote {
|
|
|
138
131
|
async removeCollection(name) {
|
|
139
132
|
return await this._request("removeCollection", [name]);
|
|
140
133
|
}
|
|
141
|
-
/**
|
|
142
|
-
* Execute a transaction.
|
|
143
|
-
*/
|
|
144
|
-
async transaction(collection, transaction) {
|
|
145
|
-
return await this._request("transaction", [collection, transaction]);
|
|
146
|
-
}
|
|
147
134
|
}
|
|
148
135
|
export default ValtheraRemote;
|
package/dist/db/valthera.d.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
+
import { EventEmitter } from "events";
|
|
1
2
|
import dbActionC from "../actions/action.js";
|
|
2
|
-
import executorC from "../helpers/executor.js";
|
|
3
3
|
import CollectionManager from "../helpers/CollectionManager.js";
|
|
4
|
-
import
|
|
4
|
+
import executorC from "../helpers/executor.js";
|
|
5
5
|
import { Arg, Search, Updater } from "../types/arg.js";
|
|
6
6
|
import Data from "../types/data.js";
|
|
7
|
-
import { Context } from "../types/types.js";
|
|
8
7
|
import FileCpu from "../types/fileCpu.js";
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
8
|
+
import { DbFindOpts, DbOpts, FindOpts } from "../types/options.js";
|
|
9
|
+
import { Context } from "../types/types.js";
|
|
11
10
|
import { ValtheraCompatible } from "../types/valthera.js";
|
|
12
11
|
/**
|
|
13
12
|
* Represents a database management class for performing CRUD operations.
|
|
@@ -48,10 +47,6 @@ declare class Valthera implements ValtheraCompatible {
|
|
|
48
47
|
* Find one data entry in a database.
|
|
49
48
|
*/
|
|
50
49
|
findOne<T = Data>(collection: string, search: Search, context?: Context, findOpts?: FindOpts): Promise<T>;
|
|
51
|
-
/**
|
|
52
|
-
* Find data in a database as a stream.
|
|
53
|
-
*/
|
|
54
|
-
findStream<T = Data>(collection: string, search: Search, context?: Context, findOpts?: FindOpts, limit?: number): Promise<AsyncGenerator<T, any, any>>;
|
|
55
50
|
/**
|
|
56
51
|
* Update data in a database.
|
|
57
52
|
*/
|
|
@@ -76,9 +71,5 @@ declare class Valthera implements ValtheraCompatible {
|
|
|
76
71
|
* Removes a database collection from the file system.
|
|
77
72
|
*/
|
|
78
73
|
removeCollection(collection: string): Promise<boolean>;
|
|
79
|
-
/**
|
|
80
|
-
* Execute a transaction.
|
|
81
|
-
*/
|
|
82
|
-
transaction(collection: string, transaction: Transaction[]): Promise<boolean>;
|
|
83
74
|
}
|
|
84
75
|
export default Valthera;
|
package/dist/db/valthera.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
+
import { EventEmitter } from "events";
|
|
1
2
|
import dbActionC from "../actions/action.js";
|
|
2
|
-
import executorC from "../helpers/executor.js";
|
|
3
|
-
import CollectionManager from "../helpers/CollectionManager.js";
|
|
4
3
|
import vFileCpu from "../file/index.js";
|
|
5
|
-
import
|
|
4
|
+
import CollectionManager from "../helpers/CollectionManager.js";
|
|
5
|
+
import executorC from "../helpers/executor.js";
|
|
6
6
|
import { version } from "../version.js";
|
|
7
7
|
/**
|
|
8
8
|
* Represents a database management class for performing CRUD operations.
|
|
@@ -70,12 +70,6 @@ class Valthera {
|
|
|
70
70
|
async findOne(collection, search, context = {}, findOpts = {}) {
|
|
71
71
|
return await this.execute("findOne", { collection, search, context, findOpts });
|
|
72
72
|
}
|
|
73
|
-
/**
|
|
74
|
-
* Find data in a database as a stream.
|
|
75
|
-
*/
|
|
76
|
-
async findStream(collection, search, context = {}, findOpts = {}, limit = -1) {
|
|
77
|
-
return await this.execute("findStream", { collection, search, context, findOpts, limit });
|
|
78
|
-
}
|
|
79
73
|
/**
|
|
80
74
|
* Update data in a database.
|
|
81
75
|
*/
|
|
@@ -112,11 +106,5 @@ class Valthera {
|
|
|
112
106
|
async removeCollection(collection) {
|
|
113
107
|
return await this.execute("removeCollection", { collection });
|
|
114
108
|
}
|
|
115
|
-
/**
|
|
116
|
-
* Execute a transaction.
|
|
117
|
-
*/
|
|
118
|
-
async transaction(collection, transaction) {
|
|
119
|
-
return await this.execute("transaction", { collection, transaction });
|
|
120
|
-
}
|
|
121
109
|
}
|
|
122
110
|
export default Valthera;
|
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import { Context } from "../types/types.js";
|
|
2
1
|
import { Search, Updater } from "../types/arg.js";
|
|
3
2
|
import Data from "../types/data.js";
|
|
4
3
|
import FileCpu from "../types/fileCpu.js";
|
|
5
4
|
import { FindOpts } from "../types/options.js";
|
|
6
|
-
import {
|
|
5
|
+
import { Context } from "../types/types.js";
|
|
7
6
|
export type WriteFile = (file: string, data: any[]) => Promise<void>;
|
|
8
7
|
export type ReadFile = (file: string) => Promise<any[]>;
|
|
9
8
|
declare class CustomFileCpu implements FileCpu {
|
|
@@ -13,9 +12,7 @@ declare class CustomFileCpu implements FileCpu {
|
|
|
13
12
|
add(file: string, data: Data): Promise<void>;
|
|
14
13
|
find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any[] | false>;
|
|
15
14
|
findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
|
|
16
|
-
findStream(file: string, arg: Search, context?: Context, findOpts?: FindOpts, limit?: number): AsyncGenerator<any>;
|
|
17
15
|
remove(file: string, one: boolean, arg: Search, context?: Context): Promise<boolean>;
|
|
18
16
|
update(file: string, one: boolean, arg: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
19
|
-
transactions(file: string, transactions: Transaction[]): Promise<void>;
|
|
20
17
|
}
|
|
21
18
|
export default CustomFileCpu;
|
|
@@ -26,9 +26,6 @@ class CustomFileCpu {
|
|
|
26
26
|
const result = entries.find(entry => typeof arg === "function" ? arg(entry, context) : hasFieldsAdvanced(entry, arg));
|
|
27
27
|
return result ? updateFindObject(result, findOpts) : false;
|
|
28
28
|
}
|
|
29
|
-
async *findStream(file, arg, context, findOpts, limit) {
|
|
30
|
-
throw new Error("Method not implemented.");
|
|
31
|
-
}
|
|
32
29
|
async remove(file, one, arg, context = {}) {
|
|
33
30
|
file = pathRepair(file);
|
|
34
31
|
let entries = await this._readFile(file);
|
|
@@ -67,8 +64,5 @@ class CustomFileCpu {
|
|
|
67
64
|
await this._writeFile(file, entries);
|
|
68
65
|
return true;
|
|
69
66
|
}
|
|
70
|
-
transactions(file, transactions) {
|
|
71
|
-
throw new Error("Method not implemented.");
|
|
72
|
-
}
|
|
73
67
|
}
|
|
74
68
|
export default CustomFileCpu;
|
package/dist/file/find.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Search } from "../types/arg.js";
|
|
2
|
-
import { Context } from "../types/types.js";
|
|
3
2
|
import { FindOpts } from "../types/options.js";
|
|
3
|
+
import { Context } from "../types/types.js";
|
|
4
4
|
/**
|
|
5
5
|
* Asynchronously finds entries in a file based on search criteria.
|
|
6
6
|
*/
|
|
@@ -9,4 +9,3 @@ export declare function find(file: string, arg: Search, context?: Context, findO
|
|
|
9
9
|
* Asynchronously finds one entry in a file based on search criteria.
|
|
10
10
|
*/
|
|
11
11
|
export declare function findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
|
|
12
|
-
export declare function findStream(file: string, arg: Search, context?: Context, findOpts?: FindOpts, limit?: number): AsyncGenerator<any>;
|
package/dist/file/find.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { existsSync, promises } from "fs";
|
|
2
|
-
import { pathRepair, createRL } from "./utils.js";
|
|
3
2
|
import { parse } from "../helpers/format.js";
|
|
4
3
|
import hasFieldsAdvanced from "../utils/hasFieldsAdvanced.js";
|
|
5
4
|
import updateFindObject from "../utils/updateFindObject.js";
|
|
5
|
+
import { createRL, pathRepair } from "./utils.js";
|
|
6
6
|
/**
|
|
7
7
|
* Processes a line of text from a file and checks if it matches the search criteria.
|
|
8
8
|
*/
|
|
@@ -71,30 +71,3 @@ export async function findOne(file, arg, context = {}, findOpts = {}) {
|
|
|
71
71
|
resolve(false);
|
|
72
72
|
});
|
|
73
73
|
}
|
|
74
|
-
export async function* findStream(file, arg, context = {}, findOpts = {}, limit = -1) {
|
|
75
|
-
file = pathRepair(file);
|
|
76
|
-
if (!existsSync(file)) {
|
|
77
|
-
await promises.writeFile(file, "");
|
|
78
|
-
return;
|
|
79
|
-
}
|
|
80
|
-
const rl = createRL(file);
|
|
81
|
-
try {
|
|
82
|
-
let count = 0;
|
|
83
|
-
for await (const line of rl) {
|
|
84
|
-
if (!line?.trim())
|
|
85
|
-
continue;
|
|
86
|
-
const res = await findProcesLine(arg, line, context, findOpts);
|
|
87
|
-
if (res) {
|
|
88
|
-
yield res;
|
|
89
|
-
count++;
|
|
90
|
-
if (limit > 0 && count >= limit) {
|
|
91
|
-
rl.close();
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
finally {
|
|
98
|
-
rl.close();
|
|
99
|
-
}
|
|
100
|
-
}
|
package/dist/file/index.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import update from "./update.js";
|
|
2
2
|
import remove from "./remove.js";
|
|
3
|
-
import { find, findOne
|
|
3
|
+
import { find, findOne } from "./find.js";
|
|
4
4
|
import { appendFileSync } from "fs";
|
|
5
5
|
import { stringify } from "../helpers/format.js";
|
|
6
|
-
import transactions from "./transactions.js";
|
|
7
6
|
const vFileCpu = {
|
|
8
7
|
add: async (file, data) => {
|
|
9
8
|
const dataString = stringify(data);
|
|
@@ -11,9 +10,7 @@ const vFileCpu = {
|
|
|
11
10
|
},
|
|
12
11
|
find,
|
|
13
12
|
findOne,
|
|
14
|
-
findStream,
|
|
15
13
|
update,
|
|
16
14
|
remove,
|
|
17
|
-
transactions
|
|
18
15
|
};
|
|
19
16
|
export default vFileCpu;
|
|
@@ -19,10 +19,6 @@ declare class CollectionManager {
|
|
|
19
19
|
* Find one data entry in a database.
|
|
20
20
|
*/
|
|
21
21
|
findOne<T = Data>(search: Search, context?: Context, findOpts?: FindOpts): Promise<T>;
|
|
22
|
-
/**
|
|
23
|
-
* Find data in a database as a stream.
|
|
24
|
-
*/
|
|
25
|
-
findStream<T = Data>(search: Search, context?: Context, findOpts?: FindOpts, limit?: number): AsyncGenerator<T>;
|
|
26
22
|
/**
|
|
27
23
|
* Update data in a database.
|
|
28
24
|
*/
|
|
@@ -23,12 +23,6 @@ class CollectionManager {
|
|
|
23
23
|
async findOne(search, context = {}, findOpts = {}) {
|
|
24
24
|
return await this.db.findOne(this.collection, search, context, findOpts);
|
|
25
25
|
}
|
|
26
|
-
/**
|
|
27
|
-
* Find data in a database as a stream.
|
|
28
|
-
*/
|
|
29
|
-
async *findStream(search, context = {}, findOpts = {}, limit = -1) {
|
|
30
|
-
return await this.db.findStream(this.collection, search, context, findOpts, limit);
|
|
31
|
-
}
|
|
32
26
|
/**
|
|
33
27
|
* Update data in a database.
|
|
34
28
|
*/
|
package/dist/types/fileCpu.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Search, Updater } from "./arg.js";
|
|
2
2
|
import Data from "./data.js";
|
|
3
3
|
import { FindOpts } from "./options.js";
|
|
4
|
-
import { Transaction } from "./transactions.js";
|
|
5
4
|
import { Context } from "./types.js";
|
|
6
5
|
interface FileCpu {
|
|
7
6
|
/**
|
|
@@ -29,16 +28,6 @@ interface FileCpu {
|
|
|
29
28
|
* @returns A promise resolving to the found entry or `false` if not found.
|
|
30
29
|
*/
|
|
31
30
|
findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
|
|
32
|
-
/**
|
|
33
|
-
* Asynchronously finds entries in a file based on search criteria and returns a stream of results.
|
|
34
|
-
* @param file The path to the file.
|
|
35
|
-
* @param arg The search criteria.
|
|
36
|
-
* @param context Additional context for the search.
|
|
37
|
-
* @param findOpts Additional options for searching.
|
|
38
|
-
* @param limit The maximum number of entries to return.
|
|
39
|
-
* @returns An async generator yielding found entries.
|
|
40
|
-
*/
|
|
41
|
-
findStream(file: string, arg: Search, context?: Context, findOpts?: FindOpts, limit?: number): AsyncGenerator<any>;
|
|
42
31
|
/**
|
|
43
32
|
* Asynchronously removes entries from a file based on search criteria.
|
|
44
33
|
* @param file The path to the file.
|
|
@@ -58,12 +47,5 @@ interface FileCpu {
|
|
|
58
47
|
* @returns A promise resolving to `true` if at least one entry was updated, otherwise `false`.
|
|
59
48
|
*/
|
|
60
49
|
update(file: string, one: boolean, arg: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
61
|
-
/**
|
|
62
|
-
* Executes a list of transactions on the specified database collection.
|
|
63
|
-
* @param file The path to the file.
|
|
64
|
-
* @param transactions An array of transactions to execute.
|
|
65
|
-
* @returns A promise resolved when all transactions have been executed.
|
|
66
|
-
*/
|
|
67
|
-
transactions(file: string, transactions: Transaction[]): Promise<void>;
|
|
68
50
|
}
|
|
69
51
|
export default FileCpu;
|
package/dist/types/options.d.ts
CHANGED
package/dist/types/query.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Context } from "./types.js";
|
|
2
2
|
import { Arg, Search, Updater } from "./arg.js";
|
|
3
3
|
import { DbFindOpts, FindOpts } from "./options.js";
|
|
4
|
-
import { Transaction } from "./transactions.js";
|
|
5
4
|
export interface VQuery {
|
|
6
5
|
collection?: string;
|
|
7
6
|
search?: Search;
|
|
@@ -13,5 +12,4 @@ export interface VQuery {
|
|
|
13
12
|
limit?: number;
|
|
14
13
|
add_arg?: Arg;
|
|
15
14
|
updater?: Updater;
|
|
16
|
-
transaction?: Transaction[];
|
|
17
15
|
}
|
package/dist/types/valthera.d.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { Arg, Search, Updater } from "./arg.js";
|
|
|
3
3
|
import Data from "./data.js";
|
|
4
4
|
import { DbFindOpts, FindOpts } from "./options.js";
|
|
5
5
|
import { VQuery } from "./query.js";
|
|
6
|
-
import { Transaction } from "./transactions.js";
|
|
7
6
|
import { Context } from "./types.js";
|
|
8
7
|
export interface ValtheraCompatible {
|
|
9
8
|
c(collection: string): CollectionManager;
|
|
@@ -13,13 +12,11 @@ export interface ValtheraCompatible {
|
|
|
13
12
|
add<T = Data>(collection: string, data: Arg, id_gen?: boolean): Promise<T>;
|
|
14
13
|
find<T = Data>(collection: string, search: Search, context?: Context, options?: DbFindOpts, findOpts?: FindOpts): Promise<T[]>;
|
|
15
14
|
findOne<T = Data>(collection: string, search: Search, context?: Context, findOpts?: FindOpts): Promise<T | null>;
|
|
16
|
-
findStream<T = Data>(collection: string, search: Search, context?: Context, findOpts?: FindOpts, limit?: number): Promise<AsyncGenerator<T>>;
|
|
17
15
|
update(collection: string, search: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
18
16
|
updateOne(collection: string, search: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
19
17
|
remove(collection: string, search: Search, context?: Context): Promise<boolean>;
|
|
20
18
|
removeOne(collection: string, search: Search, context?: Context): Promise<boolean>;
|
|
21
19
|
removeCollection(collection: string): Promise<boolean>;
|
|
22
|
-
transaction(collection: string, transaction: Transaction[]): Promise<boolean>;
|
|
23
20
|
updateOneOrAdd(collection: string, search: Search, updater: Updater, add_arg?: Arg, context?: Context, id_gen?: boolean): Promise<boolean>;
|
|
24
21
|
}
|
|
25
22
|
export interface ValtheraCompatibleInternal {
|
|
@@ -30,12 +27,10 @@ export interface ValtheraCompatibleInternal {
|
|
|
30
27
|
add<T = Data>(config: VQuery): Promise<T>;
|
|
31
28
|
find<T = Data>(config: VQuery): Promise<T[]>;
|
|
32
29
|
findOne<T = Data>(config: VQuery): Promise<T | null>;
|
|
33
|
-
findStream<T = Data>(config: VQuery): Promise<AsyncGenerator<T>>;
|
|
34
30
|
update(config: VQuery): Promise<boolean>;
|
|
35
31
|
updateOne(config: VQuery): Promise<boolean>;
|
|
36
32
|
remove(config: VQuery): Promise<boolean>;
|
|
37
33
|
removeOne(config: VQuery): Promise<boolean>;
|
|
38
34
|
removeCollection(config: VQuery): Promise<boolean>;
|
|
39
|
-
transaction(config: VQuery): Promise<boolean>;
|
|
40
35
|
updateOneOrAdd(config: VQuery): Promise<boolean>;
|
|
41
36
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function compareSafe(a: any, b: any): -1 | 0 | 1;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export function compareSafe(a, b) {
|
|
2
|
+
// Null/undefined at the end
|
|
3
|
+
if (a == null && b != null)
|
|
4
|
+
return 1;
|
|
5
|
+
if (a != null && b == null)
|
|
6
|
+
return -1;
|
|
7
|
+
if (a == null && b == null)
|
|
8
|
+
return 0;
|
|
9
|
+
// Compare primitives and Date
|
|
10
|
+
const typeA = typeof a;
|
|
11
|
+
const typeB = typeof b;
|
|
12
|
+
if (typeA === "string" && typeB === "string") {
|
|
13
|
+
return a.localeCompare(b);
|
|
14
|
+
}
|
|
15
|
+
if ((typeA === "number" || a instanceof Date) && (typeB === "number" || b instanceof Date)) {
|
|
16
|
+
return (a < b) ? -1 : (a > b) ? 1 : 0;
|
|
17
|
+
}
|
|
18
|
+
if (typeA === "boolean" && typeB === "boolean") {
|
|
19
|
+
return (a === b) ? 0 : a ? 1 : -1;
|
|
20
|
+
}
|
|
21
|
+
// fallback: if both are objects/arrays or different types, consider equal
|
|
22
|
+
return 0;
|
|
23
|
+
}
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.
|
|
1
|
+
export const version = "0.10.0";
|
package/package.json
CHANGED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { existsSync, promises } from "fs";
|
|
2
|
-
import { parse, stringify } from "../helpers/format.js";
|
|
3
|
-
import genId from "../helpers/gen.js";
|
|
4
|
-
import hasFieldsAdvanced from "../utils/hasFieldsAdvanced.js";
|
|
5
|
-
import updateObjectAdvanced from "../utils/updateObject.js";
|
|
6
|
-
import { createRL, pathRepair } from "./utils.js";
|
|
7
|
-
async function processTransactions(file, transactions) {
|
|
8
|
-
file = pathRepair(file);
|
|
9
|
-
const tempFile = file + ".tmp";
|
|
10
|
-
const processedTransactions = transactions.map(t => ({
|
|
11
|
-
...t,
|
|
12
|
-
applied: false
|
|
13
|
-
}));
|
|
14
|
-
if (existsSync(file)) {
|
|
15
|
-
await promises.copyFile(file, tempFile);
|
|
16
|
-
}
|
|
17
|
-
else {
|
|
18
|
-
await promises.writeFile(file, "");
|
|
19
|
-
await promises.writeFile(tempFile, "");
|
|
20
|
-
}
|
|
21
|
-
await promises.writeFile(file, "");
|
|
22
|
-
const rl = createRL(tempFile);
|
|
23
|
-
for await (const line of rl) {
|
|
24
|
-
const originalLine = line.trim();
|
|
25
|
-
if (!originalLine)
|
|
26
|
-
continue;
|
|
27
|
-
let data = parse(originalLine);
|
|
28
|
-
let shouldRemove = false;
|
|
29
|
-
let modified = false;
|
|
30
|
-
for (const transaction of processedTransactions) {
|
|
31
|
-
if (shouldRemove)
|
|
32
|
-
break;
|
|
33
|
-
let matches = false;
|
|
34
|
-
if (typeof transaction.search === 'function') {
|
|
35
|
-
matches = transaction.search(data, transaction.context || {}) || false;
|
|
36
|
-
}
|
|
37
|
-
else if (typeof transaction.search === 'object') {
|
|
38
|
-
matches = hasFieldsAdvanced(data, transaction.search);
|
|
39
|
-
}
|
|
40
|
-
if (!matches)
|
|
41
|
-
continue;
|
|
42
|
-
switch (transaction.type) {
|
|
43
|
-
case 'update':
|
|
44
|
-
if (transaction.updater) {
|
|
45
|
-
data = applyUpdater(data, transaction.updater, transaction.context || {});
|
|
46
|
-
modified = true;
|
|
47
|
-
}
|
|
48
|
-
break;
|
|
49
|
-
case 'updateOne':
|
|
50
|
-
if (!transaction.applied && transaction.updater) {
|
|
51
|
-
data = applyUpdater(data, transaction.updater, transaction.context || {});
|
|
52
|
-
modified = true;
|
|
53
|
-
transaction.applied = true;
|
|
54
|
-
}
|
|
55
|
-
break;
|
|
56
|
-
case 'updateOneOrAdd':
|
|
57
|
-
if (!transaction.applied && transaction.updater) {
|
|
58
|
-
data = applyUpdater(data, transaction.updater, transaction.context || {});
|
|
59
|
-
modified = true;
|
|
60
|
-
transaction.applied = true;
|
|
61
|
-
}
|
|
62
|
-
break;
|
|
63
|
-
case 'remove':
|
|
64
|
-
shouldRemove = true;
|
|
65
|
-
modified = true;
|
|
66
|
-
break;
|
|
67
|
-
case 'removeOne':
|
|
68
|
-
if (!transaction.applied) {
|
|
69
|
-
shouldRemove = true;
|
|
70
|
-
modified = true;
|
|
71
|
-
transaction.applied = true;
|
|
72
|
-
}
|
|
73
|
-
break;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
if (!shouldRemove) {
|
|
77
|
-
const outputLine = modified ? await stringify(data) : originalLine;
|
|
78
|
-
await promises.appendFile(file, outputLine + "\n");
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
for (const transaction of processedTransactions) {
|
|
82
|
-
if (transaction.type === 'updateOneOrAdd' && !transaction.applied) {
|
|
83
|
-
const assignData = [];
|
|
84
|
-
if (typeof transaction.search === 'object' && !Array.isArray(transaction.search)) {
|
|
85
|
-
assignData.push(transaction.search);
|
|
86
|
-
}
|
|
87
|
-
if (transaction.updater && typeof transaction.updater === 'object' && !Array.isArray(transaction.updater)) {
|
|
88
|
-
const newData = {};
|
|
89
|
-
Object.keys(transaction.updater).filter(key => !key.startsWith('$')).forEach(key => newData[key] = transaction.updater[key]);
|
|
90
|
-
assignData.push(newData);
|
|
91
|
-
}
|
|
92
|
-
if (transaction.addArg && typeof transaction.addArg === 'object' && !Array.isArray(transaction.addArg)) {
|
|
93
|
-
assignData.push(transaction.addArg);
|
|
94
|
-
}
|
|
95
|
-
let newData = Object.assign({}, ...assignData);
|
|
96
|
-
if (transaction.updater && typeof transaction.updater === 'object' && !Array.isArray(transaction.updater)) {
|
|
97
|
-
newData = applyUpdater(newData, transaction.updater, transaction.context || {});
|
|
98
|
-
}
|
|
99
|
-
await add(file, newData, transaction.idGen !== false);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
await promises.unlink(tempFile);
|
|
103
|
-
}
|
|
104
|
-
function applyUpdater(data, updater, context = {}) {
|
|
105
|
-
if (typeof updater === 'function') {
|
|
106
|
-
const result = updater(data, context);
|
|
107
|
-
return data === null ? result : (result || data);
|
|
108
|
-
}
|
|
109
|
-
if (typeof updater === 'object' && !Array.isArray(updater)) {
|
|
110
|
-
return updateObjectAdvanced(data || {}, updater);
|
|
111
|
-
}
|
|
112
|
-
return data;
|
|
113
|
-
}
|
|
114
|
-
async function add(file, data, idGen) {
|
|
115
|
-
if (idGen && !data._id)
|
|
116
|
-
data._id = genId();
|
|
117
|
-
await promises.appendFile(file, await stringify(data) + "\n");
|
|
118
|
-
}
|
|
119
|
-
export default processTransactions;
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { Arg, Search, Updater } from "./arg.js";
|
|
2
|
-
import { Context } from "./types.js";
|
|
3
|
-
export interface Transaction {
|
|
4
|
-
type: 'update' | 'updateOne' | 'updateOneOrAdd' | 'remove' | 'removeOne';
|
|
5
|
-
search: Search;
|
|
6
|
-
updater?: Updater;
|
|
7
|
-
addArg?: Arg;
|
|
8
|
-
idGen?: boolean;
|
|
9
|
-
context?: Context;
|
|
10
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|