Skip to content

Instantly share code, notes, and snippets.

@carldanley
Created June 3, 2017 17:16
Show Gist options
  • Save carldanley/29bbe9987fb35df6f428431d4fa82d77 to your computer and use it in GitHub Desktop.
Save carldanley/29bbe9987fb35df6f428431d4fa82d77 to your computer and use it in GitHub Desktop.
// external dependencies
const bPromise = require('bluebird');
// logic
exports.up = function(db, types) {
return db.sequelize.transaction(bPromise.coroutine(function* (t) {
yield db.createTable('listings', {
id: {
type: types.INTEGER,
allowNull: false,
autoIncrement: true
}
}, {transaction: t});
yield db.createTable('shipments', {
id: {
type: types.INTEGER,
allowNull: false,
autoIncrement: true
}
}, {transaction: t});
}));
};
exports.down = function(db, types) {
return db.sequelize.transaction(bPromise.coroutine(function* (t) {
yield db.dropTable('listings', {transaction: t});
yield db.dropTable('shipments', {transaction: t});
}));
};
// exports
module.exports = function(db, types) {
const properties = {
id: {
type: types.INTEGER,
allowNull: false,
autoIncrement: true
},
title: types.STRING,
description: types.TEXT,
namedPriceAmount: types.FLOAT,
namedPriceCurrency: types.STRING,
createdAt: types.DATE,
createdBy: types.INTEGER.UNSIGNED,
public: types.BOOLEAN,
expirationDate: types.DATE,
active: types.BOOLEAN,
brokered: types.BOOLEAN,
code: types.INTEGER.UNSIGNED,
referenceId: types.STRING,
meta: types.JSONB
};
const methods = {
associate: function(models) {
models.Listing.hasMany(models.Shipment, {
as: 'shipments',
foreignKey: 'parentId'
});
}
};
return db.define('listings', properties, methods);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment