Skip to content

Commit 84d259b

Browse files
committed
refactor: use strong type in models
export missing
1 parent 8986c88 commit 84d259b

File tree

11 files changed

+648
-451
lines changed

11 files changed

+648
-451
lines changed

lib/app.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import * as response from "./response";
2525
import * as models from "./models";
2626
import * as csp from "./csp";
2727
import {Environment} from "./config/enum";
28+
import {checkAllNotesRevision} from "./services/note";
2829

2930
import {checkVersion, versionCheckMiddleware} from "./web/middleware/checkVersion";
3031
import TooBusyMiddleware from './middleware/tooBusy'
@@ -288,7 +289,7 @@ function startListen() {
288289
models.sequelize.sync().then(function () {
289290
// check if realtime is ready
290291
if (realtime.isReady()) {
291-
models.Revision.checkAllNotesRevision(function (err, notes) {
292+
checkAllNotesRevision(function (err, notes) {
292293
if (err) throw new Error(err)
293294
if (!notes || notes.length <= 0) return startListen()
294295
})
@@ -327,7 +328,7 @@ function handleTermSignals() {
327328
})
328329
const checkCleanTimer = setInterval(function () {
329330
if (realtime.isReady()) {
330-
models.Revision.checkAllNotesRevision(function (err, notes) {
331+
checkAllNotesRevision(function (err, notes) {
331332
if (err) return logger.error(err)
332333
if (!notes || notes.length <= 0) {
333334
clearInterval(checkCleanTimer)

lib/models/author.ts

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
11
// external modules
22

3-
export = function (sequelize, DataTypes) {
4-
const Author = sequelize.define('Author', {
5-
id: {
6-
type: DataTypes.INTEGER,
7-
primaryKey: true,
8-
autoIncrement: true
9-
},
10-
color: {
11-
type: DataTypes.STRING
12-
}
13-
}, {
14-
indexes: [
3+
import {Model, DataTypes} from "sequelize";
4+
5+
export interface AuthorAttributes {
6+
id: string
7+
color: string
8+
}
9+
10+
export class Author extends Model<AuthorAttributes> implements AuthorAttributes {
11+
color: string;
12+
id: string;
13+
14+
static initialize(sequelize): void {
15+
Author.init(
1516
{
16-
unique: true,
17-
fields: ['noteId', 'userId']
18-
}
19-
]
20-
})
17+
id: {
18+
type: DataTypes.INTEGER,
19+
primaryKey: true,
20+
autoIncrement: true
21+
},
22+
color: {
23+
type: DataTypes.STRING
24+
}
25+
},
26+
{
27+
sequelize,
28+
indexes: [
29+
{
30+
unique: true,
31+
fields: ['noteId', 'userId']
32+
}
33+
]
34+
})
35+
}
2136

22-
Author.associate = function (models) {
37+
static associate(models: any): void {
2338
Author.belongsTo(models.Note, {
2439
foreignKey: 'noteId',
2540
as: 'note',
@@ -35,6 +50,4 @@ export = function (sequelize, DataTypes) {
3550
hooks: true
3651
})
3752
}
38-
39-
return Author
4053
}

lib/models/baseModel.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {Sequelize, Model} from "sequelize";
2+
3+
export type MySequelize = Sequelize & {
4+
processData?: any
5+
stripNullByte?: any
6+
}
7+
8+
export type BaseModel<T> = {
9+
name: string;
10+
initialize(sequelize: Sequelize): void
11+
associate(models: any): void
12+
}
13+
14+
export function StaticImplements<T>(t: T) {
15+
return t;
16+
}
17+
18+
export interface BaseModelStatic extends Model {
19+
initialize(sequelize: Sequelize): void
20+
21+
associate(models: any): void
22+
}

lib/models/index.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
11
// external modules
22
import * as fs from "fs";
33
import * as path from "path";
4-
import {Sequelize} from "sequelize";
4+
import {Model, Sequelize} from "sequelize";
55
import {cloneDeep} from "lodash";
66

77
// core
88
import config from "../config";
99
import {logger} from "../logger";
1010

11+
12+
import {BaseModel} from "./baseModel";
13+
14+
import {Author, AuthorAttributes} from './author'
15+
import {User, UserAttributes} from './user'
16+
import {Revision, RevisionAttributes} from "./revision";
17+
import {Note, NoteAttributes} from './note'
18+
19+
export {Author, User, Revision, Note}
20+
export {AuthorAttributes, UserAttributes, RevisionAttributes, NoteAttributes}
21+
1122
const dbconfig = cloneDeep(config.db)
1223
dbconfig.logging = config.debug ? (data) => {
1324
logger.info(data)
@@ -41,22 +52,16 @@ sequelize.processData = processData
4152

4253
const db: any = {}
4354

44-
fs.readdirSync(__dirname)
45-
.filter(function (file) {
46-
return (file.indexOf('.') !== 0) && (file !== 'index.js') && file.endsWith('.js')
47-
})
48-
.forEach(function (file) {
49-
const model = sequelize.import(path.join(__dirname, file))
50-
db[model.name] = model
51-
})
52-
53-
Object.keys(db).forEach(function (modelName) {
54-
if ('associate' in db[modelName]) {
55-
db[modelName].associate(db)
55+
const models: BaseModel<Model>[] = [User, Note, Author, Revision]
56+
57+
models.forEach(m => {
58+
m.initialize(sequelize)
59+
db[m.name] = m
60+
})
61+
models.forEach(m => {
62+
if ('associate' in m) {
63+
m.associate(db)
5664
}
5765
})
5866

59-
db.sequelize = sequelize
60-
db.Sequelize = Sequelize
61-
62-
export = db
67+
export {sequelize}

0 commit comments

Comments
 (0)