unconventional 1.14.2 → 1.15.0
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/core/base.model.js +12 -5
- package/dist/utils/db.utils.d.ts +2 -0
- package/dist/utils/db.utils.js +27 -4
- package/package.json +3 -3
package/dist/core/base.model.js
CHANGED
|
@@ -71,22 +71,26 @@ class BaseModel extends Document {
|
|
|
71
71
|
return response ? this.fromDatabaseJson(response) : null;
|
|
72
72
|
}
|
|
73
73
|
static async findOne(config) {
|
|
74
|
+
const where = (0, db_utils_1.getWhere)(this, config?.filter);
|
|
75
|
+
const expand = (0, db_utils_1.expandRelationalFilters)(this, where, (0, db_utils_1.getExpand)(this, config?.expand));
|
|
74
76
|
const response = await this.db.fetch({
|
|
75
77
|
action: db_types_1.SqlAction.Select,
|
|
76
78
|
type: unconventional_pg_queries_1.OneOrMany.One,
|
|
77
79
|
table: this.collection,
|
|
78
|
-
expand
|
|
79
|
-
where
|
|
80
|
+
expand,
|
|
81
|
+
where
|
|
80
82
|
});
|
|
81
83
|
return response ? this.fromDatabaseJson(response) : null;
|
|
82
84
|
}
|
|
83
85
|
static async findMany(config) {
|
|
86
|
+
const where = (0, db_utils_1.getWhere)(this, config?.filter);
|
|
87
|
+
const expand = (0, db_utils_1.expandRelationalFilters)(this, where, (0, db_utils_1.getExpand)(this, config?.expand));
|
|
84
88
|
const response = await this.db.fetch({
|
|
85
89
|
action: db_types_1.SqlAction.Select,
|
|
86
90
|
type: unconventional_pg_queries_1.OneOrMany.Many,
|
|
87
91
|
table: this.collection,
|
|
88
|
-
expand
|
|
89
|
-
where
|
|
92
|
+
expand,
|
|
93
|
+
where,
|
|
90
94
|
order: (0, db_utils_1.getOrder)(this, config?.sort),
|
|
91
95
|
page: (0, db_utils_1.getPage)(this, config?.cursor),
|
|
92
96
|
limit: config?.limit
|
|
@@ -139,11 +143,14 @@ class BaseModel extends Document {
|
|
|
139
143
|
return this.fromDatabaseJson(result);
|
|
140
144
|
}
|
|
141
145
|
static async deleteMany(config) {
|
|
146
|
+
const where = (0, db_utils_1.getWhere)(this, config?.filter);
|
|
147
|
+
const expand = (0, db_utils_1.expandRelationalFilters)(this, where, (0, db_utils_1.getExpand)(this, config?.expand));
|
|
142
148
|
const response = await this.db.fetch({
|
|
143
149
|
action: db_types_1.SqlAction.Delete,
|
|
144
150
|
type: unconventional_pg_queries_1.OneOrMany.Many,
|
|
145
151
|
table: this.collection,
|
|
146
|
-
|
|
152
|
+
expand,
|
|
153
|
+
where
|
|
147
154
|
});
|
|
148
155
|
if (!response)
|
|
149
156
|
return null;
|
package/dist/utils/db.utils.d.ts
CHANGED
|
@@ -12,3 +12,5 @@ export declare function getReturn<T extends typeof BaseModel>(model: T): string[
|
|
|
12
12
|
export declare function getConflict<T extends typeof BaseModel>(model: T, action: ConflictResolution, constraint?: Array<keyof InstanceType<T>>): SqlConflict | undefined;
|
|
13
13
|
/** A type-safe way to build filter strings */
|
|
14
14
|
export declare function buildFilter<T>(field: keyof T, operator: SqlWhereOperator, value?: any): string;
|
|
15
|
+
/** Creates expansions for relational filters */
|
|
16
|
+
export declare function expandRelationalFilters<T extends typeof BaseModel>(model: T, wheres: SqlWhere[], expand: Record<string, Expansion>): Record<string, Expansion>;
|
package/dist/utils/db.utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.buildFilter = exports.getConflict = exports.getReturn = exports.getPage = exports.getOrder = exports.getWhere = exports.getExpand = void 0;
|
|
3
|
+
exports.expandRelationalFilters = exports.buildFilter = exports.getConflict = exports.getReturn = exports.getPage = exports.getOrder = exports.getWhere = exports.getExpand = void 0;
|
|
4
4
|
const unconventional_pg_queries_1 = require("unconventional-pg-queries");
|
|
5
5
|
const db_types_1 = require("../types/db.types");
|
|
6
6
|
const decorator_types_1 = require("../types/decorator.types");
|
|
@@ -64,6 +64,7 @@ function getWhere(model, filterString) {
|
|
|
64
64
|
let field = filter.substring(0, filter.indexOf(operator)).trim();
|
|
65
65
|
let value = filter.substring(filter.indexOf(operator) + operator.length).trim();
|
|
66
66
|
let jsonPath = [];
|
|
67
|
+
let relationPath = undefined;
|
|
67
68
|
// Handle IS NULL and IS NOT NULL, which don't have values
|
|
68
69
|
if (operator.startsWith('IS')) {
|
|
69
70
|
value = null;
|
|
@@ -71,13 +72,28 @@ function getWhere(model, filterString) {
|
|
|
71
72
|
// Handle JSON path
|
|
72
73
|
if (field.includes('.')) {
|
|
73
74
|
jsonPath = field.split('.');
|
|
74
|
-
|
|
75
|
+
const target = jsonPath.shift();
|
|
76
|
+
// Split relational fields into relation and field. e.g. 'user.name' -> 'user' and 'name'
|
|
77
|
+
// Otherwise, assume json
|
|
78
|
+
const relation = propSummary[target]?.relation;
|
|
79
|
+
if (relation) {
|
|
80
|
+
if (jsonPath.length !== 1)
|
|
81
|
+
throw api_error_1.APIError.errInvalidQueryParameter(`Invalid filter: '${field}'. Relation filters must be in the format: 'relation.field'`);
|
|
82
|
+
const relationModel = (0, api_utils_1.getRelationModel)(relation);
|
|
83
|
+
if (!relationModel.schema.props[jsonPath[0]])
|
|
84
|
+
throw api_error_1.APIError.errInvalidQueryParameter(`Invalid filter: '${field}'.`);
|
|
85
|
+
relationPath = target;
|
|
86
|
+
field = jsonPath.shift();
|
|
87
|
+
}
|
|
88
|
+
else {
|
|
89
|
+
field = target;
|
|
90
|
+
}
|
|
75
91
|
}
|
|
76
|
-
if (!propSummary[field])
|
|
92
|
+
if (!relationPath && !propSummary[field])
|
|
77
93
|
throw api_error_1.APIError.errInvalidQueryParameter(`Invalid filter: '${field}'`);
|
|
78
94
|
if (!value && !db_types_1.NullSqlOperators.includes(operator))
|
|
79
95
|
throw api_error_1.APIError.errInvalidQueryParameter(`Invalid value in filter: '${value}'`);
|
|
80
|
-
wheres.push({ field, jsonPath, operator: operator, value, andOr });
|
|
96
|
+
wheres.push({ field, jsonPath, relationPath, operator: operator, value, andOr });
|
|
81
97
|
});
|
|
82
98
|
}
|
|
83
99
|
return wheres;
|
|
@@ -149,3 +165,10 @@ function buildFilter(field, operator, value) {
|
|
|
149
165
|
return `${field} ${operator} ${formatted}`;
|
|
150
166
|
}
|
|
151
167
|
exports.buildFilter = buildFilter;
|
|
168
|
+
/** Creates expansions for relational filters */
|
|
169
|
+
function expandRelationalFilters(model, wheres, expand) {
|
|
170
|
+
const missingExpansions = wheres.flatMap(where => (where.relationPath && !expand[where.relationPath]) ? [where.relationPath] : []);
|
|
171
|
+
const addedExpansions = getExpand(model, missingExpansions.join(','));
|
|
172
|
+
return { ...expand, ...addedExpansions };
|
|
173
|
+
}
|
|
174
|
+
exports.expandRelationalFilters = expandRelationalFilters;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unconventional",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.15.0",
|
|
4
4
|
"description": "A complete server built on Cloudflare Workers/Pages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"cloudflare",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
},
|
|
46
46
|
"peerDependencies": {
|
|
47
47
|
"hono": "^4",
|
|
48
|
-
"unconventional-pg-queries": "^1.
|
|
48
|
+
"unconventional-pg-queries": "^1.4.0"
|
|
49
49
|
},
|
|
50
50
|
"devDependencies": {
|
|
51
51
|
"@cloudflare/workers-types": "^4.20231121.0",
|
|
@@ -53,6 +53,6 @@
|
|
|
53
53
|
"@typescript-eslint/parser": "^5.62.0",
|
|
54
54
|
"hono": "^4",
|
|
55
55
|
"typescript": "^5.3.3",
|
|
56
|
-
"unconventional-pg-queries": "^1.
|
|
56
|
+
"unconventional-pg-queries": "^1.4.0"
|
|
57
57
|
}
|
|
58
58
|
}
|