@asapjs/sequelize 0.0.2 → 0.0.6
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/dto/index.d.ts +8 -4
- package/dist/dto/index.d.ts.map +1 -1
- package/dist/dto/index.js +15 -43
- package/dist/dto/index.js.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +14 -2
- package/dist/index.js.map +1 -1
- package/dist/repository/index.d.ts +23 -0
- package/dist/repository/index.d.ts.map +1 -0
- package/dist/repository/index.js +52 -0
- package/dist/repository/index.js.map +1 -0
- package/dist/sequelize/index.d.ts.map +1 -1
- package/dist/sequelize/index.js +3 -4
- package/dist/sequelize/index.js.map +1 -1
- package/dist/table/index.d.ts +3 -0
- package/dist/table/index.d.ts.map +1 -0
- package/dist/table/index.js +38 -0
- package/dist/table/index.js.map +1 -0
- package/dist/type/index.d.ts +40 -0
- package/dist/type/index.d.ts.map +1 -0
- package/dist/type/index.js +246 -0
- package/dist/type/index.js.map +1 -0
- package/dist/{util → utils}/getUserIdInQuery.d.ts +0 -0
- package/dist/utils/getUserIdInQuery.d.ts.map +1 -0
- package/dist/{util → utils}/getUserIdInQuery.js +1 -1
- package/dist/utils/getUserIdInQuery.js.map +1 -0
- package/dist/utils/isClass.d.ts +3 -0
- package/dist/utils/isClass.d.ts.map +1 -0
- package/dist/utils/isClass.js +4 -0
- package/dist/utils/isClass.js.map +1 -0
- package/package.json +2 -1
- package/src/dto/index.ts +15 -43
- package/src/index.ts +5 -1
- package/src/repository/index.ts +49 -0
- package/src/sequelize/index.ts +0 -2
- package/src/table/index.ts +43 -0
- package/src/type/index.ts +263 -0
- package/src/{util → utils}/getUserIdInQuery.ts +1 -1
- package/src/utils/isClass.ts +1 -0
- package/dist/util/getUserIdInQuery.d.ts.map +0 -1
- package/dist/util/getUserIdInQuery.js.map +0 -1
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
import { BlobSize, DataTypes, TextLength } from 'sequelize';
|
|
2
|
+
import isClass from 'src/utils/isClass';
|
|
3
|
+
import { ExtendableDto } from '../dto';
|
|
4
|
+
|
|
5
|
+
const data: { [key: string]: any } = {};
|
|
6
|
+
|
|
7
|
+
export const getTypesData = (target: any) => {
|
|
8
|
+
const values = data[target?.name || target?.constructor?.name];
|
|
9
|
+
if (!values) return {};
|
|
10
|
+
return values;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const saveNewTypesData = (attributes: any, target: any) => {
|
|
14
|
+
data[target.constructor.name] = attributes;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const saveTypesData = (target: any, propertyName: any, value: any) => {
|
|
18
|
+
const types = getTypesData(target);
|
|
19
|
+
|
|
20
|
+
types[propertyName] = value;
|
|
21
|
+
|
|
22
|
+
saveNewTypesData(types, target);
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
type TypeIsData = {
|
|
26
|
+
__name: string;
|
|
27
|
+
toSwagger?: (...args: any[]) => any;
|
|
28
|
+
toSequelize?: (...args: any[]) => any;
|
|
29
|
+
fixValue?: (o: any) => any;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const typeGenerator =
|
|
33
|
+
(data: (...args: any[]) => TypeIsData) =>
|
|
34
|
+
(...options: any) => {
|
|
35
|
+
return (target?: any, propertyName?: string, propertyDescriptor?: PropertyDescriptor): void & TypeIsData => {
|
|
36
|
+
if (target !== undefined) {
|
|
37
|
+
const type = data(...options);
|
|
38
|
+
|
|
39
|
+
saveTypesData(target, propertyName, type);
|
|
40
|
+
|
|
41
|
+
if (propertyDescriptor) {
|
|
42
|
+
propertyDescriptor.value = type;
|
|
43
|
+
}
|
|
44
|
+
} else {
|
|
45
|
+
return data(...options) as any;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const TypeIs = {
|
|
51
|
+
INT: typeGenerator((options?: { decimals?: number; precision?: number; scale?: number; comment?: string }) => ({
|
|
52
|
+
__name: 'int',
|
|
53
|
+
toSwagger: () => ({
|
|
54
|
+
type: 'integer',
|
|
55
|
+
format: 'int32',
|
|
56
|
+
description: options?.comment,
|
|
57
|
+
}),
|
|
58
|
+
toSequelize: () => ({ type: DataTypes.INTEGER(options), comment: options?.comment }),
|
|
59
|
+
fixValue: (o: any) => {
|
|
60
|
+
if (o === undefined || o === null) return o;
|
|
61
|
+
return parseInt(String(o), 10);
|
|
62
|
+
},
|
|
63
|
+
})),
|
|
64
|
+
LONG: typeGenerator((options?: { decimals?: number; precision?: number; scale?: number; comment?: string }) => ({
|
|
65
|
+
__name: 'long',
|
|
66
|
+
toSwagger: () => ({
|
|
67
|
+
type: 'integer',
|
|
68
|
+
format: 'int64',
|
|
69
|
+
description: options?.comment,
|
|
70
|
+
}),
|
|
71
|
+
toSequelize: () => ({ type: DataTypes.INTEGER(options), comment: options?.comment }),
|
|
72
|
+
fixValue: (o: any) => {
|
|
73
|
+
if (o === undefined || o === null) return o;
|
|
74
|
+
return parseInt(String(o), 10);
|
|
75
|
+
},
|
|
76
|
+
})),
|
|
77
|
+
FLOAT: typeGenerator((options: { length?: number; decimals?: number; comment?: string }) => ({
|
|
78
|
+
__name: 'float',
|
|
79
|
+
toSwagger: () => ({
|
|
80
|
+
type: 'integer',
|
|
81
|
+
format: 'float',
|
|
82
|
+
description: options?.comment,
|
|
83
|
+
}),
|
|
84
|
+
toSequelize: () => ({ type: DataTypes.FLOAT(options?.length, options?.decimals), comment: options?.comment }),
|
|
85
|
+
fixValue: (o: any) => {
|
|
86
|
+
if (o === undefined || o === null) return o;
|
|
87
|
+
return parseFloat(String(o));
|
|
88
|
+
},
|
|
89
|
+
})),
|
|
90
|
+
DOUBLE: typeGenerator((options: { length?: number; decimals?: number; comment?: string }) => ({
|
|
91
|
+
__name: 'double',
|
|
92
|
+
toSwagger: () => ({
|
|
93
|
+
type: 'number',
|
|
94
|
+
format: 'double',
|
|
95
|
+
description: options?.comment,
|
|
96
|
+
}),
|
|
97
|
+
toSequelize: () => ({ type: DataTypes.DOUBLE(options?.length, options?.decimals), comment: options?.comment }),
|
|
98
|
+
fixValue: (o: any) => {
|
|
99
|
+
if (o === undefined || o === null) return o;
|
|
100
|
+
return parseFloat(String(o));
|
|
101
|
+
},
|
|
102
|
+
})),
|
|
103
|
+
STRING: typeGenerator((options: { length?: number; binary?: boolean; comment?: string }) => ({
|
|
104
|
+
__name: 'string',
|
|
105
|
+
toSwagger: () => ({
|
|
106
|
+
type: 'string',
|
|
107
|
+
description: options?.comment,
|
|
108
|
+
}),
|
|
109
|
+
toSequelize: () => ({ type: DataTypes.STRING(options?.length, options?.binary), comment: options?.comment }),
|
|
110
|
+
fixValue: (o: any) => {
|
|
111
|
+
if (o === undefined || o === null) return o;
|
|
112
|
+
return String(o);
|
|
113
|
+
},
|
|
114
|
+
})),
|
|
115
|
+
TEXT: typeGenerator((options: { length?: TextLength; comment?: string }) => ({
|
|
116
|
+
__name: 'text',
|
|
117
|
+
toSwagger: () => ({
|
|
118
|
+
type: 'string',
|
|
119
|
+
description: options?.comment,
|
|
120
|
+
}),
|
|
121
|
+
toSequelize: () => ({ type: DataTypes.TEXT({ length: options?.length }), comment: options?.comment }),
|
|
122
|
+
fixValue: (o: any) => {
|
|
123
|
+
if (o === undefined || o === null) return o;
|
|
124
|
+
return String(o);
|
|
125
|
+
},
|
|
126
|
+
})),
|
|
127
|
+
PASSWORD: typeGenerator((options: { comment?: string }) => ({
|
|
128
|
+
__name: 'password',
|
|
129
|
+
toSwagger: () => ({
|
|
130
|
+
type: 'string',
|
|
131
|
+
format: 'password',
|
|
132
|
+
description: options?.comment,
|
|
133
|
+
}),
|
|
134
|
+
toSequelize: () => ({ type: DataTypes.STRING(512), comment: options?.comment }),
|
|
135
|
+
})),
|
|
136
|
+
ENUM: typeGenerator((options: { values: string[]; comment?: string }) => ({
|
|
137
|
+
__name: 'enum',
|
|
138
|
+
toSwagger: () => ({
|
|
139
|
+
type: 'string',
|
|
140
|
+
enum: options?.values,
|
|
141
|
+
description: options?.values?.[0],
|
|
142
|
+
}),
|
|
143
|
+
toSequelize: () => ({ type: DataTypes.ENUM({ values: options?.values }), comment: options?.comment }),
|
|
144
|
+
fixValue: (o: any) => {
|
|
145
|
+
if (o === undefined || o === null) return o;
|
|
146
|
+
return options?.values?.includes?.(String(o)) ? String(o) : undefined;
|
|
147
|
+
},
|
|
148
|
+
})),
|
|
149
|
+
JSON: typeGenerator((options: { comment?: string }) => ({
|
|
150
|
+
__name: 'json',
|
|
151
|
+
toSwagger: () => ({
|
|
152
|
+
type: 'object',
|
|
153
|
+
description: options?.comment,
|
|
154
|
+
}),
|
|
155
|
+
toSequelize: () => ({ type: DataTypes.JSON, comment: options?.comment }),
|
|
156
|
+
})),
|
|
157
|
+
BASE64: typeGenerator((options: { length?: BlobSize; comment?: string }) => ({
|
|
158
|
+
__name: 'base64',
|
|
159
|
+
toSwagger: () => ({
|
|
160
|
+
type: 'string',
|
|
161
|
+
format: 'byte',
|
|
162
|
+
description: options?.comment,
|
|
163
|
+
}),
|
|
164
|
+
toSequelize: () => ({ type: DataTypes.BLOB(options?.length), comment: options?.comment }),
|
|
165
|
+
})),
|
|
166
|
+
BINARY: typeGenerator((options: { length?: BlobSize; comment?: string }) => ({
|
|
167
|
+
__name: 'binary',
|
|
168
|
+
toSwagger: () => ({
|
|
169
|
+
type: 'string',
|
|
170
|
+
format: 'binary',
|
|
171
|
+
description: options?.comment,
|
|
172
|
+
}),
|
|
173
|
+
toSequelize: () => ({ type: DataTypes.BLOB(options?.length), comment: options?.comment }),
|
|
174
|
+
})),
|
|
175
|
+
BOOLEAN: typeGenerator((options: { comment?: string }) => ({
|
|
176
|
+
__name: 'boolean',
|
|
177
|
+
toSwagger: () => ({
|
|
178
|
+
type: 'boolean',
|
|
179
|
+
}),
|
|
180
|
+
toSequelize: () => ({ type: DataTypes.BOOLEAN, comment: options?.comment }),
|
|
181
|
+
fixValue: (o: any) => {
|
|
182
|
+
if (o === undefined || o === null) return o;
|
|
183
|
+
return String(o) === 'true' ? true : String(o) === 'false' ? false : !!o;
|
|
184
|
+
},
|
|
185
|
+
})),
|
|
186
|
+
DATEONLY: typeGenerator((options: { comment?: string }) => ({
|
|
187
|
+
__name: 'date',
|
|
188
|
+
toSwagger: () => ({
|
|
189
|
+
type: 'string',
|
|
190
|
+
format: 'date',
|
|
191
|
+
description: options?.comment,
|
|
192
|
+
}),
|
|
193
|
+
toSequelize: () => DataTypes.DATEONLY(),
|
|
194
|
+
})),
|
|
195
|
+
DATETIME: typeGenerator((options: { comment?: string }) => ({
|
|
196
|
+
__name: 'datetime',
|
|
197
|
+
toSwagger: () => ({
|
|
198
|
+
type: 'string',
|
|
199
|
+
format: 'date-time',
|
|
200
|
+
description: options?.comment,
|
|
201
|
+
}),
|
|
202
|
+
toSequelize: () => DataTypes.DATE,
|
|
203
|
+
})),
|
|
204
|
+
FOREIGNKEY: typeGenerator((table: any) => ({
|
|
205
|
+
__name: 'foreignkey',
|
|
206
|
+
toSequelize: () => table,
|
|
207
|
+
})),
|
|
208
|
+
BELONGSTO: typeGenerator((associatedClassGetter: any, optionsOrForeignKey: any) => ({
|
|
209
|
+
__name: 'belongsto',
|
|
210
|
+
toSequelize: () => ({
|
|
211
|
+
associatedClassGetter,
|
|
212
|
+
optionsOrForeignKey,
|
|
213
|
+
}),
|
|
214
|
+
})),
|
|
215
|
+
DTO: typeGenerator((options: { dto: any; comment?: string }) => ({
|
|
216
|
+
__name: 'dto',
|
|
217
|
+
toSwagger: () => {
|
|
218
|
+
const result = new (options?.dto)()?.swagger();
|
|
219
|
+
return { ...result, comment: options?.comment };
|
|
220
|
+
},
|
|
221
|
+
})),
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
// export const TypeArray = (options: { dto?: typeof ExtendableDto; type?: TypeIsData; comment?: string }) => {
|
|
225
|
+
// return {
|
|
226
|
+
// __name: 'array',
|
|
227
|
+
// toSwagger: () => ({
|
|
228
|
+
// type: 'array',
|
|
229
|
+
// items: options?.dto ? new options.dto()?.swagger() : options?.type ? options?.type?.toSwagger?.() : null,
|
|
230
|
+
// description: options?.comment,
|
|
231
|
+
// }),
|
|
232
|
+
// };
|
|
233
|
+
// };
|
|
234
|
+
|
|
235
|
+
// export const TypeArray = (options: { dto?: typeof ExtendableDto; type?: () => TypeIsData; comment?: string }) => {
|
|
236
|
+
// return class {
|
|
237
|
+
// public swagger = () => {
|
|
238
|
+
// return {
|
|
239
|
+
// type: 'array',
|
|
240
|
+
// items: options?.dto ? new options.dto()?.swagger() : options?.type ? options?.type?.()?.toSwagger?.() : null,
|
|
241
|
+
// description: options?.comment,
|
|
242
|
+
// };
|
|
243
|
+
// };
|
|
244
|
+
// };
|
|
245
|
+
// };
|
|
246
|
+
|
|
247
|
+
export type DtoOrTypeIs = typeof ExtendableDto | (() => TypeIsData);
|
|
248
|
+
|
|
249
|
+
export const TypeArray = (value: DtoOrTypeIs) => {
|
|
250
|
+
return class {
|
|
251
|
+
public swagger = () => {
|
|
252
|
+
const isDto = isClass(value);
|
|
253
|
+
|
|
254
|
+
return {
|
|
255
|
+
type: 'array',
|
|
256
|
+
items: isDto
|
|
257
|
+
? new (value as typeof ExtendableDto)()?.swagger()
|
|
258
|
+
: (value as () => TypeIsData)?.()?.toSwagger?.(),
|
|
259
|
+
// description: options?.comment,
|
|
260
|
+
};
|
|
261
|
+
};
|
|
262
|
+
};
|
|
263
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const getUserIdInQuery = (user?: any) => `"${user?.id || ''}"`;
|
|
1
|
+
export const getUserIdInQuery = (user?: any) => `"${user?.id || user?._id || ''}"`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default (v: any) => v?.prototype?.constructor !== undefined;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getUserIdInQuery.d.ts","sourceRoot":"","sources":["../../src/util/getUserIdInQuery.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,UAAW,GAAG,WAA0B,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"getUserIdInQuery.js","sourceRoot":"","sources":["../../src/util/getUserIdInQuery.ts"],"names":[],"mappings":";;;AAAO,MAAM,gBAAgB,GAAG,CAAC,IAAU,EAAE,EAAE,CAAC,IAAI,CAAA,IAAI,aAAJ,IAAI,uBAAJ,IAAI,CAAE,EAAE,KAAI,EAAE,GAAG,CAAC;AAAzD,QAAA,gBAAgB,oBAAyC"}
|