Migrate models to TypeScript

Co-authored-by: David Mehren <dmehren1@gmail.com>
Co-authored-by: Yannick Bungers <git@innay.de>
Co-authored-by: Philipp Hochkamp <me@phochkamp.de>
Co-authored-by: nzbr <mail@nzbr.de>

Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
David Mehren 2020-04-10 22:06:12 +02:00
parent 54cd556f2f
commit 1d4107fe90
No known key found for this signature in database
GPG key ID: 6017AF117F9756CB
13 changed files with 565 additions and 539 deletions

View file

@ -1,43 +1,32 @@
import {DataTypes} from 'sequelize';
import { AutoIncrement, Table, Column, DataType, PrimaryKey, Model, BelongsTo, createIndexDecorator, ForeignKey } from 'sequelize-typescript'
import { Note } from './note';
import { User } from './user';
const NoteUserIndex = createIndexDecorator({unique: true});
function createAuthorModel(sequelize) {
const Author = sequelize.define('Author', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
color: {
type: DataTypes.STRING
}
}, {
indexes: [
{
unique: true,
fields: ['noteId', 'userId']
}
]
});
@Table
export class Author extends Model<Author> {
@PrimaryKey
@AutoIncrement
@Column(DataType.INTEGER)
id: number;
Author.associate = function (models) {
Author.belongsTo(models.Note, {
foreignKey: 'noteId',
as: 'note',
constraints: false,
onDelete: 'CASCADE',
hooks: true
});
Author.belongsTo(models.User, {
foreignKey: 'userId',
as: 'user',
constraints: false,
onDelete: 'CASCADE',
hooks: true
})
};
@Column(DataType.STRING)
color: string;
return Author
@ForeignKey(() => Note)
@NoteUserIndex
@Column
noteId: string;
@BelongsTo(() => Note, { foreignKey: 'noteId', onDelete: 'CASCADE', constraints: false, hooks: true })
note: Note;
@ForeignKey(() => User)
@NoteUserIndex
@Column
userId: string;
@BelongsTo(() => User, { foreignKey: 'userId', onDelete: 'CASCADE', constraints: false, hooks: true })
user: User;
}
export = createAuthorModel