stabilize-orm 1.1.8 → 1.3.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/decorators.ts DELETED
@@ -1,165 +0,0 @@
1
- /**
2
- * @file decorators.ts
3
- * @description Contains all the decorators used by the Stabilize ORM.
4
- * @author ElectronSz
5
- */
6
-
7
- import "reflect-metadata";
8
- import { RelationType, DataTypes } from "./types";
9
-
10
-
11
- export const ModelKey = Symbol("model");
12
- export const ColumnKey = Symbol("column");
13
- export const ValidatorKey = Symbol("validator");
14
- export const RelationKey = Symbol("relation");
15
- export const SoftDeleteKey = Symbol("softDelete");
16
- export const DefaultKey = Symbol("default");
17
- export const IndexKey = Symbol("index");
18
-
19
-
20
- export interface ColumnOptions {
21
- name?: string;
22
- type: DataTypes;
23
- length?: number;
24
- precision?: number;
25
- scale?: number;
26
- }
27
-
28
- /**
29
- * Decorator to mark a class as a database model.
30
- * @param tableName The name of the table in the database.
31
- */
32
- export function Model(tableName: string) {
33
- return function (constructor: Function) {
34
- Reflect.defineMetadata(ModelKey, tableName, constructor);
35
- };
36
- }
37
-
38
- /**
39
- * Decorator to mark a property as a database column.
40
- * @param options The configuration for the column, including name, type, and length.
41
- */
42
- export function Column(options: ColumnOptions | DataTypes) {
43
- return function (target: any, propertyKey: string) {
44
- const columns = Reflect.getMetadata(ColumnKey, target) || {};
45
-
46
- const columnOptions: ColumnOptions = typeof options === 'object' ? options : { type: options };
47
-
48
- columns[propertyKey] = {
49
- name: columnOptions.name || propertyKey,
50
- ...columnOptions,
51
- };
52
- Reflect.defineMetadata(ColumnKey, columns, target);
53
- };
54
- }
55
-
56
- /**
57
- * Decorator to enforce a NOT NULL constraint on a column.
58
- */
59
- export function Required() {
60
- return function (target: any, propertyKey: string) {
61
- const validators = Reflect.getMetadata(ValidatorKey, target) || {};
62
- validators[propertyKey] = [...(validators[propertyKey] || []), "required"];
63
- Reflect.defineMetadata(ValidatorKey, validators, target);
64
- };
65
- }
66
-
67
- /**
68
- * Decorator to enforce a UNIQUE constraint on a column.
69
- */
70
- export function Unique() {
71
- return function (target: any, propertyKey: string) {
72
- const validators = Reflect.getMetadata(ValidatorKey, target) || {};
73
- validators[propertyKey] = [...(validators[propertyKey] || []), "unique"];
74
- Reflect.defineMetadata(ValidatorKey, validators, target);
75
- };
76
- }
77
-
78
- /**
79
- * Decorator to set a default value for a column.
80
- * @param value The default value.
81
- */
82
- export function Default(value: any) {
83
- return function (target: any, propertyKey: string) {
84
- Reflect.defineMetadata(DefaultKey, value, target, propertyKey);
85
- };
86
- }
87
-
88
- /**
89
- * Decorator to create a non-unique index on a column for performance.
90
- * @param indexName Optional: A custom name for the index.
91
- */
92
- export function Index(indexName?: string) {
93
- return function (target: any, propertyKey: string) {
94
- const indexes = Reflect.getMetadata(IndexKey, target) || {};
95
- indexes[propertyKey] = indexName || `idx_${propertyKey}`;
96
- Reflect.defineMetadata(IndexKey, indexes, target);
97
- };
98
- }
99
-
100
-
101
- /**
102
- * Decorator to enable soft-delete functionality on a model.
103
- * The decorated property will store the deletion timestamp.
104
- */
105
- export function SoftDelete() {
106
- return function (target: any, propertyKey: string) {
107
- Reflect.defineMetadata(SoftDeleteKey, propertyKey, target);
108
- };
109
- }
110
-
111
-
112
- export function OneToOne(model: () => any, foreignKey: string) {
113
- return function (target: any, propertyKey: string) {
114
- const relations = Reflect.getMetadata(RelationKey, target) || {};
115
- relations[propertyKey] = {
116
- type: RelationType.OneToOne,
117
- targetModel: model,
118
- foreignKey,
119
- };
120
- Reflect.defineMetadata(RelationKey, relations, target);
121
- };
122
- }
123
-
124
- export function ManyToOne(model: () => any, foreignKey: string) {
125
- return function (target: any, propertyKey: string) {
126
- const relations = Reflect.getMetadata(RelationKey, target) || {};
127
- relations[propertyKey] = {
128
- type: RelationType.ManyToOne,
129
- targetModel: model,
130
- foreignKey,
131
- };
132
- Reflect.defineMetadata(RelationKey, relations, target);
133
- };
134
- }
135
-
136
- export function OneToMany(model: () => any, inverseKey: string) {
137
- return function (target: any, propertyKey: string) {
138
- const relations = Reflect.getMetadata(RelationKey, target) || {};
139
- relations[propertyKey] = {
140
- type: RelationType.OneToMany,
141
- targetModel: model,
142
- inverseKey,
143
- };
144
- Reflect.defineMetadata(RelationKey, relations, target);
145
- };
146
- }
147
-
148
- export function ManyToMany(
149
- model: () => any,
150
- joinTable: string,
151
- foreignKey: string,
152
- inverseKey: string,
153
- ) {
154
- return function (target: any, propertyKey: string) {
155
- const relations = Reflect.getMetadata(RelationKey, target) || {};
156
- relations[propertyKey] = {
157
- type: RelationType.ManyToMany,
158
- targetModel: model,
159
- joinTable,
160
- foreignKey,
161
- inverseKey,
162
- };
163
- Reflect.defineMetadata(RelationKey, relations, target);
164
- };
165
- }