@wxn0brp/db 0.5.1 → 0.5.3

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.
@@ -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
  */
@@ -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
  */
@@ -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
@@ -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);
@@ -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
+ }
@@ -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
@@ -18,5 +18,5 @@ export declare namespace ValtheraTypes {
18
18
  type Data = import("./types/data.js").Data;
19
19
  type SearchOptions = import("./types/searchOpts.js").SearchOptions;
20
20
  }
21
- import type { RelationTypes } from "./relation.js";
21
+ import type { RelationTypes } from "./types/relation.js";
22
22
  export type { RelationTypes };
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
  */
@@ -1,26 +1,6 @@
1
- import DataBase from "./database.js";
2
1
  import { Search } from "./types/arg.js";
3
2
  import { DbFindOpts } from "./types/options.js";
4
- export declare namespace RelationTypes {
5
- type Path = [string, string];
6
- type FieldPath = string[];
7
- interface DBS {
8
- [key: string]: DataBase;
9
- }
10
- interface Relation {
11
- [key: string]: RelationConfig;
12
- }
13
- interface RelationConfig {
14
- path: Path;
15
- pk?: string;
16
- fk?: string;
17
- as?: string;
18
- select?: string[];
19
- findOpts?: DbFindOpts;
20
- type?: "1" | "1n" | "nm";
21
- relations?: Relation;
22
- }
23
- }
3
+ import { RelationTypes } from "./types/relation.js";
24
4
  declare class Relation {
25
5
  dbs: RelationTypes.DBS;
26
6
  constructor(dbs: RelationTypes.DBS);
package/dist/relation.js CHANGED
@@ -1,4 +1,6 @@
1
1
  async function processRelations(dbs, cfg, data) {
2
+ if (!data)
3
+ return;
2
4
  for (const [key, relation] of Object.entries(cfg)) {
3
5
  const { pk = "_id", fk = "_id", type = "1" } = relation;
4
6
  if (type === "1") {
@@ -43,14 +45,14 @@ async function processRelations(dbs, cfg, data) {
43
45
  function selectDataSelf(data, select) {
44
46
  if (!data)
45
47
  return null;
46
- if (select.length === 0)
48
+ if (!select || select.length === 0)
47
49
  return data;
48
50
  if (Array.isArray(data))
49
51
  return data.map(item => selectDataSelf(item, select));
50
52
  return selectDataSelf(data[select[0]], select.slice(1));
51
53
  }
52
54
  function selectData(data, select) {
53
- if (select.length === 0)
55
+ if (!select || select.length === 0)
54
56
  return data;
55
57
  const newData = {};
56
58
  for (const field of select) {
@@ -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,23 @@
1
+ import DataBaseRemote from "../client/database.js";
2
+ import DataBase from "../database.js";
3
+ import { DbFindOpts } from "./options.js";
4
+ export declare namespace RelationTypes {
5
+ type Path = [string, string];
6
+ type FieldPath = string[];
7
+ interface DBS {
8
+ [key: string]: DataBase | DataBaseRemote;
9
+ }
10
+ interface Relation {
11
+ [key: string]: RelationConfig;
12
+ }
13
+ interface RelationConfig {
14
+ path: Path;
15
+ pk?: string;
16
+ fk?: string;
17
+ as?: string;
18
+ select?: string[];
19
+ findOpts?: DbFindOpts;
20
+ type?: "1" | "1n" | "nm";
21
+ relations?: Relation;
22
+ }
23
+ }
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,51 +1,45 @@
1
1
  {
2
- "name": "@wxn0brp/db",
3
- "version": "0.5.1",
4
- "main": "dist/index.js",
5
- "types": "dist/index.d.ts",
6
- "description": "A simple file-based database management system with support for CRUD operations, custom queries, and graph structures.",
7
- "homepage": "https://github.com/wxn0brP/ValtheraDB",
8
- "repository": {
9
- "type": "git",
10
- "url": "https://github.com/wxn0brP/ValtheraDB.git"
11
- },
12
- "keywords": [
13
- "database",
14
- "file-based",
15
- "CRUD",
16
- "graph",
17
- "query"
18
- ],
19
- "author": "wxn0brP",
20
- "license": "MIT",
21
- "type": "module",
22
- "dependencies": {
23
- "json5": "^2.2.3",
24
- "ky": "^1.7.4",
25
- "readline": "^1.3.0"
26
- },
27
- "devDependencies": {
28
- "@types/node": "^22.10.2",
29
- "tsc-alias": "^1.8.10",
30
- "typescript": "^5.7.2"
31
- },
32
- "scripts": {
33
- "build": "tsc && tsc-alias",
34
- "prepare": "rm -rf dist && npm run build",
35
- "postversion": "git push && git push --tags",
36
- "build:cdn": "npm run build && cd cdn && node index.js"
37
- },
38
- "files": [
39
- "dist"
40
- ],
41
- "exports": {
42
- ".": {
43
- "types": "./dist/index.d.ts",
44
- "import": "./dist/index.js"
45
- },
46
- "./*": {
47
- "types": "./dist/*",
48
- "import": "./dist/*"
49
- }
50
- }
2
+ "name": "@wxn0brp/db",
3
+ "version": "0.5.3",
4
+ "main": "dist/index.js",
5
+ "types": "dist/index.d.ts",
6
+ "description": "A simple file-based database management system with support for CRUD operations, custom queries, and graph structures.",
7
+ "homepage": "https://github.com/wxn0brP/ValtheraDB",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/wxn0brP/ValtheraDB.git"
11
+ },
12
+ "keywords": [
13
+ "database",
14
+ "file-based",
15
+ "CRUD",
16
+ "graph",
17
+ "query"
18
+ ],
19
+ "author": "wxn0brP",
20
+ "license": "MIT",
21
+ "type": "module",
22
+ "dependencies": {
23
+ "json5": "^2.2.3",
24
+ "ky": "^1.7.4",
25
+ "readline": "^1.3.0"
26
+ },
27
+ "devDependencies": {
28
+ "@types/node": "^22.10.2",
29
+ "tsc-alias": "^1.8.10",
30
+ "typescript": "^5.7.2"
31
+ },
32
+ "files": [
33
+ "dist"
34
+ ],
35
+ "exports": {
36
+ ".": {
37
+ "types": "./dist/index.d.ts",
38
+ "import": "./dist/index.js"
39
+ },
40
+ "./*": {
41
+ "types": "./dist/*",
42
+ "import": "./dist/*"
43
+ }
44
+ }
51
45
  }