Skip to content

Instantly share code, notes, and snippets.

@maikelmclauflin
Last active October 15, 2018 20:42
Show Gist options
  • Save maikelmclauflin/fb1f0cd6bf221eb4fe6e182ece451d89 to your computer and use it in GitHub Desktop.
Save maikelmclauflin/fb1f0cd6bf221eb4fe6e182ece451d89 to your computer and use it in GitHub Desktop.
pseudocode to show how sequelize might be used
// usage
// shared
const sequelize = new Sequelize(process.env.BAT_POSTGRES_URL, {
pool: {
max: 10,
min: 0,
acquire: 30000,
idle: 10000
}
})
const Wallets = WalletsModel(sequelize, Sequelize)
const Op = Sequelize.Op
// instance
Wallets.findAll({
where: {
provider: 'uphold',
paymentStamp: {
[Op.gt]: new Date()
}
}
}) // find
Wallets.find() // findOne
Wallets.create({ // insert
paymentId: '0000-0000-0000-4000-00000000000', // will be held as snake_case
address: '000000000000000000000000',
provider: 'uphold',
balances: {
a: '1289342032394200000',
b: '389283310942049242442'
},
paymentStamp: new Date(),
altcurrency: 'BAT',
timestamp: new Date()
}) // -> Promise
// db/models/wallets.js
const WalletsModel = (sequelize, DataTypes) => {
const Wallets = sequelize.define('Wallets', {
paymentId: DataTypes.STRING, // will be held as snake_case
address: DataTypes.STRING,
provider: DataTypes.STRING,
balances: DataTypes.JSONB,
keychains: DataTypes.JSONB,
paymentStamp: DataTypes.TIME,
altcurrency: DataTypes.STRING,
timestamp: DataTypes.TIME
}, {
underscored: true,
// view stuff goes here
// https://github.com/abelosorio/sequelize-views-support
});
Wallets.associate = function(models) {
// associations can be defined here
};
return Wallets;
};
// db/migrations/wallets.js
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Wallets', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
paymentId: { // will be held as snake_case
type: Sequelize.STRING
},
address: {
type: Sequelize.STRING
},
provider: {
type: Sequelize.STRING
},
balances: {
type: Sequelize.JSONB
},
keychains: {
type: Sequelize.JSONB
},
paymentStamp: {
type: Sequelize.TIME
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Wallets');
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment