Skip to content

Instantly share code, notes, and snippets.

@vickean
Created January 8, 2021 04:34
Show Gist options
  • Select an option

  • Save vickean/9d19fab10024e52a44716b69d5622091 to your computer and use it in GitHub Desktop.

Select an option

Save vickean/9d19fab10024e52a44716b69d5622091 to your computer and use it in GitHub Desktop.
Migration to add apiKey field
"use strict";
module.exports = {
up: async (queryInterface, Sequelize) => {
/**
* Add altering commands here.
*
* Example:
* await queryInterface.createTable('users', { id: Sequelize.INTEGER });
*/
return queryInterface.sequelize.transaction((t) => {
return Promise.all([
queryInterface.addColumn("providers", "apiKey", {
allowNull: true,
type: Sequelize.STRING(50),
}),
]);
});
},
down: async (queryInterface, Sequelize) => {
/**
* Add reverting commands here.
*
* Example:
* await queryInterface.dropTable('users');
*/
return queryInterface.sequelize.transaction((t) => {
return Promise.all([
queryInterface.removeColumn("providers", "apiKey", { transaction: t }),
]);
});
},
};

Ran npx sequelize-cli db:migrate to migrate.

"use strict";
const { Model } = require("sequelize");
module.exports = (sequelize, DataTypes) => {
class provider extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
}
}
provider.init(
{
name: {
allowNull: false,
type: DataTypes.STRING(50),
},
url: {
allowNull: true,
type: DataTypes.STRING,
},
image: {
allowNull: true,
type: DataTypes.STRING,
},
isVerified: {
type: DataTypes.BOOLEAN,
defaultValue: true,
allowNull: false,
},
apiKey: {
type: DataTypes.STRING(50),
allowNull: true,
},
},
{
sequelize,
modelName: "provider",
}
);
return provider;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment