unconventional 1.4.2 → 1.5.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.
|
@@ -28,6 +28,8 @@ export declare class BaseModel extends Document {
|
|
|
28
28
|
static count<T extends typeof BaseModel>(this: T): Promise<number | null>;
|
|
29
29
|
static fromDatabaseJson<T extends typeof BaseModel>(this: T, json: object): InstanceType<T>;
|
|
30
30
|
private static addDefaults;
|
|
31
|
+
/** Gets the field name to use for the provided identifier */
|
|
32
|
+
private static getFieldName;
|
|
31
33
|
$id(): any;
|
|
32
34
|
$key(): any;
|
|
33
35
|
$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({
|
|
@@ -173,6 +174,12 @@ class BaseModel extends Document {
|
|
|
173
174
|
});
|
|
174
175
|
return data;
|
|
175
176
|
}
|
|
177
|
+
/** Gets the field name to use for the provided identifier */
|
|
178
|
+
static getFieldName(identifier) {
|
|
179
|
+
return !isNaN(+identifier) || (0, utils_1.isUUID)(`${identifier}`)
|
|
180
|
+
? this.idField
|
|
181
|
+
: this.keyField || undefined;
|
|
182
|
+
}
|
|
176
183
|
$id() { return this[this.constructor['idField']]; }
|
|
177
184
|
$key() { return this[this.constructor['keyField']]; }
|
|
178
185
|
$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 {
|
|
@@ -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;
|