mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-20 02:05:21 -04:00
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:
parent
54cd556f2f
commit
1d4107fe90
13 changed files with 565 additions and 539 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue