import { Sequelize , DataTypes } from "sequelize" ; import * as knex from "knex" ; const HOSTNAME = "localhost" ; const PORT = 5432 ; const USER = "theodor" ; const DB = "postgres" ; const PASSWORD = null ; export const sequelize = new Sequelize ( `postgres://${ HOSTNAME }:${ PORT }/${ DB }` , { username : USER , password : PASSWORD , dialect : "postgres" , logging : false , } ) ; export const queryBuilder = knex ( { client : "pg" , connection : { host : HOSTNAME , port : PORT , user : USER , password : PASSWORD , database : DB , } , } ) ; export const User = sequelize . define ( "user" , { name : { type : DataTypes . TEXT , } , email : { type : DataTypes . TEXT , } , roles : { type : DataTypes . STRING , } , } ) ; export const Tag = sequelize . define ( "tag" , { name : DataTypes . STRING , } ) ; export const PostCategory = sequelize . define ( "postCategory" , { name : DataTypes . STRING , } ) ; export const Post = sequelize . define ( "post" , { title : DataTypes . TEXT , description : DataTypes . TEXT , } ) ; export const Group = sequelize . define ( "group" , { name : DataTypes . STRING , } ) ; export const Comment = sequelize . define ( "comment" , { text : DataTypes . TEXT , } ) ; Group . belongsToMany ( User , { as : "users" , through : "UserGroup" } ) ; User . belongsToMany ( Group , { as : "groups" , through : "UserGroup" } ) ; Tag . belongsToMany ( Post , { through : "PostTag" } ) ; Post . belongsToMany ( Tag , { through : "PostTag" } ) ; PostCategory . hasMany ( Post , { as : "posts" } ) ; Post . belongsTo ( PostCategory ) ; Post . hasMany ( Comment , { as : "comments" } ) ; Comment . belongsTo ( Post ) ; User . hasMany ( Post , { as : "posts" } ) ; Post . belongsTo ( User ) ; Comment . belongsTo ( User ) ; User . hasMany ( Comment , { as : "comments" } ) ;