forked from sequelize/sequelize-sscce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsscce.js
28 lines (23 loc) · 1.14 KB
/
sscce.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'use strict';
module.exports = async function(createSequelizeInstance, log) {
const { Sequelize, Op, Model, DataTypes } = require('sequelize');
const sequelize = createSequelizeInstance();
const Ship = sequelize.define('ship', {
name: { type: DataTypes.STRING, unique: true }
}, { timestamps: false });
const Captain = sequelize.define('captain', {
nickname: { type: DataTypes.STRING, unique: true }
}, { timestamps: false });
Ship.belongsToMany(Captain, { through: 'foobar', sourceKey: 'name', targetKey: 'nickname' });
Captain.belongsToMany(Ship, { through: 'foobar', sourceKey: 'nickname', targetKey: 'name' });
await sequelize.sync();
const sparrow = await Captain.create({ nickname: 'Jack Sparrow' });
const pearl = await Ship.create({ name: 'Black Pearl' });
await sparrow.addShip(pearl);
console.log("The static fetch works:");
log(await Ship.findOne({ include: Captain }));
console.log("The instance fetch doesn't work:");
const ship = await Ship.findOne();
log("ship:", ship);
log("ship.getCaptains():", await ship.getCaptains());
};