Skip to content

Commit dc9598d

Browse files
author
pedro
authored
Merge pull request #18 from TaleLin/fix-extenduser
fix:修复模型扩展的问题
2 parents 1b377b1 + bcbde3e commit dc9598d

3 files changed

Lines changed: 95 additions & 83 deletions

File tree

lib/core.ts

Lines changed: 45 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export class Lin {
6262
userModel = userModel || User;
6363
groupModel = groupModel || Group;
6464
authModel = authModel || Auth;
65+
this.initModels(userModel, groupModel, authModel);
6566
this.applyManager(userModel, groupModel, authModel);
6667
// 4. db 同步到数据库默认为false,每次同步会很慢 todo 抽离
6768
await this.applyDB(synchronize);
@@ -76,6 +77,50 @@ export class Lin {
7677
jwt.initApp(this.app!, secret);
7778
}
7879

80+
private initModels(userModel: any, groupModel: any, authModel: any) {
81+
userModel.init(
82+
{
83+
...UserInterface.attributes
84+
},
85+
merge(
86+
{
87+
sequelize: db,
88+
tableName: 'lin_user',
89+
modelName: 'user'
90+
},
91+
UserInterface.options
92+
)
93+
);
94+
95+
groupModel.init(
96+
{
97+
...GroupInterface.attributes
98+
},
99+
merge(
100+
{
101+
sequelize: db,
102+
tableName: 'lin_group',
103+
modelName: 'group'
104+
},
105+
GroupInterface.options
106+
)
107+
);
108+
109+
authModel.init(
110+
{
111+
...AuthInterface.attributes
112+
},
113+
merge(
114+
{
115+
sequelize: db,
116+
tableName: 'lin_auth',
117+
modelName: 'auth'
118+
},
119+
AuthInterface.options
120+
)
121+
);
122+
}
123+
79124
private async applyDB(synchronize?: boolean, force?: boolean) {
80125
synchronize && db.sync({ force });
81126
}
@@ -278,20 +323,6 @@ export class User extends Model {
278323
}
279324
}
280325

281-
User.init(
282-
{
283-
...UserInterface.attributes
284-
},
285-
merge(
286-
{
287-
sequelize: db,
288-
tableName: 'lin_user',
289-
modelName: 'user'
290-
},
291-
UserInterface.options
292-
)
293-
);
294-
295326
/**
296327
* 权限系统中的Group模型
297328
*/
@@ -312,20 +343,6 @@ export class Group extends Model {
312343
}
313344
}
314345

315-
Group.init(
316-
{
317-
...GroupInterface.attributes
318-
},
319-
merge(
320-
{
321-
sequelize: db,
322-
tableName: 'lin_group',
323-
modelName: 'group'
324-
},
325-
GroupInterface.options
326-
)
327-
);
328-
329346
/**
330347
* 权限系统中的Auth模型
331348
*/
@@ -346,20 +363,6 @@ export class Auth extends Model {
346363
}
347364
}
348365

349-
Auth.init(
350-
{
351-
...AuthInterface.attributes
352-
},
353-
merge(
354-
{
355-
sequelize: db,
356-
tableName: 'lin_auth',
357-
modelName: 'auth'
358-
},
359-
AuthInterface.options
360-
)
361-
);
362-
363366
export interface LogArgs {
364367
message?: string;
365368
user_id?: number;

lib/factory.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,36 @@
99
*
1010
* ```js
1111
* const { modelExtend } = require("lin-mizar/lin/factory");
12-
* const { User } = require("lin-mizar");
12+
* const { UserInterface } = require("lin-mizar");
1313
* const Sequelize = require("sequelize");
1414
*
15-
* const User2 = modelExtend(User, {
15+
* modelExtend(UserInterface, {
1616
* phone: {
1717
* type: Sequelize.STRING(30),
1818
* unique: true,
1919
* allowNull: true
2020
* }
2121
* });
2222
*
23-
* User2.prototype.sayHello = function () {
24-
* console.log("hello world!");
25-
* };
26-
*
27-
* module.exports = { User2 };
2823
* ```
2924
*/
25+
// export function modelExtend(superModel: any, attributes) {
26+
// superModel.tableAttributes = {
27+
// ...superModel.tableAttributes,
28+
// ...attributes
29+
// };
30+
// const model = class extends superModel {};
31+
// Object.keys(attributes).forEach(key => {
32+
// Object.defineProperty(model.prototype, key, {
33+
// get() {
34+
// return this.dataValues && this.dataValues[key];
35+
// }
36+
// });
37+
// });
38+
// return model;
39+
// }
40+
3041
export function modelExtend(superModel: any, attributes) {
31-
superModel.tableAttributes = {
32-
...superModel.tableAttributes,
33-
...attributes
34-
};
35-
const model = class extends superModel {};
36-
Object.keys(attributes).forEach(key => {
37-
Object.defineProperty(model.prototype, key, {
38-
get() {
39-
return this.dataValues && this.dataValues[key];
40-
}
41-
});
42-
});
43-
return model;
42+
// UserInterface.attributes
43+
superModel.attributes = { ...superModel.attributes, ...attributes };
4444
}

lib/interface.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { generate } from './password-hash';
77
/**
88
* 记录信息的mixin
99
*/
10-
export const InfoCrudMixin = {
10+
export let InfoCrudMixin = {
1111
attributes: {},
1212
options: {
1313
createdAt: 'create_time',
@@ -30,7 +30,7 @@ export const InfoCrudMixin = {
3030
/**
3131
* 用户接口
3232
*/
33-
export const UserInterface = {
33+
export let UserInterface = {
3434
attributes: {
3535
id: {
3636
type: Sequelize.INTEGER,
@@ -78,28 +78,37 @@ export const UserInterface = {
7878
}
7979
}
8080
},
81-
options: merge(
82-
{
83-
tableName: 'lin_user',
84-
getterMethods: {
85-
isAdmin() {
86-
// @ts-ignore
87-
return this.getDataValue('admin') === UserAdmin.ADMIN;
88-
},
89-
isActive() {
90-
// @ts-ignore
91-
return this.getDataValue('active') === UserActive.ACTIVE;
92-
}
81+
options: {
82+
tableName: 'lin_user',
83+
createdAt: 'create_time',
84+
updatedAt: 'update_time',
85+
deletedAt: 'delete_time',
86+
paranoid: true,
87+
getterMethods: {
88+
isAdmin() {
89+
// @ts-ignore
90+
return this.getDataValue('admin') === UserAdmin.ADMIN;
91+
},
92+
isActive() {
93+
// @ts-ignore
94+
return this.getDataValue('active') === UserActive.ACTIVE;
95+
},
96+
createTime() {
97+
// @ts-ignore
98+
return dayjs(this.getDataValue('create_time')).unix() * 1000;
99+
},
100+
updateTime() {
101+
// @ts-ignore
102+
return dayjs(this.getDataValue('update_time')).unix() * 1000;
93103
}
94-
},
95-
InfoCrudMixin.options
96-
)
104+
}
105+
}
97106
};
98107

99108
/**
100109
* 权限接口
101110
*/
102-
export const AuthInterface = {
111+
export let AuthInterface = {
103112
attributes: {
104113
id: {
105114
type: Sequelize.INTEGER,
@@ -127,7 +136,7 @@ export const AuthInterface = {
127136
/**
128137
* 分组接口
129138
*/
130-
export const GroupInterface = {
139+
export let GroupInterface = {
131140
attributes: {
132141
id: {
133142
type: Sequelize.INTEGER,
@@ -152,7 +161,7 @@ export const GroupInterface = {
152161
/**
153162
* 日志接口
154163
*/
155-
export const LogInterface = {
164+
export let LogInterface = {
156165
attributes: {
157166
id: {
158167
type: Sequelize.INTEGER,
@@ -198,7 +207,7 @@ export const LogInterface = {
198207
/**
199208
* 文件接口
200209
*/
201-
export const FileInterface = {
210+
export let FileInterface = {
202211
id: {
203212
type: Sequelize.INTEGER,
204213
primaryKey: true,

0 commit comments

Comments
 (0)