Sequelize UI

  • sakila
    • models
import type { Sequelize, Model } from 'sequelize'
import { Actor } from './Actor'
import { Film } from './Film'
import { Language } from './Language'
import { Category } from './Category'
import { Inventory } from './Inventory'
import { Store } from './Store'
import { Staff } from './Staff'
import { Customer } from './Customer'
import { Address } from './Address'
import { Rental } from './Rental'
import { Payment } from './Payment'
import { City } from './City'
import { Country } from './Country'
import { FilmActor } from './FilmActor'
import { FilmCategory } from './FilmCategory'
export {
Actor,
Film,
Language,
Category,
Inventory,
Store,
Staff,
Customer,
Address,
Rental,
Payment,
City,
Country,
FilmActor,
FilmCategory
}
export function initModels(sequelize: Sequelize) {
Actor.initModel(sequelize)
Film.initModel(sequelize)
Language.initModel(sequelize)
Category.initModel(sequelize)
Inventory.initModel(sequelize)
Store.initModel(sequelize)
Staff.initModel(sequelize)
Customer.initModel(sequelize)
Address.initModel(sequelize)
Rental.initModel(sequelize)
Payment.initModel(sequelize)
City.initModel(sequelize)
Country.initModel(sequelize)
FilmActor.initModel(sequelize)
FilmCategory.initModel(sequelize)
Actor.belongsToMany(Film, {
as: 'films',
through: FilmActor,
foreignKey: 'actor_id',
otherKey: 'film_id',
onDelete: 'CASCADE'
})
Film.belongsTo(Language, {
as: 'language',
foreignKey: 'language_id'
})
Film.belongsTo(Language, {
as: 'originalLanguage',
foreignKey: 'original_language_id'
})
Film.hasMany(Inventory, {
as: 'inventories',
foreignKey: 'film_id'
})
Film.belongsToMany(Actor, {
as: 'actors',
through: FilmActor,
foreignKey: 'film_id',
otherKey: 'actor_id',
onDelete: 'CASCADE'
})
Film.belongsToMany(Category, {
as: 'categories',
through: FilmCategory,
foreignKey: 'film_id',
otherKey: 'category_id',
onDelete: 'CASCADE'
})
Language.hasMany(Film, {
as: 'films',
foreignKey: 'language_id'
})
Language.hasMany(Film, {
as: 'originalLanguageFilms',
foreignKey: 'original_language_id'
})
Category.belongsToMany(Film, {
as: 'films',
through: FilmCategory,
foreignKey: 'category_id',
otherKey: 'film_id',
onDelete: 'CASCADE'
})
Inventory.belongsTo(Film, {
as: 'film',
foreignKey: 'film_id'
})
Inventory.belongsTo(Store, {
as: 'store',
foreignKey: 'store_id'
})
Store.hasMany(Inventory, {
as: 'inventories',
foreignKey: 'store_id'
})
Store.hasMany(Staff, {
as: 'staffs',
foreignKey: 'store_id',
constraints: false
})
Store.hasMany(Customer, {
as: 'customers',
foreignKey: 'store_id'
})
Store.belongsTo(Staff, {
as: 'manager',
foreignKey: 'manager_staff_id',
constraints: false
})
Store.belongsTo(Address, {
as: 'address',
foreignKey: 'address_id'
})
Staff.belongsTo(Store, {
as: 'store',
foreignKey: 'store_id',
constraints: false
})
Staff.hasMany(Store, {
as: 'managedStores',
foreignKey: 'manager_staff_id',
constraints: false
})
Staff.belongsTo(Address, {
as: 'address',
foreignKey: 'address_id'
})
Staff.hasMany(Rental, {
as: 'rentals',
foreignKey: 'staff_id'
})
Staff.hasMany(Payment, {
as: 'payments',
foreignKey: 'staff_id'
})
Customer.belongsTo(Store, {
as: 'store',
foreignKey: 'store_id'
})
Customer.belongsTo(Address, {
as: 'address',
foreignKey: 'address_id'
})
Customer.hasMany(Rental, {
as: 'rentals',
foreignKey: 'customer_id'
})
Customer.hasMany(Payment, {
as: 'payments',
foreignKey: 'customer_id'
})
Address.belongsTo(City, {
as: 'city',
foreignKey: 'city_id'
})
Address.hasOne(Customer, {
as: 'customer',
foreignKey: 'address_id'
})
Address.hasOne(Staff, {
as: 'staff',
foreignKey: 'address_id'
})
Address.hasOne(Store, {
as: 'store',
foreignKey: 'address_id'
})
Rental.belongsTo(Inventory, {
as: 'inventory',
foreignKey: 'inventory_id'
})
Rental.belongsTo(Customer, {
as: 'customer',
foreignKey: 'customer_id'
})
Rental.belongsTo(Staff, {
as: 'staff',
foreignKey: 'staff_id'
})
Rental.hasMany(Payment, {
as: 'payments',
foreignKey: 'rental_id'
})
Payment.belongsTo(Customer, {
as: 'customer',
foreignKey: 'customer_id'
})
Payment.belongsTo(Staff, {
as: 'staff',
foreignKey: 'staff_id'
})
Payment.belongsTo(Rental, {
as: 'rental',
foreignKey: 'rental_id'
})
City.belongsTo(Country, {
as: 'country',
foreignKey: 'country_id'
})
City.hasMany(Address, {
as: 'addresses',
foreignKey: 'city_id'
})
Country.hasMany(City, {
as: 'cities',
foreignKey: 'country_id'
})
FilmActor.belongsTo(Film, {
as: 'film',
foreignKey: 'film_id'
})
FilmActor.belongsTo(Actor, {
as: 'actor',
foreignKey: 'actor_id'
})
FilmCategory.belongsTo(Film, {
as: 'film',
foreignKey: 'film_id'
})
FilmCategory.belongsTo(Category, {
as: 'category',
foreignKey: 'category_id'
})
return {
Actor,
Film,
Language,
Category,
Inventory,
Store,
Staff,
Customer,
Address,
Rental,
Payment,
City,
Country,
FilmActor,
FilmCategory
}
}