Created
March 4, 2021 17:40
-
-
Save kerolloz/f1aa8cb147660f572413c33535266fb1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Sequelize = require("sequelize"); | |
module.exports = new Sequelize({ | |
database: "test", | |
username: "root", | |
password: "mysecretpassword", | |
dialect: "postgres", | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { DataTypes, Model } = require("sequelize"); | |
const sequelize = require("./connection"); | |
(async () => { | |
const Ship = sequelize.define("ship", { | |
name: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
}, | |
purpose: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
}, | |
}); | |
const Member = sequelize.define("member", { | |
name: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
}, | |
species: { | |
type: DataTypes.STRING, | |
allowNull: false, | |
}, | |
}); | |
Member.belongsTo(Ship); | |
Ship.hasMany(Member); | |
await sequelize.sync({ force: true }); | |
const enterprise_ship = await Ship.create({ | |
name: "Enterprise", | |
purpose: "exploration", | |
}); | |
const m1 = await Member.create({ name: "Kirk", species: "human" }); | |
const m2 = await Member.create({ name: "Spock", species: "hybrid" }); | |
await m1.setShip(enterprise_ship); | |
await m2.setShip(enterprise_ship); | |
console.log(m1.toJSON()); | |
console.log(m2.toJSON()); | |
console.log(await m2.getShip()); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment