@wxn0brp/vql-orm 0.0.1 → 0.0.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.
- package/README.md +33 -13
- package/dist/base.d.ts +16 -0
- package/dist/base.js +66 -0
- package/dist/find.d.ts +35 -0
- package/dist/find.js +109 -0
- package/dist/index.d.ts +7 -21
- package/dist/index.js +7 -74
- package/dist/merge.js +2 -4
- package/dist/modify.d.ts +44 -0
- package/dist/modify.js +113 -0
- package/dist/orm.d.ts +24 -0
- package/dist/orm.js +56 -0
- package/dist/other.d.ts +16 -0
- package/dist/other.js +28 -0
- package/dist/relation.d.ts +20 -0
- package/dist/relation.js +69 -0
- package/package.json +39 -41
package/README.md
CHANGED
|
@@ -19,19 +19,17 @@ Here's a basic example of how to build a `find` query:
|
|
|
19
19
|
```typescript
|
|
20
20
|
import { VO } from "@wxn0brp/vql-orm";
|
|
21
21
|
|
|
22
|
-
// Create a query builder instance for the "myShop" database
|
|
23
|
-
const qb = VO("myShop");
|
|
24
|
-
|
|
25
22
|
// Build a query
|
|
26
|
-
const userQuery =
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
|
|
23
|
+
const userQuery =
|
|
24
|
+
VO("myShop")
|
|
25
|
+
.from("users")
|
|
26
|
+
.select("name", "email", "age")
|
|
27
|
+
.where({
|
|
28
|
+
status: "active",
|
|
29
|
+
$gt: { age: 18 }
|
|
30
|
+
})
|
|
31
|
+
.limit(10)
|
|
32
|
+
.query(); // This generates the final VQL query object
|
|
35
33
|
|
|
36
34
|
console.log(JSON.stringify(userQuery, null, 2));
|
|
37
35
|
```
|
|
@@ -61,6 +59,28 @@ This will produce the following VQL query object:
|
|
|
61
59
|
}
|
|
62
60
|
```
|
|
63
61
|
|
|
62
|
+
### More operations
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
// Count matching records
|
|
66
|
+
const countQuery = VO("myShop").count("users").where({ status: "active" }).query();
|
|
67
|
+
|
|
68
|
+
// Add many records at once
|
|
69
|
+
const bulkQuery = VO("myShop")
|
|
70
|
+
.bulkAdd("users", [{ name: "John" }, { name: "Jane" }])
|
|
71
|
+
.query();
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
`find` also supports `min`, `max`, `avg`, `sum`, `distinct`, `groupBy` and `count` aggregations through the chainable builder.
|
|
75
|
+
|
|
76
|
+
### Safety
|
|
77
|
+
|
|
78
|
+
Operations that can change data (`remove`, `removeOne`, `update`, `updateOne`, `updateOneOrAdd`, `toggleOne`) require an explicit `.where()`. Calling `.query()` without it throws an error instead of targeting all records.
|
|
79
|
+
|
|
80
|
+
`findOne` (`.one()`) does not support pagination or sorting options such as `limit`, `offset` or `sortBy`; using them throws.
|
|
81
|
+
|
|
82
|
+
`relation().path()` accepts the collection name and uses the ORM database name, or an explicit database and collection: `.path("users")` or `.path("main", "users")`. Relation `select` accepts strings and nested paths, for example `.select("name", ["company", "name"])`.
|
|
83
|
+
|
|
64
84
|
## License
|
|
65
85
|
|
|
66
|
-
This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.
|
|
86
|
+
This project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for details.
|
package/dist/base.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Search, VQLUQ } from "@wxn0brp/vql-client/vql";
|
|
2
|
+
export declare abstract class Builder {
|
|
3
|
+
_dbName: string;
|
|
4
|
+
_vars: Record<string, any>;
|
|
5
|
+
_collection: string;
|
|
6
|
+
_searchValue?: Search;
|
|
7
|
+
constructor(_dbName: string);
|
|
8
|
+
_buildQuery(op: string, data: any): VQLUQ;
|
|
9
|
+
var(name: string, value: any): this;
|
|
10
|
+
vars(vars: Record<string, any>): this;
|
|
11
|
+
where(search: Search): this;
|
|
12
|
+
}
|
|
13
|
+
export declare class BaseBuilder extends Builder {
|
|
14
|
+
_oneValue: boolean;
|
|
15
|
+
one(on?: boolean): this;
|
|
16
|
+
}
|
package/dist/base.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { deepMerge } from "./merge.js";
|
|
2
|
+
const REQUIRED_SEARCH = new Set([
|
|
3
|
+
"remove",
|
|
4
|
+
"removeOne",
|
|
5
|
+
"update",
|
|
6
|
+
"updateOne",
|
|
7
|
+
"updateOneOrAdd",
|
|
8
|
+
"toggleOne",
|
|
9
|
+
]);
|
|
10
|
+
const DEFAULT_SEARCH = new Set([
|
|
11
|
+
"find",
|
|
12
|
+
"findOne",
|
|
13
|
+
"count",
|
|
14
|
+
]);
|
|
15
|
+
export class Builder {
|
|
16
|
+
_dbName;
|
|
17
|
+
_vars = {};
|
|
18
|
+
_collection;
|
|
19
|
+
_searchValue;
|
|
20
|
+
constructor(_dbName) {
|
|
21
|
+
this._dbName = _dbName;
|
|
22
|
+
}
|
|
23
|
+
_buildQuery(op, data) {
|
|
24
|
+
const q = {
|
|
25
|
+
db: this._dbName,
|
|
26
|
+
d: {},
|
|
27
|
+
};
|
|
28
|
+
const thisAny = this;
|
|
29
|
+
if (thisAny._oneValue)
|
|
30
|
+
op = op + "One";
|
|
31
|
+
if (this._collection !== undefined)
|
|
32
|
+
data = {
|
|
33
|
+
collection: this._collection,
|
|
34
|
+
...data,
|
|
35
|
+
};
|
|
36
|
+
if (this._searchValue !== undefined)
|
|
37
|
+
data.search = this._searchValue;
|
|
38
|
+
else if (REQUIRED_SEARCH.has(op))
|
|
39
|
+
throw new Error(`VQL ORM: '${op}' requires an explicit search, use .where() first.`);
|
|
40
|
+
else if (DEFAULT_SEARCH.has(op))
|
|
41
|
+
data.search = {};
|
|
42
|
+
q.d[op] = data;
|
|
43
|
+
if (Object.keys(this._vars).length)
|
|
44
|
+
q.var = this._vars;
|
|
45
|
+
return q;
|
|
46
|
+
}
|
|
47
|
+
var(name, value) {
|
|
48
|
+
this._vars[name] = value;
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
vars(vars) {
|
|
52
|
+
this._vars = deepMerge(this._vars, vars);
|
|
53
|
+
return this;
|
|
54
|
+
}
|
|
55
|
+
where(search) {
|
|
56
|
+
this._searchValue = deepMerge(this._searchValue || {}, search);
|
|
57
|
+
return this;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export class BaseBuilder extends Builder {
|
|
61
|
+
_oneValue = false;
|
|
62
|
+
one(on = true) {
|
|
63
|
+
this._oneValue = on;
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
}
|
package/dist/find.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { VQLUQ } from "@wxn0brp/vql-client/vql";
|
|
2
|
+
import { BaseBuilder, Builder } from "./base.js";
|
|
3
|
+
export declare class FindBuilder extends BaseBuilder {
|
|
4
|
+
_collection: string;
|
|
5
|
+
_selectValues?: string[];
|
|
6
|
+
_limitValue?: number;
|
|
7
|
+
_excludeValues?: string[];
|
|
8
|
+
_optionsValue?: Record<string, any>;
|
|
9
|
+
constructor(dbName: string, _collection: string);
|
|
10
|
+
select(...fields: string[]): this;
|
|
11
|
+
exclude(...fields: string[]): this;
|
|
12
|
+
limit(n: number): this;
|
|
13
|
+
offset(n: number): this;
|
|
14
|
+
sortBy(field: string, asc?: boolean): this;
|
|
15
|
+
sortBy(fields: {
|
|
16
|
+
field: string;
|
|
17
|
+
asc?: boolean;
|
|
18
|
+
}[]): this;
|
|
19
|
+
sortAsc(asc?: boolean): this;
|
|
20
|
+
reverse(rev?: boolean): this;
|
|
21
|
+
min(field: any): this;
|
|
22
|
+
max(field: any): this;
|
|
23
|
+
avg(field: any): this;
|
|
24
|
+
sum(field: any): this;
|
|
25
|
+
distinct(field: any): this;
|
|
26
|
+
groupBy(field: any): this;
|
|
27
|
+
count(field: any): this;
|
|
28
|
+
_setOption(key: string, value: any): void;
|
|
29
|
+
query(): VQLUQ;
|
|
30
|
+
}
|
|
31
|
+
export declare class CountBuilder extends Builder {
|
|
32
|
+
_collection: string;
|
|
33
|
+
constructor(dbName: string, _collection: string);
|
|
34
|
+
query(): VQLUQ;
|
|
35
|
+
}
|
package/dist/find.js
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { BaseBuilder, Builder } from "./base.js";
|
|
2
|
+
export class FindBuilder extends BaseBuilder {
|
|
3
|
+
_collection;
|
|
4
|
+
_selectValues;
|
|
5
|
+
_limitValue;
|
|
6
|
+
_excludeValues;
|
|
7
|
+
_optionsValue;
|
|
8
|
+
constructor(dbName, _collection) {
|
|
9
|
+
super(dbName);
|
|
10
|
+
this._collection = _collection;
|
|
11
|
+
}
|
|
12
|
+
select(...fields) {
|
|
13
|
+
this._selectValues = (this._selectValues || []).concat(fields);
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
exclude(...fields) {
|
|
17
|
+
this._excludeValues = (this._excludeValues || []).concat(fields);
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
limit(n) {
|
|
21
|
+
this._limitValue = n;
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
offset(n) {
|
|
25
|
+
this._setOption("offset", n);
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
sortBy(field, asc) {
|
|
29
|
+
this._setOption("sortBy", field);
|
|
30
|
+
if (typeof field === "string" && asc !== undefined)
|
|
31
|
+
this._setOption("sortAsc", asc);
|
|
32
|
+
return this;
|
|
33
|
+
}
|
|
34
|
+
sortAsc(asc = true) {
|
|
35
|
+
this._setOption("sortAsc", asc);
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
reverse(rev = true) {
|
|
39
|
+
this._setOption("reverse", rev);
|
|
40
|
+
return this;
|
|
41
|
+
}
|
|
42
|
+
min(field) {
|
|
43
|
+
this._setOption("min", field);
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
max(field) {
|
|
47
|
+
this._setOption("max", field);
|
|
48
|
+
return this;
|
|
49
|
+
}
|
|
50
|
+
avg(field) {
|
|
51
|
+
this._setOption("avg", field);
|
|
52
|
+
return this;
|
|
53
|
+
}
|
|
54
|
+
sum(field) {
|
|
55
|
+
this._setOption("sum", field);
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
distinct(field) {
|
|
59
|
+
this._setOption("distinct", field);
|
|
60
|
+
return this;
|
|
61
|
+
}
|
|
62
|
+
groupBy(field) {
|
|
63
|
+
this._setOption("groupBy", field);
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
count(field) {
|
|
67
|
+
this._setOption("count", field);
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
_setOption(key, value) {
|
|
71
|
+
this._optionsValue = {
|
|
72
|
+
...this._optionsValue,
|
|
73
|
+
[key]: value,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
query() {
|
|
77
|
+
if (this._oneValue &&
|
|
78
|
+
(this._limitValue !== undefined || this._optionsValue))
|
|
79
|
+
throw new Error("VQL ORM: findOne does not support limit or options, remove .limit()/.offset()/.sortBy() and other find options.");
|
|
80
|
+
const data = {};
|
|
81
|
+
if (this._selectValues?.length)
|
|
82
|
+
data.select = this._selectValues;
|
|
83
|
+
if (this._limitValue !== undefined) {
|
|
84
|
+
data.limit = this._limitValue;
|
|
85
|
+
data.options = {
|
|
86
|
+
...this._optionsValue,
|
|
87
|
+
limit: this._limitValue,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
else if (this._optionsValue)
|
|
91
|
+
data.options = this._optionsValue;
|
|
92
|
+
const searchOpts = {};
|
|
93
|
+
if (this._excludeValues?.length)
|
|
94
|
+
searchOpts.exclude = this._excludeValues;
|
|
95
|
+
if (Object.keys(searchOpts).length)
|
|
96
|
+
data.searchOpts = searchOpts;
|
|
97
|
+
return this._buildQuery("find", data);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
export class CountBuilder extends Builder {
|
|
101
|
+
_collection;
|
|
102
|
+
constructor(dbName, _collection) {
|
|
103
|
+
super(dbName);
|
|
104
|
+
this._collection = _collection;
|
|
105
|
+
}
|
|
106
|
+
query() {
|
|
107
|
+
return this._buildQuery("count", {});
|
|
108
|
+
}
|
|
109
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,22 +1,8 @@
|
|
|
1
|
-
import
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
_one: boolean;
|
|
9
|
-
_vars: Record<string, any>;
|
|
10
|
-
constructor(dbName: string);
|
|
11
|
-
setDbName(dbName: string): void;
|
|
12
|
-
from(collection: string): this;
|
|
13
|
-
select(...select: string[]): this;
|
|
14
|
-
where(search: Search): this;
|
|
15
|
-
limit(limit: number): this;
|
|
16
|
-
one(on?: boolean): this;
|
|
17
|
-
var(name: string, value: any): this;
|
|
18
|
-
vars(vars: Record<string, any>): this;
|
|
19
|
-
query(): VQLUQ;
|
|
20
|
-
reset(): void;
|
|
21
|
-
}
|
|
1
|
+
import { VQL_ORM } from "./orm.js";
|
|
2
|
+
export * from "./orm.js";
|
|
3
|
+
export * from "./other.js";
|
|
4
|
+
export * from "./relation.js";
|
|
5
|
+
export * from "./modify.js";
|
|
6
|
+
export * from "./find.js";
|
|
7
|
+
export * from "./base.js";
|
|
22
8
|
export declare function VO(db: string): VQL_ORM;
|
package/dist/index.js
CHANGED
|
@@ -1,77 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
_one;
|
|
9
|
-
_vars;
|
|
10
|
-
constructor(dbName) {
|
|
11
|
-
this.dbName = dbName;
|
|
12
|
-
this.reset();
|
|
13
|
-
}
|
|
14
|
-
setDbName(dbName) {
|
|
15
|
-
this.dbName = dbName;
|
|
16
|
-
}
|
|
17
|
-
from(collection) {
|
|
18
|
-
this._collection = collection;
|
|
19
|
-
return this;
|
|
20
|
-
}
|
|
21
|
-
select(...select) {
|
|
22
|
-
this._select = (this._select || []).concat(select);
|
|
23
|
-
return this;
|
|
24
|
-
}
|
|
25
|
-
where(search) {
|
|
26
|
-
this._search = deepMerge(this._search, search);
|
|
27
|
-
return this;
|
|
28
|
-
}
|
|
29
|
-
limit(limit) {
|
|
30
|
-
this._limit = limit;
|
|
31
|
-
return this;
|
|
32
|
-
}
|
|
33
|
-
one(on = true) {
|
|
34
|
-
this._one = on;
|
|
35
|
-
return this;
|
|
36
|
-
}
|
|
37
|
-
var(name, value) {
|
|
38
|
-
this._vars[name] = value;
|
|
39
|
-
return this;
|
|
40
|
-
}
|
|
41
|
-
vars(vars) {
|
|
42
|
-
this._vars = deepMerge(this._vars, vars);
|
|
43
|
-
return this;
|
|
44
|
-
}
|
|
45
|
-
query() {
|
|
46
|
-
const op = "find" + (this._one ? "One" : "");
|
|
47
|
-
const q = {
|
|
48
|
-
db: this.dbName,
|
|
49
|
-
d: {}
|
|
50
|
-
};
|
|
51
|
-
const qData = {
|
|
52
|
-
collection: this._collection,
|
|
53
|
-
search: this._search,
|
|
54
|
-
};
|
|
55
|
-
if (Object.keys(this._select).length)
|
|
56
|
-
qData.select = this._select;
|
|
57
|
-
if (this._limit)
|
|
58
|
-
qData.limit = this._limit;
|
|
59
|
-
// @ts-ignore
|
|
60
|
-
q.d[op] = qData;
|
|
61
|
-
if (Object.keys(this._vars))
|
|
62
|
-
q.var = this._vars;
|
|
63
|
-
this.reset();
|
|
64
|
-
return q;
|
|
65
|
-
}
|
|
66
|
-
reset() {
|
|
67
|
-
this._collection = "";
|
|
68
|
-
this._select = [];
|
|
69
|
-
this._search = {};
|
|
70
|
-
this._limit = 0;
|
|
71
|
-
this._one = false;
|
|
72
|
-
this._vars = {};
|
|
73
|
-
}
|
|
74
|
-
}
|
|
1
|
+
import { VQL_ORM } from "./orm.js";
|
|
2
|
+
export * from "./orm.js";
|
|
3
|
+
export * from "./other.js";
|
|
4
|
+
export * from "./relation.js";
|
|
5
|
+
export * from "./modify.js";
|
|
6
|
+
export * from "./find.js";
|
|
7
|
+
export * from "./base.js";
|
|
75
8
|
export function VO(db) {
|
|
76
9
|
return new VQL_ORM(db);
|
|
77
10
|
}
|
package/dist/merge.js
CHANGED
|
@@ -3,13 +3,11 @@ export function deepMerge(...sources) {
|
|
|
3
3
|
const out = {};
|
|
4
4
|
for (const src of sources) {
|
|
5
5
|
for (const key in src) {
|
|
6
|
-
if (!Object.
|
|
6
|
+
if (!Object.hasOwn(src, key))
|
|
7
7
|
continue;
|
|
8
8
|
const sv = src[key];
|
|
9
9
|
const ov = out[key];
|
|
10
|
-
out[key] = isObj(sv) && isObj(ov)
|
|
11
|
-
? deepMerge(ov, sv)
|
|
12
|
-
: sv;
|
|
10
|
+
out[key] = isObj(sv) && isObj(ov) ? deepMerge(ov, sv) : sv;
|
|
13
11
|
}
|
|
14
12
|
}
|
|
15
13
|
return out;
|
package/dist/modify.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Arg, Updater } from "@wxn0brp/vql-client/vql";
|
|
2
|
+
import { VQLUQ } from "@wxn0brp/vql-client/vql";
|
|
3
|
+
import { BaseBuilder, Builder } from "./base.js";
|
|
4
|
+
export declare class AddBuilder extends Builder {
|
|
5
|
+
_collection: string;
|
|
6
|
+
_dataValue: Arg<any>;
|
|
7
|
+
_idGen?: boolean;
|
|
8
|
+
constructor(dbName: string, _collection: string, _dataValue: Arg<any>);
|
|
9
|
+
id_gen(id_gen: boolean): this;
|
|
10
|
+
query(): VQLUQ;
|
|
11
|
+
}
|
|
12
|
+
export declare class BulkAddBuilder extends Builder {
|
|
13
|
+
_collection: string;
|
|
14
|
+
_datasValue: Arg<any>[];
|
|
15
|
+
_idGen?: boolean;
|
|
16
|
+
constructor(dbName: string, _collection: string, _datasValue: Arg<any>[]);
|
|
17
|
+
id_gen(id_gen: boolean): this;
|
|
18
|
+
query(): VQLUQ;
|
|
19
|
+
}
|
|
20
|
+
export declare class UpdateBuilder extends BaseBuilder {
|
|
21
|
+
_collection: string;
|
|
22
|
+
_updaterValue?: Updater<any>;
|
|
23
|
+
constructor(dbName: string, _collection: string);
|
|
24
|
+
updater(updater: Updater<any>): this;
|
|
25
|
+
query(): VQLUQ;
|
|
26
|
+
}
|
|
27
|
+
export declare class UpdateOneOrAddBuilder extends Builder {
|
|
28
|
+
_collection: string;
|
|
29
|
+
_updaterValue: Updater<any>;
|
|
30
|
+
_addArg?: Arg<any>;
|
|
31
|
+
_idGen?: boolean;
|
|
32
|
+
constructor(dbName: string, _collection: string);
|
|
33
|
+
updater(updater: Updater<any>): this;
|
|
34
|
+
add_arg(add_arg: Arg<any>): this;
|
|
35
|
+
id_gen(id_gen: boolean): this;
|
|
36
|
+
query(): VQLUQ;
|
|
37
|
+
}
|
|
38
|
+
export declare class ToggleOneBuilder extends Builder {
|
|
39
|
+
_collection: string;
|
|
40
|
+
_dataValue?: Arg<any>;
|
|
41
|
+
constructor(dbName: string, _collection: string);
|
|
42
|
+
data(data: Arg<any>): this;
|
|
43
|
+
query(): VQLUQ;
|
|
44
|
+
}
|
package/dist/modify.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { BaseBuilder, Builder } from "./base.js";
|
|
2
|
+
export class AddBuilder extends Builder {
|
|
3
|
+
_collection;
|
|
4
|
+
_dataValue;
|
|
5
|
+
_idGen;
|
|
6
|
+
constructor(dbName, _collection, _dataValue) {
|
|
7
|
+
super(dbName);
|
|
8
|
+
this._collection = _collection;
|
|
9
|
+
this._dataValue = _dataValue;
|
|
10
|
+
}
|
|
11
|
+
id_gen(id_gen) {
|
|
12
|
+
this._idGen = id_gen;
|
|
13
|
+
return this;
|
|
14
|
+
}
|
|
15
|
+
query() {
|
|
16
|
+
const data = {
|
|
17
|
+
data: this._dataValue,
|
|
18
|
+
};
|
|
19
|
+
if (this._idGen !== undefined)
|
|
20
|
+
data.id_gen = this._idGen;
|
|
21
|
+
return this._buildQuery("add", data);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export class BulkAddBuilder extends Builder {
|
|
25
|
+
_collection;
|
|
26
|
+
_datasValue;
|
|
27
|
+
_idGen;
|
|
28
|
+
constructor(dbName, _collection, _datasValue) {
|
|
29
|
+
super(dbName);
|
|
30
|
+
this._collection = _collection;
|
|
31
|
+
this._datasValue = _datasValue;
|
|
32
|
+
}
|
|
33
|
+
id_gen(id_gen) {
|
|
34
|
+
this._idGen = id_gen;
|
|
35
|
+
return this;
|
|
36
|
+
}
|
|
37
|
+
query() {
|
|
38
|
+
const data = {
|
|
39
|
+
datas: this._datasValue,
|
|
40
|
+
};
|
|
41
|
+
if (this._idGen !== undefined)
|
|
42
|
+
data.id_gen = this._idGen;
|
|
43
|
+
return this._buildQuery("bulkAdd", data);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export class UpdateBuilder extends BaseBuilder {
|
|
47
|
+
_collection;
|
|
48
|
+
_updaterValue;
|
|
49
|
+
constructor(dbName, _collection) {
|
|
50
|
+
super(dbName);
|
|
51
|
+
this._collection = _collection;
|
|
52
|
+
}
|
|
53
|
+
updater(updater) {
|
|
54
|
+
this._updaterValue = updater;
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
query() {
|
|
58
|
+
const data = {
|
|
59
|
+
updater: this._updaterValue,
|
|
60
|
+
};
|
|
61
|
+
return this._buildQuery("update", data);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
export class UpdateOneOrAddBuilder extends Builder {
|
|
65
|
+
_collection;
|
|
66
|
+
_updaterValue;
|
|
67
|
+
_addArg;
|
|
68
|
+
_idGen;
|
|
69
|
+
constructor(dbName, _collection) {
|
|
70
|
+
super(dbName);
|
|
71
|
+
this._collection = _collection;
|
|
72
|
+
}
|
|
73
|
+
updater(updater) {
|
|
74
|
+
this._updaterValue = updater;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
add_arg(add_arg) {
|
|
78
|
+
this._addArg = add_arg;
|
|
79
|
+
return this;
|
|
80
|
+
}
|
|
81
|
+
id_gen(id_gen) {
|
|
82
|
+
this._idGen = id_gen;
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
query() {
|
|
86
|
+
const data = {
|
|
87
|
+
updater: this._updaterValue,
|
|
88
|
+
};
|
|
89
|
+
if (this._addArg)
|
|
90
|
+
data.add_arg = this._addArg;
|
|
91
|
+
if (this._idGen !== undefined)
|
|
92
|
+
data.id_gen = this._idGen;
|
|
93
|
+
return this._buildQuery("updateOneOrAdd", data);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
export class ToggleOneBuilder extends Builder {
|
|
97
|
+
_collection;
|
|
98
|
+
_dataValue;
|
|
99
|
+
constructor(dbName, _collection) {
|
|
100
|
+
super(dbName);
|
|
101
|
+
this._collection = _collection;
|
|
102
|
+
}
|
|
103
|
+
data(data) {
|
|
104
|
+
this._dataValue = data;
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
query() {
|
|
108
|
+
const data = {};
|
|
109
|
+
if (this._dataValue)
|
|
110
|
+
data.data = this._dataValue;
|
|
111
|
+
return this._buildQuery("toggleOne", data);
|
|
112
|
+
}
|
|
113
|
+
}
|
package/dist/orm.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Arg } from "@wxn0brp/vql-client/vql";
|
|
2
|
+
import { CountBuilder, FindBuilder } from "./find.js";
|
|
3
|
+
import { AddBuilder, BulkAddBuilder, ToggleOneBuilder, UpdateBuilder, UpdateOneOrAddBuilder } from "./modify.js";
|
|
4
|
+
import { CollectionOpBuilder, GetCollectionsBuilder, RemoveBuilder } from "./other.js";
|
|
5
|
+
import { RelationBuilder } from "./relation.js";
|
|
6
|
+
export declare class VQL_ORM {
|
|
7
|
+
_dbName: string;
|
|
8
|
+
constructor(_dbName: string);
|
|
9
|
+
setDbName(dbName: string): this;
|
|
10
|
+
find(collection: string): FindBuilder;
|
|
11
|
+
from(collection: string): FindBuilder;
|
|
12
|
+
add(collection: string, data: Arg<any>): AddBuilder;
|
|
13
|
+
bulkAdd(collection: string, datas: Arg<any>[]): BulkAddBuilder;
|
|
14
|
+
count(collection: string): CountBuilder;
|
|
15
|
+
update(collection: string): UpdateBuilder;
|
|
16
|
+
remove(collection: string): RemoveBuilder;
|
|
17
|
+
updateOneOrAdd(collection: string): UpdateOneOrAddBuilder;
|
|
18
|
+
toggleOne(collection: string): ToggleOneBuilder;
|
|
19
|
+
removeCollection(collection: string): CollectionOpBuilder;
|
|
20
|
+
ensureCollection(collection: string): CollectionOpBuilder;
|
|
21
|
+
issetCollection(collection: string): CollectionOpBuilder;
|
|
22
|
+
getCollections(): GetCollectionsBuilder;
|
|
23
|
+
relation(): RelationBuilder;
|
|
24
|
+
}
|
package/dist/orm.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { CountBuilder, FindBuilder } from "./find.js";
|
|
2
|
+
import { AddBuilder, BulkAddBuilder, ToggleOneBuilder, UpdateBuilder, UpdateOneOrAddBuilder, } from "./modify.js";
|
|
3
|
+
import { CollectionOpBuilder, GetCollectionsBuilder, RemoveBuilder, } from "./other.js";
|
|
4
|
+
import { RelationBuilder } from "./relation.js";
|
|
5
|
+
export class VQL_ORM {
|
|
6
|
+
_dbName;
|
|
7
|
+
constructor(_dbName) {
|
|
8
|
+
this._dbName = _dbName;
|
|
9
|
+
}
|
|
10
|
+
setDbName(dbName) {
|
|
11
|
+
this._dbName = dbName;
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
find(collection) {
|
|
15
|
+
return new FindBuilder(this._dbName, collection);
|
|
16
|
+
}
|
|
17
|
+
from(collection) {
|
|
18
|
+
return new FindBuilder(this._dbName, collection);
|
|
19
|
+
}
|
|
20
|
+
add(collection, data) {
|
|
21
|
+
return new AddBuilder(this._dbName, collection, data);
|
|
22
|
+
}
|
|
23
|
+
bulkAdd(collection, datas) {
|
|
24
|
+
return new BulkAddBuilder(this._dbName, collection, datas);
|
|
25
|
+
}
|
|
26
|
+
count(collection) {
|
|
27
|
+
return new CountBuilder(this._dbName, collection);
|
|
28
|
+
}
|
|
29
|
+
update(collection) {
|
|
30
|
+
return new UpdateBuilder(this._dbName, collection);
|
|
31
|
+
}
|
|
32
|
+
remove(collection) {
|
|
33
|
+
return new RemoveBuilder(this._dbName, collection);
|
|
34
|
+
}
|
|
35
|
+
updateOneOrAdd(collection) {
|
|
36
|
+
return new UpdateOneOrAddBuilder(this._dbName, collection);
|
|
37
|
+
}
|
|
38
|
+
toggleOne(collection) {
|
|
39
|
+
return new ToggleOneBuilder(this._dbName, collection);
|
|
40
|
+
}
|
|
41
|
+
removeCollection(collection) {
|
|
42
|
+
return new CollectionOpBuilder(this._dbName, "removeCollection", collection);
|
|
43
|
+
}
|
|
44
|
+
ensureCollection(collection) {
|
|
45
|
+
return new CollectionOpBuilder(this._dbName, "ensureCollection", collection);
|
|
46
|
+
}
|
|
47
|
+
issetCollection(collection) {
|
|
48
|
+
return new CollectionOpBuilder(this._dbName, "issetCollection", collection);
|
|
49
|
+
}
|
|
50
|
+
getCollections() {
|
|
51
|
+
return new GetCollectionsBuilder(this._dbName);
|
|
52
|
+
}
|
|
53
|
+
relation() {
|
|
54
|
+
return new RelationBuilder(this._dbName);
|
|
55
|
+
}
|
|
56
|
+
}
|
package/dist/other.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { VQLUQ } from "@wxn0brp/vql-client/vql";
|
|
2
|
+
import { BaseBuilder, Builder } from "./base.js";
|
|
3
|
+
export declare class RemoveBuilder extends BaseBuilder {
|
|
4
|
+
_collection: string;
|
|
5
|
+
constructor(dbName: string, _collection: string);
|
|
6
|
+
query(): VQLUQ;
|
|
7
|
+
}
|
|
8
|
+
export declare class CollectionOpBuilder extends Builder {
|
|
9
|
+
_op: string;
|
|
10
|
+
_collection: string;
|
|
11
|
+
constructor(dbName: string, _op: string, _collection: string);
|
|
12
|
+
query(): VQLUQ;
|
|
13
|
+
}
|
|
14
|
+
export declare class GetCollectionsBuilder extends Builder {
|
|
15
|
+
query(): VQLUQ;
|
|
16
|
+
}
|
package/dist/other.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { BaseBuilder, Builder } from "./base.js";
|
|
2
|
+
export class RemoveBuilder extends BaseBuilder {
|
|
3
|
+
_collection;
|
|
4
|
+
constructor(dbName, _collection) {
|
|
5
|
+
super(dbName);
|
|
6
|
+
this._collection = _collection;
|
|
7
|
+
}
|
|
8
|
+
query() {
|
|
9
|
+
return this._buildQuery("remove", {});
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export class CollectionOpBuilder extends Builder {
|
|
13
|
+
_op;
|
|
14
|
+
_collection;
|
|
15
|
+
constructor(dbName, _op, _collection) {
|
|
16
|
+
super(dbName);
|
|
17
|
+
this._op = _op;
|
|
18
|
+
this._collection = _collection;
|
|
19
|
+
}
|
|
20
|
+
query() {
|
|
21
|
+
return this._buildQuery(this._op, {});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
export class GetCollectionsBuilder extends Builder {
|
|
25
|
+
query() {
|
|
26
|
+
return this._buildQuery("getCollections", {});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { VQLUQ } from "@wxn0brp/vql-client/vql";
|
|
2
|
+
import { Builder } from "./base.js";
|
|
3
|
+
export declare class RelationBuilder extends Builder {
|
|
4
|
+
_path?: [
|
|
5
|
+
string,
|
|
6
|
+
string
|
|
7
|
+
];
|
|
8
|
+
_relations?: Record<string, any>;
|
|
9
|
+
_manyValue?: boolean;
|
|
10
|
+
_selectValues?: string[][];
|
|
11
|
+
_optionsValue?: Record<string, any>;
|
|
12
|
+
constructor(dbName: string);
|
|
13
|
+
path(collection: string): this;
|
|
14
|
+
path(db: string, collection: string): this;
|
|
15
|
+
relations(relations: Record<string, any>): this;
|
|
16
|
+
many(many?: boolean): this;
|
|
17
|
+
select(...fields: (string | string[])[]): this;
|
|
18
|
+
options(options: Record<string, any>): this;
|
|
19
|
+
query(): VQLUQ;
|
|
20
|
+
}
|
package/dist/relation.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Builder } from "./base.js";
|
|
2
|
+
export class RelationBuilder extends Builder {
|
|
3
|
+
_path;
|
|
4
|
+
_relations;
|
|
5
|
+
_manyValue;
|
|
6
|
+
_selectValues;
|
|
7
|
+
_optionsValue;
|
|
8
|
+
constructor(dbName) {
|
|
9
|
+
super(dbName);
|
|
10
|
+
}
|
|
11
|
+
path(dbOrCollection, collection) {
|
|
12
|
+
this._path =
|
|
13
|
+
collection === undefined
|
|
14
|
+
? [
|
|
15
|
+
this._dbName,
|
|
16
|
+
dbOrCollection,
|
|
17
|
+
]
|
|
18
|
+
: [
|
|
19
|
+
dbOrCollection,
|
|
20
|
+
collection,
|
|
21
|
+
];
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
relations(relations) {
|
|
25
|
+
this._relations = relations;
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
many(many = true) {
|
|
29
|
+
this._manyValue = many;
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
select(...fields) {
|
|
33
|
+
this._selectValues = [
|
|
34
|
+
...(this._selectValues || []),
|
|
35
|
+
...fields.map(field => Array.isArray(field)
|
|
36
|
+
? field
|
|
37
|
+
: [
|
|
38
|
+
field,
|
|
39
|
+
]),
|
|
40
|
+
];
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
options(options) {
|
|
44
|
+
this._optionsValue = options;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
query() {
|
|
48
|
+
if (!this._path)
|
|
49
|
+
throw new Error("VQL ORM: relation requires .path(collection) or .path(db, collection).");
|
|
50
|
+
const r = {
|
|
51
|
+
path: this._path,
|
|
52
|
+
search: this._searchValue || {},
|
|
53
|
+
relations: this._relations || {},
|
|
54
|
+
};
|
|
55
|
+
if (this._manyValue !== undefined)
|
|
56
|
+
r.many = this._manyValue;
|
|
57
|
+
if (this._selectValues?.length)
|
|
58
|
+
r.select = this._selectValues;
|
|
59
|
+
if (this._optionsValue)
|
|
60
|
+
r.options = this._optionsValue;
|
|
61
|
+
const q = {
|
|
62
|
+
db: this._dbName,
|
|
63
|
+
r,
|
|
64
|
+
};
|
|
65
|
+
if (Object.keys(this._vars).length)
|
|
66
|
+
q.var = this._vars;
|
|
67
|
+
return q;
|
|
68
|
+
}
|
|
69
|
+
}
|
package/package.json
CHANGED
|
@@ -1,42 +1,40 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
}
|
|
2
|
+
"name": "@wxn0brp/vql-orm",
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"types": "dist/index.d.ts",
|
|
6
|
+
"description": "VQL orm for VQL-client",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/wxn0brP/VQL-orm.git"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/wxn0brP/VQL-orm",
|
|
12
|
+
"author": "wxn0brP",
|
|
13
|
+
"license": "MIT",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/bun": "*",
|
|
17
|
+
"@wxn0brp/biome": "*",
|
|
18
|
+
"@wxn0brp/vql-client": "^0.3.1",
|
|
19
|
+
"tsc-alias": "^1",
|
|
20
|
+
"typescript": "^7"
|
|
21
|
+
},
|
|
22
|
+
"peerDependencies": {
|
|
23
|
+
"@wxn0brp/vql-client": "^0.3.1"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"exports": {
|
|
29
|
+
".": {
|
|
30
|
+
"types": "./dist/index.d.ts",
|
|
31
|
+
"import": "./dist/index.js",
|
|
32
|
+
"default": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./*": {
|
|
35
|
+
"types": "./dist/*.d.ts",
|
|
36
|
+
"import": "./dist/*.js",
|
|
37
|
+
"default": "./dist/*.js"
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
}
|