metal-orm 1.0.24 → 1.0.27
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/README.md +2 -0
- package/dist/decorators/index.cjs +91 -7
- package/dist/decorators/index.cjs.map +1 -1
- package/dist/decorators/index.d.cts +24 -7
- package/dist/decorators/index.d.ts +24 -7
- package/dist/decorators/index.js +91 -7
- package/dist/decorators/index.js.map +1 -1
- package/package.json +4 -1
- package/scripts/generate-entities.mjs +76 -6
- package/src/core/execution/executors/mssql-executor.ts +139 -0
- package/src/decorators/column.ts +38 -4
- package/src/decorators/decorator-metadata.ts +53 -0
- package/src/decorators/entity.ts +37 -4
- package/src/decorators/relations.ts +34 -4
- package/src/index.ts +5 -5
|
@@ -1,10 +1,27 @@
|
|
|
1
1
|
import { a2 as TableHooks, a4 as ColumnType, h as ColumnDef, aV as EntityOrTableTargetResolver, ab as CascadeMode, i as TableDef, aW as EntityConstructor, Z as SelectQueryBuilder } from '../select-BuMpVcVt.cjs';
|
|
2
2
|
|
|
3
|
+
interface StandardDecoratorContext {
|
|
4
|
+
kind: string;
|
|
5
|
+
name?: string | symbol;
|
|
6
|
+
metadata?: Record<PropertyKey, unknown>;
|
|
7
|
+
addInitializer?(initializer: (this: unknown) => void): void;
|
|
8
|
+
static?: boolean;
|
|
9
|
+
private?: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface DualModePropertyDecorator {
|
|
12
|
+
(target: object, propertyKey: string | symbol): void;
|
|
13
|
+
(value: unknown, context: StandardDecoratorContext): void;
|
|
14
|
+
}
|
|
15
|
+
interface DualModeClassDecorator {
|
|
16
|
+
<TFunction extends Function>(value: TFunction): void | TFunction;
|
|
17
|
+
<TFunction extends Function>(value: TFunction, context: StandardDecoratorContext): void | TFunction;
|
|
18
|
+
}
|
|
19
|
+
|
|
3
20
|
interface EntityOptions {
|
|
4
21
|
tableName?: string;
|
|
5
22
|
hooks?: TableHooks;
|
|
6
23
|
}
|
|
7
|
-
declare function Entity(options?: EntityOptions):
|
|
24
|
+
declare function Entity(options?: EntityOptions): DualModeClassDecorator;
|
|
8
25
|
|
|
9
26
|
interface ColumnOptions {
|
|
10
27
|
type: ColumnType;
|
|
@@ -13,8 +30,8 @@ interface ColumnOptions {
|
|
|
13
30
|
primary?: boolean;
|
|
14
31
|
}
|
|
15
32
|
type ColumnInput = ColumnOptions | ColumnDef;
|
|
16
|
-
declare function Column(definition: ColumnInput):
|
|
17
|
-
declare function PrimaryKey(definition: ColumnInput):
|
|
33
|
+
declare function Column(definition: ColumnInput): DualModePropertyDecorator;
|
|
34
|
+
declare function PrimaryKey(definition: ColumnInput): DualModePropertyDecorator;
|
|
18
35
|
|
|
19
36
|
interface BaseRelationOptions {
|
|
20
37
|
target: EntityOrTableTargetResolver;
|
|
@@ -41,10 +58,10 @@ interface BelongsToManyOptions {
|
|
|
41
58
|
defaultPivotColumns?: string[];
|
|
42
59
|
cascade?: CascadeMode;
|
|
43
60
|
}
|
|
44
|
-
declare function HasMany(options: HasManyOptions):
|
|
45
|
-
declare function HasOne(options: HasOneOptions):
|
|
46
|
-
declare function BelongsTo(options: BelongsToOptions):
|
|
47
|
-
declare function BelongsToMany(options: BelongsToManyOptions):
|
|
61
|
+
declare function HasMany(options: HasManyOptions): DualModePropertyDecorator;
|
|
62
|
+
declare function HasOne(options: HasOneOptions): DualModePropertyDecorator;
|
|
63
|
+
declare function BelongsTo(options: BelongsToOptions): DualModePropertyDecorator;
|
|
64
|
+
declare function BelongsToMany(options: BelongsToManyOptions): DualModePropertyDecorator;
|
|
48
65
|
|
|
49
66
|
declare const bootstrapEntities: () => TableDef[];
|
|
50
67
|
declare const getTableDefFromEntity: (ctor: EntityConstructor) => TableDef | undefined;
|
|
@@ -1,10 +1,27 @@
|
|
|
1
1
|
import { a2 as TableHooks, a4 as ColumnType, h as ColumnDef, aV as EntityOrTableTargetResolver, ab as CascadeMode, i as TableDef, aW as EntityConstructor, Z as SelectQueryBuilder } from '../select-BuMpVcVt.js';
|
|
2
2
|
|
|
3
|
+
interface StandardDecoratorContext {
|
|
4
|
+
kind: string;
|
|
5
|
+
name?: string | symbol;
|
|
6
|
+
metadata?: Record<PropertyKey, unknown>;
|
|
7
|
+
addInitializer?(initializer: (this: unknown) => void): void;
|
|
8
|
+
static?: boolean;
|
|
9
|
+
private?: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface DualModePropertyDecorator {
|
|
12
|
+
(target: object, propertyKey: string | symbol): void;
|
|
13
|
+
(value: unknown, context: StandardDecoratorContext): void;
|
|
14
|
+
}
|
|
15
|
+
interface DualModeClassDecorator {
|
|
16
|
+
<TFunction extends Function>(value: TFunction): void | TFunction;
|
|
17
|
+
<TFunction extends Function>(value: TFunction, context: StandardDecoratorContext): void | TFunction;
|
|
18
|
+
}
|
|
19
|
+
|
|
3
20
|
interface EntityOptions {
|
|
4
21
|
tableName?: string;
|
|
5
22
|
hooks?: TableHooks;
|
|
6
23
|
}
|
|
7
|
-
declare function Entity(options?: EntityOptions):
|
|
24
|
+
declare function Entity(options?: EntityOptions): DualModeClassDecorator;
|
|
8
25
|
|
|
9
26
|
interface ColumnOptions {
|
|
10
27
|
type: ColumnType;
|
|
@@ -13,8 +30,8 @@ interface ColumnOptions {
|
|
|
13
30
|
primary?: boolean;
|
|
14
31
|
}
|
|
15
32
|
type ColumnInput = ColumnOptions | ColumnDef;
|
|
16
|
-
declare function Column(definition: ColumnInput):
|
|
17
|
-
declare function PrimaryKey(definition: ColumnInput):
|
|
33
|
+
declare function Column(definition: ColumnInput): DualModePropertyDecorator;
|
|
34
|
+
declare function PrimaryKey(definition: ColumnInput): DualModePropertyDecorator;
|
|
18
35
|
|
|
19
36
|
interface BaseRelationOptions {
|
|
20
37
|
target: EntityOrTableTargetResolver;
|
|
@@ -41,10 +58,10 @@ interface BelongsToManyOptions {
|
|
|
41
58
|
defaultPivotColumns?: string[];
|
|
42
59
|
cascade?: CascadeMode;
|
|
43
60
|
}
|
|
44
|
-
declare function HasMany(options: HasManyOptions):
|
|
45
|
-
declare function HasOne(options: HasOneOptions):
|
|
46
|
-
declare function BelongsTo(options: BelongsToOptions):
|
|
47
|
-
declare function BelongsToMany(options: BelongsToManyOptions):
|
|
61
|
+
declare function HasMany(options: HasManyOptions): DualModePropertyDecorator;
|
|
62
|
+
declare function HasOne(options: HasOneOptions): DualModePropertyDecorator;
|
|
63
|
+
declare function BelongsTo(options: BelongsToOptions): DualModePropertyDecorator;
|
|
64
|
+
declare function BelongsToMany(options: BelongsToManyOptions): DualModePropertyDecorator;
|
|
48
65
|
|
|
49
66
|
declare const bootstrapEntities: () => TableDef[];
|
|
50
67
|
declare const getTableDefFromEntity: (ctor: EntityConstructor) => TableDef | undefined;
|
package/dist/decorators/index.js
CHANGED
|
@@ -75,6 +75,28 @@ var buildTableDef = (meta) => {
|
|
|
75
75
|
return table;
|
|
76
76
|
};
|
|
77
77
|
|
|
78
|
+
// src/decorators/decorator-metadata.ts
|
|
79
|
+
var METADATA_KEY = "metal-orm:decorators";
|
|
80
|
+
var isStandardDecoratorContext = (value) => {
|
|
81
|
+
return typeof value === "object" && value !== null && "kind" in value;
|
|
82
|
+
};
|
|
83
|
+
var getOrCreateMetadataBag = (context) => {
|
|
84
|
+
const metadata = context.metadata || (context.metadata = {});
|
|
85
|
+
const existing = metadata[METADATA_KEY];
|
|
86
|
+
if (existing) {
|
|
87
|
+
return existing;
|
|
88
|
+
}
|
|
89
|
+
const bag = { columns: [], relations: [] };
|
|
90
|
+
metadata[METADATA_KEY] = bag;
|
|
91
|
+
return bag;
|
|
92
|
+
};
|
|
93
|
+
var readMetadataBag = (context) => {
|
|
94
|
+
return context.metadata?.[METADATA_KEY];
|
|
95
|
+
};
|
|
96
|
+
var registerInitializer = (context, initializer) => {
|
|
97
|
+
context.addInitializer?.(initializer);
|
|
98
|
+
};
|
|
99
|
+
|
|
78
100
|
// src/decorators/entity.ts
|
|
79
101
|
var toSnakeCase = (value) => {
|
|
80
102
|
return value.replace(/([a-z0-9])([A-Z])/g, "$1_$2").replace(/[^a-z0-9_]+/gi, "_").replace(/__+/g, "_").replace(/^_|_$/g, "").toLowerCase();
|
|
@@ -95,7 +117,28 @@ function Entity(options = {}) {
|
|
|
95
117
|
setEntityTableName(value, tableName, options.hooks);
|
|
96
118
|
return value;
|
|
97
119
|
};
|
|
98
|
-
|
|
120
|
+
const decoratorWithContext = (value, context) => {
|
|
121
|
+
const ctor = value;
|
|
122
|
+
decorator(ctor);
|
|
123
|
+
if (context && isStandardDecoratorContext(context)) {
|
|
124
|
+
const bag = readMetadataBag(context);
|
|
125
|
+
if (bag) {
|
|
126
|
+
const meta = ensureEntityMetadata(ctor);
|
|
127
|
+
for (const entry of bag.columns) {
|
|
128
|
+
if (!meta.columns[entry.propertyName]) {
|
|
129
|
+
addColumnMetadata(ctor, entry.propertyName, { ...entry.column });
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
for (const entry of bag.relations) {
|
|
133
|
+
if (!meta.relations[entry.propertyName]) {
|
|
134
|
+
addRelationMetadata(ctor, entry.propertyName, entry.relation);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return ctor;
|
|
140
|
+
};
|
|
141
|
+
return decoratorWithContext;
|
|
99
142
|
}
|
|
100
143
|
|
|
101
144
|
// src/decorators/column.ts
|
|
@@ -142,11 +185,32 @@ var registerColumn = (ctor, propertyName, column) => {
|
|
|
142
185
|
}
|
|
143
186
|
addColumnMetadata(ctor, propertyName, column);
|
|
144
187
|
};
|
|
188
|
+
var registerColumnFromContext = (context, column) => {
|
|
189
|
+
if (!context.name) {
|
|
190
|
+
throw new Error("Column decorator requires a property name");
|
|
191
|
+
}
|
|
192
|
+
const propertyName = normalizePropertyName(context.name);
|
|
193
|
+
const bag = getOrCreateMetadataBag(context);
|
|
194
|
+
if (!bag.columns.some((entry) => entry.propertyName === propertyName)) {
|
|
195
|
+
bag.columns.push({ propertyName, column: { ...column } });
|
|
196
|
+
}
|
|
197
|
+
registerInitializer(context, function() {
|
|
198
|
+
const ctor = resolveConstructor(this);
|
|
199
|
+
if (!ctor) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
registerColumn(ctor, propertyName, column);
|
|
203
|
+
});
|
|
204
|
+
};
|
|
145
205
|
function Column(definition) {
|
|
146
206
|
const normalized = normalizeColumnInput(definition);
|
|
147
|
-
const decorator = (
|
|
148
|
-
|
|
149
|
-
|
|
207
|
+
const decorator = (targetOrValue, propertyKeyOrContext) => {
|
|
208
|
+
if (isStandardDecoratorContext(propertyKeyOrContext)) {
|
|
209
|
+
registerColumnFromContext(propertyKeyOrContext, normalized);
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
const propertyName = normalizePropertyName(propertyKeyOrContext);
|
|
213
|
+
const ctor = resolveConstructor(targetOrValue);
|
|
150
214
|
if (!ctor) {
|
|
151
215
|
throw new Error("Unable to resolve constructor when registering column metadata");
|
|
152
216
|
}
|
|
@@ -225,9 +289,29 @@ var registerRelation = (ctor, propertyName, metadata) => {
|
|
|
225
289
|
addRelationMetadata(ctor, propertyName, metadata);
|
|
226
290
|
};
|
|
227
291
|
var createFieldDecorator = (metadataFactory) => {
|
|
228
|
-
const decorator = (
|
|
229
|
-
|
|
230
|
-
|
|
292
|
+
const decorator = (targetOrValue, propertyKeyOrContext) => {
|
|
293
|
+
if (isStandardDecoratorContext(propertyKeyOrContext)) {
|
|
294
|
+
const ctx = propertyKeyOrContext;
|
|
295
|
+
if (!ctx.name) {
|
|
296
|
+
throw new Error("Relation decorator requires a property name");
|
|
297
|
+
}
|
|
298
|
+
const propertyName2 = normalizePropertyName2(ctx.name);
|
|
299
|
+
const bag = getOrCreateMetadataBag(ctx);
|
|
300
|
+
const relationMetadata = metadataFactory(propertyName2);
|
|
301
|
+
if (!bag.relations.some((entry) => entry.propertyName === propertyName2)) {
|
|
302
|
+
bag.relations.push({ propertyName: propertyName2, relation: relationMetadata });
|
|
303
|
+
}
|
|
304
|
+
registerInitializer(ctx, function() {
|
|
305
|
+
const ctor2 = resolveConstructor2(this);
|
|
306
|
+
if (!ctor2) {
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
registerRelation(ctor2, propertyName2, relationMetadata);
|
|
310
|
+
});
|
|
311
|
+
return;
|
|
312
|
+
}
|
|
313
|
+
const propertyName = normalizePropertyName2(propertyKeyOrContext);
|
|
314
|
+
const ctor = resolveConstructor2(targetOrValue);
|
|
231
315
|
if (!ctor) {
|
|
232
316
|
throw new Error("Unable to resolve constructor when registering relation metadata");
|
|
233
317
|
}
|