@eggjs/tegg-orm-decorator 4.0.0-beta.6 → 4.0.0-beta.8
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/index.d.ts +114 -13
- package/dist/index.js +247 -16
- package/package.json +3 -3
- package/dist/builder/AttributeMetaBuilder.d.ts +0 -12
- package/dist/builder/AttributeMetaBuilder.js +0 -28
- package/dist/builder/IndexMetaBuilder.d.ts +0 -14
- package/dist/builder/IndexMetaBuilder.js +0 -32
- package/dist/builder/ModelMetaBuilder.d.ts +0 -11
- package/dist/builder/ModelMetaBuilder.js +0 -24
- package/dist/builder/index.js +0 -5
- package/dist/decorator/Attribute.d.ts +0 -6
- package/dist/decorator/Attribute.js +0 -15
- package/dist/decorator/DataSource.d.ts +0 -6
- package/dist/decorator/DataSource.js +0 -12
- package/dist/decorator/Model.d.ts +0 -6
- package/dist/decorator/Model.js +0 -22
- package/dist/decorator/ModelIndex.d.ts +0 -6
- package/dist/decorator/ModelIndex.js +0 -12
- package/dist/decorator/index.js +0 -6
- package/dist/model/AttributeMeta.d.ts +0 -13
- package/dist/model/AttributeMeta.js +0 -22
- package/dist/model/IndexMeta.d.ts +0 -10
- package/dist/model/IndexMeta.js +0 -16
- package/dist/model/ModelMetadata.d.ts +0 -13
- package/dist/model/ModelMetadata.js +0 -16
- package/dist/util/ModelInfoUtil.d.ts +0 -18
- package/dist/util/ModelInfoUtil.js +0 -45
- package/dist/util/ModelMetadataUtil.d.ts +0 -11
- package/dist/util/ModelMetadataUtil.js +0 -15
- package/dist/util/NameUtil.d.ts +0 -25
- package/dist/util/NameUtil.js +0 -36
- package/dist/util/index.js +0 -5
package/dist/index.d.ts
CHANGED
|
@@ -1,15 +1,116 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { IndexMeta } from "./model/IndexMeta.js";
|
|
3
|
-
import { ModelMetadata } from "./model/ModelMetadata.js";
|
|
4
|
-
import { AttributeMetaBuilder } from "./builder/AttributeMetaBuilder.js";
|
|
5
|
-
import { IndexMetaBuilder } from "./builder/IndexMetaBuilder.js";
|
|
6
|
-
import { ModelMetaBuilder } from "./builder/ModelMetaBuilder.js";
|
|
7
|
-
import { Attribute } from "./decorator/Attribute.js";
|
|
8
|
-
import { DataSource } from "./decorator/DataSource.js";
|
|
9
|
-
import { Model } from "./decorator/Model.js";
|
|
10
|
-
import { Index } from "./decorator/ModelIndex.js";
|
|
11
|
-
import { ModelInfoUtil } from "./util/ModelInfoUtil.js";
|
|
12
|
-
import { MODEL_METADATA, ModelMetadataUtil } from "./util/ModelMetadataUtil.js";
|
|
13
|
-
import { NameUtil } from "./util/NameUtil.js";
|
|
1
|
+
import { AttributeOptions, EggProtoImplClass, IndexOptions, ModelAttributeInfo, ModelIndexInfo, ModelParams } from "@eggjs/tegg-types";
|
|
14
2
|
export * from "@eggjs/tegg-types/orm";
|
|
3
|
+
|
|
4
|
+
//#region src/model/AttributeMeta.d.ts
|
|
5
|
+
declare class AttributeMeta {
|
|
6
|
+
readonly dataType: string;
|
|
7
|
+
readonly propertyName: string;
|
|
8
|
+
readonly attributeName: string;
|
|
9
|
+
readonly allowNull: boolean;
|
|
10
|
+
readonly autoIncrement: boolean;
|
|
11
|
+
readonly primary: boolean;
|
|
12
|
+
readonly unique: boolean;
|
|
13
|
+
constructor(dataType: string, propertyName: string, attributeName: string, allowNull: boolean, autoIncrement: boolean, primary: boolean, unique: boolean);
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/model/IndexMeta.d.ts
|
|
17
|
+
declare class IndexMeta {
|
|
18
|
+
readonly name: string;
|
|
19
|
+
readonly fields: string[];
|
|
20
|
+
readonly unique: boolean;
|
|
21
|
+
readonly primary: boolean;
|
|
22
|
+
constructor(name: string, fields: string[], unique: boolean, primary: boolean);
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
//#region src/model/ModelMetadata.d.ts
|
|
26
|
+
declare class ModelMetadata {
|
|
27
|
+
readonly dataSource: string | undefined;
|
|
28
|
+
readonly tableName: string;
|
|
29
|
+
readonly attributes: Array<AttributeMeta>;
|
|
30
|
+
readonly indices: Array<IndexMeta>;
|
|
31
|
+
constructor(dataSource: string | undefined, tableName: string, attributes: Array<AttributeMeta>, indices: Array<IndexMeta>);
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
//#region src/builder/AttributeMetaBuilder.d.ts
|
|
35
|
+
declare class AttributeMetaBuilder {
|
|
36
|
+
private readonly clazz;
|
|
37
|
+
constructor(clazz: EggProtoImplClass);
|
|
38
|
+
build(): Array<AttributeMeta>;
|
|
39
|
+
private buildAttributeMeta;
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/builder/IndexMetaBuilder.d.ts
|
|
43
|
+
declare class IndexMetaBuilder {
|
|
44
|
+
private readonly clazz;
|
|
45
|
+
private readonly attributes;
|
|
46
|
+
constructor(clazz: EggProtoImplClass, attributes: Array<AttributeMeta>);
|
|
47
|
+
build(): Array<IndexMeta>;
|
|
48
|
+
private buildIndexMeta;
|
|
49
|
+
}
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/builder/ModelMetaBuilder.d.ts
|
|
52
|
+
declare class ModelMetaBuilder {
|
|
53
|
+
private readonly clazz;
|
|
54
|
+
constructor(clazz: EggProtoImplClass);
|
|
55
|
+
build(): ModelMetadata;
|
|
56
|
+
}
|
|
57
|
+
//#endregion
|
|
58
|
+
//#region src/decorator/Attribute.d.ts
|
|
59
|
+
declare function Attribute(dataType: string, options?: AttributeOptions): (target: any, propertyKey: PropertyKey) => void;
|
|
60
|
+
//#endregion
|
|
61
|
+
//#region src/decorator/DataSource.d.ts
|
|
62
|
+
declare function DataSource(dataSource: string): (clazz: EggProtoImplClass) => void;
|
|
63
|
+
//#endregion
|
|
64
|
+
//#region src/decorator/Model.d.ts
|
|
65
|
+
declare function Model(param?: ModelParams): (clazz: EggProtoImplClass) => void;
|
|
66
|
+
//#endregion
|
|
67
|
+
//#region src/decorator/ModelIndex.d.ts
|
|
68
|
+
declare function Index(fields: string[], params?: IndexOptions): (clazz: EggProtoImplClass) => void;
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/util/ModelInfoUtil.d.ts
|
|
71
|
+
type ModelAttributeMap = Map<string, ModelAttributeInfo>;
|
|
72
|
+
declare class ModelInfoUtil {
|
|
73
|
+
static setIsModel(isModel: boolean, clazz: EggProtoImplClass): void;
|
|
74
|
+
static getIsModel(clazz: EggProtoImplClass): boolean;
|
|
75
|
+
static setDataSource(dataSource: string, clazz: EggProtoImplClass): void;
|
|
76
|
+
static getDataSource(clazz: EggProtoImplClass): string | undefined;
|
|
77
|
+
static setTableName(tableName: string, clazz: EggProtoImplClass): void;
|
|
78
|
+
static getTableName(clazz: EggProtoImplClass): string | undefined;
|
|
79
|
+
static addModelIndex(fields: string[], options: IndexOptions | undefined, clazz: EggProtoImplClass): void;
|
|
80
|
+
static getModelIndices(clazz: EggProtoImplClass): Array<ModelIndexInfo>;
|
|
81
|
+
static addModelAttribute(dataType: string, options: AttributeOptions | undefined, clazz: EggProtoImplClass, property: string): void;
|
|
82
|
+
static getModelAttributes(clazz: EggProtoImplClass): ModelAttributeMap | undefined;
|
|
83
|
+
}
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/util/ModelMetadataUtil.d.ts
|
|
86
|
+
declare const MODEL_METADATA: unique symbol;
|
|
87
|
+
declare class ModelMetadataUtil {
|
|
88
|
+
static setModelMetadata(clazz: EggProtoImplClass, metaData: ModelMetadata): void;
|
|
89
|
+
static getModelMetadata(clazz: EggProtoImplClass): ModelMetadata | undefined;
|
|
90
|
+
}
|
|
91
|
+
//#endregion
|
|
92
|
+
//#region src/util/NameUtil.d.ts
|
|
93
|
+
declare class NameUtil {
|
|
94
|
+
/**
|
|
95
|
+
* get table name
|
|
96
|
+
* StudentScore -> student_scores
|
|
97
|
+
*/
|
|
98
|
+
static getTableName(modelName: string): string;
|
|
99
|
+
/**
|
|
100
|
+
* get attribute name
|
|
101
|
+
* userName -> user_name
|
|
102
|
+
*/
|
|
103
|
+
static getAttributeName(propertyName: string): string;
|
|
104
|
+
/**
|
|
105
|
+
* [ 'user_name' ], unique
|
|
106
|
+
* uk_user_name
|
|
107
|
+
*
|
|
108
|
+
* [ 'user_name', 'gender' ]
|
|
109
|
+
* idx_user_name_gender
|
|
110
|
+
*/
|
|
111
|
+
static getIndexName(fields: string[], options?: {
|
|
112
|
+
unique?: boolean;
|
|
113
|
+
}): string;
|
|
114
|
+
}
|
|
115
|
+
//#endregion
|
|
15
116
|
export { Attribute, AttributeMeta, AttributeMetaBuilder, DataSource, Index, IndexMeta, IndexMetaBuilder, MODEL_METADATA, Model, ModelInfoUtil, ModelMetaBuilder, ModelMetadata, ModelMetadataUtil, NameUtil };
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,251 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import { NameUtil } from "./util/NameUtil.js";
|
|
7
|
-
import "./util/index.js";
|
|
8
|
-
import { AttributeMetaBuilder } from "./builder/AttributeMetaBuilder.js";
|
|
9
|
-
import { IndexMetaBuilder } from "./builder/IndexMetaBuilder.js";
|
|
10
|
-
import { ModelMetaBuilder } from "./builder/ModelMetaBuilder.js";
|
|
11
|
-
import "./builder/index.js";
|
|
12
|
-
import { Attribute } from "./decorator/Attribute.js";
|
|
13
|
-
import { DataSource } from "./decorator/DataSource.js";
|
|
14
|
-
import { Model } from "./decorator/Model.js";
|
|
15
|
-
import { Index } from "./decorator/ModelIndex.js";
|
|
16
|
-
import "./decorator/index.js";
|
|
1
|
+
import { MetadataUtil, SingletonProto } from "@eggjs/core-decorator";
|
|
2
|
+
import { AccessLevel, IS_MODEL, MODEL_DATA_ATTRIBUTES, MODEL_DATA_INDICES, MODEL_DATA_SOURCE, MODEL_DATA_TABLE_NAME, MODEL_PROTO_IMPL_TYPE } from "@eggjs/tegg-types";
|
|
3
|
+
import _ from "lodash";
|
|
4
|
+
import pluralize from "pluralize";
|
|
5
|
+
import assert from "node:assert";
|
|
17
6
|
|
|
18
7
|
export * from "@eggjs/tegg-types/orm"
|
|
19
8
|
|
|
9
|
+
//#region src/model/AttributeMeta.ts
|
|
10
|
+
var AttributeMeta = class {
|
|
11
|
+
dataType;
|
|
12
|
+
propertyName;
|
|
13
|
+
attributeName;
|
|
14
|
+
allowNull;
|
|
15
|
+
autoIncrement;
|
|
16
|
+
primary;
|
|
17
|
+
unique;
|
|
18
|
+
constructor(dataType, propertyName, attributeName, allowNull, autoIncrement, primary, unique) {
|
|
19
|
+
this.dataType = dataType;
|
|
20
|
+
this.propertyName = propertyName;
|
|
21
|
+
this.attributeName = attributeName;
|
|
22
|
+
this.allowNull = allowNull;
|
|
23
|
+
this.autoIncrement = autoIncrement;
|
|
24
|
+
this.primary = primary;
|
|
25
|
+
this.unique = unique;
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/model/IndexMeta.ts
|
|
31
|
+
var IndexMeta = class {
|
|
32
|
+
name;
|
|
33
|
+
fields;
|
|
34
|
+
unique;
|
|
35
|
+
primary;
|
|
36
|
+
constructor(name, fields, unique, primary) {
|
|
37
|
+
this.name = name;
|
|
38
|
+
this.fields = fields;
|
|
39
|
+
this.unique = unique;
|
|
40
|
+
this.primary = primary;
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
//#endregion
|
|
45
|
+
//#region src/model/ModelMetadata.ts
|
|
46
|
+
var ModelMetadata = class {
|
|
47
|
+
dataSource;
|
|
48
|
+
tableName;
|
|
49
|
+
attributes;
|
|
50
|
+
indices;
|
|
51
|
+
constructor(dataSource, tableName, attributes, indices) {
|
|
52
|
+
this.dataSource = dataSource;
|
|
53
|
+
this.tableName = tableName;
|
|
54
|
+
this.attributes = attributes;
|
|
55
|
+
this.indices = indices;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
//#endregion
|
|
60
|
+
//#region src/util/ModelInfoUtil.ts
|
|
61
|
+
var ModelInfoUtil = class {
|
|
62
|
+
static setIsModel(isModel, clazz) {
|
|
63
|
+
MetadataUtil.defineMetaData(IS_MODEL, isModel, clazz);
|
|
64
|
+
}
|
|
65
|
+
static getIsModel(clazz) {
|
|
66
|
+
return MetadataUtil.getBooleanMetaData(IS_MODEL, clazz);
|
|
67
|
+
}
|
|
68
|
+
static setDataSource(dataSource, clazz) {
|
|
69
|
+
MetadataUtil.defineMetaData(MODEL_DATA_SOURCE, dataSource, clazz);
|
|
70
|
+
}
|
|
71
|
+
static getDataSource(clazz) {
|
|
72
|
+
return MetadataUtil.getMetaData(MODEL_DATA_SOURCE, clazz);
|
|
73
|
+
}
|
|
74
|
+
static setTableName(tableName, clazz) {
|
|
75
|
+
MetadataUtil.defineMetaData(MODEL_DATA_TABLE_NAME, tableName, clazz);
|
|
76
|
+
}
|
|
77
|
+
static getTableName(clazz) {
|
|
78
|
+
return MetadataUtil.getMetaData(MODEL_DATA_TABLE_NAME, clazz);
|
|
79
|
+
}
|
|
80
|
+
static addModelIndex(fields, options, clazz) {
|
|
81
|
+
MetadataUtil.initOwnArrayMetaData(MODEL_DATA_INDICES, clazz, []).push({
|
|
82
|
+
fields,
|
|
83
|
+
options
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
static getModelIndices(clazz) {
|
|
87
|
+
return MetadataUtil.getArrayMetaData(MODEL_DATA_INDICES, clazz);
|
|
88
|
+
}
|
|
89
|
+
static addModelAttribute(dataType, options, clazz, property) {
|
|
90
|
+
MetadataUtil.initOwnMapMetaData(MODEL_DATA_ATTRIBUTES, clazz, /* @__PURE__ */ new Map()).set(property, {
|
|
91
|
+
dataType,
|
|
92
|
+
options
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
static getModelAttributes(clazz) {
|
|
96
|
+
return MetadataUtil.getMetaData(MODEL_DATA_ATTRIBUTES, clazz);
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
//#endregion
|
|
101
|
+
//#region src/util/ModelMetadataUtil.ts
|
|
102
|
+
const MODEL_METADATA = Symbol.for("EggPrototype#model#metadata");
|
|
103
|
+
var ModelMetadataUtil = class {
|
|
104
|
+
static setModelMetadata(clazz, metaData) {
|
|
105
|
+
MetadataUtil.defineMetaData(MODEL_METADATA, metaData, clazz);
|
|
106
|
+
}
|
|
107
|
+
static getModelMetadata(clazz) {
|
|
108
|
+
return MetadataUtil.getMetaData(MODEL_METADATA, clazz);
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
//#endregion
|
|
113
|
+
//#region src/util/NameUtil.ts
|
|
114
|
+
var NameUtil = class {
|
|
115
|
+
/**
|
|
116
|
+
* get table name
|
|
117
|
+
* StudentScore -> student_scores
|
|
118
|
+
*/
|
|
119
|
+
static getTableName(modelName) {
|
|
120
|
+
const modelNames = pluralize(modelName);
|
|
121
|
+
return _.snakeCase(modelNames);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* get attribute name
|
|
125
|
+
* userName -> user_name
|
|
126
|
+
*/
|
|
127
|
+
static getAttributeName(propertyName) {
|
|
128
|
+
return _.snakeCase(propertyName);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* [ 'user_name' ], unique
|
|
132
|
+
* uk_user_name
|
|
133
|
+
*
|
|
134
|
+
* [ 'user_name', 'gender' ]
|
|
135
|
+
* idx_user_name_gender
|
|
136
|
+
*/
|
|
137
|
+
static getIndexName(fields, options) {
|
|
138
|
+
const prefix = options?.unique ? "uk_" : "idx_";
|
|
139
|
+
const names = fields.join("_");
|
|
140
|
+
return prefix + names;
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
//#endregion
|
|
145
|
+
//#region src/builder/AttributeMetaBuilder.ts
|
|
146
|
+
var AttributeMetaBuilder = class {
|
|
147
|
+
clazz;
|
|
148
|
+
constructor(clazz) {
|
|
149
|
+
this.clazz = clazz;
|
|
150
|
+
}
|
|
151
|
+
build() {
|
|
152
|
+
const modelAttributes = ModelInfoUtil.getModelAttributes(this.clazz);
|
|
153
|
+
const attributes = [];
|
|
154
|
+
if (!modelAttributes) throw new Error(`model ${this.clazz.name} has no attributes`);
|
|
155
|
+
for (const [propertyName, attributeInfo] of modelAttributes) {
|
|
156
|
+
const attribute = this.buildAttributeMeta(propertyName, attributeInfo);
|
|
157
|
+
attributes.push(attribute);
|
|
158
|
+
}
|
|
159
|
+
return attributes;
|
|
160
|
+
}
|
|
161
|
+
buildAttributeMeta(propertyName, attributeInfo) {
|
|
162
|
+
return new AttributeMeta(attributeInfo.dataType, propertyName, attributeInfo.options?.name ?? NameUtil.getAttributeName(propertyName), attributeInfo.options?.allowNull ?? true, attributeInfo.options?.autoIncrement ?? false, attributeInfo.options?.primary ?? false, attributeInfo.options?.unique ?? false);
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
//#endregion
|
|
167
|
+
//#region src/builder/IndexMetaBuilder.ts
|
|
168
|
+
var IndexMetaBuilder = class {
|
|
169
|
+
clazz;
|
|
170
|
+
attributes;
|
|
171
|
+
constructor(clazz, attributes) {
|
|
172
|
+
this.clazz = clazz;
|
|
173
|
+
this.attributes = attributes;
|
|
174
|
+
}
|
|
175
|
+
build() {
|
|
176
|
+
return ModelInfoUtil.getModelIndices(this.clazz).map((indexInfo) => this.buildIndexMeta(indexInfo));
|
|
177
|
+
}
|
|
178
|
+
buildIndexMeta(indexInfo) {
|
|
179
|
+
const fields = [];
|
|
180
|
+
for (const field of indexInfo.fields) {
|
|
181
|
+
const attribute = this.attributes.find((t) => t.propertyName === field);
|
|
182
|
+
if (!attribute) throw new Error(`model ${this.clazz.name} has no attribute named ${field}`);
|
|
183
|
+
fields.push(attribute.attributeName);
|
|
184
|
+
}
|
|
185
|
+
let indexName;
|
|
186
|
+
if (indexInfo.options?.name) indexName = indexInfo.options.name;
|
|
187
|
+
else indexName = NameUtil.getIndexName(fields, { unique: indexInfo.options?.unique });
|
|
188
|
+
return new IndexMeta(indexName, fields, indexInfo.options?.unique ?? false, indexInfo.options?.primary ?? false);
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
//#endregion
|
|
193
|
+
//#region src/builder/ModelMetaBuilder.ts
|
|
194
|
+
var ModelMetaBuilder = class {
|
|
195
|
+
clazz;
|
|
196
|
+
constructor(clazz) {
|
|
197
|
+
this.clazz = clazz;
|
|
198
|
+
}
|
|
199
|
+
build() {
|
|
200
|
+
const dataSource = ModelInfoUtil.getDataSource(this.clazz);
|
|
201
|
+
const tableName = ModelInfoUtil.getTableName(this.clazz) || NameUtil.getTableName(this.clazz.name);
|
|
202
|
+
const attributes = new AttributeMetaBuilder(this.clazz).build();
|
|
203
|
+
const indices = new IndexMetaBuilder(this.clazz, attributes).build();
|
|
204
|
+
return new ModelMetadata(dataSource, tableName, attributes, indices);
|
|
205
|
+
}
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region src/decorator/Attribute.ts
|
|
210
|
+
function Attribute(dataType, options) {
|
|
211
|
+
return function(target, propertyKey) {
|
|
212
|
+
const clazz = target.constructor;
|
|
213
|
+
assert(typeof propertyKey === "string", `[model/${clazz.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
|
|
214
|
+
ModelInfoUtil.addModelAttribute(dataType, options, clazz, propertyKey);
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
//#endregion
|
|
219
|
+
//#region src/decorator/DataSource.ts
|
|
220
|
+
function DataSource(dataSource) {
|
|
221
|
+
return function(clazz) {
|
|
222
|
+
ModelInfoUtil.setDataSource(dataSource, clazz);
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
//#endregion
|
|
227
|
+
//#region src/decorator/Model.ts
|
|
228
|
+
function Model(param) {
|
|
229
|
+
return function(clazz) {
|
|
230
|
+
ModelInfoUtil.setIsModel(true, clazz);
|
|
231
|
+
const func = SingletonProto({
|
|
232
|
+
name: clazz.name,
|
|
233
|
+
accessLevel: AccessLevel.PUBLIC,
|
|
234
|
+
protoImplType: MODEL_PROTO_IMPL_TYPE
|
|
235
|
+
});
|
|
236
|
+
if (param?.tableName) ModelInfoUtil.setTableName(param.tableName, clazz);
|
|
237
|
+
if (param?.dataSource) ModelInfoUtil.setDataSource(param.dataSource, clazz);
|
|
238
|
+
func(clazz);
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
//#endregion
|
|
243
|
+
//#region src/decorator/ModelIndex.ts
|
|
244
|
+
function Index(fields, params) {
|
|
245
|
+
return function(clazz) {
|
|
246
|
+
ModelInfoUtil.addModelIndex(fields, params, clazz);
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
//#endregion
|
|
20
251
|
export { Attribute, AttributeMeta, AttributeMetaBuilder, DataSource, Index, IndexMeta, IndexMetaBuilder, MODEL_METADATA, Model, ModelInfoUtil, ModelMetaBuilder, ModelMetadata, ModelMetadataUtil, NameUtil };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eggjs/tegg-orm-decorator",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.8",
|
|
4
4
|
"description": "tegg orm decorator",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -33,8 +33,8 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"lodash": "^4.17.21",
|
|
35
35
|
"pluralize": "^8.0.0",
|
|
36
|
-
"@eggjs/core-decorator": "4.0.0-beta.
|
|
37
|
-
"@eggjs/tegg-types": "4.0.0-beta.
|
|
36
|
+
"@eggjs/core-decorator": "4.0.0-beta.8",
|
|
37
|
+
"@eggjs/tegg-types": "4.0.0-beta.8"
|
|
38
38
|
},
|
|
39
39
|
"publishConfig": {
|
|
40
40
|
"access": "public"
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { AttributeMeta } from "../model/AttributeMeta.js";
|
|
2
|
-
import { EggProtoImplClass } from "@eggjs/tegg-types";
|
|
3
|
-
|
|
4
|
-
//#region src/builder/AttributeMetaBuilder.d.ts
|
|
5
|
-
declare class AttributeMetaBuilder {
|
|
6
|
-
private readonly clazz;
|
|
7
|
-
constructor(clazz: EggProtoImplClass);
|
|
8
|
-
build(): Array<AttributeMeta>;
|
|
9
|
-
private buildAttributeMeta;
|
|
10
|
-
}
|
|
11
|
-
//#endregion
|
|
12
|
-
export { AttributeMetaBuilder };
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { AttributeMeta } from "../model/AttributeMeta.js";
|
|
2
|
-
import { ModelInfoUtil } from "../util/ModelInfoUtil.js";
|
|
3
|
-
import { NameUtil } from "../util/NameUtil.js";
|
|
4
|
-
import "../util/index.js";
|
|
5
|
-
|
|
6
|
-
//#region src/builder/AttributeMetaBuilder.ts
|
|
7
|
-
var AttributeMetaBuilder = class {
|
|
8
|
-
clazz;
|
|
9
|
-
constructor(clazz) {
|
|
10
|
-
this.clazz = clazz;
|
|
11
|
-
}
|
|
12
|
-
build() {
|
|
13
|
-
const modelAttributes = ModelInfoUtil.getModelAttributes(this.clazz);
|
|
14
|
-
const attributes = [];
|
|
15
|
-
if (!modelAttributes) throw new Error(`model ${this.clazz.name} has no attributes`);
|
|
16
|
-
for (const [propertyName, attributeInfo] of modelAttributes) {
|
|
17
|
-
const attribute = this.buildAttributeMeta(propertyName, attributeInfo);
|
|
18
|
-
attributes.push(attribute);
|
|
19
|
-
}
|
|
20
|
-
return attributes;
|
|
21
|
-
}
|
|
22
|
-
buildAttributeMeta(propertyName, attributeInfo) {
|
|
23
|
-
return new AttributeMeta(attributeInfo.dataType, propertyName, attributeInfo.options?.name ?? NameUtil.getAttributeName(propertyName), attributeInfo.options?.allowNull ?? true, attributeInfo.options?.autoIncrement ?? false, attributeInfo.options?.primary ?? false, attributeInfo.options?.unique ?? false);
|
|
24
|
-
}
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
//#endregion
|
|
28
|
-
export { AttributeMetaBuilder };
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { AttributeMeta } from "../model/AttributeMeta.js";
|
|
2
|
-
import { IndexMeta } from "../model/IndexMeta.js";
|
|
3
|
-
import { EggProtoImplClass } from "@eggjs/tegg-types";
|
|
4
|
-
|
|
5
|
-
//#region src/builder/IndexMetaBuilder.d.ts
|
|
6
|
-
declare class IndexMetaBuilder {
|
|
7
|
-
private readonly clazz;
|
|
8
|
-
private readonly attributes;
|
|
9
|
-
constructor(clazz: EggProtoImplClass, attributes: Array<AttributeMeta>);
|
|
10
|
-
build(): Array<IndexMeta>;
|
|
11
|
-
private buildIndexMeta;
|
|
12
|
-
}
|
|
13
|
-
//#endregion
|
|
14
|
-
export { IndexMetaBuilder };
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { IndexMeta } from "../model/IndexMeta.js";
|
|
2
|
-
import { ModelInfoUtil } from "../util/ModelInfoUtil.js";
|
|
3
|
-
import { NameUtil } from "../util/NameUtil.js";
|
|
4
|
-
import "../util/index.js";
|
|
5
|
-
|
|
6
|
-
//#region src/builder/IndexMetaBuilder.ts
|
|
7
|
-
var IndexMetaBuilder = class {
|
|
8
|
-
clazz;
|
|
9
|
-
attributes;
|
|
10
|
-
constructor(clazz, attributes) {
|
|
11
|
-
this.clazz = clazz;
|
|
12
|
-
this.attributes = attributes;
|
|
13
|
-
}
|
|
14
|
-
build() {
|
|
15
|
-
return ModelInfoUtil.getModelIndices(this.clazz).map((indexInfo) => this.buildIndexMeta(indexInfo));
|
|
16
|
-
}
|
|
17
|
-
buildIndexMeta(indexInfo) {
|
|
18
|
-
const fields = [];
|
|
19
|
-
for (const field of indexInfo.fields) {
|
|
20
|
-
const attribute = this.attributes.find((t) => t.propertyName === field);
|
|
21
|
-
if (!attribute) throw new Error(`model ${this.clazz.name} has no attribute named ${field}`);
|
|
22
|
-
fields.push(attribute.attributeName);
|
|
23
|
-
}
|
|
24
|
-
let indexName;
|
|
25
|
-
if (indexInfo.options?.name) indexName = indexInfo.options.name;
|
|
26
|
-
else indexName = NameUtil.getIndexName(fields, { unique: indexInfo.options?.unique });
|
|
27
|
-
return new IndexMeta(indexName, fields, indexInfo.options?.unique ?? false, indexInfo.options?.primary ?? false);
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
//#endregion
|
|
32
|
-
export { IndexMetaBuilder };
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { ModelMetadata } from "../model/ModelMetadata.js";
|
|
2
|
-
import { EggProtoImplClass } from "@eggjs/tegg-types";
|
|
3
|
-
|
|
4
|
-
//#region src/builder/ModelMetaBuilder.d.ts
|
|
5
|
-
declare class ModelMetaBuilder {
|
|
6
|
-
private readonly clazz;
|
|
7
|
-
constructor(clazz: EggProtoImplClass);
|
|
8
|
-
build(): ModelMetadata;
|
|
9
|
-
}
|
|
10
|
-
//#endregion
|
|
11
|
-
export { ModelMetaBuilder };
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { ModelMetadata } from "../model/ModelMetadata.js";
|
|
2
|
-
import { ModelInfoUtil } from "../util/ModelInfoUtil.js";
|
|
3
|
-
import { NameUtil } from "../util/NameUtil.js";
|
|
4
|
-
import "../util/index.js";
|
|
5
|
-
import { AttributeMetaBuilder } from "./AttributeMetaBuilder.js";
|
|
6
|
-
import { IndexMetaBuilder } from "./IndexMetaBuilder.js";
|
|
7
|
-
|
|
8
|
-
//#region src/builder/ModelMetaBuilder.ts
|
|
9
|
-
var ModelMetaBuilder = class {
|
|
10
|
-
clazz;
|
|
11
|
-
constructor(clazz) {
|
|
12
|
-
this.clazz = clazz;
|
|
13
|
-
}
|
|
14
|
-
build() {
|
|
15
|
-
const dataSource = ModelInfoUtil.getDataSource(this.clazz);
|
|
16
|
-
const tableName = ModelInfoUtil.getTableName(this.clazz) || NameUtil.getTableName(this.clazz.name);
|
|
17
|
-
const attributes = new AttributeMetaBuilder(this.clazz).build();
|
|
18
|
-
const indices = new IndexMetaBuilder(this.clazz, attributes).build();
|
|
19
|
-
return new ModelMetadata(dataSource, tableName, attributes, indices);
|
|
20
|
-
}
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
//#endregion
|
|
24
|
-
export { ModelMetaBuilder };
|
package/dist/builder/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { ModelInfoUtil } from "../util/ModelInfoUtil.js";
|
|
2
|
-
import "../util/index.js";
|
|
3
|
-
import assert from "node:assert";
|
|
4
|
-
|
|
5
|
-
//#region src/decorator/Attribute.ts
|
|
6
|
-
function Attribute(dataType, options) {
|
|
7
|
-
return function(target, propertyKey) {
|
|
8
|
-
const clazz = target.constructor;
|
|
9
|
-
assert(typeof propertyKey === "string", `[model/${clazz.name}] expect method name be typeof string, but now is ${String(propertyKey)}`);
|
|
10
|
-
ModelInfoUtil.addModelAttribute(dataType, options, clazz, propertyKey);
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
//#endregion
|
|
15
|
-
export { Attribute };
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { ModelInfoUtil } from "../util/ModelInfoUtil.js";
|
|
2
|
-
import "../util/index.js";
|
|
3
|
-
|
|
4
|
-
//#region src/decorator/DataSource.ts
|
|
5
|
-
function DataSource(dataSource) {
|
|
6
|
-
return function(clazz) {
|
|
7
|
-
ModelInfoUtil.setDataSource(dataSource, clazz);
|
|
8
|
-
};
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
//#endregion
|
|
12
|
-
export { DataSource };
|
package/dist/decorator/Model.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { ModelInfoUtil } from "../util/ModelInfoUtil.js";
|
|
2
|
-
import "../util/index.js";
|
|
3
|
-
import { SingletonProto } from "@eggjs/core-decorator";
|
|
4
|
-
import { AccessLevel, MODEL_PROTO_IMPL_TYPE } from "@eggjs/tegg-types";
|
|
5
|
-
|
|
6
|
-
//#region src/decorator/Model.ts
|
|
7
|
-
function Model(param) {
|
|
8
|
-
return function(clazz) {
|
|
9
|
-
ModelInfoUtil.setIsModel(true, clazz);
|
|
10
|
-
const func = SingletonProto({
|
|
11
|
-
name: clazz.name,
|
|
12
|
-
accessLevel: AccessLevel.PUBLIC,
|
|
13
|
-
protoImplType: MODEL_PROTO_IMPL_TYPE
|
|
14
|
-
});
|
|
15
|
-
if (param?.tableName) ModelInfoUtil.setTableName(param.tableName, clazz);
|
|
16
|
-
if (param?.dataSource) ModelInfoUtil.setDataSource(param.dataSource, clazz);
|
|
17
|
-
func(clazz);
|
|
18
|
-
};
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
//#endregion
|
|
22
|
-
export { Model };
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { ModelInfoUtil } from "../util/ModelInfoUtil.js";
|
|
2
|
-
import "../util/index.js";
|
|
3
|
-
|
|
4
|
-
//#region src/decorator/ModelIndex.ts
|
|
5
|
-
function Index(fields, params) {
|
|
6
|
-
return function(clazz) {
|
|
7
|
-
ModelInfoUtil.addModelIndex(fields, params, clazz);
|
|
8
|
-
};
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
//#endregion
|
|
12
|
-
export { Index };
|
package/dist/decorator/index.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
//#region src/model/AttributeMeta.d.ts
|
|
2
|
-
declare class AttributeMeta {
|
|
3
|
-
readonly dataType: string;
|
|
4
|
-
readonly propertyName: string;
|
|
5
|
-
readonly attributeName: string;
|
|
6
|
-
readonly allowNull: boolean;
|
|
7
|
-
readonly autoIncrement: boolean;
|
|
8
|
-
readonly primary: boolean;
|
|
9
|
-
readonly unique: boolean;
|
|
10
|
-
constructor(dataType: string, propertyName: string, attributeName: string, allowNull: boolean, autoIncrement: boolean, primary: boolean, unique: boolean);
|
|
11
|
-
}
|
|
12
|
-
//#endregion
|
|
13
|
-
export { AttributeMeta };
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
//#region src/model/AttributeMeta.ts
|
|
2
|
-
var AttributeMeta = class {
|
|
3
|
-
dataType;
|
|
4
|
-
propertyName;
|
|
5
|
-
attributeName;
|
|
6
|
-
allowNull;
|
|
7
|
-
autoIncrement;
|
|
8
|
-
primary;
|
|
9
|
-
unique;
|
|
10
|
-
constructor(dataType, propertyName, attributeName, allowNull, autoIncrement, primary, unique) {
|
|
11
|
-
this.dataType = dataType;
|
|
12
|
-
this.propertyName = propertyName;
|
|
13
|
-
this.attributeName = attributeName;
|
|
14
|
-
this.allowNull = allowNull;
|
|
15
|
-
this.autoIncrement = autoIncrement;
|
|
16
|
-
this.primary = primary;
|
|
17
|
-
this.unique = unique;
|
|
18
|
-
}
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
//#endregion
|
|
22
|
-
export { AttributeMeta };
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
//#region src/model/IndexMeta.d.ts
|
|
2
|
-
declare class IndexMeta {
|
|
3
|
-
readonly name: string;
|
|
4
|
-
readonly fields: string[];
|
|
5
|
-
readonly unique: boolean;
|
|
6
|
-
readonly primary: boolean;
|
|
7
|
-
constructor(name: string, fields: string[], unique: boolean, primary: boolean);
|
|
8
|
-
}
|
|
9
|
-
//#endregion
|
|
10
|
-
export { IndexMeta };
|
package/dist/model/IndexMeta.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
//#region src/model/IndexMeta.ts
|
|
2
|
-
var IndexMeta = class {
|
|
3
|
-
name;
|
|
4
|
-
fields;
|
|
5
|
-
unique;
|
|
6
|
-
primary;
|
|
7
|
-
constructor(name, fields, unique, primary) {
|
|
8
|
-
this.name = name;
|
|
9
|
-
this.fields = fields;
|
|
10
|
-
this.unique = unique;
|
|
11
|
-
this.primary = primary;
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
//#endregion
|
|
16
|
-
export { IndexMeta };
|
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { AttributeMeta } from "./AttributeMeta.js";
|
|
2
|
-
import { IndexMeta } from "./IndexMeta.js";
|
|
3
|
-
|
|
4
|
-
//#region src/model/ModelMetadata.d.ts
|
|
5
|
-
declare class ModelMetadata {
|
|
6
|
-
readonly dataSource: string | undefined;
|
|
7
|
-
readonly tableName: string;
|
|
8
|
-
readonly attributes: Array<AttributeMeta>;
|
|
9
|
-
readonly indices: Array<IndexMeta>;
|
|
10
|
-
constructor(dataSource: string | undefined, tableName: string, attributes: Array<AttributeMeta>, indices: Array<IndexMeta>);
|
|
11
|
-
}
|
|
12
|
-
//#endregion
|
|
13
|
-
export { ModelMetadata };
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
//#region src/model/ModelMetadata.ts
|
|
2
|
-
var ModelMetadata = class {
|
|
3
|
-
dataSource;
|
|
4
|
-
tableName;
|
|
5
|
-
attributes;
|
|
6
|
-
indices;
|
|
7
|
-
constructor(dataSource, tableName, attributes, indices) {
|
|
8
|
-
this.dataSource = dataSource;
|
|
9
|
-
this.tableName = tableName;
|
|
10
|
-
this.attributes = attributes;
|
|
11
|
-
this.indices = indices;
|
|
12
|
-
}
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
//#endregion
|
|
16
|
-
export { ModelMetadata };
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { AttributeOptions, EggProtoImplClass, IndexOptions, ModelAttributeInfo, ModelIndexInfo } from "@eggjs/tegg-types";
|
|
2
|
-
|
|
3
|
-
//#region src/util/ModelInfoUtil.d.ts
|
|
4
|
-
type ModelAttributeMap = Map<string, ModelAttributeInfo>;
|
|
5
|
-
declare class ModelInfoUtil {
|
|
6
|
-
static setIsModel(isModel: boolean, clazz: EggProtoImplClass): void;
|
|
7
|
-
static getIsModel(clazz: EggProtoImplClass): boolean;
|
|
8
|
-
static setDataSource(dataSource: string, clazz: EggProtoImplClass): void;
|
|
9
|
-
static getDataSource(clazz: EggProtoImplClass): string | undefined;
|
|
10
|
-
static setTableName(tableName: string, clazz: EggProtoImplClass): void;
|
|
11
|
-
static getTableName(clazz: EggProtoImplClass): string | undefined;
|
|
12
|
-
static addModelIndex(fields: string[], options: IndexOptions | undefined, clazz: EggProtoImplClass): void;
|
|
13
|
-
static getModelIndices(clazz: EggProtoImplClass): Array<ModelIndexInfo>;
|
|
14
|
-
static addModelAttribute(dataType: string, options: AttributeOptions | undefined, clazz: EggProtoImplClass, property: string): void;
|
|
15
|
-
static getModelAttributes(clazz: EggProtoImplClass): ModelAttributeMap | undefined;
|
|
16
|
-
}
|
|
17
|
-
//#endregion
|
|
18
|
-
export { ModelInfoUtil };
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { MetadataUtil } from "@eggjs/core-decorator";
|
|
2
|
-
import { IS_MODEL, MODEL_DATA_ATTRIBUTES, MODEL_DATA_INDICES, MODEL_DATA_SOURCE, MODEL_DATA_TABLE_NAME } from "@eggjs/tegg-types";
|
|
3
|
-
|
|
4
|
-
//#region src/util/ModelInfoUtil.ts
|
|
5
|
-
var ModelInfoUtil = class {
|
|
6
|
-
static setIsModel(isModel, clazz) {
|
|
7
|
-
MetadataUtil.defineMetaData(IS_MODEL, isModel, clazz);
|
|
8
|
-
}
|
|
9
|
-
static getIsModel(clazz) {
|
|
10
|
-
return MetadataUtil.getBooleanMetaData(IS_MODEL, clazz);
|
|
11
|
-
}
|
|
12
|
-
static setDataSource(dataSource, clazz) {
|
|
13
|
-
MetadataUtil.defineMetaData(MODEL_DATA_SOURCE, dataSource, clazz);
|
|
14
|
-
}
|
|
15
|
-
static getDataSource(clazz) {
|
|
16
|
-
return MetadataUtil.getMetaData(MODEL_DATA_SOURCE, clazz);
|
|
17
|
-
}
|
|
18
|
-
static setTableName(tableName, clazz) {
|
|
19
|
-
MetadataUtil.defineMetaData(MODEL_DATA_TABLE_NAME, tableName, clazz);
|
|
20
|
-
}
|
|
21
|
-
static getTableName(clazz) {
|
|
22
|
-
return MetadataUtil.getMetaData(MODEL_DATA_TABLE_NAME, clazz);
|
|
23
|
-
}
|
|
24
|
-
static addModelIndex(fields, options, clazz) {
|
|
25
|
-
MetadataUtil.initOwnArrayMetaData(MODEL_DATA_INDICES, clazz, []).push({
|
|
26
|
-
fields,
|
|
27
|
-
options
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
static getModelIndices(clazz) {
|
|
31
|
-
return MetadataUtil.getArrayMetaData(MODEL_DATA_INDICES, clazz);
|
|
32
|
-
}
|
|
33
|
-
static addModelAttribute(dataType, options, clazz, property) {
|
|
34
|
-
MetadataUtil.initOwnMapMetaData(MODEL_DATA_ATTRIBUTES, clazz, /* @__PURE__ */ new Map()).set(property, {
|
|
35
|
-
dataType,
|
|
36
|
-
options
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
static getModelAttributes(clazz) {
|
|
40
|
-
return MetadataUtil.getMetaData(MODEL_DATA_ATTRIBUTES, clazz);
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
//#endregion
|
|
45
|
-
export { ModelInfoUtil };
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { ModelMetadata } from "../model/ModelMetadata.js";
|
|
2
|
-
import { EggProtoImplClass } from "@eggjs/tegg-types";
|
|
3
|
-
|
|
4
|
-
//#region src/util/ModelMetadataUtil.d.ts
|
|
5
|
-
declare const MODEL_METADATA: unique symbol;
|
|
6
|
-
declare class ModelMetadataUtil {
|
|
7
|
-
static setModelMetadata(clazz: EggProtoImplClass, metaData: ModelMetadata): void;
|
|
8
|
-
static getModelMetadata(clazz: EggProtoImplClass): ModelMetadata | undefined;
|
|
9
|
-
}
|
|
10
|
-
//#endregion
|
|
11
|
-
export { MODEL_METADATA, ModelMetadataUtil };
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { MetadataUtil } from "@eggjs/core-decorator";
|
|
2
|
-
|
|
3
|
-
//#region src/util/ModelMetadataUtil.ts
|
|
4
|
-
const MODEL_METADATA = Symbol.for("EggPrototype#model#metadata");
|
|
5
|
-
var ModelMetadataUtil = class {
|
|
6
|
-
static setModelMetadata(clazz, metaData) {
|
|
7
|
-
MetadataUtil.defineMetaData(MODEL_METADATA, metaData, clazz);
|
|
8
|
-
}
|
|
9
|
-
static getModelMetadata(clazz) {
|
|
10
|
-
return MetadataUtil.getMetaData(MODEL_METADATA, clazz);
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
//#endregion
|
|
15
|
-
export { MODEL_METADATA, ModelMetadataUtil };
|
package/dist/util/NameUtil.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
//#region src/util/NameUtil.d.ts
|
|
2
|
-
declare class NameUtil {
|
|
3
|
-
/**
|
|
4
|
-
* get table name
|
|
5
|
-
* StudentScore -> student_scores
|
|
6
|
-
*/
|
|
7
|
-
static getTableName(modelName: string): string;
|
|
8
|
-
/**
|
|
9
|
-
* get attribute name
|
|
10
|
-
* userName -> user_name
|
|
11
|
-
*/
|
|
12
|
-
static getAttributeName(propertyName: string): string;
|
|
13
|
-
/**
|
|
14
|
-
* [ 'user_name' ], unique
|
|
15
|
-
* uk_user_name
|
|
16
|
-
*
|
|
17
|
-
* [ 'user_name', 'gender' ]
|
|
18
|
-
* idx_user_name_gender
|
|
19
|
-
*/
|
|
20
|
-
static getIndexName(fields: string[], options?: {
|
|
21
|
-
unique?: boolean;
|
|
22
|
-
}): string;
|
|
23
|
-
}
|
|
24
|
-
//#endregion
|
|
25
|
-
export { NameUtil };
|
package/dist/util/NameUtil.js
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import _ from "lodash";
|
|
2
|
-
import pluralize from "pluralize";
|
|
3
|
-
|
|
4
|
-
//#region src/util/NameUtil.ts
|
|
5
|
-
var NameUtil = class {
|
|
6
|
-
/**
|
|
7
|
-
* get table name
|
|
8
|
-
* StudentScore -> student_scores
|
|
9
|
-
*/
|
|
10
|
-
static getTableName(modelName) {
|
|
11
|
-
const modelNames = pluralize(modelName);
|
|
12
|
-
return _.snakeCase(modelNames);
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* get attribute name
|
|
16
|
-
* userName -> user_name
|
|
17
|
-
*/
|
|
18
|
-
static getAttributeName(propertyName) {
|
|
19
|
-
return _.snakeCase(propertyName);
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* [ 'user_name' ], unique
|
|
23
|
-
* uk_user_name
|
|
24
|
-
*
|
|
25
|
-
* [ 'user_name', 'gender' ]
|
|
26
|
-
* idx_user_name_gender
|
|
27
|
-
*/
|
|
28
|
-
static getIndexName(fields, options) {
|
|
29
|
-
const prefix = options?.unique ? "uk_" : "idx_";
|
|
30
|
-
const names = fields.join("_");
|
|
31
|
-
return prefix + names;
|
|
32
|
-
}
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
//#endregion
|
|
36
|
-
export { NameUtil };
|