unconventional 0.0.5 → 0.0.6
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 +3 -3
- package/dist/core/abstractbase.controller.js +4 -6
- package/dist/core/base.cache.d.ts +2 -2
- package/dist/core/base.cache.js +2 -1
- package/dist/core/base.model.d.ts +1 -1
- package/dist/core/base.model.js +2 -2
- package/dist/core/base.options.d.ts +1 -1
- package/dist/core/base.service.d.ts +2 -2
- package/dist/core/base.service.js +19 -21
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.js +1 -0
- package/dist/decorators/index.decorator.d.ts +1 -1
- package/dist/decorators/prop.decorator.d.ts +1 -1
- package/dist/types/decorator.types.d.ts +5 -1
- package/dist/utils/db.utils.d.ts +1 -1
- package/dist/utils/server.utils.js +3 -6
- package/package.json +1 -1
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
2
|
import { Context } from "hono";
|
|
3
3
|
import { BaseHandler, Env } from "../types/api.types";
|
|
4
|
-
import BaseModel from "./base.model";
|
|
4
|
+
import { BaseModel } from "./base.model";
|
|
5
5
|
import { ControllerMethodOptions, ControllerOptions, DownloadMethodOptions, I, PI, RequestContext } from "./base.options";
|
|
6
|
-
import BaseService from "./base.service";
|
|
7
|
-
export
|
|
6
|
+
import { BaseService } from "./base.service";
|
|
7
|
+
export declare abstract class AbstractBaseController<M extends typeof BaseModel, E extends Env = Env> {
|
|
8
8
|
protected _model: M;
|
|
9
9
|
protected _service: BaseService<M>;
|
|
10
10
|
protected _pageLimitDefault: number;
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AbstractBaseController = void 0;
|
|
6
4
|
const api_types_1 = require("../types/api.types");
|
|
7
|
-
const base_service_1 =
|
|
5
|
+
const base_service_1 = require("./base.service");
|
|
8
6
|
class AbstractBaseController {
|
|
9
7
|
_model;
|
|
10
8
|
_service;
|
|
@@ -14,7 +12,7 @@ class AbstractBaseController {
|
|
|
14
12
|
/** A controller providing CRUD endpoints for any BaseModel */
|
|
15
13
|
constructor(model, options) {
|
|
16
14
|
this._model = model;
|
|
17
|
-
this._service = new base_service_1.
|
|
15
|
+
this._service = new base_service_1.BaseService(model);
|
|
18
16
|
this._options = options || {};
|
|
19
17
|
}
|
|
20
18
|
create(options) { return this._create.bind(this, options); }
|
|
@@ -286,4 +284,4 @@ class AbstractBaseController {
|
|
|
286
284
|
return useCache ? ctx.env.CACHE : undefined;
|
|
287
285
|
}
|
|
288
286
|
}
|
|
289
|
-
exports.
|
|
287
|
+
exports.AbstractBaseController = AbstractBaseController;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import BaseModel from "./base.model";
|
|
3
|
-
export
|
|
2
|
+
import { BaseModel } from "./base.model";
|
|
3
|
+
export declare class BaseCache {
|
|
4
4
|
private static kvKeySizeLimit;
|
|
5
5
|
static get<T>(cache: KVNamespace, key: string): Promise<T | null>;
|
|
6
6
|
static set<T>(cache: KVNamespace, key: string, value: T, ttl?: number): Promise<void>;
|
package/dist/core/base.cache.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseCache = void 0;
|
|
3
4
|
const api_types_1 = require("../types/api.types");
|
|
4
5
|
const array_utils_1 = require("../utils/array.utils");
|
|
5
6
|
const crypto_utils_1 = require("../utils/crypto.utils");
|
|
@@ -86,4 +87,4 @@ class BaseCache {
|
|
|
86
87
|
return modifier && !(0, array_utils_1.isEmpty)(modifier) ? await (0, crypto_utils_1.sha256)(JSON.stringify(modifier)) : "";
|
|
87
88
|
}
|
|
88
89
|
}
|
|
89
|
-
exports.
|
|
90
|
+
exports.BaseCache = BaseCache;
|
|
@@ -9,7 +9,7 @@ interface BaseModelSchema {
|
|
|
9
9
|
export declare class Document {
|
|
10
10
|
static schema: BaseModelSchema;
|
|
11
11
|
}
|
|
12
|
-
export
|
|
12
|
+
export declare class BaseModel extends Document {
|
|
13
13
|
static collection: string;
|
|
14
14
|
static idField: string;
|
|
15
15
|
static keyField?: string;
|
package/dist/core/base.model.js
CHANGED
|
@@ -9,7 +9,7 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
9
9
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.TimeStamped = exports.Document = void 0;
|
|
12
|
+
exports.TimeStamped = exports.BaseModel = exports.Document = void 0;
|
|
13
13
|
const unconventional_pg_queries_1 = require("unconventional-pg-queries");
|
|
14
14
|
const prop_decorator_1 = require("../decorators/prop.decorator");
|
|
15
15
|
const timestamp_decorator_1 = require("../decorators/timestamp.decorator");
|
|
@@ -171,7 +171,7 @@ class BaseModel extends Document {
|
|
|
171
171
|
$key() { return this[this.constructor['keyField']]; }
|
|
172
172
|
$owner() { return this[this.constructor['ownerField']]; }
|
|
173
173
|
}
|
|
174
|
-
exports.
|
|
174
|
+
exports.BaseModel = BaseModel;
|
|
175
175
|
__decorate([
|
|
176
176
|
(0, prop_decorator_1.prop)({ required: true, unique: true }),
|
|
177
177
|
__metadata("design:type", Number)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
2
|
import { ConflictResolution } from "unconventional-pg-queries";
|
|
3
3
|
import { Env } from "../types/api.types";
|
|
4
|
-
import BaseModel from "./base.model";
|
|
4
|
+
import { BaseModel } from "./base.model";
|
|
5
5
|
export interface PageConfig {
|
|
6
6
|
limit?: number;
|
|
7
7
|
cursor?: string;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import BaseModel from "./base.model";
|
|
1
|
+
import { BaseModel } from "./base.model";
|
|
2
2
|
import { FilterConfig, PageConfig, ServiceConfig } from "./base.options";
|
|
3
|
-
export
|
|
3
|
+
export declare class BaseService<M extends typeof BaseModel> {
|
|
4
4
|
private model;
|
|
5
5
|
/** A service providing CRUD methods for any Model */
|
|
6
6
|
constructor(model: M);
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseService = void 0;
|
|
6
4
|
const unconventional_pg_queries_1 = require("unconventional-pg-queries");
|
|
7
5
|
const api_error_1 = require("./api-error");
|
|
8
|
-
const base_cache_1 =
|
|
6
|
+
const base_cache_1 = require("./base.cache");
|
|
9
7
|
const db_utils_1 = require("../utils/db.utils");
|
|
10
8
|
class BaseService {
|
|
11
9
|
model;
|
|
@@ -28,8 +26,8 @@ class BaseService {
|
|
|
28
26
|
throw api_error_1.APIError.errResourceCreationFailed(`Failed to create ${this.model.name}`);
|
|
29
27
|
}
|
|
30
28
|
else if (config?.cache) {
|
|
31
|
-
await base_cache_1.
|
|
32
|
-
await base_cache_1.
|
|
29
|
+
await base_cache_1.BaseCache.clearPage(config.cache, this.model);
|
|
30
|
+
await base_cache_1.BaseCache.setModel(config.cache, response, undefined, config.cacheTTL);
|
|
33
31
|
}
|
|
34
32
|
return response;
|
|
35
33
|
}
|
|
@@ -45,8 +43,8 @@ class BaseService {
|
|
|
45
43
|
}
|
|
46
44
|
else if (config?.cache) {
|
|
47
45
|
const cache = config.cache;
|
|
48
|
-
await base_cache_1.
|
|
49
|
-
await Promise.all(response.map(item => base_cache_1.
|
|
46
|
+
await base_cache_1.BaseCache.clearPage(cache, this.model);
|
|
47
|
+
await Promise.all(response.map(item => base_cache_1.BaseCache.setModel(cache, item, undefined, config.cacheTTL)));
|
|
50
48
|
}
|
|
51
49
|
return response;
|
|
52
50
|
}
|
|
@@ -59,7 +57,7 @@ class BaseService {
|
|
|
59
57
|
const { cache, cacheTTL, ...modifiers } = config || {};
|
|
60
58
|
let entity = null;
|
|
61
59
|
if (cache) {
|
|
62
|
-
entity = await base_cache_1.
|
|
60
|
+
entity = await base_cache_1.BaseCache.getModel(cache, this.model, identifier, modifiers);
|
|
63
61
|
}
|
|
64
62
|
if (!entity) {
|
|
65
63
|
if (!isNaN(+identifier)) {
|
|
@@ -73,7 +71,7 @@ class BaseService {
|
|
|
73
71
|
if (!entity)
|
|
74
72
|
throw api_error_1.APIError.errNotFound(`No ${this.model.name} found with id ${identifier}`);
|
|
75
73
|
if (cache) {
|
|
76
|
-
await base_cache_1.
|
|
74
|
+
await base_cache_1.BaseCache.setModel(cache, entity, modifiers, cacheTTL);
|
|
77
75
|
}
|
|
78
76
|
}
|
|
79
77
|
return entity;
|
|
@@ -91,13 +89,13 @@ class BaseService {
|
|
|
91
89
|
const { cache, cacheTTL, ...modifiers } = config || {};
|
|
92
90
|
let page = null;
|
|
93
91
|
if (cache) {
|
|
94
|
-
page = await base_cache_1.
|
|
92
|
+
page = await base_cache_1.BaseCache.getPage(cache, this.model, modifiers);
|
|
95
93
|
}
|
|
96
94
|
if (!page) {
|
|
97
95
|
const limit = modifiers.limit || 12;
|
|
98
96
|
page = await this.model.findMany({ limit, ...modifiers });
|
|
99
97
|
if (cache) {
|
|
100
|
-
await base_cache_1.
|
|
98
|
+
await base_cache_1.BaseCache.setPage(cache, page, modifiers, cacheTTL);
|
|
101
99
|
}
|
|
102
100
|
}
|
|
103
101
|
return page;
|
|
@@ -114,8 +112,8 @@ class BaseService {
|
|
|
114
112
|
throw api_error_1.APIError.errNotFound(`No ${this.model.name} found with id ${identifier}`);
|
|
115
113
|
}
|
|
116
114
|
else if (config?.cache) {
|
|
117
|
-
await base_cache_1.
|
|
118
|
-
await base_cache_1.
|
|
115
|
+
await base_cache_1.BaseCache.clearModel(config.cache, response);
|
|
116
|
+
await base_cache_1.BaseCache.setModel(config.cache, response, undefined, config.cacheTTL);
|
|
119
117
|
}
|
|
120
118
|
return response;
|
|
121
119
|
}
|
|
@@ -131,8 +129,8 @@ class BaseService {
|
|
|
131
129
|
}
|
|
132
130
|
else if (config?.cache) {
|
|
133
131
|
const cache = config.cache;
|
|
134
|
-
await Promise.all(response.map(item => base_cache_1.
|
|
135
|
-
await Promise.all(response.map(item => base_cache_1.
|
|
132
|
+
await Promise.all(response.map(item => base_cache_1.BaseCache.clearModel(cache, item)));
|
|
133
|
+
await Promise.all(response.map(item => base_cache_1.BaseCache.setModel(cache, item, undefined, config.cacheTTL)));
|
|
136
134
|
}
|
|
137
135
|
return response;
|
|
138
136
|
}
|
|
@@ -147,8 +145,8 @@ class BaseService {
|
|
|
147
145
|
throw api_error_1.APIError.errNotFound(`No ${this.model.name} found with id ${identifier}`);
|
|
148
146
|
}
|
|
149
147
|
else if (config?.cache) {
|
|
150
|
-
await base_cache_1.
|
|
151
|
-
await base_cache_1.
|
|
148
|
+
await base_cache_1.BaseCache.clearModel(config.cache, response);
|
|
149
|
+
await base_cache_1.BaseCache.clearPage(config.cache, this.model);
|
|
152
150
|
}
|
|
153
151
|
return response;
|
|
154
152
|
}
|
|
@@ -165,10 +163,10 @@ class BaseService {
|
|
|
165
163
|
}
|
|
166
164
|
else if (config?.cache) {
|
|
167
165
|
const cache = config.cache;
|
|
168
|
-
await base_cache_1.
|
|
169
|
-
await Promise.all(response.map(item => base_cache_1.
|
|
166
|
+
await base_cache_1.BaseCache.clearPage(config.cache, this.model);
|
|
167
|
+
await Promise.all(response.map(item => base_cache_1.BaseCache.clearModel(cache, item)));
|
|
170
168
|
}
|
|
171
169
|
return response;
|
|
172
170
|
}
|
|
173
171
|
}
|
|
174
|
-
exports.
|
|
172
|
+
exports.BaseService = BaseService;
|
package/dist/core/index.d.ts
CHANGED
package/dist/core/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import BaseModel from '../core/base.model';
|
|
1
|
+
import { BaseModel } from '../core/base.model';
|
|
2
2
|
import { IndexDefinition, IndexOptions, TypedClassDecorator } from '../types/decorator.types';
|
|
3
3
|
/**
|
|
4
4
|
* Defines a index for this Class which will then be added to the collection.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import BaseModel from "../core/base.model";
|
|
1
|
+
import { BaseModel } from "../core/base.model";
|
|
2
2
|
export declare type TypedClassDecorator<T> = (target: T) => T | void;
|
|
3
3
|
/** Definition of a sortable index. 1=ascending, -1=descending */
|
|
4
4
|
export type IndexDefinition<T> = {
|
|
@@ -62,6 +62,10 @@ export interface PropOptions<T = any, S extends typeof BaseModel = any, P extend
|
|
|
62
62
|
enum?: any;
|
|
63
63
|
/** Give the Property a default Value */
|
|
64
64
|
default?: T | (() => T);
|
|
65
|
+
/** Protect field from edits by non-privileged users */
|
|
66
|
+
system?: boolean;
|
|
67
|
+
/** Hide field in responses to non-privileged users */
|
|
68
|
+
private?: boolean;
|
|
65
69
|
/** Relational field data */
|
|
66
70
|
relation?: T extends typeof BaseModel ? Relation<T, S, P> : never;
|
|
67
71
|
}
|
package/dist/utils/db.utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ConflictResolution, Expansion, SqlConflict, SqlOrder, SqlPaginate, SqlWhere, SqlWhereOperator } from "unconventional-pg-queries";
|
|
2
|
-
import BaseModel from "../core/base.model";
|
|
2
|
+
import { BaseModel } from "../core/base.model";
|
|
3
3
|
/** Converts a stringified list of relational fields to an array of data needed to expand those fields in a SQL environment */
|
|
4
4
|
export declare function getExpand<T extends typeof BaseModel>(model: T, expand?: string): Record<string, Expansion>;
|
|
5
5
|
/** Converts a string of filters to an array of 'where' clause outlines for a SQL environment */
|
|
@@ -1,11 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
3
|
exports.defaultErrorHandler = exports.bindDatabase = exports.defaultCors = void 0;
|
|
7
4
|
const cors_1 = require("hono/cors");
|
|
8
|
-
const base_model_1 =
|
|
5
|
+
const base_model_1 = require("../core/base.model");
|
|
9
6
|
const core_1 = require("../core");
|
|
10
7
|
/** Default CORS middleware permitting all origins and methods */
|
|
11
8
|
exports.defaultCors = (0, cors_1.cors)({
|
|
@@ -15,8 +12,8 @@ exports.defaultCors = (0, cors_1.cors)({
|
|
|
15
12
|
/** Binds the database proxy to the BaseModel */
|
|
16
13
|
const bindDatabase = (getDB) => {
|
|
17
14
|
return async (ctx, next) => {
|
|
18
|
-
if (!base_model_1.
|
|
19
|
-
base_model_1.
|
|
15
|
+
if (!base_model_1.BaseModel.db)
|
|
16
|
+
base_model_1.BaseModel.db = getDB(ctx);
|
|
20
17
|
await next();
|
|
21
18
|
};
|
|
22
19
|
};
|