|
| 1 | +import {Model, Column, Table, BelongsToMany, Scopes, CreatedAt, UpdatedAt} from "sequelize-typescript"; |
| 2 | +import * as Promise from "bluebird"; |
| 3 | +import {MovieActor} from "./MovieActor"; |
| 4 | +import {Actor} from "./Actor"; |
| 5 | +import {ICreateOptions} from "sequelize-typescript/lib/interfaces/ICreateOptions"; |
| 6 | +import {Genre} from "./Genre"; |
| 7 | +import {MovieGenre} from "./MovieGenre"; |
| 8 | + |
| 9 | +@Scopes({ |
| 10 | + cast: { |
| 11 | + include: [{ |
| 12 | + model: () => Actor, |
| 13 | + through: {attributes: []}, |
| 14 | + }], |
| 15 | + }, |
| 16 | + genre: { |
| 17 | + include: [{ |
| 18 | + model: () => Genre, |
| 19 | + through: {attributes: []}, |
| 20 | + }] |
| 21 | + }, |
| 22 | + full: { |
| 23 | + include: [{ |
| 24 | + model: () => Actor, |
| 25 | + through: {attributes: []}, |
| 26 | + }, { |
| 27 | + model: () => Genre, |
| 28 | + through: {attributes: []}, |
| 29 | + }] |
| 30 | + } |
| 31 | +}) |
| 32 | +@Table |
| 33 | +export class Movie extends Model<Movie> { |
| 34 | + |
| 35 | + @Column |
| 36 | + title: string; |
| 37 | + |
| 38 | + @Column |
| 39 | + year: number; |
| 40 | + |
| 41 | + @BelongsToMany(() => Actor, () => MovieActor) |
| 42 | + cast?: Actor[]; |
| 43 | + |
| 44 | + @BelongsToMany(() => Genre, () => MovieGenre) |
| 45 | + genres?: Genre[]; |
| 46 | + |
| 47 | + @CreatedAt |
| 48 | + @Column |
| 49 | + createdAt: Date; |
| 50 | + |
| 51 | + @UpdatedAt |
| 52 | + @Column |
| 53 | + updatedAt: Date; |
| 54 | + |
| 55 | + static create(values?: any, options?: ICreateOptions): Promise<Movie> { |
| 56 | + const include: any = []; |
| 57 | + if (values) { |
| 58 | + if (values.cast) include.push(Actor); |
| 59 | + if (values.genres) include.push(Genre); |
| 60 | + } |
| 61 | + if (!options) options = {}; |
| 62 | + options.include = options.include ? options.include.concat(include) : include; |
| 63 | + |
| 64 | + return super.create.call(this, values, options); |
| 65 | + } |
| 66 | + |
| 67 | + static scope(name: string = 'defaultScope'): typeof Movie { |
| 68 | + return super.scope.call(this, name); |
| 69 | + } |
| 70 | +} |
0 commit comments