@wxn0brp/db 0.10.0 → 0.20.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/LICENSE +1 -1
- package/dist/{helpers/autoCreate.d.ts → autoCreate.d.ts} +2 -2
- package/dist/{helpers/autoCreate.js → autoCreate.js} +2 -2
- package/dist/{db/graph.d.ts → graph.d.ts} +1 -1
- package/dist/index.d.ts +8 -15
- package/dist/index.js +7 -10
- package/dist/valthera.d.ts +7 -0
- package/dist/valthera.js +14 -0
- package/dist/version.js +1 -1
- package/package.json +11 -21
- package/dist/actions/action.d.ts +0 -69
- package/dist/actions/action.js +0 -234
- package/dist/actions/memory.d.ts +0 -72
- package/dist/actions/memory.js +0 -145
- package/dist/base/actions.d.ts +0 -17
- package/dist/base/actions.js +0 -56
- package/dist/base/db.d.ts +0 -21
- package/dist/base/db.js +0 -56
- package/dist/client/function.d.ts +0 -5
- package/dist/client/function.js +0 -30
- package/dist/client/graph.d.ts +0 -54
- package/dist/client/graph.js +0 -108
- package/dist/client/remote.d.ts +0 -15
- package/dist/client/remote.js +0 -1
- package/dist/client/valthera.d.ts +0 -74
- package/dist/client/valthera.js +0 -135
- package/dist/db/valthera.d.ts +0 -75
- package/dist/db/valthera.js +0 -110
- package/dist/file/customFileCpu.d.ts +0 -18
- package/dist/file/customFileCpu.js +0 -68
- package/dist/file/find.d.ts +0 -11
- package/dist/file/find.js +0 -73
- package/dist/file/index.d.ts +0 -3
- package/dist/file/index.js +0 -16
- package/dist/file/remove.d.ts +0 -7
- package/dist/file/remove.js +0 -41
- package/dist/file/update.d.ts +0 -7
- package/dist/file/update.js +0 -50
- package/dist/file/utils.d.ts +0 -8
- package/dist/file/utils.js +0 -84
- package/dist/helpers/CollectionManager.d.ts +0 -43
- package/dist/helpers/CollectionManager.js +0 -57
- package/dist/helpers/executor.d.ts +0 -28
- package/dist/helpers/executor.js +0 -45
- package/dist/helpers/format.d.ts +0 -12
- package/dist/helpers/format.js +0 -23
- package/dist/helpers/gen.d.ts +0 -8
- package/dist/helpers/gen.js +0 -134
- package/dist/helpers/relation.d.ts +0 -10
- package/dist/helpers/relation.js +0 -155
- package/dist/helpers/updateOneOrAdd.d.ts +0 -2
- package/dist/helpers/updateOneOrAdd.js +0 -18
- package/dist/types/Id.d.ts +0 -3
- package/dist/types/Id.js +0 -1
- package/dist/types/arg.d.ts +0 -12
- package/dist/types/arg.js +0 -1
- package/dist/types/data.d.ts +0 -4
- package/dist/types/data.js +0 -1
- package/dist/types/export.d.ts +0 -11
- package/dist/types/export.js +0 -1
- package/dist/types/fileCpu.d.ts +0 -51
- package/dist/types/fileCpu.js +0 -1
- package/dist/types/options.d.ts +0 -19
- package/dist/types/options.js +0 -1
- package/dist/types/query.d.ts +0 -15
- package/dist/types/query.js +0 -1
- package/dist/types/relation.d.ts +0 -28
- package/dist/types/relation.js +0 -1
- package/dist/types/searchOpts.d.ts +0 -62
- package/dist/types/searchOpts.js +0 -7
- package/dist/types/types.d.ts +0 -3
- package/dist/types/types.js +0 -1
- package/dist/types/updater.d.ts +0 -26
- package/dist/types/updater.js +0 -4
- package/dist/types/valthera.d.ts +0 -36
- package/dist/types/valthera.js +0 -1
- package/dist/utils/hasFields.d.ts +0 -7
- package/dist/utils/hasFields.js +0 -18
- package/dist/utils/hasFieldsAdvanced.d.ts +0 -5
- package/dist/utils/hasFieldsAdvanced.js +0 -176
- package/dist/utils/sort.d.ts +0 -1
- package/dist/utils/sort.js +0 -23
- package/dist/utils/updateFindObject.d.ts +0 -11
- package/dist/utils/updateFindObject.js +0 -28
- package/dist/utils/updateObject.d.ts +0 -7
- package/dist/utils/updateObject.js +0 -145
- /package/dist/{db/graph.js → graph.js} +0 -0
package/LICENSE
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Remote } from "
|
|
2
|
-
import { ValtheraCompatible } from "
|
|
1
|
+
import { Remote } from "@wxn0brp/db-client/remote";
|
|
2
|
+
import { ValtheraCompatible } from "@wxn0brp/db-core/types/valthera";
|
|
3
3
|
/**
|
|
4
4
|
* Creates a database instance based on the provided configuration.
|
|
5
5
|
* If the configuration is an object, it creates a DataBaseRemote instance.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import ValtheraRemote from "
|
|
2
|
-
import Valthera from "
|
|
1
|
+
import ValtheraRemote from "@wxn0brp/db-client/valthera";
|
|
2
|
+
import Valthera from "./valthera.js";
|
|
3
3
|
/**
|
|
4
4
|
* Creates a database instance based on the provided configuration.
|
|
5
5
|
* If the configuration is an object, it creates a DataBaseRemote instance.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,16 +1,9 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import genId from "./helpers/gen.js";
|
|
6
|
-
import Relation from "./helpers/relation.js";
|
|
7
|
-
import CustomFileCpu from "./file/customFileCpu.js";
|
|
8
|
-
import ValtheraMemory, { createMemoryValthera } from "./actions/memory.js";
|
|
9
|
-
import { ValtheraAutoCreate } from "./helpers/autoCreate.js";
|
|
10
|
-
import { RelationTypes } from "./types/relation.js";
|
|
11
|
-
import { ValtheraCompatible } from "./types/valthera.js";
|
|
12
|
-
import { ValtheraTypes } from "./types/export.js";
|
|
13
|
-
export { Valthera, Graph, ValtheraRemote, GraphRemote, Relation, genId, CustomFileCpu, ValtheraMemory, createMemoryValthera, ValtheraAutoCreate, };
|
|
1
|
+
import GraphRemote from "@wxn0brp/db-client/graph";
|
|
2
|
+
import { ValtheraAutoCreate } from "./autoCreate.js";
|
|
3
|
+
import Graph from "./graph.js";
|
|
4
|
+
import { Valthera } from "./valthera.js";
|
|
14
5
|
type GraphCompatible = Graph | GraphRemote;
|
|
15
|
-
export
|
|
16
|
-
export
|
|
6
|
+
export * from "@wxn0brp/db-core";
|
|
7
|
+
export * from "@wxn0brp/db-client";
|
|
8
|
+
export { Graph, GraphCompatible, GraphRemote, Valthera, ValtheraAutoCreate };
|
|
9
|
+
export type Id = import("@wxn0brp/db-core/types/Id").Id;
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
import ValtheraMemory, { createMemoryValthera } from "./actions/memory.js";
|
|
9
|
-
import { ValtheraAutoCreate } from "./helpers/autoCreate.js";
|
|
10
|
-
export { Valthera, Graph, ValtheraRemote, GraphRemote, Relation, genId, CustomFileCpu, ValtheraMemory, createMemoryValthera, ValtheraAutoCreate, };
|
|
1
|
+
import GraphRemote from "@wxn0brp/db-client/graph";
|
|
2
|
+
import { ValtheraAutoCreate } from "./autoCreate.js";
|
|
3
|
+
import Graph from "./graph.js";
|
|
4
|
+
import { Valthera } from "./valthera.js";
|
|
5
|
+
export * from "@wxn0brp/db-core";
|
|
6
|
+
export * from "@wxn0brp/db-client";
|
|
7
|
+
export { Graph, GraphRemote, Valthera, ValtheraAutoCreate };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import ValtheraClass from "@wxn0brp/db-core/db/valthera";
|
|
2
|
+
import FileCpu from "@wxn0brp/db-core/types/fileCpu";
|
|
3
|
+
import { DbOpts } from "@wxn0brp/db-core/types/options";
|
|
4
|
+
export declare class Valthera extends ValtheraClass {
|
|
5
|
+
constructor(folder: string, options?: DbOpts, fileCpu?: FileCpu);
|
|
6
|
+
}
|
|
7
|
+
export default Valthera;
|
package/dist/valthera.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import ValtheraClass from "@wxn0brp/db-core/db/valthera";
|
|
2
|
+
import executorC from "@wxn0brp/db-core/helpers/executor";
|
|
3
|
+
import dbActionC from "@wxn0brp/db-storage-dir/action";
|
|
4
|
+
import vFileCpu from "@wxn0brp/db-storage-dir/file/index";
|
|
5
|
+
export class Valthera extends ValtheraClass {
|
|
6
|
+
constructor(folder, options = {}, fileCpu) {
|
|
7
|
+
super(options);
|
|
8
|
+
if (!fileCpu)
|
|
9
|
+
fileCpu = vFileCpu;
|
|
10
|
+
this.dbAction = options.dbAction || new dbActionC(folder, options, fileCpu);
|
|
11
|
+
this.executor = options.executor || new executorC();
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export default Valthera;
|
package/dist/version.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "0.
|
|
1
|
+
export const version = "0.20.2";
|
package/package.json
CHANGED
|
@@ -1,32 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wxn0brp/db",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.2",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
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
6
|
"author": "wxn0brP",
|
|
20
7
|
"license": "MIT",
|
|
21
8
|
"type": "module",
|
|
22
9
|
"dependencies": {
|
|
23
|
-
"
|
|
24
|
-
"
|
|
10
|
+
"@wxn0brp/db-client": ">=0.0.1 <0.1.0",
|
|
11
|
+
"@wxn0brp/db-core": ">=0.0.1 <0.1.0",
|
|
12
|
+
"@wxn0brp/db-storage-dir": ">=0.0.1 <0.1.0"
|
|
25
13
|
},
|
|
26
14
|
"devDependencies": {
|
|
27
|
-
"@types/node": "^22.
|
|
15
|
+
"@types/node": "^22.12.0",
|
|
28
16
|
"tsc-alias": "^1.8.10",
|
|
29
|
-
"typescript": "^5.7.
|
|
17
|
+
"typescript": "^5.7.3"
|
|
30
18
|
},
|
|
31
19
|
"files": [
|
|
32
20
|
"dist"
|
|
@@ -34,11 +22,13 @@
|
|
|
34
22
|
"exports": {
|
|
35
23
|
".": {
|
|
36
24
|
"types": "./dist/index.d.ts",
|
|
37
|
-
"import": "./dist/index.js"
|
|
25
|
+
"import": "./dist/index.js",
|
|
26
|
+
"default": "./dist/index.js"
|
|
38
27
|
},
|
|
39
28
|
"./*": {
|
|
40
|
-
"types": "./dist
|
|
41
|
-
"import": "./dist
|
|
29
|
+
"types": "./dist/*.d.ts",
|
|
30
|
+
"import": "./dist/*.js",
|
|
31
|
+
"default": "./dist/*.js"
|
|
42
32
|
}
|
|
43
33
|
}
|
|
44
34
|
}
|
package/dist/actions/action.d.ts
DELETED
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import dbActionBase from "../base/actions.js";
|
|
2
|
-
import Data from "../types/data.js";
|
|
3
|
-
import FileCpu from "../types/fileCpu.js";
|
|
4
|
-
import { DbOpts } from "../types/options.js";
|
|
5
|
-
import { VQuery } from "../types/query.js";
|
|
6
|
-
/**
|
|
7
|
-
* A class representing database actions on files.
|
|
8
|
-
* @class
|
|
9
|
-
*/
|
|
10
|
-
declare class dbActionC extends dbActionBase {
|
|
11
|
-
folder: string;
|
|
12
|
-
options: DbOpts;
|
|
13
|
-
fileCpu: FileCpu;
|
|
14
|
-
/**
|
|
15
|
-
* Creates a new instance of dbActionC.
|
|
16
|
-
* @constructor
|
|
17
|
-
* @param folder - The folder where database files are stored.
|
|
18
|
-
* @param options - The options object.
|
|
19
|
-
*/
|
|
20
|
-
constructor(folder: string, options: DbOpts, fileCpu: FileCpu);
|
|
21
|
-
_getCollectionPath(collection: string): string;
|
|
22
|
-
/**
|
|
23
|
-
* Get a list of available databases in the specified folder.
|
|
24
|
-
*/
|
|
25
|
-
getCollections(): Promise<string[]>;
|
|
26
|
-
/**
|
|
27
|
-
* Check and create the specified collection if it doesn't exist.
|
|
28
|
-
*/
|
|
29
|
-
checkCollection({ collection }: VQuery): Promise<boolean>;
|
|
30
|
-
/**
|
|
31
|
-
* Check if a collection exists.
|
|
32
|
-
*/
|
|
33
|
-
issetCollection({ collection }: VQuery): Promise<boolean>;
|
|
34
|
-
/**
|
|
35
|
-
* Add a new entry to the specified database.
|
|
36
|
-
*/
|
|
37
|
-
add({ collection, data, id_gen }: VQuery): Promise<import("../types/arg.js").Arg>;
|
|
38
|
-
/**
|
|
39
|
-
* Find entries in the specified database based on search criteria.
|
|
40
|
-
*/
|
|
41
|
-
find({ collection, search, context, dbFindOpts, findOpts }: VQuery): Promise<Data[]>;
|
|
42
|
-
/**
|
|
43
|
-
* Find the first matching entry in the specified database based on search criteria.
|
|
44
|
-
*/
|
|
45
|
-
findOne({ collection, search, context, findOpts }: VQuery): Promise<Data>;
|
|
46
|
-
/**
|
|
47
|
-
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
48
|
-
*/
|
|
49
|
-
update({ collection, search, updater, context }: VQuery): Promise<boolean>;
|
|
50
|
-
/**
|
|
51
|
-
* Update the first matching entry in the specified database based on search criteria and an updater function or object.
|
|
52
|
-
*/
|
|
53
|
-
updateOne({ collection, search, updater, context }: VQuery): Promise<boolean>;
|
|
54
|
-
/**
|
|
55
|
-
* Remove entries from the specified database based on search criteria.
|
|
56
|
-
*/
|
|
57
|
-
remove({ collection, search, context }: VQuery): Promise<boolean>;
|
|
58
|
-
/**
|
|
59
|
-
* Remove the first matching entry from the specified database based on search criteria.
|
|
60
|
-
*/
|
|
61
|
-
removeOne({ collection, search, context }: VQuery): Promise<boolean>;
|
|
62
|
-
/**
|
|
63
|
-
* Removes a database collection from the file system.
|
|
64
|
-
*/
|
|
65
|
-
removeCollection({ collection }: {
|
|
66
|
-
collection: any;
|
|
67
|
-
}): Promise<boolean>;
|
|
68
|
-
}
|
|
69
|
-
export default dbActionC;
|
package/dist/actions/action.js
DELETED
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
import { existsSync, mkdirSync, promises, statSync } from "fs";
|
|
2
|
-
import dbActionBase from "../base/actions.js";
|
|
3
|
-
import gen from "../helpers/gen.js";
|
|
4
|
-
import { resolve, sep } from "path";
|
|
5
|
-
import { compareSafe } from "../utils/sort.js";
|
|
6
|
-
/**
|
|
7
|
-
* A class representing database actions on files.
|
|
8
|
-
* @class
|
|
9
|
-
*/
|
|
10
|
-
class dbActionC extends dbActionBase {
|
|
11
|
-
folder;
|
|
12
|
-
options;
|
|
13
|
-
fileCpu;
|
|
14
|
-
/**
|
|
15
|
-
* Creates a new instance of dbActionC.
|
|
16
|
-
* @constructor
|
|
17
|
-
* @param folder - The folder where database files are stored.
|
|
18
|
-
* @param options - The options object.
|
|
19
|
-
*/
|
|
20
|
-
constructor(folder, options, fileCpu) {
|
|
21
|
-
super();
|
|
22
|
-
this.folder = folder;
|
|
23
|
-
this.options = {
|
|
24
|
-
maxFileSize: 2 * 1024 * 1024, //2 MB
|
|
25
|
-
...options,
|
|
26
|
-
};
|
|
27
|
-
this.fileCpu = fileCpu;
|
|
28
|
-
if (!existsSync(folder))
|
|
29
|
-
mkdirSync(folder, { recursive: true });
|
|
30
|
-
}
|
|
31
|
-
_getCollectionPath(collection) {
|
|
32
|
-
return this.folder + "/" + collection + "/";
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Get a list of available databases in the specified folder.
|
|
36
|
-
*/
|
|
37
|
-
async getCollections() {
|
|
38
|
-
const allCollections = await promises.readdir(this.folder, { recursive: true, withFileTypes: true });
|
|
39
|
-
const collections = allCollections
|
|
40
|
-
.filter(dirent => dirent.isDirectory())
|
|
41
|
-
.map(dirent => {
|
|
42
|
-
const parentPath = resolve(dirent.parentPath);
|
|
43
|
-
const baseFolder = resolve(this.folder);
|
|
44
|
-
if (parentPath === baseFolder)
|
|
45
|
-
return dirent.name;
|
|
46
|
-
return parentPath.replace(baseFolder + sep, "") + "/" + dirent.name;
|
|
47
|
-
});
|
|
48
|
-
return collections;
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Check and create the specified collection if it doesn't exist.
|
|
52
|
-
*/
|
|
53
|
-
async checkCollection({ collection }) {
|
|
54
|
-
if (await this.issetCollection(collection))
|
|
55
|
-
return;
|
|
56
|
-
const cpath = this._getCollectionPath(collection);
|
|
57
|
-
await promises.mkdir(cpath, { recursive: true });
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Check if a collection exists.
|
|
62
|
-
*/
|
|
63
|
-
async issetCollection({ collection }) {
|
|
64
|
-
const path = this._getCollectionPath(collection);
|
|
65
|
-
try {
|
|
66
|
-
await promises.access(path);
|
|
67
|
-
return true;
|
|
68
|
-
}
|
|
69
|
-
catch {
|
|
70
|
-
return false;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Add a new entry to the specified database.
|
|
75
|
-
*/
|
|
76
|
-
async add({ collection, data, id_gen = true }) {
|
|
77
|
-
await this.checkCollection(arguments[0]);
|
|
78
|
-
const cpath = this._getCollectionPath(collection);
|
|
79
|
-
const file = cpath + await getLastFile(cpath, this.options.maxFileSize);
|
|
80
|
-
if (id_gen)
|
|
81
|
-
data._id = data._id || gen();
|
|
82
|
-
await this.fileCpu.add(file, data);
|
|
83
|
-
return data;
|
|
84
|
-
}
|
|
85
|
-
/**
|
|
86
|
-
* Find entries in the specified database based on search criteria.
|
|
87
|
-
*/
|
|
88
|
-
async find({ collection, search, context = {}, dbFindOpts = {}, findOpts = {} }) {
|
|
89
|
-
const { reverse = false, max = -1, offset = 0, sortBy, sortAsc = true } = dbFindOpts;
|
|
90
|
-
await this.checkCollection(arguments[0]);
|
|
91
|
-
const cpath = this._getCollectionPath(collection);
|
|
92
|
-
let files = await getSortedFiles(cpath);
|
|
93
|
-
if (reverse && !sortBy)
|
|
94
|
-
files.reverse();
|
|
95
|
-
let datas = [];
|
|
96
|
-
let totalEntries = 0;
|
|
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;
|
|
111
|
-
}
|
|
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
|
-
}
|
|
121
|
-
}
|
|
122
|
-
datas.push(...entries);
|
|
123
|
-
if (max !== -1 && totalEntries >= max)
|
|
124
|
-
break;
|
|
125
|
-
}
|
|
126
|
-
else {
|
|
127
|
-
datas.push(...entries);
|
|
128
|
-
}
|
|
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);
|
|
136
|
-
}
|
|
137
|
-
return datas;
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Find the first matching entry in the specified database based on search criteria.
|
|
141
|
-
*/
|
|
142
|
-
async findOne({ collection, search, context = {}, findOpts = {} }) {
|
|
143
|
-
await this.checkCollection(arguments[0]);
|
|
144
|
-
const cpath = this._getCollectionPath(collection);
|
|
145
|
-
const files = await getSortedFiles(cpath);
|
|
146
|
-
for (let f of files) {
|
|
147
|
-
let data = await this.fileCpu.findOne(cpath + f, search, context, findOpts);
|
|
148
|
-
if (data)
|
|
149
|
-
return data;
|
|
150
|
-
}
|
|
151
|
-
return null;
|
|
152
|
-
}
|
|
153
|
-
/**
|
|
154
|
-
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
155
|
-
*/
|
|
156
|
-
async update({ collection, search, updater, context = {} }) {
|
|
157
|
-
await this.checkCollection(arguments[0]);
|
|
158
|
-
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), false, search, updater, context);
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Update the first matching entry in the specified database based on search criteria and an updater function or object.
|
|
162
|
-
*/
|
|
163
|
-
async updateOne({ collection, search, updater, context = {} }) {
|
|
164
|
-
await this.checkCollection(arguments[0]);
|
|
165
|
-
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.update.bind(this.fileCpu), true, search, updater, context);
|
|
166
|
-
}
|
|
167
|
-
/**
|
|
168
|
-
* Remove entries from the specified database based on search criteria.
|
|
169
|
-
*/
|
|
170
|
-
async remove({ collection, search, context = {} }) {
|
|
171
|
-
await this.checkCollection(arguments[0]);
|
|
172
|
-
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), false, search, context);
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Remove the first matching entry from the specified database based on search criteria.
|
|
176
|
-
*/
|
|
177
|
-
async removeOne({ collection, search, context = {} }) {
|
|
178
|
-
await this.checkCollection(arguments[0]);
|
|
179
|
-
return await operationUpdater(this._getCollectionPath(collection), this.fileCpu.remove.bind(this.fileCpu), true, search, context);
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Removes a database collection from the file system.
|
|
183
|
-
*/
|
|
184
|
-
async removeCollection({ collection }) {
|
|
185
|
-
await promises.rm(this.folder + "/" + collection, { recursive: true, force: true });
|
|
186
|
-
return true;
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Get the last file in the specified directory.
|
|
191
|
-
*/
|
|
192
|
-
async function getLastFile(path, maxFileSize = 1024 * 1024) {
|
|
193
|
-
if (!existsSync(path))
|
|
194
|
-
mkdirSync(path, { recursive: true });
|
|
195
|
-
const files = await getSortedFiles(path);
|
|
196
|
-
if (files.length == 0) {
|
|
197
|
-
await promises.writeFile(path + "/1.db", "");
|
|
198
|
-
return "1.db";
|
|
199
|
-
}
|
|
200
|
-
const last = files[files.length - 1];
|
|
201
|
-
const info = path + "/" + last;
|
|
202
|
-
if (statSync(info).size < maxFileSize)
|
|
203
|
-
return last;
|
|
204
|
-
const num = parseInt(last.replace(".db", ""), 10) + 1;
|
|
205
|
-
await promises.writeFile(path + "/" + num + ".db", "");
|
|
206
|
-
return num + ".db";
|
|
207
|
-
}
|
|
208
|
-
/**
|
|
209
|
-
* Get all files in a directory sorted by name.
|
|
210
|
-
*/
|
|
211
|
-
async function getSortedFiles(folder) {
|
|
212
|
-
const files = await promises.readdir(folder, { withFileTypes: true });
|
|
213
|
-
return files
|
|
214
|
-
.filter(file => file.isFile() && !file.name.endsWith(".tmp"))
|
|
215
|
-
.map(file => file.name)
|
|
216
|
-
.filter(name => /^\d+\.db$/.test(name))
|
|
217
|
-
.sort((a, b) => {
|
|
218
|
-
const numA = parseInt(a, 10);
|
|
219
|
-
const numB = parseInt(b, 10);
|
|
220
|
-
return numA - numB;
|
|
221
|
-
});
|
|
222
|
-
}
|
|
223
|
-
async function operationUpdater(cpath, worker, one, ...args) {
|
|
224
|
-
const files = await getSortedFiles(cpath);
|
|
225
|
-
let update = false;
|
|
226
|
-
for (const file of files) {
|
|
227
|
-
const updated = await worker(cpath + file, one, ...args);
|
|
228
|
-
update = update || updated;
|
|
229
|
-
if (one && updated)
|
|
230
|
-
break;
|
|
231
|
-
}
|
|
232
|
-
return update;
|
|
233
|
-
}
|
|
234
|
-
export default dbActionC;
|
package/dist/actions/memory.d.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import dbActionBase from "../base/actions.js";
|
|
2
|
-
import Valthera from "../db/valthera.js";
|
|
3
|
-
import Data from "../types/data.js";
|
|
4
|
-
import FileCpu from "../types/fileCpu.js";
|
|
5
|
-
import { DbOpts } from "../types/options.js";
|
|
6
|
-
import { VQuery } from "../types/query.js";
|
|
7
|
-
export declare class MemoryAction extends dbActionBase {
|
|
8
|
-
folder: string;
|
|
9
|
-
options: DbOpts;
|
|
10
|
-
fileCpu: FileCpu;
|
|
11
|
-
memory: Map<string, any[]>;
|
|
12
|
-
/**
|
|
13
|
-
* Creates a new instance of dbActionC.
|
|
14
|
-
* @constructor
|
|
15
|
-
* @param folder - The folder where database files are stored.
|
|
16
|
-
* @param options - The options object.
|
|
17
|
-
*/
|
|
18
|
-
constructor();
|
|
19
|
-
_readMemory(key: string): any[];
|
|
20
|
-
_writeMemory(key: string, data: any[]): void;
|
|
21
|
-
_getCollectionPath(collection: string): string;
|
|
22
|
-
/**
|
|
23
|
-
* Get a list of available databases in the specified folder.
|
|
24
|
-
*/
|
|
25
|
-
getCollections(): Promise<string[]>;
|
|
26
|
-
/**
|
|
27
|
-
* Check and create the specified collection if it doesn't exist.
|
|
28
|
-
*/
|
|
29
|
-
checkCollection({ collection }: VQuery): Promise<boolean>;
|
|
30
|
-
/**
|
|
31
|
-
* Check if a collection exists.
|
|
32
|
-
*/
|
|
33
|
-
issetCollection({ collection }: VQuery): Promise<boolean>;
|
|
34
|
-
/**
|
|
35
|
-
* Add a new entry to the specified database.
|
|
36
|
-
*/
|
|
37
|
-
add({ collection, data, id_gen }: VQuery): Promise<import("../types/arg.js").Arg>;
|
|
38
|
-
/**
|
|
39
|
-
* Find entries in the specified database based on search criteria.
|
|
40
|
-
*/
|
|
41
|
-
find({ collection, search, context, dbFindOpts, findOpts }: VQuery): Promise<Data[]>;
|
|
42
|
-
/**
|
|
43
|
-
* Find the first matching entry in the specified database based on search criteria.
|
|
44
|
-
*/
|
|
45
|
-
findOne({ collection, search, context, findOpts }: VQuery): Promise<Data>;
|
|
46
|
-
/**
|
|
47
|
-
* Update entries in the specified database based on search criteria and an updater function or object.
|
|
48
|
-
*/
|
|
49
|
-
update({ collection, search, updater, context }: VQuery): Promise<boolean>;
|
|
50
|
-
/**
|
|
51
|
-
* Update the first matching entry in the specified database based on search criteria and an updater function or object.
|
|
52
|
-
*/
|
|
53
|
-
updateOne({ collection, search, updater, context }: VQuery): Promise<boolean>;
|
|
54
|
-
/**
|
|
55
|
-
* Remove entries from the specified database based on search criteria.
|
|
56
|
-
*/
|
|
57
|
-
remove({ collection, search, context }: VQuery): Promise<boolean>;
|
|
58
|
-
/**
|
|
59
|
-
* Remove the first matching entry from the specified database based on search criteria.
|
|
60
|
-
*/
|
|
61
|
-
removeOne({ collection, search, context }: VQuery): Promise<boolean>;
|
|
62
|
-
/**
|
|
63
|
-
* Removes a database collection from the file system.
|
|
64
|
-
*/
|
|
65
|
-
removeCollection({ collection }: VQuery): Promise<boolean>;
|
|
66
|
-
}
|
|
67
|
-
export default class ValtheraMemory extends Valthera {
|
|
68
|
-
constructor(...args: any[]);
|
|
69
|
-
}
|
|
70
|
-
export declare function createMemoryValthera<T = {
|
|
71
|
-
[key: string]: Data[];
|
|
72
|
-
}>(data?: T): ValtheraMemory;
|