unconventional 0.0.2 → 0.0.4
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/abstractbase.controller.d.ts +34 -1
- package/dist/core/abstractbase.controller.js +101 -10
- package/dist/core/api-error.d.ts +0 -1
- package/dist/core/api-error.js +5 -1
- package/dist/core/base.cache.d.ts +6 -1
- package/dist/core/base.cache.js +31 -14
- package/dist/core/base.model.d.ts +0 -1
- package/dist/core/base.model.js +51 -46
- package/dist/core/base.options.d.ts +12 -1
- package/dist/core/base.options.js +2 -1
- package/dist/core/base.service.d.ts +46 -1
- package/dist/core/base.service.js +82 -30
- package/dist/core/index.d.ts +0 -1
- package/dist/core/index.js +22 -6
- package/dist/core/pg.driver.d.ts +1 -1
- package/dist/core/pg.driver.js +6 -1
- package/dist/decorators/index.d.ts +0 -1
- package/dist/decorators/index.decorator.d.ts +10 -1
- package/dist/decorators/index.decorator.js +16 -2
- package/dist/decorators/index.js +20 -4
- package/dist/decorators/prop.decorator.d.ts +14 -1
- package/dist/decorators/prop.decorator.js +19 -1
- package/dist/decorators/timestamp.decorator.d.ts +8 -1
- package/dist/decorators/timestamp.decorator.js +14 -2
- package/dist/decorators/utils.d.ts +5 -1
- package/dist/decorators/utils.js +10 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.js +21 -5
- package/dist/server.d.ts +11 -1
- package/dist/server.js +30 -10
- package/dist/types/api.types.d.ts +0 -1
- package/dist/types/api.types.js +11 -8
- package/dist/types/db.types.d.ts +0 -1
- package/dist/types/db.types.js +10 -7
- package/dist/types/decorator.types.d.ts +31 -1
- package/dist/types/decorator.types.js +9 -2
- package/dist/types/index.d.ts +0 -1
- package/dist/types/index.js +20 -4
- package/dist/types/server.types.d.ts +0 -1
- package/dist/types/server.types.js +2 -1
- package/dist/utils/api.utils.d.ts +15 -1
- package/dist/utils/api.utils.js +34 -8
- package/dist/utils/array.utils.d.ts +2 -1
- package/dist/utils/array.utils.js +9 -2
- package/dist/utils/cache.utils.d.ts +0 -1
- package/dist/utils/cache.utils.js +9 -2
- package/dist/utils/crypto.utils.d.ts +1 -1
- package/dist/utils/crypto.utils.js +7 -1
- package/dist/utils/db.utils.d.ts +5 -1
- package/dist/utils/db.utils.js +55 -36
- package/dist/utils/function.utils.d.ts +1 -1
- package/dist/utils/function.utils.js +6 -3
- package/dist/utils/index.d.ts +0 -1
- package/dist/utils/index.js +24 -8
- package/dist/utils/middleware.utils.d.ts +4 -1
- package/dist/utils/middleware.utils.js +9 -1
- package/dist/utils/server.utils.d.ts +3 -1
- package/dist/utils/server.utils.js +20 -9
- package/package.json +1 -1
- package/dist/core/abstractbase.controller.d.ts.map +0 -1
- package/dist/core/api-error.d.ts.map +0 -1
- package/dist/core/base.cache.d.ts.map +0 -1
- package/dist/core/base.model.d.ts.map +0 -1
- package/dist/core/base.options.d.ts.map +0 -1
- package/dist/core/base.service.d.ts.map +0 -1
- package/dist/core/index.d.ts.map +0 -1
- package/dist/core/pg.driver.d.ts.map +0 -1
- package/dist/decorators/index.d.ts.map +0 -1
- package/dist/decorators/index.decorator.d.ts.map +0 -1
- package/dist/decorators/prop.decorator.d.ts.map +0 -1
- package/dist/decorators/timestamp.decorator.d.ts.map +0 -1
- package/dist/decorators/utils.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/server.d.ts.map +0 -1
- package/dist/types/api.types.d.ts.map +0 -1
- package/dist/types/db.types.d.ts.map +0 -1
- package/dist/types/decorator.types.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/server.types.d.ts.map +0 -1
- package/dist/utils/api.utils.d.ts.map +0 -1
- package/dist/utils/array.utils.d.ts.map +0 -1
- package/dist/utils/cache.utils.d.ts.map +0 -1
- package/dist/utils/crypto.utils.d.ts.map +0 -1
- package/dist/utils/db.utils.d.ts.map +0 -1
- package/dist/utils/function.utils.d.ts.map +0 -1
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/middleware.utils.d.ts.map +0 -1
- package/dist/utils/server.utils.d.ts.map +0 -1
package/dist/core/base.model.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
"use strict";
|
|
1
2
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
3
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
4
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
@@ -7,15 +8,18 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
8
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
10
|
};
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.TimeStamped = exports.Document = void 0;
|
|
13
|
+
const unconventional_pg_queries_1 = require("unconventional-pg-queries");
|
|
14
|
+
const prop_decorator_1 = require("../decorators/prop.decorator");
|
|
15
|
+
const timestamp_decorator_1 = require("../decorators/timestamp.decorator");
|
|
16
|
+
const db_types_1 = require("../types/db.types");
|
|
17
|
+
const db_utils_1 = require("../utils/db.utils");
|
|
18
|
+
class Document {
|
|
16
19
|
static schema = { indexes: [], props: {} };
|
|
17
20
|
}
|
|
18
|
-
|
|
21
|
+
exports.Document = Document;
|
|
22
|
+
class BaseModel extends Document {
|
|
19
23
|
static collection;
|
|
20
24
|
static idField = 'id';
|
|
21
25
|
static keyField;
|
|
@@ -28,10 +32,10 @@ export default class BaseModel extends Document {
|
|
|
28
32
|
createdAt: new Date()
|
|
29
33
|
} : {};
|
|
30
34
|
const response = await this.db.fetch({
|
|
31
|
-
action: SqlAction.Insert,
|
|
32
|
-
type: OneOrMany.One,
|
|
35
|
+
action: db_types_1.SqlAction.Insert,
|
|
36
|
+
type: unconventional_pg_queries_1.OneOrMany.One,
|
|
33
37
|
table: this.collection,
|
|
34
|
-
conflict: getConflict(this, upsertConfig?.action || ConflictResolution.doUpdate, upsertConfig?.constraint),
|
|
38
|
+
conflict: (0, db_utils_1.getConflict)(this, upsertConfig?.action || unconventional_pg_queries_1.ConflictResolution.doUpdate, upsertConfig?.constraint),
|
|
35
39
|
data: this.addDefaults({ ...data, ...timestamp })
|
|
36
40
|
});
|
|
37
41
|
if (!response)
|
|
@@ -45,10 +49,10 @@ export default class BaseModel extends Document {
|
|
|
45
49
|
createdAt: new Date()
|
|
46
50
|
} : {};
|
|
47
51
|
const response = await this.db.fetch({
|
|
48
|
-
action: SqlAction.Insert,
|
|
49
|
-
type: OneOrMany.Many,
|
|
52
|
+
action: db_types_1.SqlAction.Insert,
|
|
53
|
+
type: unconventional_pg_queries_1.OneOrMany.Many,
|
|
50
54
|
table: this.collection,
|
|
51
|
-
conflict: getConflict(this, upsertConfig?.action || ConflictResolution.doUpdate, upsertConfig?.constraint),
|
|
55
|
+
conflict: (0, db_utils_1.getConflict)(this, upsertConfig?.action || unconventional_pg_queries_1.ConflictResolution.doUpdate, upsertConfig?.constraint),
|
|
52
56
|
data: data.map(item => this.addDefaults({ ...item, ...timestamp }))
|
|
53
57
|
});
|
|
54
58
|
if (!response)
|
|
@@ -57,33 +61,33 @@ export default class BaseModel extends Document {
|
|
|
57
61
|
}
|
|
58
62
|
static async findById(id, config) {
|
|
59
63
|
const response = await this.db.fetch({
|
|
60
|
-
action: SqlAction.Select,
|
|
61
|
-
type: OneOrMany.One,
|
|
64
|
+
action: db_types_1.SqlAction.Select,
|
|
65
|
+
type: unconventional_pg_queries_1.OneOrMany.One,
|
|
62
66
|
table: this.collection,
|
|
63
|
-
expand: getExpand(this, config?.expand),
|
|
64
|
-
where: [{ field: this.idField, operator: SqlWhereOperator.Eq, value: id }]
|
|
67
|
+
expand: (0, db_utils_1.getExpand)(this, config?.expand),
|
|
68
|
+
where: [{ field: this.idField, operator: unconventional_pg_queries_1.SqlWhereOperator.Eq, value: id }]
|
|
65
69
|
});
|
|
66
70
|
return response ? this.fromDatabaseJson(response) : null;
|
|
67
71
|
}
|
|
68
72
|
static async findOne(config) {
|
|
69
73
|
const response = await this.db.fetch({
|
|
70
|
-
action: SqlAction.Select,
|
|
71
|
-
type: OneOrMany.One,
|
|
74
|
+
action: db_types_1.SqlAction.Select,
|
|
75
|
+
type: unconventional_pg_queries_1.OneOrMany.One,
|
|
72
76
|
table: this.collection,
|
|
73
|
-
expand: getExpand(this, config?.expand),
|
|
74
|
-
where: getWhere(this, config?.filter)
|
|
77
|
+
expand: (0, db_utils_1.getExpand)(this, config?.expand),
|
|
78
|
+
where: (0, db_utils_1.getWhere)(this, config?.filter)
|
|
75
79
|
});
|
|
76
80
|
return response ? this.fromDatabaseJson(response) : null;
|
|
77
81
|
}
|
|
78
82
|
static async findMany(config) {
|
|
79
83
|
const response = await this.db.fetch({
|
|
80
|
-
action: SqlAction.Select,
|
|
81
|
-
type: OneOrMany.Many,
|
|
84
|
+
action: db_types_1.SqlAction.Select,
|
|
85
|
+
type: unconventional_pg_queries_1.OneOrMany.Many,
|
|
82
86
|
table: this.collection,
|
|
83
|
-
expand: getExpand(this, config?.expand),
|
|
84
|
-
where: getWhere(this, config?.filter),
|
|
85
|
-
order: getOrder(this, config?.sort),
|
|
86
|
-
page: getPage(this, config?.cursor),
|
|
87
|
+
expand: (0, db_utils_1.getExpand)(this, config?.expand),
|
|
88
|
+
where: (0, db_utils_1.getWhere)(this, config?.filter),
|
|
89
|
+
order: (0, db_utils_1.getOrder)(this, config?.sort),
|
|
90
|
+
page: (0, db_utils_1.getPage)(this, config?.cursor),
|
|
87
91
|
limit: config?.limit
|
|
88
92
|
});
|
|
89
93
|
return response ? response.map(item => this.fromDatabaseJson(item)) : [];
|
|
@@ -92,10 +96,10 @@ export default class BaseModel extends Document {
|
|
|
92
96
|
const timestamp = this.schema.timestamped ? { updatedAt: new Date() } : {};
|
|
93
97
|
delete data[this.idField];
|
|
94
98
|
const response = await this.db.fetch({
|
|
95
|
-
action: SqlAction.Update,
|
|
96
|
-
type: OneOrMany.One,
|
|
99
|
+
action: db_types_1.SqlAction.Update,
|
|
100
|
+
type: unconventional_pg_queries_1.OneOrMany.One,
|
|
97
101
|
table: this.collection,
|
|
98
|
-
where: [{ field: this.idField, operator: SqlWhereOperator.Eq, value: id }],
|
|
102
|
+
where: [{ field: this.idField, operator: unconventional_pg_queries_1.SqlWhereOperator.Eq, value: id }],
|
|
99
103
|
data: { ...data, ...timestamp }
|
|
100
104
|
});
|
|
101
105
|
if (!response)
|
|
@@ -106,8 +110,8 @@ export default class BaseModel extends Document {
|
|
|
106
110
|
static async updateMany(data) {
|
|
107
111
|
const timestamp = this.schema.timestamped ? { updatedAt: new Date() } : {};
|
|
108
112
|
const response = await this.db.fetch({
|
|
109
|
-
action: SqlAction.Update,
|
|
110
|
-
type: OneOrMany.Many,
|
|
113
|
+
action: db_types_1.SqlAction.Update,
|
|
114
|
+
type: unconventional_pg_queries_1.OneOrMany.Many,
|
|
111
115
|
table: this.collection,
|
|
112
116
|
data: data.map(item => ({ ...item, ...timestamp }))
|
|
113
117
|
});
|
|
@@ -117,10 +121,10 @@ export default class BaseModel extends Document {
|
|
|
117
121
|
}
|
|
118
122
|
static async delete(id) {
|
|
119
123
|
const response = await this.db.fetch({
|
|
120
|
-
action: SqlAction.Delete,
|
|
121
|
-
type: OneOrMany.One,
|
|
124
|
+
action: db_types_1.SqlAction.Delete,
|
|
125
|
+
type: unconventional_pg_queries_1.OneOrMany.One,
|
|
122
126
|
table: this.collection,
|
|
123
|
-
where: [{ field: this.idField, operator: SqlWhereOperator.Eq, value: id }]
|
|
127
|
+
where: [{ field: this.idField, operator: unconventional_pg_queries_1.SqlWhereOperator.Eq, value: id }]
|
|
124
128
|
});
|
|
125
129
|
if (!response)
|
|
126
130
|
return null;
|
|
@@ -129,10 +133,10 @@ export default class BaseModel extends Document {
|
|
|
129
133
|
}
|
|
130
134
|
static async deleteMany(config) {
|
|
131
135
|
const response = await this.db.fetch({
|
|
132
|
-
action: SqlAction.Delete,
|
|
133
|
-
type: OneOrMany.Many,
|
|
136
|
+
action: db_types_1.SqlAction.Delete,
|
|
137
|
+
type: unconventional_pg_queries_1.OneOrMany.Many,
|
|
134
138
|
table: this.collection,
|
|
135
|
-
where: getWhere(this, config?.filter)
|
|
139
|
+
where: (0, db_utils_1.getWhere)(this, config?.filter)
|
|
136
140
|
});
|
|
137
141
|
if (!response)
|
|
138
142
|
return null;
|
|
@@ -140,8 +144,8 @@ export default class BaseModel extends Document {
|
|
|
140
144
|
}
|
|
141
145
|
static async count() {
|
|
142
146
|
const response = await this.db.fetch({
|
|
143
|
-
action: SqlAction.Select,
|
|
144
|
-
type: OneOrMany.Many,
|
|
147
|
+
action: db_types_1.SqlAction.Select,
|
|
148
|
+
type: unconventional_pg_queries_1.OneOrMany.Many,
|
|
145
149
|
table: this.collection,
|
|
146
150
|
count: true
|
|
147
151
|
});
|
|
@@ -167,23 +171,24 @@ export default class BaseModel extends Document {
|
|
|
167
171
|
$key() { return this[this.constructor['keyField']]; }
|
|
168
172
|
$owner() { return this[this.constructor['ownerField']]; }
|
|
169
173
|
}
|
|
174
|
+
exports.default = BaseModel;
|
|
170
175
|
__decorate([
|
|
171
|
-
prop({ required: true, unique: true }),
|
|
176
|
+
(0, prop_decorator_1.prop)({ required: true, unique: true }),
|
|
172
177
|
__metadata("design:type", Number)
|
|
173
178
|
], BaseModel.prototype, "id", void 0);
|
|
174
179
|
let TimeStamped = class TimeStamped extends BaseModel {
|
|
175
180
|
createdAt;
|
|
176
181
|
updatedAt;
|
|
177
182
|
};
|
|
183
|
+
exports.TimeStamped = TimeStamped;
|
|
178
184
|
__decorate([
|
|
179
|
-
prop(),
|
|
185
|
+
(0, prop_decorator_1.prop)(),
|
|
180
186
|
__metadata("design:type", Date)
|
|
181
187
|
], TimeStamped.prototype, "createdAt", void 0);
|
|
182
188
|
__decorate([
|
|
183
|
-
prop(),
|
|
189
|
+
(0, prop_decorator_1.prop)(),
|
|
184
190
|
__metadata("design:type", Date)
|
|
185
191
|
], TimeStamped.prototype, "updatedAt", void 0);
|
|
186
|
-
TimeStamped = __decorate([
|
|
187
|
-
timestamp()
|
|
192
|
+
exports.TimeStamped = TimeStamped = __decorate([
|
|
193
|
+
(0, timestamp_decorator_1.timestamp)()
|
|
188
194
|
], TimeStamped);
|
|
189
|
-
export { TimeStamped };
|
|
@@ -24,12 +24,16 @@ export interface ServiceConfig<M extends typeof BaseModel> {
|
|
|
24
24
|
cacheTTL?: number;
|
|
25
25
|
upsertConfig?: UpsertConfig<M>;
|
|
26
26
|
}
|
|
27
|
+
/** The configuration for a BaseController */
|
|
27
28
|
export interface ControllerOptions {
|
|
28
29
|
cache?: boolean;
|
|
29
30
|
cacheTTL?: number;
|
|
30
31
|
}
|
|
32
|
+
/** Shorthand for Partial<InstanceType<T>> */
|
|
31
33
|
export type PI<T extends abstract new (...args: any[]) => any> = Partial<InstanceType<T>>;
|
|
34
|
+
/** Shorthand for Partial<InstanceType<T>> */
|
|
32
35
|
export type I<T extends abstract new (...args: any[]) => any> = InstanceType<T>;
|
|
36
|
+
/** The configuration for a BaseController method */
|
|
33
37
|
export interface ControllerMethodOptions<M extends typeof BaseModel, V extends Env, U extends PI<M> | PI<M>[] = PI<M>, W extends I<M> | I<M>[] = I<M>> {
|
|
34
38
|
before?: BodyProcessor<M, V, U>;
|
|
35
39
|
after?: ResponseProcessor<M, V, U, W>;
|
|
@@ -41,6 +45,11 @@ export interface ControllerMethodOptions<M extends typeof BaseModel, V extends E
|
|
|
41
45
|
export interface DownloadMethodOptions<M extends typeof BaseModel, V extends Env, U extends PI<M> | PI<M>[] = any, W extends I<M> | I<M>[] = any> {
|
|
42
46
|
download?: DownloadProcessor<M, V, U, W>;
|
|
43
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* The salient context of a hono request.
|
|
50
|
+
* Information is limited so downstream controllers cannot prematurely
|
|
51
|
+
* break the req or res by operating on their streams.
|
|
52
|
+
*/
|
|
44
53
|
export interface RequestContext<M extends typeof BaseModel, V extends Env, U extends PI<M> | PI<M>[] = PI<M>> {
|
|
45
54
|
headers: Headers;
|
|
46
55
|
waitUntil: (promise: Promise<unknown>) => void;
|
|
@@ -50,8 +59,10 @@ export interface RequestContext<M extends typeof BaseModel, V extends Env, U ext
|
|
|
50
59
|
env: V['Bindings'];
|
|
51
60
|
get: <Key extends keyof V['Variables']>(key: Key) => V['Variables'][Key];
|
|
52
61
|
}
|
|
62
|
+
/** A method that operates on an express request */
|
|
53
63
|
export type BodyProcessor<M extends typeof BaseModel, V extends Env, U extends PI<M> | PI<M>[] = PI<M>> = (requestContext: RequestContext<M, V, U>) => Promise<void>;
|
|
64
|
+
/** A method that operates on an express request and a response Model */
|
|
54
65
|
export type ResponseProcessor<M extends typeof BaseModel, V extends Env = Env, U extends PI<M> | PI<M>[] = PI<M>, W extends I<M> | I<M>[] = I<M>> = (requestContext: RequestContext<M, V, U>, response: W) => Promise<void>;
|
|
66
|
+
/** A method that operates on an express request and a response Model */
|
|
55
67
|
export type DownloadProcessor<M extends typeof BaseModel, V extends Env = Env, U extends PI<M> | PI<M>[] = any, W = any> = (requestContext: RequestContext<M, V, U>, response: W) => Promise<DownloadResponse>;
|
|
56
68
|
export {};
|
|
57
|
-
//# sourceMappingURL=base.options.d.ts.map
|
|
@@ -2,15 +2,60 @@ import BaseModel from "./base.model";
|
|
|
2
2
|
import { FilterConfig, PageConfig, ServiceConfig } from "./base.options";
|
|
3
3
|
export default class BaseService<M extends typeof BaseModel> {
|
|
4
4
|
private model;
|
|
5
|
+
/** A service providing CRUD methods for any Model */
|
|
5
6
|
constructor(model: M);
|
|
7
|
+
/** Creates a service providing CRUD methods for any Model */
|
|
6
8
|
static for<M extends typeof BaseModel>(model: M): BaseService<M>;
|
|
9
|
+
/**
|
|
10
|
+
* Creates a new instance of M from the provided data
|
|
11
|
+
* @param data - A new object to be persisted.
|
|
12
|
+
* @returns The newly created document.
|
|
13
|
+
*/
|
|
7
14
|
create(data: Partial<InstanceType<M>>, config?: ServiceConfig<M>): Promise<InstanceType<M>>;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new instance of M for each item in the provided data array
|
|
17
|
+
* @param data - An array of new objects to be persisted.
|
|
18
|
+
* @returns The newly created documents.
|
|
19
|
+
*/
|
|
8
20
|
createMany(data: Partial<InstanceType<M>>[], config?: ServiceConfig<M>): Promise<InstanceType<M>[]>;
|
|
21
|
+
/**
|
|
22
|
+
* Gets one instance of the document
|
|
23
|
+
* @param identifier - The id or key of a model. If type is number, lookup will be done with the model's idColumn. Otherwise, keyColumn is used.
|
|
24
|
+
* @returns A document.
|
|
25
|
+
*/
|
|
9
26
|
get(identifier: string | number, config?: FilterConfig & ServiceConfig<M>): Promise<InstanceType<M>>;
|
|
27
|
+
/**
|
|
28
|
+
* Gets all instances of the document
|
|
29
|
+
* @param config - An object containing pagination and sorting options.
|
|
30
|
+
* @param config.limit - The maximum number of documents to return.
|
|
31
|
+
* @param config.cursor - The id of the document to start the query from.
|
|
32
|
+
* @param config.sort - An object containing the fields to sort by and their sort order.
|
|
33
|
+
* @param config.filters - A string containing the fields to filter by and their values.
|
|
34
|
+
* @returns An array of documents.
|
|
35
|
+
*/
|
|
10
36
|
getAll(config?: PageConfig & FilterConfig & ServiceConfig<M>): Promise<Array<InstanceType<M>>>;
|
|
37
|
+
/**
|
|
38
|
+
* Patches all fields provided in data on the document identified by the identifier. Certain fields may be restricted.
|
|
39
|
+
* @param identifier - The id or key of a model. If type is number, lookup will be done with the model's idColumn. Otherwise, keyColumn is used.
|
|
40
|
+
* @param data - An object containing all the fields to be modified and their new values.
|
|
41
|
+
* @returns The updated document.
|
|
42
|
+
*/
|
|
11
43
|
update(identifier: string, data: Partial<InstanceType<M>>, config?: ServiceConfig<M>): Promise<InstanceType<M>>;
|
|
44
|
+
/**
|
|
45
|
+
* Patches all fields provided in the array of documents. Certain fields may be restricted.
|
|
46
|
+
* @param data - An array of objects containing all the fields to be modified and their new values.
|
|
47
|
+
* @returns An array of updated documents.
|
|
48
|
+
*/
|
|
12
49
|
updateMany(data: Partial<InstanceType<M>>[], config?: ServiceConfig<M>): Promise<InstanceType<M>[]>;
|
|
50
|
+
/**
|
|
51
|
+
* Deletes the document with the given identifier.
|
|
52
|
+
* @param identifier - The id or key of a document. If type is a valid MongoDB ObjectID, lookup will be done with the model's idColumn. Otherwise, keyColumn is used.
|
|
53
|
+
* @returns The deleted document.
|
|
54
|
+
*/
|
|
13
55
|
delete(identifier: string, config?: ServiceConfig<M>): Promise<InstanceType<M>>;
|
|
56
|
+
/**
|
|
57
|
+
* Deletes the documents identified by config.filter.
|
|
58
|
+
* @returns An array of the deleted documents.
|
|
59
|
+
*/
|
|
14
60
|
deleteMany(config?: FilterConfig & ServiceConfig<M>): Promise<InstanceType<M>[]>;
|
|
15
61
|
}
|
|
16
|
-
//# sourceMappingURL=base.service.d.ts.map
|
|
@@ -1,122 +1,174 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const unconventional_pg_queries_1 = require("unconventional-pg-queries");
|
|
7
|
+
const api_error_1 = require("./api-error");
|
|
8
|
+
const base_cache_1 = __importDefault(require("./base.cache"));
|
|
9
|
+
const db_utils_1 = require("../utils/db.utils");
|
|
10
|
+
class BaseService {
|
|
6
11
|
model;
|
|
12
|
+
/** A service providing CRUD methods for any Model */
|
|
7
13
|
constructor(model) {
|
|
8
14
|
this.model = model;
|
|
9
15
|
}
|
|
16
|
+
/** Creates a service providing CRUD methods for any Model */
|
|
10
17
|
static for(model) {
|
|
11
18
|
return new this(model);
|
|
12
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates a new instance of M from the provided data
|
|
22
|
+
* @param data - A new object to be persisted.
|
|
23
|
+
* @returns The newly created document.
|
|
24
|
+
*/
|
|
13
25
|
async create(data, config) {
|
|
14
26
|
const response = await this.model.create(data, config?.upsertConfig);
|
|
15
27
|
if (!response) {
|
|
16
|
-
throw APIError.errResourceCreationFailed(`Failed to create ${this.model.name}`);
|
|
28
|
+
throw api_error_1.APIError.errResourceCreationFailed(`Failed to create ${this.model.name}`);
|
|
17
29
|
}
|
|
18
30
|
else if (config?.cache) {
|
|
19
|
-
await
|
|
20
|
-
await
|
|
31
|
+
await base_cache_1.default.clearPage(config.cache, this.model);
|
|
32
|
+
await base_cache_1.default.setModel(config.cache, response, undefined, config.cacheTTL);
|
|
21
33
|
}
|
|
22
34
|
return response;
|
|
23
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new instance of M for each item in the provided data array
|
|
38
|
+
* @param data - An array of new objects to be persisted.
|
|
39
|
+
* @returns The newly created documents.
|
|
40
|
+
*/
|
|
24
41
|
async createMany(data, config) {
|
|
25
42
|
const response = await this.model.createMany(data, config?.upsertConfig);
|
|
26
43
|
if (!response) {
|
|
27
|
-
throw APIError.errResourceCreationFailed(`Failed to create ${this.model.name}s`);
|
|
44
|
+
throw api_error_1.APIError.errResourceCreationFailed(`Failed to create ${this.model.name}s`);
|
|
28
45
|
}
|
|
29
46
|
else if (config?.cache) {
|
|
30
47
|
const cache = config.cache;
|
|
31
|
-
await
|
|
32
|
-
await Promise.all(response.map(item =>
|
|
48
|
+
await base_cache_1.default.clearPage(cache, this.model);
|
|
49
|
+
await Promise.all(response.map(item => base_cache_1.default.setModel(cache, item, undefined, config.cacheTTL)));
|
|
33
50
|
}
|
|
34
51
|
return response;
|
|
35
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Gets one instance of the document
|
|
55
|
+
* @param identifier - The id or key of a model. If type is number, lookup will be done with the model's idColumn. Otherwise, keyColumn is used.
|
|
56
|
+
* @returns A document.
|
|
57
|
+
*/
|
|
36
58
|
async get(identifier, config) {
|
|
37
59
|
const { cache, cacheTTL, ...modifiers } = config || {};
|
|
38
60
|
let entity = null;
|
|
39
61
|
if (cache) {
|
|
40
|
-
entity = await
|
|
62
|
+
entity = await base_cache_1.default.getModel(cache, this.model, identifier, modifiers);
|
|
41
63
|
}
|
|
42
64
|
if (!entity) {
|
|
43
65
|
if (!isNaN(+identifier)) {
|
|
44
66
|
entity = await this.model.findById(identifier, modifiers);
|
|
45
67
|
}
|
|
46
68
|
else {
|
|
47
|
-
const keyFilter = buildFilter(this.model.keyField, SqlWhereOperator.Eq, identifier);
|
|
69
|
+
const keyFilter = (0, db_utils_1.buildFilter)(this.model.keyField, unconventional_pg_queries_1.SqlWhereOperator.Eq, identifier);
|
|
48
70
|
const filter = modifiers.filter ? `${modifiers.filter} AND ${keyFilter}` : keyFilter;
|
|
49
71
|
entity = await this.model.findOne({ ...modifiers, filter });
|
|
50
72
|
}
|
|
51
73
|
if (!entity)
|
|
52
|
-
throw APIError.errNotFound(`No ${this.model.name} found with id ${identifier}`);
|
|
74
|
+
throw api_error_1.APIError.errNotFound(`No ${this.model.name} found with id ${identifier}`);
|
|
53
75
|
if (cache) {
|
|
54
|
-
await
|
|
76
|
+
await base_cache_1.default.setModel(cache, entity, modifiers, cacheTTL);
|
|
55
77
|
}
|
|
56
78
|
}
|
|
57
79
|
return entity;
|
|
58
80
|
}
|
|
81
|
+
/**
|
|
82
|
+
* Gets all instances of the document
|
|
83
|
+
* @param config - An object containing pagination and sorting options.
|
|
84
|
+
* @param config.limit - The maximum number of documents to return.
|
|
85
|
+
* @param config.cursor - The id of the document to start the query from.
|
|
86
|
+
* @param config.sort - An object containing the fields to sort by and their sort order.
|
|
87
|
+
* @param config.filters - A string containing the fields to filter by and their values.
|
|
88
|
+
* @returns An array of documents.
|
|
89
|
+
*/
|
|
59
90
|
async getAll(config) {
|
|
60
91
|
const { cache, cacheTTL, ...modifiers } = config || {};
|
|
61
92
|
let page = null;
|
|
62
93
|
if (cache) {
|
|
63
|
-
page = await
|
|
94
|
+
page = await base_cache_1.default.getPage(cache, this.model, modifiers);
|
|
64
95
|
}
|
|
65
96
|
if (!page) {
|
|
66
97
|
const limit = modifiers.limit || 12;
|
|
67
98
|
page = await this.model.findMany({ limit, ...modifiers });
|
|
68
99
|
if (cache) {
|
|
69
|
-
await
|
|
100
|
+
await base_cache_1.default.setPage(cache, page, modifiers, cacheTTL);
|
|
70
101
|
}
|
|
71
102
|
}
|
|
72
103
|
return page;
|
|
73
104
|
}
|
|
105
|
+
/**
|
|
106
|
+
* Patches all fields provided in data on the document identified by the identifier. Certain fields may be restricted.
|
|
107
|
+
* @param identifier - The id or key of a model. If type is number, lookup will be done with the model's idColumn. Otherwise, keyColumn is used.
|
|
108
|
+
* @param data - An object containing all the fields to be modified and their new values.
|
|
109
|
+
* @returns The updated document.
|
|
110
|
+
*/
|
|
74
111
|
async update(identifier, data, config) {
|
|
75
112
|
const response = await this.model.update(identifier, data);
|
|
76
113
|
if (!response) {
|
|
77
|
-
throw APIError.errNotFound(`No ${this.model.name} found with id ${identifier}`);
|
|
114
|
+
throw api_error_1.APIError.errNotFound(`No ${this.model.name} found with id ${identifier}`);
|
|
78
115
|
}
|
|
79
116
|
else if (config?.cache) {
|
|
80
|
-
await
|
|
81
|
-
await
|
|
117
|
+
await base_cache_1.default.clearModel(config.cache, response);
|
|
118
|
+
await base_cache_1.default.setModel(config.cache, response, undefined, config.cacheTTL);
|
|
82
119
|
}
|
|
83
120
|
return response;
|
|
84
121
|
}
|
|
122
|
+
/**
|
|
123
|
+
* Patches all fields provided in the array of documents. Certain fields may be restricted.
|
|
124
|
+
* @param data - An array of objects containing all the fields to be modified and their new values.
|
|
125
|
+
* @returns An array of updated documents.
|
|
126
|
+
*/
|
|
85
127
|
async updateMany(data, config) {
|
|
86
128
|
const response = await this.model.updateMany(data);
|
|
87
129
|
if (!response) {
|
|
88
|
-
throw APIError.errResourceUpdateFailed(`Failed to update ${this.model.name}s`);
|
|
130
|
+
throw api_error_1.APIError.errResourceUpdateFailed(`Failed to update ${this.model.name}s`);
|
|
89
131
|
}
|
|
90
132
|
else if (config?.cache) {
|
|
91
133
|
const cache = config.cache;
|
|
92
|
-
await Promise.all(response.map(item =>
|
|
93
|
-
await Promise.all(response.map(item =>
|
|
134
|
+
await Promise.all(response.map(item => base_cache_1.default.clearModel(cache, item)));
|
|
135
|
+
await Promise.all(response.map(item => base_cache_1.default.setModel(cache, item, undefined, config.cacheTTL)));
|
|
94
136
|
}
|
|
95
137
|
return response;
|
|
96
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Deletes the document with the given identifier.
|
|
141
|
+
* @param identifier - The id or key of a document. If type is a valid MongoDB ObjectID, lookup will be done with the model's idColumn. Otherwise, keyColumn is used.
|
|
142
|
+
* @returns The deleted document.
|
|
143
|
+
*/
|
|
97
144
|
async delete(identifier, config) {
|
|
98
145
|
const response = await this.model.delete(identifier);
|
|
99
146
|
if (!response) {
|
|
100
|
-
throw APIError.errNotFound(`No ${this.model.name} found with id ${identifier}`);
|
|
147
|
+
throw api_error_1.APIError.errNotFound(`No ${this.model.name} found with id ${identifier}`);
|
|
101
148
|
}
|
|
102
149
|
else if (config?.cache) {
|
|
103
|
-
await
|
|
104
|
-
await
|
|
150
|
+
await base_cache_1.default.clearModel(config.cache, response);
|
|
151
|
+
await base_cache_1.default.clearPage(config.cache, this.model);
|
|
105
152
|
}
|
|
106
153
|
return response;
|
|
107
154
|
}
|
|
155
|
+
/**
|
|
156
|
+
* Deletes the documents identified by config.filter.
|
|
157
|
+
* @returns An array of the deleted documents.
|
|
158
|
+
*/
|
|
108
159
|
async deleteMany(config) {
|
|
109
160
|
if (!config?.filter)
|
|
110
|
-
throw APIError.errBadRequest("No filter provided");
|
|
161
|
+
throw api_error_1.APIError.errBadRequest("No filter provided");
|
|
111
162
|
const response = await this.model.deleteMany(config);
|
|
112
163
|
if (!response) {
|
|
113
|
-
throw APIError.errResourceDeletionFailed(`Failed to delete ${this.model.name}s`);
|
|
164
|
+
throw api_error_1.APIError.errResourceDeletionFailed(`Failed to delete ${this.model.name}s`);
|
|
114
165
|
}
|
|
115
166
|
else if (config?.cache) {
|
|
116
167
|
const cache = config.cache;
|
|
117
|
-
await
|
|
118
|
-
await Promise.all(response.map(item =>
|
|
168
|
+
await base_cache_1.default.clearPage(config.cache, this.model);
|
|
169
|
+
await Promise.all(response.map(item => base_cache_1.default.clearModel(cache, item)));
|
|
119
170
|
}
|
|
120
171
|
return response;
|
|
121
172
|
}
|
|
122
173
|
}
|
|
174
|
+
exports.default = BaseService;
|
package/dist/core/index.d.ts
CHANGED
package/dist/core/index.js
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./abstractbase.controller"), exports);
|
|
18
|
+
__exportStar(require("./api-error"), exports);
|
|
19
|
+
__exportStar(require("./base.cache"), exports);
|
|
20
|
+
__exportStar(require("./base.model"), exports);
|
|
21
|
+
__exportStar(require("./base.options"), exports);
|
|
22
|
+
__exportStar(require("./base.service"), exports);
|
package/dist/core/pg.driver.d.ts
CHANGED
package/dist/core/pg.driver.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PGFactory = void 0;
|
|
1
4
|
async function PGFn(binding, endpoint, query) {
|
|
2
5
|
const response = await binding.fetch(new Request(`http://worker/${endpoint}`, {
|
|
3
6
|
method: 'POST',
|
|
@@ -9,7 +12,8 @@ async function PGFn(binding, endpoint, query) {
|
|
|
9
12
|
const body = await response.json();
|
|
10
13
|
return body;
|
|
11
14
|
}
|
|
12
|
-
|
|
15
|
+
/** Produces an object with PostgreSQL proxy methods */
|
|
16
|
+
const PGFactory = (ctx) => {
|
|
13
17
|
return {
|
|
14
18
|
fetch: async (query) => {
|
|
15
19
|
return await PGFn(ctx.env.DB_PROXY, 'query', query);
|
|
@@ -19,3 +23,4 @@ export const PGFactory = (ctx) => {
|
|
|
19
23
|
}
|
|
20
24
|
};
|
|
21
25
|
};
|
|
26
|
+
exports.PGFactory = PGFactory;
|
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import BaseModel from '../core/base.model';
|
|
2
2
|
import { IndexDefinition, IndexOptions, TypedClassDecorator } from '../types/decorator.types';
|
|
3
|
+
/**
|
|
4
|
+
* Defines a index for this Class which will then be added to the collection.
|
|
5
|
+
* @param fields Which fields to index (if multiple fields are set, it will be a compound index)
|
|
6
|
+
* @param options Options to pass to MongoDB when creating the index
|
|
7
|
+
* @example Example:
|
|
8
|
+
* ```ts
|
|
9
|
+
* @index({ article: 1, user: 1 }, { unique: true })
|
|
10
|
+
* class ClassName {}
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
3
13
|
export declare function index<T extends typeof BaseModel>(fields: IndexDefinition<InstanceType<T>>, options?: IndexOptions): TypedClassDecorator<T>;
|
|
4
14
|
export { index as Index };
|
|
5
|
-
//# sourceMappingURL=index.decorator.d.ts.map
|
|
@@ -1,6 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Index = exports.index = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Defines a index for this Class which will then be added to the collection.
|
|
6
|
+
* @param fields Which fields to index (if multiple fields are set, it will be a compound index)
|
|
7
|
+
* @param options Options to pass to MongoDB when creating the index
|
|
8
|
+
* @example Example:
|
|
9
|
+
* ```ts
|
|
10
|
+
* @index({ article: 1, user: 1 }, { unique: true })
|
|
11
|
+
* class ClassName {}
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
function index(fields, options) {
|
|
2
15
|
return (target) => {
|
|
3
16
|
target.schema.indexes.push({ definition: fields, options });
|
|
4
17
|
};
|
|
5
18
|
}
|
|
6
|
-
|
|
19
|
+
exports.index = index;
|
|
20
|
+
exports.Index = index;
|
package/dist/decorators/index.js
CHANGED
|
@@ -1,4 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./index.decorator"), exports);
|
|
18
|
+
__exportStar(require("./prop.decorator"), exports);
|
|
19
|
+
__exportStar(require("./timestamp.decorator"), exports);
|
|
20
|
+
__exportStar(require("./utils"), exports);
|