@wxn0brp/db-core 0.11.5 → 0.12.0-alpha.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/base/actions.d.ts +6 -1
- package/dist/base/actions.js +21 -6
- package/dist/base/custom.d.ts +0 -1
- package/dist/base/custom.js +4 -5
- package/dist/customFileCpu.js +4 -6
- package/dist/db/memory.js +8 -6
- package/dist/db/multiStorage.js +24 -16
- package/dist/db/routedStorage.js +16 -6
- package/dist/db/valthera.d.ts +1 -26
- package/dist/db/valthera.js +21 -21
- package/dist/helpers/addId.js +16 -7
- package/dist/helpers/assignDataPush.js +1 -1
- package/dist/helpers/collection.d.ts +0 -3
- package/dist/helpers/collection.js +54 -12
- package/dist/helpers/executor.js +12 -7
- package/dist/helpers/forge.d.ts +0 -6
- package/dist/helpers/forge.js +1 -4
- package/dist/helpers/gen.js +11 -4
- package/dist/helpers/relation.d.ts +4 -1
- package/dist/helpers/relation.js +82 -23
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/types/action.d.ts +3 -1
- package/dist/types/export.d.ts +1 -0
- package/dist/types/fileCpu.d.ts +4 -4
- package/dist/types/options.d.ts +12 -12
- package/dist/types/query.js +1 -5
- package/dist/types/relation.d.ts +4 -1
- package/dist/types/searchOpts.d.ts +10 -13
- package/dist/types/updater.d.ts +0 -12
- package/dist/utils/action.js +69 -13
- package/dist/utils/compare.js +4 -3
- package/dist/utils/hasFieldsAdvanced.js +6 -1
- package/dist/utils/id.d.ts +10 -0
- package/dist/utils/id.js +10 -2
- package/dist/utils/merge.js +7 -3
- package/dist/utils/process.d.ts +3 -0
- package/dist/utils/process.js +10 -3
- package/dist/utils/updateFindObject.js +3 -2
- package/dist/utils/updateObject.d.ts +1 -1
- package/dist/utils/updateObject.js +15 -11
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/base/actions.d.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { ActionsBaseInterface } from "../types/action.js";
|
|
2
2
|
import { DataInternal } from "../types/data.js";
|
|
3
|
+
import { AdapterOpts } from "../types/options.js";
|
|
3
4
|
import { VQueryT } from "../types/query.js";
|
|
4
5
|
export declare abstract class ActionsBase implements ActionsBaseInterface {
|
|
5
6
|
_inited: boolean;
|
|
6
|
-
|
|
7
|
+
adapterOpts: AdapterOpts;
|
|
7
8
|
smartExecutor: boolean;
|
|
9
|
+
get numberId(): boolean;
|
|
10
|
+
set numberId(value: boolean);
|
|
11
|
+
get idKey(): string;
|
|
12
|
+
set idKey(value: string);
|
|
8
13
|
init(...args: any[]): Promise<void>;
|
|
9
14
|
close(...args: any[]): Promise<void>;
|
|
10
15
|
abstract getCollections(): Promise<string[]>;
|
package/dist/base/actions.js
CHANGED
|
@@ -1,8 +1,23 @@
|
|
|
1
|
-
import { setDataForToggleOne, setDataForUpdateOneOrAdd } from "../helpers/assignDataPush.js";
|
|
1
|
+
import { setDataForToggleOne, setDataForUpdateOneOrAdd, } from "../helpers/assignDataPush.js";
|
|
2
2
|
export class ActionsBase {
|
|
3
3
|
_inited = true;
|
|
4
|
-
|
|
4
|
+
adapterOpts = {
|
|
5
|
+
numberId: false,
|
|
6
|
+
idKey: "_id",
|
|
7
|
+
};
|
|
5
8
|
smartExecutor = false;
|
|
9
|
+
get numberId() {
|
|
10
|
+
return this.adapterOpts.numberId ?? false;
|
|
11
|
+
}
|
|
12
|
+
set numberId(value) {
|
|
13
|
+
this.adapterOpts.numberId = value;
|
|
14
|
+
}
|
|
15
|
+
get idKey() {
|
|
16
|
+
return this.adapterOpts.idKey ?? "_id";
|
|
17
|
+
}
|
|
18
|
+
set idKey(value) {
|
|
19
|
+
this.adapterOpts.idKey = value;
|
|
20
|
+
}
|
|
6
21
|
async init(...args) { }
|
|
7
22
|
async close(...args) { }
|
|
8
23
|
async updateOneOrAdd(config) {
|
|
@@ -10,13 +25,13 @@ export class ActionsBase {
|
|
|
10
25
|
if (res)
|
|
11
26
|
return {
|
|
12
27
|
data: res,
|
|
13
|
-
type: "updated"
|
|
28
|
+
type: "updated",
|
|
14
29
|
};
|
|
15
30
|
// transform UpdateOneQuery to AddQuery
|
|
16
31
|
setDataForUpdateOneOrAdd(config);
|
|
17
32
|
return {
|
|
18
33
|
data: await this.add(config),
|
|
19
|
-
type: "added"
|
|
34
|
+
type: "added",
|
|
20
35
|
};
|
|
21
36
|
}
|
|
22
37
|
async toggleOne(config) {
|
|
@@ -24,13 +39,13 @@ export class ActionsBase {
|
|
|
24
39
|
if (res)
|
|
25
40
|
return {
|
|
26
41
|
data: res,
|
|
27
|
-
type: "removed"
|
|
42
|
+
type: "removed",
|
|
28
43
|
};
|
|
29
44
|
// transform ToggleOneQuery to AddQuery
|
|
30
45
|
setDataForToggleOne(config);
|
|
31
46
|
return {
|
|
32
47
|
data: await this.add(config),
|
|
33
|
-
type: "added"
|
|
48
|
+
type: "added",
|
|
34
49
|
};
|
|
35
50
|
}
|
|
36
51
|
}
|
package/dist/base/custom.d.ts
CHANGED
package/dist/base/custom.js
CHANGED
|
@@ -3,9 +3,6 @@ import { findUtil } from "../utils/action.js";
|
|
|
3
3
|
import { ActionsBase } from "./actions.js";
|
|
4
4
|
export class CustomActionsBase extends ActionsBase {
|
|
5
5
|
fileCpu;
|
|
6
|
-
constructor() {
|
|
7
|
-
super();
|
|
8
|
-
}
|
|
9
6
|
/**
|
|
10
7
|
* Add a new entry to the specified database.
|
|
11
8
|
*/
|
|
@@ -21,7 +18,9 @@ export class CustomActionsBase extends ActionsBase {
|
|
|
21
18
|
*/
|
|
22
19
|
async find(query) {
|
|
23
20
|
await this.ensureCollection(query.collection);
|
|
24
|
-
const data = await findUtil(query, this.fileCpu, [
|
|
21
|
+
const data = await findUtil(query, this.fileCpu, [
|
|
22
|
+
query.collection,
|
|
23
|
+
]);
|
|
25
24
|
return data;
|
|
26
25
|
}
|
|
27
26
|
/**
|
|
@@ -29,7 +28,7 @@ export class CustomActionsBase extends ActionsBase {
|
|
|
29
28
|
*/
|
|
30
29
|
async findOne(query) {
|
|
31
30
|
await this.ensureCollection(query.collection);
|
|
32
|
-
|
|
31
|
+
const data = (await this.fileCpu.findOne(query.collection, query));
|
|
33
32
|
return data || null;
|
|
34
33
|
}
|
|
35
34
|
/**
|
package/dist/customFileCpu.js
CHANGED
|
@@ -11,16 +11,14 @@ export class CustomFileCpu {
|
|
|
11
11
|
}
|
|
12
12
|
async add(file, config) {
|
|
13
13
|
file = pathRepair(file);
|
|
14
|
-
|
|
14
|
+
const entries = await this._readFile(file);
|
|
15
15
|
entries.push(config.data);
|
|
16
16
|
await this._writeFile(file, entries);
|
|
17
17
|
}
|
|
18
18
|
async find(file, config) {
|
|
19
19
|
file = pathRepair(file);
|
|
20
20
|
const entries = await this._readFile(file);
|
|
21
|
-
return entries
|
|
22
|
-
.map((entry) => findObj(config, entry))
|
|
23
|
-
.filter(Boolean);
|
|
21
|
+
return entries.map(entry => findObj(config, entry)).filter(Boolean);
|
|
24
22
|
}
|
|
25
23
|
async findOne(file, config) {
|
|
26
24
|
file = pathRepair(file);
|
|
@@ -36,7 +34,7 @@ export class CustomFileCpu {
|
|
|
36
34
|
file = pathRepair(file);
|
|
37
35
|
let entries = await this._readFile(file);
|
|
38
36
|
const removed = [];
|
|
39
|
-
entries = entries.filter(
|
|
37
|
+
entries = entries.filter(entry => {
|
|
40
38
|
if (removed.length && one)
|
|
41
39
|
return true;
|
|
42
40
|
if (matchObj(config, entry)) {
|
|
@@ -53,7 +51,7 @@ export class CustomFileCpu {
|
|
|
53
51
|
file = pathRepair(file);
|
|
54
52
|
let entries = await this._readFile(file);
|
|
55
53
|
const updated = [];
|
|
56
|
-
entries = entries.map(
|
|
54
|
+
entries = entries.map(entry => {
|
|
57
55
|
if (updated.length && one)
|
|
58
56
|
return entry;
|
|
59
57
|
if (matchObj(config, entry)) {
|
package/dist/db/memory.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { CustomActionsBase } from "../base/custom.js";
|
|
2
2
|
import { CustomFileCpu } from "../customFileCpu.js";
|
|
3
|
-
import {
|
|
3
|
+
import { forgeTypedValthera } from "../helpers/forge.js";
|
|
4
4
|
import { ValtheraClass } from "./valthera.js";
|
|
5
5
|
export class MemoryAction extends CustomActionsBase {
|
|
6
6
|
memory;
|
|
@@ -13,10 +13,10 @@ export class MemoryAction extends CustomActionsBase {
|
|
|
13
13
|
_readMemory(key) {
|
|
14
14
|
if (!this.memory.has(key))
|
|
15
15
|
return [];
|
|
16
|
-
return this.memory.get(key);
|
|
16
|
+
return structuredClone(this.memory.get(key));
|
|
17
17
|
}
|
|
18
18
|
_writeMemory(key, data) {
|
|
19
|
-
this.memory.set(key, data);
|
|
19
|
+
this.memory.set(key, structuredClone(data));
|
|
20
20
|
}
|
|
21
21
|
async getCollections() {
|
|
22
22
|
const collections = Array.from(this.memory.keys());
|
|
@@ -40,15 +40,17 @@ export class MemoryAction extends CustomActionsBase {
|
|
|
40
40
|
}
|
|
41
41
|
export class ValtheraMemory extends ValtheraClass {
|
|
42
42
|
constructor(...args) {
|
|
43
|
-
super({
|
|
43
|
+
super({
|
|
44
|
+
adapter: new MemoryAction(),
|
|
45
|
+
});
|
|
44
46
|
}
|
|
45
47
|
}
|
|
46
48
|
export function createMemoryValthera(data) {
|
|
47
49
|
const db = new ValtheraMemory();
|
|
48
50
|
if (!data)
|
|
49
|
-
return
|
|
51
|
+
return forgeTypedValthera(db);
|
|
50
52
|
for (const collection of Object.keys(data)) {
|
|
51
53
|
db.adapter.memory.set(collection, data[collection]);
|
|
52
54
|
}
|
|
53
|
-
return
|
|
55
|
+
return forgeTypedValthera(db);
|
|
54
56
|
}
|
package/dist/db/multiStorage.js
CHANGED
|
@@ -14,57 +14,65 @@ export class MultiBackend extends ActionsBase {
|
|
|
14
14
|
await Promise.all(this.backends.map(b => b.close(...args)));
|
|
15
15
|
}
|
|
16
16
|
async add(config) {
|
|
17
|
-
return await this.primaryBackend.add({
|
|
17
|
+
return await this.primaryBackend.add({
|
|
18
|
+
...config,
|
|
19
|
+
});
|
|
18
20
|
}
|
|
19
21
|
async find(config) {
|
|
20
|
-
const results = await Promise.all(this.backends.map(b => b.find({
|
|
22
|
+
const results = await Promise.all(this.backends.map(b => b.find({
|
|
23
|
+
...config,
|
|
24
|
+
})));
|
|
21
25
|
return results.flat();
|
|
22
26
|
}
|
|
23
27
|
async findOne(config) {
|
|
24
28
|
for (const backend of this.backends) {
|
|
25
29
|
try {
|
|
26
|
-
const result = await backend.findOne({
|
|
30
|
+
const result = await backend.findOne({
|
|
31
|
+
...config,
|
|
32
|
+
});
|
|
27
33
|
if (result !== null && result !== undefined) {
|
|
28
34
|
return result;
|
|
29
35
|
}
|
|
30
36
|
}
|
|
31
|
-
catch {
|
|
32
|
-
continue;
|
|
33
|
-
}
|
|
37
|
+
catch { }
|
|
34
38
|
}
|
|
35
39
|
return null;
|
|
36
40
|
}
|
|
37
41
|
async update(config) {
|
|
38
|
-
const results = await Promise.all(this.backends.map(b => b.update({
|
|
42
|
+
const results = await Promise.all(this.backends.map(b => b.update({
|
|
43
|
+
...config,
|
|
44
|
+
})));
|
|
39
45
|
return results.flat();
|
|
40
46
|
}
|
|
41
47
|
async updateOne(config) {
|
|
42
48
|
for (const backend of this.backends) {
|
|
43
49
|
try {
|
|
44
|
-
const result = await backend.updateOne({
|
|
50
|
+
const result = await backend.updateOne({
|
|
51
|
+
...config,
|
|
52
|
+
});
|
|
45
53
|
if (result)
|
|
46
54
|
return result;
|
|
47
55
|
}
|
|
48
|
-
catch (error) {
|
|
49
|
-
continue;
|
|
50
|
-
}
|
|
56
|
+
catch (error) { }
|
|
51
57
|
}
|
|
52
58
|
return null;
|
|
53
59
|
}
|
|
54
60
|
async remove(config) {
|
|
55
|
-
const results = await Promise.all(this.backends.map(b => b.remove({
|
|
61
|
+
const results = await Promise.all(this.backends.map(b => b.remove({
|
|
62
|
+
...config,
|
|
63
|
+
})));
|
|
56
64
|
return results.flat();
|
|
57
65
|
}
|
|
58
66
|
async removeOne(config) {
|
|
59
67
|
for (const backend of this.backends) {
|
|
60
68
|
try {
|
|
61
|
-
const result = await backend.removeOne({
|
|
69
|
+
const result = await backend.removeOne({
|
|
70
|
+
...config,
|
|
71
|
+
});
|
|
62
72
|
if (result)
|
|
63
73
|
return result;
|
|
64
74
|
}
|
|
65
|
-
catch (error) {
|
|
66
|
-
continue;
|
|
67
|
-
}
|
|
75
|
+
catch (error) { }
|
|
68
76
|
}
|
|
69
77
|
return null;
|
|
70
78
|
}
|
package/dist/db/routedStorage.js
CHANGED
|
@@ -5,14 +5,19 @@ export class RoutedStorage extends ActionsBase {
|
|
|
5
5
|
constructor(rules, defaultBackend) {
|
|
6
6
|
super();
|
|
7
7
|
this.rules = rules;
|
|
8
|
-
this.defaultBackend = Array.isArray(defaultBackend)
|
|
8
|
+
this.defaultBackend = Array.isArray(defaultBackend)
|
|
9
|
+
? defaultBackend
|
|
10
|
+
: [
|
|
11
|
+
defaultBackend,
|
|
12
|
+
];
|
|
9
13
|
}
|
|
10
14
|
_matchBackends(config) {
|
|
11
15
|
const matched = [];
|
|
12
16
|
const { collection } = config;
|
|
13
17
|
for (const rule of this.rules) {
|
|
14
18
|
const { match, backends } = rule;
|
|
15
|
-
if ((typeof match === "string" &&
|
|
19
|
+
if ((typeof match === "string" && match === collection) ||
|
|
20
|
+
match === "*" ||
|
|
16
21
|
(match instanceof RegExp && match.test(collection)) ||
|
|
17
22
|
(typeof match === "function" && match(config))) {
|
|
18
23
|
matched.push(...backends);
|
|
@@ -44,13 +49,19 @@ export class RoutedStorage extends ActionsBase {
|
|
|
44
49
|
async close(...args) {
|
|
45
50
|
const all = new Set([
|
|
46
51
|
...this.rules.flatMap(r => r.backends),
|
|
47
|
-
...this.defaultBackend
|
|
52
|
+
...this.defaultBackend,
|
|
48
53
|
]);
|
|
49
54
|
await Promise.all(Array.from(all).map(b => b.close(...args)));
|
|
50
55
|
}
|
|
51
56
|
async add(config) {
|
|
52
57
|
const res = await this._withFirst(config, b => b.add(config));
|
|
53
|
-
await this._withAll({
|
|
58
|
+
await this._withAll({
|
|
59
|
+
...config,
|
|
60
|
+
data: res,
|
|
61
|
+
}, b => b.add({
|
|
62
|
+
...config,
|
|
63
|
+
data: res,
|
|
64
|
+
}));
|
|
54
65
|
return res;
|
|
55
66
|
}
|
|
56
67
|
async find(config) {
|
|
@@ -81,8 +92,7 @@ export class RoutedStorage extends ActionsBase {
|
|
|
81
92
|
return this._withAll(collection, b => b.removeCollection(collection), true);
|
|
82
93
|
}
|
|
83
94
|
async getCollections() {
|
|
84
|
-
const all = await Promise.all(Array.from(new Set(this.rules.flatMap(r => r.backends)))
|
|
85
|
-
.map(b => b.getCollections()));
|
|
95
|
+
const all = await Promise.all(Array.from(new Set(this.rules.flatMap(r => r.backends))).map(b => b.getCollections()));
|
|
86
96
|
const merged = new Set(all.flat());
|
|
87
97
|
return Array.from(merged);
|
|
88
98
|
}
|
package/dist/db/valthera.d.ts
CHANGED
|
@@ -20,32 +20,10 @@ export declare class ValtheraClass implements ValtheraCompatible {
|
|
|
20
20
|
} & {
|
|
21
21
|
"*": (name: keyof ValtheraCompatible, query: VQuery, result: any) => void;
|
|
22
22
|
}>;
|
|
23
|
-
/** @deprecated typo */
|
|
24
|
-
emiter: VEE<{
|
|
25
|
-
c: (query: VQuery, result: Collection<unknown>) => void;
|
|
26
|
-
getCollections: (query: VQuery, result: string[]) => void;
|
|
27
|
-
ensureCollection: (query: VQuery, result: boolean) => void;
|
|
28
|
-
issetCollection: (query: VQuery, result: boolean) => void;
|
|
29
|
-
add: (query: VQuery, result: unknown) => void;
|
|
30
|
-
find: (query: VQuery, result: unknown[]) => void;
|
|
31
|
-
findOne: (query: VQuery, result: unknown) => void;
|
|
32
|
-
update: (query: VQuery, result: unknown[]) => void;
|
|
33
|
-
updateOne: (query: VQuery, result: unknown) => void;
|
|
34
|
-
remove: (query: VQuery, result: unknown[]) => void;
|
|
35
|
-
removeOne: (query: VQuery, result: unknown) => void;
|
|
36
|
-
removeCollection: (query: VQuery, result: boolean) => void;
|
|
37
|
-
updateOneOrAdd: (query: VQuery, result: VQueryT.UpdateOneOrAddResult<unknown>) => void;
|
|
38
|
-
toggleOne: (query: VQuery, result: VQueryT.ToggleOneResult<unknown>) => void;
|
|
39
|
-
} & {
|
|
40
|
-
"*": (name: keyof ValtheraCompatible, query: VQuery, result: any) => void;
|
|
41
|
-
}>;
|
|
42
23
|
version: string;
|
|
43
24
|
_plugins: ValtheraPlugin[];
|
|
25
|
+
_collections: Map<string, Collection<any>>;
|
|
44
26
|
plugin(p: ValtheraPlugin): () => void;
|
|
45
|
-
/** @deprecated use `adapter` */
|
|
46
|
-
get dbAction(): ActionsBase;
|
|
47
|
-
/** @deprecated use `adapter` */
|
|
48
|
-
set dbAction(action: ActionsBase);
|
|
49
27
|
constructor(options: DbOpts);
|
|
50
28
|
init(...args: any[]): Promise<any>;
|
|
51
29
|
close(...args: any[]): Promise<any>;
|
|
@@ -100,9 +78,6 @@ export declare class ValtheraClass implements ValtheraCompatible {
|
|
|
100
78
|
updateOneOrAdd<T = Data>(query: VQueryT.UpdateOneOrAdd<T>): Promise<VQueryT.UpdateOneOrAddResult<T>>;
|
|
101
79
|
/**
|
|
102
80
|
* Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
|
|
103
|
-
* Returns a promise resolving to `false` if the entry was found and removed,
|
|
104
|
-
* or `true` if the entry was added. The returned value reflects the state of the database
|
|
105
|
-
* after the operation.
|
|
106
81
|
*/
|
|
107
82
|
toggleOne<T = Data>(query: VQueryT.ToggleOne<T>): Promise<VQueryT.ToggleOneResult<T>>;
|
|
108
83
|
/**
|
package/dist/db/valthera.js
CHANGED
|
@@ -11,10 +11,9 @@ export class ValtheraClass {
|
|
|
11
11
|
adapter;
|
|
12
12
|
executor;
|
|
13
13
|
emitter = new VEE();
|
|
14
|
-
/** @deprecated typo */
|
|
15
|
-
emiter = this.emitter;
|
|
16
14
|
version = version;
|
|
17
15
|
_plugins = [];
|
|
16
|
+
_collections = new Map();
|
|
18
17
|
plugin(p) {
|
|
19
18
|
p.init?.(this);
|
|
20
19
|
this._plugins.push(p);
|
|
@@ -24,23 +23,17 @@ export class ValtheraClass {
|
|
|
24
23
|
this._plugins.splice(i, 1);
|
|
25
24
|
};
|
|
26
25
|
}
|
|
27
|
-
/** @deprecated use `adapter` */
|
|
28
|
-
get dbAction() {
|
|
29
|
-
return this.adapter;
|
|
30
|
-
}
|
|
31
|
-
/** @deprecated use `adapter` */
|
|
32
|
-
set dbAction(action) {
|
|
33
|
-
this.adapter = action;
|
|
34
|
-
}
|
|
35
26
|
constructor(options) {
|
|
36
27
|
this.options = options;
|
|
37
28
|
this.executor = options.executor || new SmartExecutor(undefined, false);
|
|
38
|
-
options.adapter ??= options.dbAction;
|
|
39
|
-
options.dbAction ??= options.adapter;
|
|
40
29
|
if (typeof options.adapter === "function")
|
|
41
30
|
return;
|
|
42
|
-
else
|
|
31
|
+
else {
|
|
43
32
|
this.adapter = options.adapter;
|
|
33
|
+
if (options.adapterOpts) {
|
|
34
|
+
this.adapter.adapterOpts = options.adapterOpts;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
44
37
|
}
|
|
45
38
|
async init(...args) {
|
|
46
39
|
if (this.adapter?._inited)
|
|
@@ -52,10 +45,16 @@ export class ValtheraClass {
|
|
|
52
45
|
if (typeof self.options.adapter === "function")
|
|
53
46
|
self.adapter = await self.options.adapter();
|
|
54
47
|
// if the executor is not set, and the action wants a smart executor
|
|
55
|
-
if (!self.options.executor &&
|
|
48
|
+
if (!self.options.executor &&
|
|
49
|
+
self.adapter.smartExecutor &&
|
|
50
|
+
self.executor instanceof SmartExecutor)
|
|
56
51
|
self.executor.aware = true;
|
|
57
|
-
if (self.options.
|
|
58
|
-
self.adapter.
|
|
52
|
+
if (self.options.adapterOpts) {
|
|
53
|
+
self.adapter.adapterOpts = {
|
|
54
|
+
...self.adapter.adapterOpts,
|
|
55
|
+
...self.options.adapterOpts,
|
|
56
|
+
};
|
|
57
|
+
}
|
|
59
58
|
await self.adapter.init(...args);
|
|
60
59
|
self.adapter._inited = true;
|
|
61
60
|
});
|
|
@@ -83,7 +82,7 @@ export class ValtheraClass {
|
|
|
83
82
|
if (idx < plugins.length)
|
|
84
83
|
return plugins[idx++].execute(ctx);
|
|
85
84
|
return self.executor.addOp(self.adapter[ctx.op].bind(self.adapter), ctx.query, typeof ctx.query === "string" ? ctx.query : ctx.query.collection);
|
|
86
|
-
}
|
|
85
|
+
},
|
|
87
86
|
};
|
|
88
87
|
const result = await ctx.next();
|
|
89
88
|
this.emitter.emit(ctx.op, ctx.query, result);
|
|
@@ -93,7 +92,11 @@ export class ValtheraClass {
|
|
|
93
92
|
* Create a new instance of a Collection class.
|
|
94
93
|
*/
|
|
95
94
|
c(collection) {
|
|
96
|
-
|
|
95
|
+
if (this._collections.has(collection))
|
|
96
|
+
return this._collections.get(collection);
|
|
97
|
+
const col = new Collection(this, collection);
|
|
98
|
+
this._collections.set(collection, col);
|
|
99
|
+
return col;
|
|
97
100
|
}
|
|
98
101
|
/**
|
|
99
102
|
* Get the names of all available databases.
|
|
@@ -185,9 +188,6 @@ export class ValtheraClass {
|
|
|
185
188
|
}
|
|
186
189
|
/**
|
|
187
190
|
* Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
|
|
188
|
-
* Returns a promise resolving to `false` if the entry was found and removed,
|
|
189
|
-
* or `true` if the entry was added. The returned value reflects the state of the database
|
|
190
|
-
* after the operation.
|
|
191
191
|
*/
|
|
192
192
|
toggleOne(query) {
|
|
193
193
|
query.data ||= {};
|
package/dist/helpers/addId.js
CHANGED
|
@@ -2,22 +2,31 @@ import { genId } from "./gen.js";
|
|
|
2
2
|
export async function addId(query, actions, defaultGen = true) {
|
|
3
3
|
const { collection, data } = query;
|
|
4
4
|
const id_gen = query.id_gen ?? defaultGen;
|
|
5
|
+
const { idKey = "_id", numberId } = actions.adapterOpts;
|
|
5
6
|
if (!id_gen)
|
|
6
7
|
return data;
|
|
7
|
-
if (data
|
|
8
|
+
if (data[idKey])
|
|
8
9
|
return data;
|
|
9
|
-
const { numberId } = actions;
|
|
10
10
|
if (!numberId) {
|
|
11
|
-
data
|
|
11
|
+
data[idKey] = genId();
|
|
12
12
|
return;
|
|
13
13
|
}
|
|
14
|
-
const find = await actions.findOne({
|
|
15
|
-
|
|
14
|
+
const find = (await actions.findOne({
|
|
15
|
+
collection: "__vdb_id",
|
|
16
|
+
search: {
|
|
17
|
+
c: collection,
|
|
18
|
+
},
|
|
19
|
+
}));
|
|
20
|
+
data[idKey] = find?.i ? find.i + 1 : 1;
|
|
16
21
|
await actions.updateOneOrAdd({
|
|
17
22
|
collection: "__vdb_id",
|
|
18
|
-
search: {
|
|
23
|
+
search: {
|
|
24
|
+
c: collection,
|
|
25
|
+
},
|
|
19
26
|
updater: {
|
|
20
|
-
$inc: {
|
|
27
|
+
$inc: {
|
|
28
|
+
i: 1,
|
|
29
|
+
},
|
|
21
30
|
},
|
|
22
31
|
id_gen: false,
|
|
23
32
|
});
|
|
@@ -46,9 +46,6 @@ export declare class Collection<D = Data> {
|
|
|
46
46
|
updateOneOrAdd(search: Search<D>, updater: Updater<D>, { add_arg, context, id_gen }?: UpdateOneOrAdd<D>): Promise<VQueryT.UpdateOneOrAddResult<D>>;
|
|
47
47
|
/**
|
|
48
48
|
* Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
|
|
49
|
-
* Returns a promise resolving to `false` if the entry was found and removed,
|
|
50
|
-
* or `true` if the entry was added. The returned value reflects the state of the database
|
|
51
|
-
* after the operation.
|
|
52
49
|
*/
|
|
53
50
|
toggleOne(search: Search<D>, data?: Arg<D>, context?: VContext): Promise<VQueryT.ToggleOneResult<D>>;
|
|
54
51
|
}
|
|
@@ -6,57 +6,99 @@ export class Collection {
|
|
|
6
6
|
this.collection = collection;
|
|
7
7
|
}
|
|
8
8
|
add(data, id_gen) {
|
|
9
|
-
return this.db.add({
|
|
9
|
+
return this.db.add({
|
|
10
|
+
collection: this.collection,
|
|
11
|
+
data,
|
|
12
|
+
id_gen,
|
|
13
|
+
});
|
|
10
14
|
}
|
|
11
15
|
/**
|
|
12
16
|
* Find data in a database.
|
|
13
17
|
*/
|
|
14
18
|
find(search = {}, options = {}, findOpts = {}, context = {}) {
|
|
15
|
-
return this.db.find({
|
|
19
|
+
return this.db.find({
|
|
20
|
+
collection: this.collection,
|
|
21
|
+
search,
|
|
22
|
+
dbFindOpts: options,
|
|
23
|
+
findOpts,
|
|
24
|
+
context,
|
|
25
|
+
});
|
|
16
26
|
}
|
|
17
27
|
/**
|
|
18
28
|
* Find one data entry in a database.
|
|
19
29
|
*/
|
|
20
30
|
findOne(search = {}, findOpts = {}, context = {}) {
|
|
21
|
-
return this.db.findOne({
|
|
31
|
+
return this.db.findOne({
|
|
32
|
+
collection: this.collection,
|
|
33
|
+
search,
|
|
34
|
+
findOpts,
|
|
35
|
+
context,
|
|
36
|
+
});
|
|
22
37
|
}
|
|
23
38
|
/**
|
|
24
39
|
* Update data in a database.
|
|
25
40
|
*/
|
|
26
41
|
update(search, updater, context = {}) {
|
|
27
|
-
return this.db.update({
|
|
42
|
+
return this.db.update({
|
|
43
|
+
collection: this.collection,
|
|
44
|
+
search,
|
|
45
|
+
updater,
|
|
46
|
+
context,
|
|
47
|
+
});
|
|
28
48
|
}
|
|
29
49
|
/**
|
|
30
50
|
* Update one data entry in a database.
|
|
31
51
|
*/
|
|
32
52
|
updateOne(search, updater, context = {}) {
|
|
33
|
-
return this.db.updateOne({
|
|
53
|
+
return this.db.updateOne({
|
|
54
|
+
collection: this.collection,
|
|
55
|
+
search,
|
|
56
|
+
updater,
|
|
57
|
+
context,
|
|
58
|
+
});
|
|
34
59
|
}
|
|
35
60
|
/**
|
|
36
61
|
* Remove data from a database.
|
|
37
62
|
*/
|
|
38
63
|
remove(search, context = {}) {
|
|
39
|
-
return this.db.remove({
|
|
64
|
+
return this.db.remove({
|
|
65
|
+
collection: this.collection,
|
|
66
|
+
search,
|
|
67
|
+
context,
|
|
68
|
+
});
|
|
40
69
|
}
|
|
41
70
|
/**
|
|
42
71
|
* Remove one data entry from a database.
|
|
43
72
|
*/
|
|
44
73
|
removeOne(search, context = {}) {
|
|
45
|
-
return this.db.removeOne({
|
|
74
|
+
return this.db.removeOne({
|
|
75
|
+
collection: this.collection,
|
|
76
|
+
search,
|
|
77
|
+
context,
|
|
78
|
+
});
|
|
46
79
|
}
|
|
47
80
|
/**
|
|
48
81
|
* Asynchronously updates one entry in a database or adds a new one if it doesn't exist.
|
|
49
82
|
*/
|
|
50
83
|
updateOneOrAdd(search, updater, { add_arg = {}, context = {}, id_gen = true } = {}) {
|
|
51
|
-
return this.db.updateOneOrAdd({
|
|
84
|
+
return this.db.updateOneOrAdd({
|
|
85
|
+
collection: this.collection,
|
|
86
|
+
search,
|
|
87
|
+
updater,
|
|
88
|
+
add_arg,
|
|
89
|
+
context,
|
|
90
|
+
id_gen,
|
|
91
|
+
});
|
|
52
92
|
}
|
|
53
93
|
/**
|
|
54
94
|
* Asynchronously removes one entry in a database or adds a new one if it doesn't exist. Usage e.g. for toggling a flag.
|
|
55
|
-
* Returns a promise resolving to `false` if the entry was found and removed,
|
|
56
|
-
* or `true` if the entry was added. The returned value reflects the state of the database
|
|
57
|
-
* after the operation.
|
|
58
95
|
*/
|
|
59
96
|
toggleOne(search, data = {}, context = {}) {
|
|
60
|
-
return this.db.toggleOne({
|
|
97
|
+
return this.db.toggleOne({
|
|
98
|
+
collection: this.collection,
|
|
99
|
+
search,
|
|
100
|
+
data,
|
|
101
|
+
context,
|
|
102
|
+
});
|
|
61
103
|
}
|
|
62
104
|
}
|