unconventional 1.4.2 → 1.6.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.d.ts +6 -0
- package/dist/core/base.model.js +34 -3
- package/dist/core/base.service.js +2 -1
- package/dist/types/db.types.d.ts +2 -1
- package/dist/types/db.types.js +1 -0
- package/dist/utils/api.utils.d.ts +3 -1
- package/dist/utils/api.utils.js +23 -2
- package/package.json +3 -3
|
@@ -26,8 +26,14 @@ export declare class BaseModel extends Document {
|
|
|
26
26
|
static delete<T extends typeof BaseModel>(this: T, id: string | number): Promise<InstanceType<T> | null>;
|
|
27
27
|
static deleteMany<T extends typeof BaseModel>(this: T, config?: FilterConfig): Promise<InstanceType<T>[] | null>;
|
|
28
28
|
static count<T extends typeof BaseModel>(this: T): Promise<number | null>;
|
|
29
|
+
static increment<T extends typeof BaseModel>(this: T, id: string | number, data: {
|
|
30
|
+
[key in keyof InstanceType<T>]: number;
|
|
31
|
+
}): Promise<InstanceType<T> | null>;
|
|
32
|
+
static truncate<T extends typeof BaseModel>(this: T): Promise<void>;
|
|
29
33
|
static fromDatabaseJson<T extends typeof BaseModel>(this: T, json: object): InstanceType<T>;
|
|
30
34
|
private static addDefaults;
|
|
35
|
+
/** Gets the field name to use for the provided identifier */
|
|
36
|
+
private static getFieldName;
|
|
31
37
|
$id(): any;
|
|
32
38
|
$key(): any;
|
|
33
39
|
$owner(): any;
|
package/dist/core/base.model.js
CHANGED
|
@@ -15,6 +15,7 @@ const prop_decorator_1 = require("../decorators/prop.decorator");
|
|
|
15
15
|
const timestamp_decorator_1 = require("../decorators/timestamp.decorator");
|
|
16
16
|
const db_types_1 = require("../types/db.types");
|
|
17
17
|
const db_utils_1 = require("../utils/db.utils");
|
|
18
|
+
const utils_1 = require("../utils");
|
|
18
19
|
class Document {
|
|
19
20
|
static schema = { indexes: [], props: {} };
|
|
20
21
|
}
|
|
@@ -94,7 +95,7 @@ class BaseModel extends Document {
|
|
|
94
95
|
}
|
|
95
96
|
static async update(id, data) {
|
|
96
97
|
const timestamp = this.schema.timestamped ? { updatedAt: new Date() } : {};
|
|
97
|
-
const targetField =
|
|
98
|
+
const targetField = this.getFieldName(id);
|
|
98
99
|
if (!targetField)
|
|
99
100
|
return null;
|
|
100
101
|
delete data[targetField];
|
|
@@ -123,7 +124,7 @@ class BaseModel extends Document {
|
|
|
123
124
|
return response.map(item => this.fromDatabaseJson(item));
|
|
124
125
|
}
|
|
125
126
|
static async delete(id) {
|
|
126
|
-
const targetField =
|
|
127
|
+
const targetField = this.getFieldName(id);
|
|
127
128
|
if (!targetField)
|
|
128
129
|
return null;
|
|
129
130
|
const response = await this.db.fetch({
|
|
@@ -153,12 +154,36 @@ class BaseModel extends Document {
|
|
|
153
154
|
action: db_types_1.SqlAction.Select,
|
|
154
155
|
type: unconventional_pg_queries_1.OneOrMany.Many,
|
|
155
156
|
table: this.collection,
|
|
156
|
-
|
|
157
|
+
subAction: unconventional_pg_queries_1.SubAction.Count
|
|
157
158
|
});
|
|
158
159
|
if (!response)
|
|
159
160
|
return null;
|
|
160
161
|
return response.count;
|
|
161
162
|
}
|
|
163
|
+
static async increment(id, data) {
|
|
164
|
+
const targetField = this.getFieldName(id);
|
|
165
|
+
if (!targetField)
|
|
166
|
+
return null;
|
|
167
|
+
const response = await this.db.fetch({
|
|
168
|
+
action: db_types_1.SqlAction.Update,
|
|
169
|
+
type: unconventional_pg_queries_1.OneOrMany.One,
|
|
170
|
+
table: this.collection,
|
|
171
|
+
where: [{ field: targetField, operator: unconventional_pg_queries_1.SqlWhereOperator.Eq, value: id }],
|
|
172
|
+
subAction: unconventional_pg_queries_1.SubAction.Increment,
|
|
173
|
+
data
|
|
174
|
+
});
|
|
175
|
+
if (!response)
|
|
176
|
+
return null;
|
|
177
|
+
const result = Array.isArray(response) ? response[0] : response;
|
|
178
|
+
return this.fromDatabaseJson(result);
|
|
179
|
+
}
|
|
180
|
+
static async truncate() {
|
|
181
|
+
await this.db.fetch({
|
|
182
|
+
action: db_types_1.SqlAction.Truncate,
|
|
183
|
+
type: unconventional_pg_queries_1.OneOrMany.Many,
|
|
184
|
+
table: this.collection
|
|
185
|
+
});
|
|
186
|
+
}
|
|
162
187
|
static fromDatabaseJson(json) {
|
|
163
188
|
const model = new this();
|
|
164
189
|
Object.assign(model, json);
|
|
@@ -173,6 +198,12 @@ class BaseModel extends Document {
|
|
|
173
198
|
});
|
|
174
199
|
return data;
|
|
175
200
|
}
|
|
201
|
+
/** Gets the field name to use for the provided identifier */
|
|
202
|
+
static getFieldName(identifier) {
|
|
203
|
+
return !isNaN(+identifier) || (0, utils_1.isUUID)(`${identifier}`)
|
|
204
|
+
? this.idField
|
|
205
|
+
: this.keyField || undefined;
|
|
206
|
+
}
|
|
176
207
|
$id() { return this[this.constructor['idField']]; }
|
|
177
208
|
$key() { return this[this.constructor['keyField']]; }
|
|
178
209
|
$owner() { return this[this.constructor['ownerField']]; }
|
|
@@ -5,6 +5,7 @@ const unconventional_pg_queries_1 = require("unconventional-pg-queries");
|
|
|
5
5
|
const api_error_1 = require("./api-error");
|
|
6
6
|
const base_cache_1 = require("./base.cache");
|
|
7
7
|
const db_utils_1 = require("../utils/db.utils");
|
|
8
|
+
const utils_1 = require("../utils");
|
|
8
9
|
class BaseService {
|
|
9
10
|
model;
|
|
10
11
|
/** A service providing CRUD methods for any Model */
|
|
@@ -60,7 +61,7 @@ class BaseService {
|
|
|
60
61
|
entity = await base_cache_1.BaseCache.getModel(cache, this.model, identifier, modifiers);
|
|
61
62
|
}
|
|
62
63
|
if (!entity) {
|
|
63
|
-
if (!isNaN(+identifier)) {
|
|
64
|
+
if (!isNaN(+identifier) || (0, utils_1.isUUID)(`${identifier}`)) {
|
|
64
65
|
entity = await this.model.findById(identifier, modifiers);
|
|
65
66
|
}
|
|
66
67
|
else {
|
package/dist/types/db.types.d.ts
CHANGED
package/dist/types/db.types.js
CHANGED
|
@@ -19,5 +19,7 @@ export declare function splitExpandUnit(input: string): {
|
|
|
19
19
|
};
|
|
20
20
|
/** Parse a filter string and replace clauses that reference the target field with the provided replacement. */
|
|
21
21
|
export declare function replaceFilter(filter: string | string[] | undefined, target: string, replacement: string, andOr?: AndOr): string;
|
|
22
|
-
/** Check
|
|
22
|
+
/** Check whether the filter has the specified target field. */
|
|
23
23
|
export declare function hasFilter(filter: string | string[] | undefined, target: string): boolean;
|
|
24
|
+
/** Check whether a string is UUID */
|
|
25
|
+
export declare function isUUID(str: string): boolean;
|
package/dist/utils/api.utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.hasFilter = exports.replaceFilter = exports.splitExpandUnit = exports.parseExpandString = void 0;
|
|
3
|
+
exports.isUUID = exports.hasFilter = exports.replaceFilter = exports.splitExpandUnit = exports.parseExpandString = 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 defaultTarget = ',';
|
|
@@ -63,10 +63,31 @@ function replaceFilter(filter, target, replacement, andOr = unconventional_pg_qu
|
|
|
63
63
|
return otherFilters.join(' ');
|
|
64
64
|
}
|
|
65
65
|
exports.replaceFilter = replaceFilter;
|
|
66
|
-
/** Check
|
|
66
|
+
/** Check whether the filter has the specified target field. */
|
|
67
67
|
function hasFilter(filter, target) {
|
|
68
68
|
if (!filter)
|
|
69
69
|
return false;
|
|
70
70
|
return filter.split(db_types_1.andOrPattern).some(x => x.startsWith(target));
|
|
71
71
|
}
|
|
72
72
|
exports.hasFilter = hasFilter;
|
|
73
|
+
/** Check whether a string is UUID */
|
|
74
|
+
function isUUID(str) {
|
|
75
|
+
if (str.length !== 36)
|
|
76
|
+
return false;
|
|
77
|
+
for (let i = 0; i < str.length; i++) {
|
|
78
|
+
if (i === 8 || i === 13 || i === 18 || i === 23) {
|
|
79
|
+
if (str[i] !== '-')
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
const charCode = str.charCodeAt(i);
|
|
84
|
+
const isDigit = charCode >= 48 && charCode <= 57; // 0-9
|
|
85
|
+
const isLowerHex = charCode >= 97 && charCode <= 102; // a-f
|
|
86
|
+
const isUpperHex = charCode >= 65 && charCode <= 70; // A-F
|
|
87
|
+
if (!isDigit && !isLowerHex && !isUpperHex)
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
exports.isUUID = isUUID;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "unconventional",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.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": "^0.0
|
|
48
|
+
"unconventional-pg-queries": "^1.0.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": "^0.0
|
|
56
|
+
"unconventional-pg-queries": "^1.0.0"
|
|
57
57
|
}
|
|
58
58
|
}
|