-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrudBase.ts
61 lines (53 loc) · 1.86 KB
/
crudBase.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { Column, CreateDateColumn, Index, UpdateDateColumn, VersionColumn } from 'typeorm';
/**
* ColumnNames defines an object whose keys are the same of a data model
* except for the base class values and the special case of 'id'
*
* @param T the entity model
* @param Y optional fields to exclude from column names that are properties on T
*/
export type ColumnNames<T extends CrudBase, Y extends keyof T = never> = {
[P in keyof Omit<T, keyof VersionedCrudBase | 'id' | Y>]-?: string;
};
/**
* Only entity fields that excludes fields from CrudBase and any option other fields (like pseudo objects)
*
* For example:
*
* class User extends CrudBase {
* id: number;
* ignore: string
* }
*
* EntityFieldsOnly<User, 'ignore'> === { id: number }
*/
export type EntityFieldsOnly<T extends CrudBase, K extends keyof T = never> = Omit<T, keyof CrudBase | K>;
function TableScopedIndex(name: string): PropertyDecorator {
return (target, propertyKey) => {
// sqlite requires that indexes be globally unique named.
// since this is only applied on the root crud indexes ignore them for sqlite
if (!process.env.JEST_TEST) {
return Index(name)(target, propertyKey);
}
};
}
export abstract class CrudBase {
static columns: { [key: string]: string } = {
createdAt: 'created_at',
updatedAt: 'updated_at',
deletedAt: 'deleted_at',
};
@TableScopedIndex('created_at_idx')
@CreateDateColumn({ name: CrudBase.columns.createdAt, type: 'datetime' })
createdAt!: Date;
@TableScopedIndex('updated_at_idx')
@UpdateDateColumn({ name: CrudBase.columns.updatedAt, type: 'datetime' })
updatedAt!: Date;
@TableScopedIndex('deleted_at_idx')
@Column({ name: CrudBase.columns.deletedAt, type: 'datetime', nullable: true })
deletedAt?: Date | null;
}
export class VersionedCrudBase extends CrudBase {
@VersionColumn()
version!: number;
}