Skip to content

Instantly share code, notes, and snippets.

@kerolloz
Created March 4, 2021 17:40
Show Gist options
  • Save kerolloz/f1aa8cb147660f572413c33535266fb1 to your computer and use it in GitHub Desktop.
Save kerolloz/f1aa8cb147660f572413c33535266fb1 to your computer and use it in GitHub Desktop.
const Sequelize = require("sequelize");
module.exports = new Sequelize({
database: "test",
username: "root",
password: "mysecretpassword",
dialect: "postgres",
});
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