unconventional 1.7.0 → 1.9.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 +5 -2
- package/dist/core/base.model.js +8 -8
- package/dist/decorators/prop.decorator.js +1 -1
- package/dist/decorators/timestamp.decorator.d.ts +3 -3
- package/dist/decorators/timestamp.decorator.js +5 -2
- package/dist/types/decorator.types.d.ts +8 -1
- package/package.json +1 -1
|
@@ -4,7 +4,10 @@ import { FilterConfig, PageConfig, UpsertConfig } from "./base.options";
|
|
|
4
4
|
interface BaseModelSchema {
|
|
5
5
|
indexes: IndexSummary[];
|
|
6
6
|
props: PropSummary;
|
|
7
|
-
|
|
7
|
+
timestamp?: {
|
|
8
|
+
createdField: string;
|
|
9
|
+
updatedField: string;
|
|
10
|
+
};
|
|
8
11
|
}
|
|
9
12
|
export declare class Document {
|
|
10
13
|
static schema: BaseModelSchema;
|
|
@@ -38,7 +41,7 @@ export declare class BaseModel extends Document {
|
|
|
38
41
|
$key(): any;
|
|
39
42
|
$owner(): any;
|
|
40
43
|
}
|
|
41
|
-
export declare
|
|
44
|
+
export declare class TimeStamped extends BaseModel {
|
|
42
45
|
createdAt?: Date;
|
|
43
46
|
updatedAt?: Date;
|
|
44
47
|
}
|
package/dist/core/base.model.js
CHANGED
|
@@ -28,9 +28,9 @@ class BaseModel extends Document {
|
|
|
28
28
|
static db;
|
|
29
29
|
id;
|
|
30
30
|
static async create(data, upsertConfig) {
|
|
31
|
-
const timestamp = this.schema.
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
const timestamp = this.schema.timestamp ? {
|
|
32
|
+
[this.schema.timestamp.createdField]: new Date(),
|
|
33
|
+
[this.schema.timestamp.updatedField]: new Date()
|
|
34
34
|
} : {};
|
|
35
35
|
const response = await this.db.fetch({
|
|
36
36
|
action: db_types_1.SqlAction.Insert,
|
|
@@ -45,9 +45,9 @@ class BaseModel extends Document {
|
|
|
45
45
|
return this.fromDatabaseJson(result);
|
|
46
46
|
}
|
|
47
47
|
static async createMany(data, upsertConfig) {
|
|
48
|
-
const timestamp = this.schema.
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
const timestamp = this.schema.timestamp ? {
|
|
49
|
+
[this.schema.timestamp.createdField]: new Date(),
|
|
50
|
+
[this.schema.timestamp.updatedField]: new Date()
|
|
51
51
|
} : {};
|
|
52
52
|
const response = await this.db.fetch({
|
|
53
53
|
action: db_types_1.SqlAction.Insert,
|
|
@@ -94,7 +94,7 @@ class BaseModel extends Document {
|
|
|
94
94
|
return response ? response.map(item => this.fromDatabaseJson(item)) : [];
|
|
95
95
|
}
|
|
96
96
|
static async update(id, data) {
|
|
97
|
-
const timestamp = this.schema.
|
|
97
|
+
const timestamp = this.schema.timestamp ? { [this.schema.timestamp.updatedField]: new Date() } : {};
|
|
98
98
|
const targetField = this.getFieldName(id);
|
|
99
99
|
if (!targetField)
|
|
100
100
|
return null;
|
|
@@ -112,7 +112,7 @@ class BaseModel extends Document {
|
|
|
112
112
|
return this.fromDatabaseJson(result);
|
|
113
113
|
}
|
|
114
114
|
static async updateMany(data) {
|
|
115
|
-
const timestamp = this.schema.
|
|
115
|
+
const timestamp = this.schema.timestamp ? { [this.schema.timestamp.updatedField]: new Date() } : {};
|
|
116
116
|
const response = await this.db.fetch({
|
|
117
117
|
action: db_types_1.SqlAction.Update,
|
|
118
118
|
type: unconventional_pg_queries_1.OneOrMany.Many,
|
|
@@ -21,7 +21,7 @@ function prop(options) {
|
|
|
21
21
|
const child = target.constructor;
|
|
22
22
|
if (child.schema === parent.schema) {
|
|
23
23
|
child.schema = parent.schema
|
|
24
|
-
? { indexes: [...parent.schema.indexes], props: { ...parent.schema.props },
|
|
24
|
+
? { indexes: [...parent.schema.indexes], props: { ...parent.schema.props }, timestamp: parent.schema.timestamp }
|
|
25
25
|
: { indexes: [], props: {} };
|
|
26
26
|
}
|
|
27
27
|
child.schema.props[key] = options || {};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { TypedClassDecorator } from '../types/decorator.types';
|
|
1
|
+
import { BaseModel } from '../core/base.model';
|
|
2
|
+
import { TimestampOptions, TypedClassDecorator } from '../types/decorator.types';
|
|
3
3
|
/**
|
|
4
4
|
* Adds createdAt and updatedAt fields to the class and updates them on create/update calls.
|
|
5
5
|
* @example Example:
|
|
@@ -8,5 +8,5 @@ import { TypedClassDecorator } from '../types/decorator.types';
|
|
|
8
8
|
* class ClassName {}
|
|
9
9
|
* ```
|
|
10
10
|
*/
|
|
11
|
-
export declare function timestamp<T extends typeof
|
|
11
|
+
export declare function timestamp<T extends typeof BaseModel>(options?: TimestampOptions): TypedClassDecorator<T>;
|
|
12
12
|
export { timestamp as Timestamp };
|
|
@@ -9,9 +9,12 @@ exports.Timestamp = exports.timestamp = void 0;
|
|
|
9
9
|
* class ClassName {}
|
|
10
10
|
* ```
|
|
11
11
|
*/
|
|
12
|
-
function timestamp() {
|
|
12
|
+
function timestamp(options) {
|
|
13
13
|
return (target) => {
|
|
14
|
-
target.schema.
|
|
14
|
+
target.schema.timestamp = {
|
|
15
|
+
createdField: options?.createdField || 'createdAt',
|
|
16
|
+
updatedField: options?.updatedField || 'updatedAt'
|
|
17
|
+
};
|
|
15
18
|
};
|
|
16
19
|
}
|
|
17
20
|
exports.timestamp = timestamp;
|
|
@@ -47,7 +47,7 @@ export interface Relation<Target extends typeof BaseModel = any, Source extends
|
|
|
47
47
|
/** Details of the pivot table connecting this model to the related model */
|
|
48
48
|
through?: RelationPivot<Pivot>;
|
|
49
49
|
}
|
|
50
|
-
/** Options to pass to
|
|
50
|
+
/** Options to pass to the DB when creating or validating a property */
|
|
51
51
|
export interface PropOptions<T = any, S extends typeof BaseModel = any, P extends typeof BaseModel = any> {
|
|
52
52
|
/**
|
|
53
53
|
* Is this value required?
|
|
@@ -71,3 +71,10 @@ export interface PropOptions<T = any, S extends typeof BaseModel = any, P extend
|
|
|
71
71
|
}
|
|
72
72
|
/** Type for the Values stored in the Reflection for Properties */
|
|
73
73
|
export type PropSummary<Target = any, Source extends typeof BaseModel = any> = Record<keyof Source, PropOptions<Target, Source>>;
|
|
74
|
+
/** Options to guide the timestamping of a model on create/update */
|
|
75
|
+
export interface TimestampOptions {
|
|
76
|
+
/** The field to store the created date in */
|
|
77
|
+
createdField: string;
|
|
78
|
+
/** The field to store the updated date in */
|
|
79
|
+
updatedField: string;
|
|
80
|
+
}
|