Skip to content

Commit 4f163b2

Browse files
author
pedro
committed
fix: 修复sequelize升级带来的版本问题
1 parent b4a977b commit 4f163b2

6 files changed

Lines changed: 140 additions & 77 deletions

File tree

lib/core.ts

Lines changed: 90 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Application from "koa";
22
import consola from "consola";
3+
import { Model } from "sequelize";
34
import { IMiddleware } from "koa-router";
45
import { jwt } from "./jwt";
56
import { assert } from "./util";
@@ -158,14 +159,33 @@ export class Manager {
158159
/**
159160
* 权限系统中的User模型
160161
*/
161-
export const User = db.define(
162-
"lin_user",
162+
export class User extends Model {
163+
public id!: number;
164+
public nickname!: string;
165+
public admin!: number;
166+
public active!: number;
167+
public email!: string;
168+
// tslint:disable-next-line:variable-name
169+
public group_id!: number;
170+
public password!: string;
171+
172+
// tslint:disable-next-line:variable-name
173+
public create_time!: Date;
174+
// tslint:disable-next-line:variable-name
175+
public update_time!: Date;
176+
// tslint:disable-next-line:variable-name
177+
public delete_time!: Date;
178+
}
179+
180+
User.init(
163181
{
164182
...UserInterface.attributes
165183
},
166184
merge(
167185
{
168-
tableName: "lin_user"
186+
sequelize: db,
187+
tableName: "lin_user",
188+
modelName: "user"
169189
},
170190
UserInterface.options
171191
)
@@ -188,27 +208,36 @@ User.verify = async function(nickname: string, password: string) {
188208

189209
// @ts-ignore
190210
User.prototype.checkPassword = function(raw: string) {
211+
// @ts-ignore
191212
if (!this.password || this.password === "") {
192213
return false;
193214
}
215+
// @ts-ignore
194216
return verify(raw, this.password);
195217
};
196218

197219
// @ts-ignore
198-
User.prototype.softDelete = function() {
199-
this.delete_time = new Date();
200-
this.save();
201-
};
220+
// User.prototype.softDelete = function() {
221+
// this.delete_time = new Date();
222+
// this.save();
223+
// };
202224

203225
// @ts-ignore
204226
User.prototype.toJSON = function() {
205227
const origin = {
228+
// @ts-ignore
206229
id: this.id,
230+
// @ts-ignore
207231
nickname: this.nickname,
232+
// @ts-ignore
208233
admin: this.admin,
234+
// @ts-ignore
209235
active: this.active,
236+
// @ts-ignore
210237
email: this.email,
238+
// @ts-ignore
211239
group_id: this.groupId,
240+
// @ts-ignore
212241
create_time: this.create_time
213242
};
214243
if (has(this, "auths")) {
@@ -223,6 +252,7 @@ User.prototype.toJSON = function() {
223252
// @ts-ignore
224253
User.prototype.resetPassword = function(newPassword: string) {
225254
// 注意,重置密码后记得提交至数据库
255+
// @ts-ignore
226256
this.password = newPassword;
227257
};
228258

@@ -231,7 +261,9 @@ User.prototype.changePassword = function(
231261
oldPassword: string,
232262
newPassword: string
233263
) {
264+
// @ts-ignore
234265
if (this.checkPassword(oldPassword)) {
266+
// @ts-ignore
235267
this.password = newPassword;
236268
return true;
237269
}
@@ -241,12 +273,21 @@ User.prototype.changePassword = function(
241273
/**
242274
* 权限系统中的Group模型
243275
*/
244-
export const Group = db.define(
245-
"lin_group",
246-
{ ...GroupInterface.attributes },
276+
export class Group extends Model {
277+
public id!: number;
278+
public name!: string;
279+
public info!: string;
280+
}
281+
282+
Group.init(
283+
{
284+
...GroupInterface.attributes
285+
},
247286
merge(
248287
{
249-
tableName: "lin_group"
288+
sequelize: db,
289+
tableName: "lin_group",
290+
modelName: "group"
250291
},
251292
GroupInterface.options
252293
)
@@ -255,8 +296,11 @@ export const Group = db.define(
255296
// @ts-ignore
256297
Group.prototype.toJSON = function() {
257298
let origin = {
299+
// @ts-ignore
258300
id: this.id,
301+
// @ts-ignore
259302
name: this.name,
303+
// @ts-ignore
260304
info: this.info
261305
};
262306
return has(this, "auths")
@@ -267,14 +311,23 @@ Group.prototype.toJSON = function() {
267311
/**
268312
* 权限系统中的Auth模型
269313
*/
270-
export const Auth = db.define(
271-
"lin_auth",
314+
export class Auth extends Model {
315+
public id!: number;
316+
// tslint:disable-next-line:variable-name
317+
public group_id!: number;
318+
public auth!: string;
319+
public module!: string;
320+
}
321+
322+
Auth.init(
272323
{
273324
...AuthInterface.attributes
274325
},
275326
merge(
276327
{
277-
tableName: "lin_auth"
328+
sequelize: db,
329+
tableName: "lin_auth",
330+
modelName: "auth"
278331
},
279332
AuthInterface.options
280333
)
@@ -283,9 +336,13 @@ export const Auth = db.define(
283336
// @ts-ignore
284337
Auth.prototype.toJSON = function() {
285338
return {
339+
// @ts-ignore
286340
id: this.id,
341+
// @ts-ignore
287342
group_id: this.group_id,
343+
// @ts-ignore
288344
module: this.module,
345+
// @ts-ignore
289346
auth: this.auth
290347
};
291348
};
@@ -300,26 +357,37 @@ export interface LogArgs {
300357
authority?: string;
301358
}
302359

303-
export const Log = db.define(
304-
"lin_log",
360+
export class Log extends Model {
361+
public id!: number;
362+
public message!: string;
363+
// tslint:disable-next-line:variable-name
364+
public user_id!: number;
365+
// tslint:disable-next-line:variable-name
366+
public user_name!: string;
367+
// tslint:disable-next-line:variable-name
368+
public status_code!: number;
369+
public method!: string;
370+
public path!: string;
371+
public authority!: string;
372+
public time!: Date;
373+
}
374+
375+
Log.init(
305376
{
306377
...LogInterface.attributes
307378
},
308379
merge(
309380
{
310-
tableName: "lin_log"
381+
sequelize: db,
382+
tableName: "lin_log",
383+
modelName: "log"
311384
},
312385
LogInterface.options
313386
)
314387
);
315388

316389
// @ts-ignore
317390
Log.createLog = function(args?: LogArgs, commit?: boolean) {
318-
// if (args) {
319-
// Object.keys(args).forEach(arg => {
320-
// set(log, arg, get(args, arg));
321-
// });
322-
// }
323391
const log = Log.build(args as any);
324392
// @ts-ignore
325393
commit && log.save();

lib/db.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Sequelize from "sequelize";
1+
import { Sequelize } from "sequelize";
22
import { config } from "./config";
33

44
// 拿到配置

lib/factory.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
// phone: {
2-
// type: Sequelize.STRING,
3-
// unique: true,
4-
// allowNull: true
5-
// }
1+
/**
2+
* 工厂方法
3+
*/
64

75
export function modelExtend(superModel: any, attributes) {
86
superModel.tableAttributes = {

lib/interface.ts

Lines changed: 43 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import dayjs from "dayjs";
55
import { generate } from "./password-hash";
66

77
export const InfoCrudMixin = {
8-
attributes: {
9-
delete_time: Sequelize.DATE
10-
},
8+
attributes: {},
119
options: {
1210
createdAt: "create_time",
1311
updatedAt: "update_time",
12+
deletedAt: "delete_time",
13+
paranoid: true,
1414
getterMethods: {
1515
createTime() {
1616
// @ts-ignore
@@ -21,51 +21,48 @@ export const InfoCrudMixin = {
2121
};
2222

2323
export const UserInterface = {
24-
attributes: merge(
25-
{
26-
id: {
27-
type: Sequelize.INTEGER,
28-
primaryKey: true,
29-
autoIncrement: true
30-
},
31-
nickname: {
32-
type: Sequelize.STRING({ length: 24 }),
33-
allowNull: false,
34-
unique: true
35-
},
36-
admin: {
37-
type: Sequelize.TINYINT,
38-
allowNull: false,
39-
defaultValue: 1
40-
},
41-
active: {
42-
type: Sequelize.TINYINT,
43-
allowNull: false,
44-
defaultValue: 1
45-
},
46-
email: {
47-
type: Sequelize.STRING({ length: 100 }),
48-
unique: true,
49-
allowNull: true
50-
},
51-
group_id: {
52-
type: Sequelize.INTEGER,
53-
allowNull: true
24+
attributes: {
25+
id: {
26+
type: Sequelize.INTEGER,
27+
primaryKey: true,
28+
autoIncrement: true
29+
},
30+
nickname: {
31+
type: Sequelize.STRING({ length: 24 }),
32+
allowNull: false,
33+
unique: true
34+
},
35+
admin: {
36+
type: Sequelize.TINYINT,
37+
allowNull: false,
38+
defaultValue: 1
39+
},
40+
active: {
41+
type: Sequelize.TINYINT,
42+
allowNull: false,
43+
defaultValue: 1
44+
},
45+
email: {
46+
type: Sequelize.STRING({ length: 100 }),
47+
unique: true,
48+
allowNull: true
49+
},
50+
group_id: {
51+
type: Sequelize.INTEGER,
52+
allowNull: true
53+
},
54+
password: {
55+
type: Sequelize.STRING({ length: 100 }),
56+
set(ps) {
57+
// @ts-ignore
58+
this.setDataValue("password", generate(ps));
5459
},
55-
password: {
56-
type: Sequelize.STRING({ length: 100 }),
57-
set(ps) {
58-
// @ts-ignore
59-
this.setDataValue("password", generate(ps));
60-
},
61-
get() {
62-
// @ts-ignore
63-
return this.getDataValue("password");
64-
}
60+
get() {
61+
// @ts-ignore
62+
return this.getDataValue("password");
6563
}
66-
},
67-
InfoCrudMixin.attributes
68-
),
64+
}
65+
},
6966
options: merge(
7067
{
7168
tableName: "lin_user",

lib/jwt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ async function parseHeader(ctx: RouterContext, type = TokenType.ACCESS) {
139139
if (!get(obj, "type") || get(obj, "type") !== type) {
140140
ctx.throw(new AuthFailed({ msg: "请使用正确类型的令牌" }));
141141
}
142-
const user = await ctx.manager.userModel.findById(get(obj, "identity"));
142+
const user = await ctx.manager.userModel.findByPk(get(obj, "identity"));
143143
if (!user) {
144144
ctx.throw(new NotFound({ msg: "用户不存在" }));
145145
}

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lin-mizar",
3-
"version": "0.0.1-alpha.2",
3+
"version": "0.0.1-alpha.5",
44
"description": "The core library of Lin CMS",
55
"main": "lin/index.js",
66
"scripts": {
@@ -24,7 +24,7 @@
2424
"koa-router": "^7.4.0",
2525
"lodash": "^4.17.11",
2626
"mysql2": "^1.6.5",
27-
"sequelize": "^4.43.0",
27+
"sequelize": "^5.3.5",
2828
"tslib": "^1.9.3"
2929
},
3030
"devDependencies": {

0 commit comments

Comments
 (0)