@wxn0brp/db 0.4.2 → 0.5.2
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/CollectionManager.d.ts +4 -0
- package/dist/CollectionManager.js +6 -0
- package/dist/action.d.ts +1 -0
- package/dist/action.js +15 -0
- package/dist/client/database.d.ts +4 -0
- package/dist/client/database.js +6 -0
- package/dist/database.d.ts +5 -1
- package/dist/database.js +7 -1
- package/dist/file/customFileCpu.d.ts +1 -0
- package/dist/file/customFileCpu.js +3 -0
- package/dist/file/find.d.ts +1 -0
- package/dist/file/find.js +27 -0
- package/dist/file/index.js +2 -1
- package/dist/index.d.ts +15 -8
- package/dist/index.js +2 -2
- package/dist/memory.d.ts +4 -0
- package/dist/memory.js +15 -0
- package/dist/relation.d.ts +5 -29
- package/dist/relation.js +70 -52
- package/dist/types/fileCpu.d.ts +10 -0
- package/dist/types/relation.d.ts +22 -0
- package/dist/types/relation.js +1 -0
- package/package.json +1 -1
|
@@ -20,6 +20,10 @@ declare class CollectionManager {
|
|
|
20
20
|
* Find one data entry in a database.
|
|
21
21
|
*/
|
|
22
22
|
findOne<T = Data>(search: Search, context?: Context, findOpts?: FindOpts): Promise<T>;
|
|
23
|
+
/**
|
|
24
|
+
* Find data in a database as a stream.
|
|
25
|
+
*/
|
|
26
|
+
findStream<T = Data>(search: Search, context?: Context, findOpts?: FindOpts, limit?: number): AsyncGenerator<T>;
|
|
23
27
|
/**
|
|
24
28
|
* Update data in a database.
|
|
25
29
|
*/
|
|
@@ -23,6 +23,12 @@ 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
|
+
}
|
|
26
32
|
/**
|
|
27
33
|
* Update data in a database.
|
|
28
34
|
*/
|
package/dist/action.d.ts
CHANGED
|
@@ -45,6 +45,7 @@ declare class dbActionC {
|
|
|
45
45
|
* Find the first matching entry in the specified database based on search criteria.
|
|
46
46
|
*/
|
|
47
47
|
findOne(collection: string, arg: SearchOptions, context?: Context, findOpts?: FindOpts): Promise<Data>;
|
|
48
|
+
findStream(collection: string, arg: Search, context?: Context, findOpts?: FindOpts, limit?: number): AsyncGenerator<any>;
|
|
48
49
|
/**
|
|
49
50
|
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
50
51
|
*/
|
package/dist/action.js
CHANGED
|
@@ -122,6 +122,21 @@ class dbActionC {
|
|
|
122
122
|
}
|
|
123
123
|
return null;
|
|
124
124
|
}
|
|
125
|
+
async *findStream(collection, arg, context = {}, findOpts = {}, limit = -1) {
|
|
126
|
+
await this.checkCollection(collection);
|
|
127
|
+
const cpath = this._getCollectionPath(collection);
|
|
128
|
+
const files = await getSortedFiles(cpath);
|
|
129
|
+
let count = 0;
|
|
130
|
+
for (let f of files) {
|
|
131
|
+
for await (const data of this.fileCpu.findStream(cpath + f, arg, context, findOpts, limit)) {
|
|
132
|
+
yield data;
|
|
133
|
+
count++;
|
|
134
|
+
if (limit !== -1 && count >= limit) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
125
140
|
/**
|
|
126
141
|
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
127
142
|
*/
|
|
@@ -45,6 +45,10 @@ declare class DataBaseRemote {
|
|
|
45
45
|
* Find one data entry in a database.
|
|
46
46
|
*/
|
|
47
47
|
findOne<T = Data>(collection: string, search: Search, context?: Context, findOpts?: FindOpts): Promise<T>;
|
|
48
|
+
/**
|
|
49
|
+
* Find data in a database as a stream.
|
|
50
|
+
*/
|
|
51
|
+
findStream<T = Data>(collection: string, search: Search, context?: Context, dbFindOpts?: DbFindOpts, findOpts?: FindOpts): Promise<void>;
|
|
48
52
|
/**
|
|
49
53
|
* Update data in a database.
|
|
50
54
|
*/
|
package/dist/client/database.js
CHANGED
|
@@ -74,6 +74,12 @@ class DataBaseRemote {
|
|
|
74
74
|
async findOne(collection, search, context = {}, findOpts = {}) {
|
|
75
75
|
return await this._request("findOne", [collection, search, context, findOpts]);
|
|
76
76
|
}
|
|
77
|
+
/**
|
|
78
|
+
* Find data in a database as a stream.
|
|
79
|
+
*/
|
|
80
|
+
async findStream(collection, search, context = {}, dbFindOpts = {}, findOpts = {}) {
|
|
81
|
+
throw new Error("Method not implemented.");
|
|
82
|
+
}
|
|
77
83
|
/**
|
|
78
84
|
* Update data in a database.
|
|
79
85
|
*/
|
package/dist/database.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ import Data from "./types/data.js";
|
|
|
7
7
|
import { Context } from "./types/types.js";
|
|
8
8
|
import FileCpu from "./types/fileCpu.js";
|
|
9
9
|
import { Transaction } from "./types/transactions.js";
|
|
10
|
-
import EventEmitter from "events";
|
|
10
|
+
import { EventEmitter } from "events";
|
|
11
11
|
/**
|
|
12
12
|
* Represents a database management class for performing CRUD operations.
|
|
13
13
|
* @class
|
|
@@ -46,6 +46,10 @@ declare class DataBase {
|
|
|
46
46
|
* Find one data entry in a database.
|
|
47
47
|
*/
|
|
48
48
|
findOne<T = Data>(collection: string, search: Search, context?: Context, findOpts?: FindOpts): Promise<T>;
|
|
49
|
+
/**
|
|
50
|
+
* Find data in a database as a stream.
|
|
51
|
+
*/
|
|
52
|
+
findStream<T = Data>(collection: string, search: Search, context?: Context, findOpts?: FindOpts, limit?: number): Promise<AsyncGenerator<T, any, any>>;
|
|
49
53
|
/**
|
|
50
54
|
* Update data in a database.
|
|
51
55
|
*/
|
package/dist/database.js
CHANGED
|
@@ -2,7 +2,7 @@ import dbActionC from "./action.js";
|
|
|
2
2
|
import executorC from "./executor.js";
|
|
3
3
|
import CollectionManager from "./CollectionManager.js";
|
|
4
4
|
import vFileCpu from "./file/index.js";
|
|
5
|
-
import EventEmitter from "events";
|
|
5
|
+
import { EventEmitter } from "events";
|
|
6
6
|
/**
|
|
7
7
|
* Represents a database management class for performing CRUD operations.
|
|
8
8
|
* @class
|
|
@@ -68,6 +68,12 @@ class DataBase {
|
|
|
68
68
|
async findOne(collection, search, context = {}, findOpts = {}) {
|
|
69
69
|
return await this.execute("findOne", collection, search, context, findOpts);
|
|
70
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Find data in a database as a stream.
|
|
73
|
+
*/
|
|
74
|
+
async findStream(collection, search, context = {}, findOpts = {}, limit = -1) {
|
|
75
|
+
return await this.execute("findStream", collection, search, context, findOpts, limit);
|
|
76
|
+
}
|
|
71
77
|
/**
|
|
72
78
|
* Update data in a database.
|
|
73
79
|
*/
|
|
@@ -13,6 +13,7 @@ declare class CustomFileCpu implements FileCpu {
|
|
|
13
13
|
add(file: string, data: Data): Promise<void>;
|
|
14
14
|
find(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any[] | false>;
|
|
15
15
|
findOne(file: string, arg: Search, context?: Context, findOpts?: FindOpts): Promise<any | false>;
|
|
16
|
+
findStream(file: string, arg: Search, context?: Context, findOpts?: FindOpts, limit?: number): AsyncGenerator<any>;
|
|
16
17
|
remove(file: string, one: boolean, arg: Search, context?: Context): Promise<boolean>;
|
|
17
18
|
update(file: string, one: boolean, arg: Search, updater: Updater, context?: Context): Promise<boolean>;
|
|
18
19
|
transactions(file: string, transactions: Transaction[]): Promise<void>;
|
|
@@ -26,6 +26,9 @@ 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
|
+
}
|
|
29
32
|
async remove(file, one, arg, context = {}) {
|
|
30
33
|
file = pathRepair(file);
|
|
31
34
|
let entries = await this._readFile(file);
|
package/dist/file/find.d.ts
CHANGED
|
@@ -9,3 +9,4 @@ 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
|
@@ -71,3 +71,30 @@ 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,6 +1,6 @@
|
|
|
1
1
|
import update from "./update.js";
|
|
2
2
|
import remove from "./remove.js";
|
|
3
|
-
import { find, findOne } from "./find.js";
|
|
3
|
+
import { find, findOne, findStream } from "./find.js";
|
|
4
4
|
import { appendFileSync } from "fs";
|
|
5
5
|
import { stringify } from "../format.js";
|
|
6
6
|
import transactions from "./transactions.js";
|
|
@@ -11,6 +11,7 @@ const vFileCpu = {
|
|
|
11
11
|
},
|
|
12
12
|
find,
|
|
13
13
|
findOne,
|
|
14
|
+
findStream,
|
|
14
15
|
update,
|
|
15
16
|
remove,
|
|
16
17
|
transactions
|
package/dist/index.d.ts
CHANGED
|
@@ -5,11 +5,18 @@ import GraphRemote from "./client/graph.js";
|
|
|
5
5
|
import genId from "./gen.js";
|
|
6
6
|
import Relation from "./relation.js";
|
|
7
7
|
import CustomFileCpu from "./file/customFileCpu.js";
|
|
8
|
-
import ValtheraMemory from "./memory.js";
|
|
9
|
-
export { DataBase
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
import ValtheraMemory, { createMemoryValthera } from "./memory.js";
|
|
9
|
+
export { DataBase as Valthera, Graph, DataBaseRemote as ValtheraRemote, GraphRemote, Relation, genId, CustomFileCpu, ValtheraMemory, createMemoryValthera };
|
|
10
|
+
export type Id = import("./types/Id.js").Id;
|
|
11
|
+
export declare namespace ValtheraTypes {
|
|
12
|
+
type Arg = import("./types/arg.js").Arg;
|
|
13
|
+
type Search = import("./types/arg.js").Search;
|
|
14
|
+
type Updater = import("./types/arg.js").Updater;
|
|
15
|
+
type DbFindOpts = import("./types/options.js").DbFindOpts;
|
|
16
|
+
type FindOpts = import("./types/options.js").FindOpts;
|
|
17
|
+
type DbOpts = import("./types/options.js").DbOpts;
|
|
18
|
+
type Data = import("./types/data.js").Data;
|
|
19
|
+
type SearchOptions = import("./types/searchOpts.js").SearchOptions;
|
|
20
|
+
}
|
|
21
|
+
import type { RelationTypes } from "./types/relation.js";
|
|
22
|
+
export type { RelationTypes };
|
package/dist/index.js
CHANGED
|
@@ -5,5 +5,5 @@ import GraphRemote from "./client/graph.js";
|
|
|
5
5
|
import genId from "./gen.js";
|
|
6
6
|
import Relation from "./relation.js";
|
|
7
7
|
import CustomFileCpu from "./file/customFileCpu.js";
|
|
8
|
-
import ValtheraMemory from "./memory.js";
|
|
9
|
-
export { DataBase
|
|
8
|
+
import ValtheraMemory, { createMemoryValthera } from "./memory.js";
|
|
9
|
+
export { DataBase as Valthera, Graph, DataBaseRemote as ValtheraRemote, GraphRemote, Relation, genId, CustomFileCpu, ValtheraMemory, createMemoryValthera };
|
package/dist/memory.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import DataBase from "./database.js";
|
|
2
|
+
import Data from "./types/data.js";
|
|
2
3
|
export default class ValtheraMemory extends DataBase {
|
|
3
4
|
constructor(...args: any[]);
|
|
4
5
|
}
|
|
6
|
+
export declare function createMemoryValthera(data?: {
|
|
7
|
+
[key: string]: Data[];
|
|
8
|
+
}): ValtheraMemory;
|
package/dist/memory.js
CHANGED
|
@@ -80,6 +80,12 @@ class MemoryAction {
|
|
|
80
80
|
let data = await this.fileCpu.findOne(collection, arg, context, findOpts);
|
|
81
81
|
return data || null;
|
|
82
82
|
}
|
|
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
|
+
}
|
|
83
89
|
/**
|
|
84
90
|
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
85
91
|
*/
|
|
@@ -127,3 +133,12 @@ export default class ValtheraMemory extends DataBase {
|
|
|
127
133
|
super("", { dbAction: new MemoryAction() });
|
|
128
134
|
}
|
|
129
135
|
}
|
|
136
|
+
export function createMemoryValthera(data) {
|
|
137
|
+
const db = new ValtheraMemory();
|
|
138
|
+
if (!data)
|
|
139
|
+
return db;
|
|
140
|
+
for (const collection of Object.keys(data)) {
|
|
141
|
+
db.dbAction.memory.set(collection, data[collection]);
|
|
142
|
+
}
|
|
143
|
+
return db;
|
|
144
|
+
}
|
package/dist/relation.d.ts
CHANGED
|
@@ -1,34 +1,10 @@
|
|
|
1
|
-
import DataBase from "./database.js";
|
|
2
1
|
import { Search } from "./types/arg.js";
|
|
3
2
|
import { DbFindOpts } from "./types/options.js";
|
|
4
|
-
|
|
5
|
-
[key: string]: DataBase;
|
|
6
|
-
}
|
|
7
|
-
interface RelationConfig {
|
|
8
|
-
from: string;
|
|
9
|
-
localField: string;
|
|
10
|
-
foreignField: string;
|
|
11
|
-
as?: string;
|
|
12
|
-
multiple?: boolean;
|
|
13
|
-
}
|
|
3
|
+
import { RelationTypes } from "./types/relation.js";
|
|
14
4
|
declare class Relation {
|
|
15
|
-
|
|
16
|
-
constructor(
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
*/
|
|
20
|
-
private _resolvePath;
|
|
21
|
-
/**
|
|
22
|
-
* Processes relations for a single item.
|
|
23
|
-
*/
|
|
24
|
-
private _processItemRelations;
|
|
25
|
-
/**
|
|
26
|
-
* Finds multiple items with relations.
|
|
27
|
-
*/
|
|
28
|
-
find(path: string, search: Search, relations?: Record<string, RelationConfig>, options?: DbFindOpts): Promise<Record<string, any>[]>;
|
|
29
|
-
/**
|
|
30
|
-
* Finds a single item with relations.
|
|
31
|
-
*/
|
|
32
|
-
findOne(path: string, search: Search, relations?: Record<string, RelationConfig>): Promise<Record<string, any> | null>;
|
|
5
|
+
dbs: RelationTypes.DBS;
|
|
6
|
+
constructor(dbs: RelationTypes.DBS);
|
|
7
|
+
findOne(path: RelationTypes.Path, search: Search, relations: RelationTypes.Relation, select: RelationTypes.FieldPath[]): Promise<any>;
|
|
8
|
+
find(path: RelationTypes.Path, search: Search, relations: RelationTypes.Relation, select: RelationTypes.FieldPath[], findOpts?: DbFindOpts): Promise<any[]>;
|
|
33
9
|
}
|
|
34
10
|
export default Relation;
|
package/dist/relation.js
CHANGED
|
@@ -1,62 +1,80 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
throw new Error(`Invalid path format "${path}". Expected format 'dbName.collectionName'.`);
|
|
12
|
-
}
|
|
13
|
-
const sanitizedPath = path.replace(/\\\./g, "\uffff");
|
|
14
|
-
const [dbName, collectionName] = sanitizedPath.split(".", 2).map(part => part.replace(/\uffff/g, "."));
|
|
15
|
-
const db = this.databases[dbName];
|
|
16
|
-
if (!db) {
|
|
17
|
-
throw new Error(`Database "${dbName}" not found.`);
|
|
18
|
-
}
|
|
19
|
-
return { db, collection: collectionName };
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Processes relations for a single item.
|
|
23
|
-
*/
|
|
24
|
-
async _processItemRelations(item, relations) {
|
|
25
|
-
if (!item || typeof item !== "object")
|
|
26
|
-
return item;
|
|
27
|
-
const result = { ...item };
|
|
28
|
-
for (const [field, relationConfig] of Object.entries(relations)) {
|
|
29
|
-
if (!relationConfig.from || !relationConfig.localField || !relationConfig.foreignField) {
|
|
30
|
-
console.warn(`Skipping invalid relation configuration for field: "${field}"`);
|
|
1
|
+
async function processRelations(dbs, cfg, data) {
|
|
2
|
+
for (const [key, relation] of Object.entries(cfg)) {
|
|
3
|
+
const { pk = "_id", fk = "_id", type = "1" } = relation;
|
|
4
|
+
if (type === "1") {
|
|
5
|
+
const db = dbs[relation.path[0]];
|
|
6
|
+
const collection = relation.path[1];
|
|
7
|
+
const item = await db.findOne(collection, { [fk]: data[pk] }, {}, { select: relation.select || null });
|
|
8
|
+
const field = relation.as || key;
|
|
9
|
+
if (!item) {
|
|
10
|
+
data[field] = null;
|
|
31
11
|
continue;
|
|
32
12
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
13
|
+
if (relation.relations) {
|
|
14
|
+
await processRelations(dbs, relation.relations, item);
|
|
15
|
+
}
|
|
16
|
+
data[field] = item;
|
|
17
|
+
}
|
|
18
|
+
else if (type === "1n") {
|
|
19
|
+
const db = dbs[relation.path[0]];
|
|
20
|
+
const collection = relation.path[1];
|
|
21
|
+
const items = await db.find(collection, { [fk]: data[pk] }, {}, relation.findOpts || {}, { select: relation.select || null });
|
|
22
|
+
const field = relation.as || key;
|
|
23
|
+
if (relation.relations) {
|
|
24
|
+
await Promise.all(items.map(item => processRelations(dbs, relation.relations, item)));
|
|
38
25
|
}
|
|
39
|
-
|
|
40
|
-
|
|
26
|
+
data[field] = items;
|
|
27
|
+
}
|
|
28
|
+
else if (type === "nm") {
|
|
29
|
+
const db = dbs[relation.path[0]];
|
|
30
|
+
const collection = relation.path[1];
|
|
31
|
+
const items = await db.find(collection, {}, {}, {}, { select: relation.select || null });
|
|
32
|
+
const field = relation.as || key;
|
|
33
|
+
if (relation.relations) {
|
|
34
|
+
await Promise.all(items.map(item => processRelations(dbs, relation.relations, item)));
|
|
41
35
|
}
|
|
36
|
+
data[field] = items;
|
|
42
37
|
}
|
|
43
|
-
|
|
38
|
+
else {
|
|
39
|
+
throw new Error(`Unknown relation type: ${relation.type}`);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function selectDataSelf(data, select) {
|
|
44
|
+
if (!data)
|
|
45
|
+
return null;
|
|
46
|
+
if (select.length === 0)
|
|
47
|
+
return data;
|
|
48
|
+
if (Array.isArray(data))
|
|
49
|
+
return data.map(item => selectDataSelf(item, select));
|
|
50
|
+
return selectDataSelf(data[select[0]], select.slice(1));
|
|
51
|
+
}
|
|
52
|
+
function selectData(data, select) {
|
|
53
|
+
if (select.length === 0)
|
|
54
|
+
return data;
|
|
55
|
+
const newData = {};
|
|
56
|
+
for (const field of select) {
|
|
57
|
+
const key = field.map(f => f.replaceAll(".", "\\.")).join(".");
|
|
58
|
+
newData[key] = selectDataSelf(data, field);
|
|
59
|
+
}
|
|
60
|
+
return newData;
|
|
61
|
+
}
|
|
62
|
+
class Relation {
|
|
63
|
+
dbs;
|
|
64
|
+
constructor(dbs) {
|
|
65
|
+
this.dbs = dbs;
|
|
44
66
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const items = await db.find(collection, search, {}, options);
|
|
51
|
-
return Promise.all(items.map(item => this._processItemRelations(item, relations)));
|
|
67
|
+
async findOne(path, search, relations, select) {
|
|
68
|
+
const db = this.dbs[path[0]];
|
|
69
|
+
const data = await db.findOne(path[1], search);
|
|
70
|
+
await processRelations(this.dbs, relations, data);
|
|
71
|
+
return selectData(data, select);
|
|
52
72
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
const item = await db.findOne(collection, search);
|
|
59
|
-
return item ? this._processItemRelations(item, relations) : null;
|
|
73
|
+
async find(path, search, relations, select, findOpts = {}) {
|
|
74
|
+
const db = this.dbs[path[0]];
|
|
75
|
+
const data = await db.find(path[1], search, {}, findOpts);
|
|
76
|
+
await Promise.all(data.map(item => processRelations(this.dbs, relations, item)));
|
|
77
|
+
return data.map(item => selectData(item, select));
|
|
60
78
|
}
|
|
61
79
|
}
|
|
62
80
|
export default Relation;
|
package/dist/types/fileCpu.d.ts
CHANGED
|
@@ -29,6 +29,16 @@ interface FileCpu {
|
|
|
29
29
|
* @returns A promise resolving to the found entry or `false` if not found.
|
|
30
30
|
*/
|
|
31
31
|
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>;
|
|
32
42
|
/**
|
|
33
43
|
* Asynchronously removes entries from a file based on search criteria.
|
|
34
44
|
* @param file The path to the file.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import DataBase from "../database.js";
|
|
2
|
+
import { DbFindOpts } from "./options.js";
|
|
3
|
+
export declare namespace RelationTypes {
|
|
4
|
+
type Path = [string, string];
|
|
5
|
+
type FieldPath = string[];
|
|
6
|
+
interface DBS {
|
|
7
|
+
[key: string]: DataBase;
|
|
8
|
+
}
|
|
9
|
+
interface Relation {
|
|
10
|
+
[key: string]: RelationConfig;
|
|
11
|
+
}
|
|
12
|
+
interface RelationConfig {
|
|
13
|
+
path: Path;
|
|
14
|
+
pk?: string;
|
|
15
|
+
fk?: string;
|
|
16
|
+
as?: string;
|
|
17
|
+
select?: string[];
|
|
18
|
+
findOpts?: DbFindOpts;
|
|
19
|
+
type?: "1" | "1n" | "nm";
|
|
20
|
+
relations?: Relation;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED