@wxn0brp/db-core 0.12.16 → 0.12.17
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/db/transaction.d.ts +4 -3
- package/dist/helpers/addId.d.ts +2 -2
- package/dist/helpers/addId.js +29 -63
- package/dist/types/export.d.ts +1 -0
- package/dist/types/options.d.ts +11 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
package/dist/db/transaction.d.ts
CHANGED
|
@@ -3,12 +3,13 @@ import { Data } from "../types/data.js";
|
|
|
3
3
|
import { IndexOpts } from "../types/idx.js";
|
|
4
4
|
import { VQueryT } from "../types/query.js";
|
|
5
5
|
import { TransactionHandle } from "../types/transaction.js";
|
|
6
|
+
import { ValtheraCompatible } from "../types/valthera.js";
|
|
6
7
|
import { ValtheraClass } from "./valthera.js";
|
|
7
8
|
/**
|
|
8
9
|
* Transaction context. Operations are executed directly through the parent
|
|
9
10
|
* database with the transaction handle passed to the executor.
|
|
10
11
|
*/
|
|
11
|
-
export declare class Transaction {
|
|
12
|
+
export declare class Transaction implements ValtheraCompatible {
|
|
12
13
|
readonly handle: TransactionHandle;
|
|
13
14
|
_parent: ValtheraClass;
|
|
14
15
|
constructor(handle: TransactionHandle, _parent: ValtheraClass);
|
|
@@ -28,8 +29,8 @@ export declare class Transaction {
|
|
|
28
29
|
issetCollection(collection: string | VQueryT.Collection): Promise<boolean>;
|
|
29
30
|
getCollections(): Promise<string[]>;
|
|
30
31
|
removeCollection(target: string | VQueryT.Collection): Promise<boolean>;
|
|
31
|
-
createIndex(collection: string, fields: string[], opts?: IndexOpts): Promise<
|
|
32
|
-
removeIndex(collection: string, fields: string[]): Promise<
|
|
32
|
+
createIndex(collection: string, fields: string[], opts?: IndexOpts): Promise<void>;
|
|
33
|
+
removeIndex(collection: string, fields: string[]): Promise<void>;
|
|
33
34
|
capabilities(): Promise<import("../index.js").AdapterCapabilities>;
|
|
34
35
|
commit(): Promise<void>;
|
|
35
36
|
rollback(): Promise<void>;
|
package/dist/helpers/addId.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { ActionsBase } from "../base/actions.js";
|
|
2
|
-
import {
|
|
3
|
-
export declare function addId(query:
|
|
2
|
+
import { VQueryT } from "../types/query.js";
|
|
3
|
+
export declare function addId(query: VQueryT.Add, actions: ActionsBase, defaultGen?: boolean): Promise<void>;
|
|
4
4
|
export declare function addIdBulk(query: VQueryT.BulkAdd, actions: ActionsBase, defaultGen?: boolean): Promise<void>;
|
package/dist/helpers/addId.js
CHANGED
|
@@ -9,91 +9,57 @@ function assertSafeIdKey(idKey) {
|
|
|
9
9
|
idKey,
|
|
10
10
|
});
|
|
11
11
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
const
|
|
15
|
-
const id_gen = query.id_gen ?? defaultGen;
|
|
16
|
-
const { idKey = "_id", numberId } = actions.adapterOpts;
|
|
17
|
-
if (!id_gen)
|
|
18
|
-
return data;
|
|
19
|
-
assertSafeIdKey(idKey);
|
|
20
|
-
if (data[idKey])
|
|
21
|
-
return data;
|
|
22
|
-
if (!numberId) {
|
|
23
|
-
data[idKey] = genId();
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
const txOpts = query.transaction
|
|
12
|
+
const defaultIdGen = (data, _collection, adapter) => data.map(() => genId(adapter.adapterOpts.idParts));
|
|
13
|
+
const numberIdGen = async (data, collection, adapter, transaction) => {
|
|
14
|
+
const txOpts = transaction
|
|
27
15
|
? {
|
|
28
|
-
transaction
|
|
16
|
+
transaction,
|
|
29
17
|
}
|
|
30
18
|
: {};
|
|
31
|
-
const
|
|
19
|
+
const found = (await adapter.findOne({
|
|
32
20
|
collection: "__vdb_id",
|
|
33
21
|
search: {
|
|
34
22
|
c: collection,
|
|
35
23
|
},
|
|
36
24
|
...txOpts,
|
|
37
25
|
}));
|
|
38
|
-
|
|
39
|
-
await
|
|
26
|
+
const start = found?.i ?? 0;
|
|
27
|
+
await adapter.updateOneOrAdd({
|
|
40
28
|
collection: "__vdb_id",
|
|
41
29
|
search: {
|
|
42
30
|
c: collection,
|
|
43
31
|
},
|
|
44
32
|
updater: {
|
|
45
33
|
$inc: {
|
|
46
|
-
i:
|
|
34
|
+
i: data.length,
|
|
47
35
|
},
|
|
48
36
|
},
|
|
49
37
|
id_gen: false,
|
|
50
38
|
...txOpts,
|
|
51
39
|
});
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const { idKey = "_id", numberId } = actions.adapterOpts;
|
|
57
|
-
if (!id_gen)
|
|
40
|
+
return data.map((_, i) => (start + i + 1));
|
|
41
|
+
};
|
|
42
|
+
async function applyIdGen(records, collection, actions, idGenEnabled, transaction) {
|
|
43
|
+
if (!idGenEnabled)
|
|
58
44
|
return;
|
|
45
|
+
const { idKey = "_id", numberId, idGen } = actions.adapterOpts;
|
|
59
46
|
assertSafeIdKey(idKey);
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
if (!data[idKey])
|
|
63
|
-
data[idKey] = genId();
|
|
64
|
-
}
|
|
47
|
+
const targets = records.filter(data => !data[idKey]);
|
|
48
|
+
if (!targets.length)
|
|
65
49
|
return;
|
|
50
|
+
const generator = idGen ?? (numberId ? numberIdGen : defaultIdGen);
|
|
51
|
+
const ids = await generator(targets, collection, actions, transaction);
|
|
52
|
+
for (let i = 0; i < targets.length; i++) {
|
|
53
|
+
targets[i][idKey] = ids[i];
|
|
66
54
|
}
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
...txOpts,
|
|
78
|
-
}));
|
|
79
|
-
const start = find?.i ?? 0;
|
|
80
|
-
for (let i = 0; i < query.datas.length; i++) {
|
|
81
|
-
const data = query.datas[i];
|
|
82
|
-
if (!data[idKey]) {
|
|
83
|
-
data[idKey] = start + i + 1;
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
await actions.updateOneOrAdd({
|
|
87
|
-
collection: "__vdb_id",
|
|
88
|
-
search: {
|
|
89
|
-
c: collection,
|
|
90
|
-
},
|
|
91
|
-
updater: {
|
|
92
|
-
$inc: {
|
|
93
|
-
i: query.datas.length,
|
|
94
|
-
},
|
|
95
|
-
},
|
|
96
|
-
id_gen: false,
|
|
97
|
-
...txOpts,
|
|
98
|
-
});
|
|
55
|
+
}
|
|
56
|
+
export async function addId(query, actions, defaultGen = true) {
|
|
57
|
+
const id_gen = query.id_gen ?? defaultGen;
|
|
58
|
+
await applyIdGen([
|
|
59
|
+
query.data,
|
|
60
|
+
], query.collection, actions, id_gen, query.transaction);
|
|
61
|
+
}
|
|
62
|
+
export async function addIdBulk(query, actions, defaultGen = true) {
|
|
63
|
+
const id_gen = query.id_gen ?? defaultGen;
|
|
64
|
+
await applyIdGen(query.datas, query.collection, actions, id_gen, query.transaction);
|
|
99
65
|
}
|
package/dist/types/export.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export declare namespace ValtheraTypes {
|
|
|
6
6
|
type FindOpts = import("./options.js").FindOpts;
|
|
7
7
|
type DbOpts = import("./options.js").DbOpts;
|
|
8
8
|
type AdapterOpts = import("./options.js").AdapterOpts;
|
|
9
|
+
type IdGen = import("./options.js").IdGen;
|
|
9
10
|
type AdapterCapabilities = import("./action.js").AdapterCapabilities;
|
|
10
11
|
type Data = import("./data.js").Data;
|
|
11
12
|
type SearchOptions = import("./searchOpts.js").SearchOptions;
|
package/dist/types/options.d.ts
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
import { ActionsBase } from "../base/actions.js";
|
|
2
2
|
import { ExecutorInterface } from "../helpers/executor.js";
|
|
3
|
+
import { Data } from "./data.js";
|
|
4
|
+
import { Id } from "./Id.js";
|
|
5
|
+
import { TransactionHandle } from "./transaction.js";
|
|
3
6
|
import { KeysMatching } from "./utils.js";
|
|
4
7
|
export type AdapterValue = ActionsBase | (() => Promise<ActionsBase>);
|
|
8
|
+
/**
|
|
9
|
+
* Custom ID generator. Receives only records without the ID key set and
|
|
10
|
+
* returns generated IDs in the same order.
|
|
11
|
+
*/
|
|
12
|
+
export type IdGen = (data: Data[], collection: string, adapter: ActionsBase, transaction?: TransactionHandle) => Id[] | Promise<Id[]>;
|
|
5
13
|
export interface DbOpts {
|
|
6
14
|
adapter: AdapterValue;
|
|
7
15
|
executor?: ExecutorInterface;
|
|
@@ -11,6 +19,9 @@ export interface DbOpts {
|
|
|
11
19
|
export interface AdapterOpts {
|
|
12
20
|
numberId?: boolean;
|
|
13
21
|
idKey?: string;
|
|
22
|
+
idGen?: IdGen;
|
|
23
|
+
/** Parts lengths passed to `genId` for the default ID generator. */
|
|
24
|
+
idParts?: number[];
|
|
14
25
|
}
|
|
15
26
|
export type FieldPath<T = any> = KeysMatching<T, any> | (string & {}) | string[];
|
|
16
27
|
export interface DbFindOpts<T = any> {
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const version = "0.12.
|
|
1
|
+
export declare const version = "0.12.17";
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.12.
|
|
1
|
+
export const version = "0.12.17";
|