PageSourceSearch

https://sequelize.org/assets/js/1a20bc57.0294e624.js

js sequelize.org collected 2026-09-24 08:30:01 UTC 35,670 bytes, 1 lines download raw bytes

1"use strict";(globalThis.webpackChunk=globalThis.webpackChunk||[]).push([[9114],{46768(e,n,i){i.r(n),i.d(n,{assets:()=>l,contentTitle:()=>o,default:()=>h,frontMatter:()=>r,metadata:()=>s,toc:()=>d});const s=JSON.parse('{"id":"cli","title":"Sequelize CLI","description":"Neither the Sequelize CLI nor its documentation is ready for use in Sequelize 7. If you rely on the CLI, please stay on Sequelize 6 for now.","source":"@site/docs/cli.md","sourceDirName":".","slug":"/cli","permalink":"/docs/v7/cli","draft":false,"unlisted":false,"editUrl":"https://github.com/sequelize/website/tree/main/docs/cli.md","tags":[],"version":"current","lastUpdatedBy":"renovate[bot]","lastUpdatedAt":1775795365000,"sidebarPosition":9,"frontMatter":{"title":"Sequelize CLI","sidebar_position":9},"sidebar":"tutorialSidebar","previous":{"title":"Utility TypeScript Types","permalink":"/docs/v7/other-topics/utility-types"},"next":{"title":"Terminology","permalink":"/docs/v7/terminology"}}');var a=i(74848),t=i(28453);const r={title:"Sequelize CLI",sidebar_position:9},o=void 0,l={},d=[{value:"Installing the CLI",id:"installing-the-cli",level:2},{value:"Project bootstrapping",id:"project-bootstrapping",level:2},{value:"Configuration",id:"configuration",level:3},{value:"Creating the first Model (and Migration)",id:"creating-the-first-model-and-migration",level:2},{value:"Writing a migration",id:"writing-a-migration",level:2},{value:"Running Migrations",id:"running-migrations",level:2},{value:"Undoing Migrations",id:"undoing-migrations",level:2},{value:"Creating the first Seed",id:"creating-the-first-seed",level:3},{value:"Running Seeds",id:"running-seeds",level:2},{value:"Undoing Seeds",id:"undoing-seeds",level:2},{value:"The <code>.sequelizerc</code> file",id:"the-sequelizerc-file",level:3},{value:"Dynamic configuration",id:"dynamic-configuration",level:3},{value:"Using Babel",id:"using-babel",level:3},{value:"Security tip",id:"security-tip",level:3},{value:"Storage",id:"storage",level:3},{value:"Migration Storage",id:"migration-storage",level:4},{value:"Seed Storage",id:"seed-storage",level:4},{value:"Configuration Connection String",id:"configuration-connection-string",level:3},{value:"Programmatic usage",id:"programmatic-usage",level:3}];function c(e){const n={a:"a",admonition:"admonition",code:"code",em:"em",h2:"h2",h3:"h3",h4:"h4",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,t.R)(),...e.components};return(0,a.jsxs)(a.Fragment,{children:[(0,a.jsxs)(n.admonition,{type:"warning",children:[(0,a.jsx)(n.p,{children:"Neither the Sequelize CLI nor its documentation is ready for use in Sequelize 7. If you rely on the CLI, please stay on Sequelize 6 for now."}),(0,a.jsxs)(n.p,{children:["Considering the main purpose of the CLI is to run migrations, you can also try using ",(0,a.jsx)(n.a,{href:"https://github.com/sequelize/umzug",children:"umzug"}),", or any\nother database migration tool instead."]})]}),"\n",(0,a.jsxs)(n.p,{children:["Just like you use ",(0,a.jsx)(n.a,{href:"https://en.wikipedia.org/wiki/Version_control",children:"version control"})," systems such as ",(0,a.jsx)(n.a,{href:"https://en.wikipedia.org/wiki/Git",children:"Git"})," to manage changes in your source code, you can use ",(0,a.jsx)(n.strong,{children:"migrations"})," to keep track of changes to the database. With migrations, you can transfer your existing database into another state and vice versa: Those state transitions are saved in migration files, which describe how to get to the new state and how to revert the changes to get back to the old state."]}),"\n",(0,a.jsxs)(n.p,{children:["You will need the ",(0,a.jsx)(n.a,{href:"https://github.com/sequelize/cli",children:"Sequelize Command-Line Interface (CLI)"}),". The CLI ships support for migrations and project bootstrapping."]}),"\n",(0,a.jsxs)(n.p,{children:["A Migration in Sequelize is a JavaScript file that exports two functions, ",(0,a.jsx)(n.code,{children:"up"})," and ",(0,a.jsx)(n.code,{children:"down"}),", that dictate how to perform the migration and undo it. You define those functions manually, but you don't call them manually; the CLI will call them automatically. In these functions, you should perform whatever queries you need, with the help of ",(0,a.jsx)(n.code,{children:"sequelize.query"})," and whichever other methods Sequelize provides to you. There is no extra magic beyond that."]}),"\n",(0,a.jsx)(n.h2,{id:"installing-the-cli",children:"Installing the CLI"}),"\n",(0,a.jsx)(n.p,{children:"To install the Sequelize CLI:"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"# using npm\nnpm install --save-dev sequelize-cli\n# using yarn\nyarn add sequelize-cli --dev\n"})}),"\n",(0,a.jsxs)(n.p,{children:["For details see the ",(0,a.jsx)(n.a,{href:"https://github.com/sequelize/cli",children:"CLI GitHub repository"}),"."]}),"\n",(0,a.jsx)(n.h2,{id:"project-bootstrapping",children:"Project bootstrapping"}),"\n",(0,a.jsxs)(n.p,{children:["To create an empty project you will need to execute ",(0,a.jsx)(n.code,{children:"init"})," command"]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"# using npm\nnpx sequelize-cli init\n# using yarn\nyarn sequelize-cli init\n"})}),"\n",(0,a.jsx)(n.p,{children:"This will create the following folders"}),"\n",(0,a.jsxs)(n.ul,{children:["\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"config"}),", contains the config file, which tells CLI how to connect with the database"]}),"\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"models"}),", contains all models for your project"]}),"\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"migrations"}),", contains all migration files"]}),"\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"seeders"}),", contains all seed files"]}),"\n"]}),"\n",(0,a.jsx)(n.h3,{id:"configuration",children:"Configuration"}),"\n",(0,a.jsxs)(n.p,{children:["Before continuing further we will need to tell the CLI how to connect to the database. To do that let's open the default config file ",(0,a.jsx)(n.code,{children:"config/config.json"}),". It looks something like this:"]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-json",children:'{\n  "development": {\n    "username": "root",\n    "password": null,\n    "database": "database_development",\n    "host": "127.0.0.1",\n    "dialect": "mysql"\n  },\n  "test": {\n    "username": "root",\n    "password": null,\n    "database": "database_test",\n    "host": "127.0.0.1",\n    "dialect": "mysql"\n  },\n  "production": {\n    "username": "root",\n    "password": null,\n    "database": "database_production",\n    "host": "127.0.0.1",\n    "dialect": "mysql"\n  }\n}\n'})}),"\n",(0,a.jsxs)(n.p,{children:["Note that the Sequelize CLI assumes MySQL by default. If you're using another dialect, you need to change the content of the ",(0,a.jsx)(n.code,{children:'"dialect"'})," option."]}),"\n",(0,a.jsxs)(n.p,{children:['Now edit this file and set correct database credentials and dialect. The keys of the objects (e.g. "development") are used on ',(0,a.jsx)(n.code,{children:"model/index.js"})," for matching ",(0,a.jsx)(n.code,{children:"process.env.NODE_ENV"}),' (When undefined, "development" is a default value).']}),"\n",(0,a.jsxs)(n.p,{children:["Sequelize will use the default connection port for each dialect (for example, for Postgres, it is port 5432). If you need to specify a different port, use the ",(0,a.jsx)(n.code,{children:'"port"'})," field (it is not present by default in ",(0,a.jsx)(n.code,{children:"config/config.js"})," but you can simply add it)."]}),"\n",(0,a.jsxs)(n.p,{children:[(0,a.jsx)(n.strong,{children:"Note:"})," ",(0,a.jsxs)(n.em,{children:["If your database doesn't exist yet, you can just call ",(0,a.jsx)(n.code,{children:"db:create"})," command. With proper access, it will create that database for you."]})]}),"\n",(0,a.jsx)(n.h2,{id:"creating-the-first-model-and-migration",children:"Creating the first Model (and Migration)"}),"\n",(0,a.jsx)(n.p,{children:"Once you have properly configured the CLI config file you are ready to create your first migration. It's as simple as executing a simple c
1ommand."}),"\n",(0,a.jsxs)(n.p,{children:["We will use ",(0,a.jsx)(n.code,{children:"model:generate"})," command. This command requires two options:"]}),"\n",(0,a.jsxs)(n.ul,{children:["\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"name"}),": the name of the model;"]}),"\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"attributes"}),": the list of model attributes."]}),"\n"]}),"\n",(0,a.jsxs)(n.p,{children:["Let's create a model named ",(0,a.jsx)(n.code,{children:"User"}),"."]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"# using npm\nnpx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string\n# using yarn\nyarn sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string\n"})}),"\n",(0,a.jsx)(n.p,{children:"This will:"}),"\n",(0,a.jsxs)(n.ul,{children:["\n",(0,a.jsxs)(n.li,{children:["Create a model file ",(0,a.jsx)(n.code,{children:"user"})," in ",(0,a.jsx)(n.code,{children:"models"})," folder;"]}),"\n",(0,a.jsxs)(n.li,{children:["Create a migration file with a name like ",(0,a.jsx)(n.code,{children:"XXXXXXXXXXXXXX-create-user.js"})," in the ",(0,a.jsx)(n.code,{children:"migrations"})," folder."]}),"\n"]}),"\n",(0,a.jsxs)(n.p,{children:[(0,a.jsx)(n.strong,{children:"Note:"})," ",(0,a.jsx)(n.em,{children:"Sequelize will only use Model files, it's the table representation. On the other hand, the migration file is a change in that model or more specifically that table, used by CLI. Treat migrations like a commit or a log for some change in the database."})]}),"\n",(0,a.jsx)(n.h2,{id:"writing-a-migration",children:"Writing a migration"}),"\n",(0,a.jsx)(n.p,{children:"The following skeleton shows a typical migration file."}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-js",children:"module.exports = {\n  up: (queryInterface, Sequelize) => {\n    // logic for transforming into the new state\n  },\n  down: (queryInterface, Sequelize) => {\n    // logic for reverting the changes\n  },\n};\n"})}),"\n",(0,a.jsxs)(n.p,{children:["We can generate this file using ",(0,a.jsx)(n.code,{children:"migration:generate"}),". This will create ",(0,a.jsx)(n.code,{children:"xxx-migration-example.js"})," in your migration folder."]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"# using npm\nnpx sequelize-cli migration:generate --name migration-example\n# using yarn\nyarn sequelize-cli migration:generate --name migration-example\n"})}),"\n",(0,a.jsxs)(n.p,{children:["The passed ",(0,a.jsx)(n.code,{children:"queryInterface"})," object can be used to modify the database. The ",(0,a.jsx)(n.code,{children:"Sequelize"})," object stores the available data types such as ",(0,a.jsx)(n.code,{children:"STRING"})," or ",(0,a.jsx)(n.code,{children:"INTEGER"}),". The function ",(0,a.jsx)(n.code,{children:"up"})," or ",(0,a.jsx)(n.code,{children:"down"})," should return a ",(0,a.jsx)(n.code,{children:"Promise"}),". Let's look at an example:"]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-js",children:"const { DataTypes } = require('@sequelize/core');\n\nmodule.exports = {\n  up: (queryInterface, Sequelize) => {\n    return queryInterface.createTable('Person', {\n      name: DataTypes.STRING,\n      isBetaMember: {\n        type: DataTypes.BOOLEAN,\n        defaultValue: false,\n        allowNull: false,\n      },\n    });\n  },\n  down: (queryInterface, Sequelize) => {\n    return queryInterface.dropTable('Person');\n  },\n};\n"})}),"\n",(0,a.jsx)(n.p,{children:"The following is an example of a migration that performs two changes in the database,\nusing an automatically managed transaction to ensure that all instructions are successfully executed or rolled back in case of failure:"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-js",children:"const { DataTypes } = require('@sequelize/core');\n\nmodule.exports = {\n  up: (queryInterface, Sequelize) => {\n    return queryInterface.sequelize.transaction(transaction => {\n      return Promise.all([\n        queryInterface.addColumn(\n          'Person',\n          'petName',\n          {\n            type: DataTypes.STRING,\n          },\n          { transaction },\n        ),\n        queryInterface.addColumn(\n          'Person',\n          'favoriteColor',\n          {\n            type: DataTypes.STRING,\n          },\n          { transaction },\n        ),\n      ]);\n    });\n  },\n  down: (queryInterface, Sequelize) => {\n    return queryInterface.sequelize.transaction(transaction => {\n      return Promise.all([\n        queryInterface.removeColumn('Person', 'petName', { transaction }),\n        queryInterface.removeColumn('Person', 'favoriteColor', { transaction }),\n      ]);\n    });\n  },\n};\n"})}),"\n",(0,a.jsx)(n.p,{children:"The next example is of a migration that has a foreign key. You can use references to specify a foreign key:"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-js",children:"const { DataTypes } = require('@sequelize/core');\n\nmodule.exports = {\n  up: queryInterface => {\n    return queryInterface.createTable('Person', {\n      name: DataTypes.STRING,\n      isBetaMember: {\n        type: DataTypes.BOOLEAN,\n        defaultValue: false,\n        allowNull: false,\n      },\n      userId: {\n        type: DataTypes.INTEGER,\n        references: {\n          model: {\
1n            tableName: 'users',\n            schema: 'schema',\n          },\n          key: 'id',\n        },\n        allowNull: false,\n      },\n    });\n  },\n  down: (queryInterface, Sequelize) => {\n    return queryInterface.dropTable('Person');\n  },\n};\n"})}),"\n",(0,a.jsx)(n.p,{children:"The next example is of a migration that uses async/await where you create a unique index on a new column, with a manually-managed transaction:"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-js",children:"const { DataTypes } = require('@sequelize/core');\n\nmodule.exports = {\n  async up(queryInterface) {\n    const transaction = await queryInterface.sequelize.startUnmanagedTransaction();\n    try {\n      await queryInterface.addColumn(\n        'Person',\n        'petName',\n        {\n          type: DataTypes.STRING,\n        },\n        { transaction },\n      );\n      await queryInterface.addIndex('Person', 'petName', {\n        fields: 'petName',\n        unique: true,\n        transaction,\n      });\n      await transaction.commit();\n    } catch (err) {\n      await transaction.rollback();\n      throw err;\n    }\n  },\n  async down(queryInterface) {\n    const transaction = await queryInterface.sequelize.startUnmanagedTransaction();\n    try {\n      await queryInterface.removeColumn('Person', 'petName', { transaction });\n      await transaction.commit();\n    } catch (err) {\n      await transaction.rollback();\n      throw err;\n    }\n  },\n};\n"})}),"\n",(0,a.jsx)(n.p,{children:"The next example is of a migration that creates a unique index composed of multiple fields with a condition, which allows a relation to exist multiple times but only one can satisfy the condition:"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-js",children:"const { DataTypes } = require('@sequelize/core');\n\nmodule.exports = {\n  up: queryInterface => {\n    queryInterface\n      .createTable('Person', {\n        name: DataTypes.STRING,\n        bool: {\n          type: DataTypes.BOOLEAN,\n          defaultValue: false,\n        },\n      })\n      .then((queryInterface, Sequelize) => {\n        queryInterface.addIndex('Person', ['name', 'bool'], {\n          type: 'UNIQUE',\n          where: { bool: 'true' },\n        });\n      });\n  },\n  down: queryInterface => {\n    return queryInterface.dropTable('Person');\n  },\n};\n"})}),"\n",(0,a.jsx)(n.h2,{id:"running-migrations",children:"Running Migrations"}),"\n",(0,a.jsxs)(n.p,{children:["Until this step, we haven't inserted anything into the database. We have just created the required model and migration files for our first model, ",(0,a.jsx)(n.code,{children:"User"}),". Now to actually create that table in the database you need to run ",(0,a.jsx)(n.code,{children:"db:migrate"})," command."]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"# using npm\nnpx sequelize-cli db:migrate\n# using yarn\nyarn sequelize-cli db:migrate\n"})}),"\n",(0,a.jsx)(n.p,{children:"This command will execute these steps:"}),"\n",(0,a.jsxs)(n.ul,{children:["\n",(0,a.jsxs)(n.li,{children:["Will ensure a table called ",(0,a.jsx)(n.code,{children:"SequelizeMeta"})," in the database. This table is used to record which migrations have run on the current database"]}),"\n",(0,a.jsxs)(n.li,{children:["Start looking for any migration files that haven't run yet. This is possible by checking ",(0,a.jsx)(n.code,{children:"SequelizeMeta"})," table. In this case, it will run ",(0,a.jsx)(n.code,{children:"XXXXXXXXXXXXXX-create-user.js"})," migration, which we created in the last step."]}),"\n",(0,a.jsxs)(n.li,{children:["Creates a table called ",(0,a.jsx)(n.code,{children:"Users"})," with all columns as specified in its migration file."]}),"\n"]}),"\n",(0,a.jsx)(n.h2,{id:"undoing-migrations",children:"Undoing Migrations"}),"\n",(0,a.jsx)(n.p,{children:"Now our table has been created and saved in the database. With migration, you can revert to the old state by just running a command."}),"\n",(0,a.jsxs)(n.p,{children:["You can use ",(0,a.jsx)(n.code,{children:"db:migrate:undo"}),", this command will revert the most recent migration."]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"# using npm\nnpx sequelize-cli db:migrate:undo\n# using yarn\nyarn sequelize-cli db:migrate:undo\n"})}),"\n",(0,a.jsxs)(n.p,{children:["You can revert to the initial state by undoing all migrations with the ",(0,a.jsx)(n.code,{children:"db:migrate:undo:all"})," command. You can also revert to a specific migration by passing its name with the ",(0,a.jsx)(n.code,{children:"--to"})," option."]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"# using npm\nnpx sequelize-cli db:migrate:undo:all --to XXXXXXXXXXXXXX-create-posts.js\n# using yarn\nyarn sequelize-cli db:migrate:undo:all --to XXXXXXXXXXXXXX-create-posts.js\n"})}),"\n",(0,a.jsx)(n.h3,{id:"creating-the-first-seed",children:"Creating the first Seed"}),"\n",(0,a.jsxs)(n.p,{children:["Suppose we want to insert some data into a few tables by default. If we follow up on the previous example we can consider creating a demo user for the ",(0,a.jsx)(n.code,{children:"User"})," table."]}),"\n",(0,a.jsx)(n.p,{children:"To manage all data migrations you can use seeders. Seed files are some changes in data that can be used to populate database tables with sample or test data."}),"\n",(0,a.jsxs)(n.p,{children:["Let's create a seed file that will add a demo user to our ",(0,a.jsx)(n.code,{children:"User"})," table."]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"# using npm\nnpx sequelize-cli seed:generate --name demo-user\n# using yarn\nyarn sequelize-cli seed:generate --name demo-user\n"})}),"\n",(0,a.jsxs)(n.p,{children:["This command will create a seed file in ",(0,a.jsx)(n.code,{children:"seeders"})," folder. The file name will look something like ",(0,a.jsx)(n.code,{children:"XXXXXXXXXXXXXX-demo-user.js"}),". It follows the same ",(0,a.jsx)(n.code,{children:"up / down"})," semantics as the migration files."]}),"\n",(0,a.jsxs)(n.p,{children:["Now we should edit this file to insert the demo user to ",(0,a.jsx)(n.code,{children:"User"})," table."]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-js",children:"module.exports = {\n  up: (queryInterface, Sequelize) => {\n    return queryInterface.bulkInsert('Users', [\n      {\n        firstName: 'John',\n        lastName: 'Doe',\n        email: '[email protected]',\n        createdAt: new Date(),\n        updatedAt: new Date(),\n      },\n    ]);\n  },\n  down: (queryInterface, Sequelize) => {\n    return queryInterface.bulkDelete('Users', null, {});\n  },\n};\n"})}),"\n",(0,a.jsx)(n.h2,{id:"running-seeds",children:"Running Seeds"}),"\n",(0,a.jsx)(n.p,{children:"In last step you created a seed file; however, it has not been committed to the database. To do that we run a simple c
1ommand."}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"# using npm\nnpx sequelize-cli db:seed:all\n# using yarn\nyarn sequelize-cli db:seed:all\n"})}),"\n",(0,a.jsxs)(n.p,{children:["This will execute that seed file and a demo user will be inserted into the ",(0,a.jsx)(n.code,{children:"User"})," table."]}),"\n",(0,a.jsxs)(n.p,{children:[(0,a.jsx)(n.strong,{children:"Note:"})," ",(0,a.jsxs)(n.em,{children:["Seeder execution history is not stored anywhere, unlike migrations, which use the ",(0,a.jsx)(n.code,{children:"SequelizeMeta"})," table. If you wish to change this behavior, please read the ",(0,a.jsx)(n.code,{children:"Storage"})," section."]})]}),"\n",(0,a.jsx)(n.h2,{id:"undoing-seeds",children:"Undoing Seeds"}),"\n",(0,a.jsx)(n.p,{children:"Seeders can be undone if they are using any storage. There are two commands available for that:"}),"\n",(0,a.jsx)(n.p,{children:"If you wish to undo the most recent seed:"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"# using npm\nnpx sequelize-cli db:seed:undo\n# using yarn\nyarn sequelize-cli db:seed:undo\n"})}),"\n",(0,a.jsx)(n.p,{children:"If you wish to undo a specific seed:"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"# using npm\nnpx sequelize-cli db:seed:undo --seed name-of-seed-as-in-data\n# using yarn\nyarn sequelize-cli db:seed:undo --seed name-of-seed-as-in-data\n"})}),"\n",(0,a.jsx)(n.p,{children:"If you wish to undo all seeds:"}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"# using npm\nnpx sequelize-cli db:seed:undo:all\n# using yarn\nyarn sequelize-cli db:seed:undo:all\n"})}),"\n",(0,a.jsxs)(n.h3,{id:"the-sequelizerc-file",children:["The ",(0,a.jsx)(n.code,{children:".sequelizerc"})," file"]}),"\n",(0,a.jsx)(n.p,{children:"This is a special configuration file. It lets you specify the following options that you would usually pass as arguments to CLI:"}),"\n",(0,a.jsxs)(n.ul,{children:["\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"env"}),": The environment to run the command in"]}),"\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"config"}),": The path to the config file"]}),"\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"options-path"}),": The path to a JSON file with additional options"]}),"\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"migrations-path"}),": The path to the migrations folder"]}),"\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"seeders-path"}),": The path to the seeders folder"]}),"\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"models-path"}),": The path to the models folder"]}),"\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"url"}),": The database connection string to use. Alternative to using --config files"]}),"\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"debug"}),": When available show various debug information"]}),"\n"]}),"\n",(0,a.jsx)(n.p,{children:"Some scenarios where you can use it:"}),"\n",(0,a.jsxs)(n.ul,{children:["\n",(0,a.jsxs)(n.li,{children:["You want to override default path to ",(0,a.jsx)(n.code,{children:"migrations"}),", ",(0,a.jsx)(n.code,{children:"models"}),", ",(0,a.jsx)(n.code,{children:"seeders"})," or ",(0,a.jsx)(n.code,{children:"config"})," folder."]}),"\n",(0,a.jsxs)(n.li,{children:["You want to rename ",(0,a.jsx)(n.code,{children:"config.json"})," to something else like ",(0,a.jsx)(n.code,{children:"database.json"})]}),"\n"]}),"\n",(0,a.jsx)(n.p,{children:"And a whole lot more. Let's see how you can use this file for custom configuration."}),"\n",(0,a.jsxs)(n.p,{children:["To begin, let's create the ",(0,a.jsx)(n.code,{children:".sequelizerc"})," file in the root directory of your project, with the following content:"]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-js",children:"// .sequelizerc\n\nconst path = require('path');\n\nmodule.exports = {\n  config: path.resolve('config', 'database.json'),\n  'models-path': path.resolve('db', 'models'),\n  'seeders-path': path.resolve('db', 'seeders'),\n  'migrations-path': path.resolve('db', 'migrations'),\n};\n"})}),"\n",(0,a.jsx)(n.p,{children:"With this config, you are telling the CLI to:"}),"\n",(0,a.jsxs)(n.ul,{children:["\n",(0,a.jsxs)(n.li,{children:["Use ",(0,a.jsx)(n.code,{children:"config/database.json"})," file for config settings;"]}),"\n",(0,a.jsxs)(n.li,{children:["Use ",(0,a.jsx)(n.code,{children:"db/models"})," as models folder;"]}),"\n",(0,a.jsxs)(n.li,{children:["Use ",(0,a.jsx)(n.code,{children:"db/seeders"})," as seeders folder;"]}),"\n",(0,a.jsxs)(n.li,{children:["Use ",(0,a.jsx)(n.code,{children:"db/migrations"})," as the migrations folder."]}
1),"\n"]}),"\n",(0,a.jsx)(n.h3,{id:"dynamic-configuration",children:"Dynamic configuration"}),"\n",(0,a.jsxs)(n.p,{children:["The configuration file is by default a JSON file called ",(0,a.jsx)(n.code,{children:"config.json"}),". But sometimes you need a dynamic configuration, for example, to access environment variables or execute some other code to determine the configuration."]}),"\n",(0,a.jsxs)(n.p,{children:["Thankfully, the Sequelize CLI can read from both ",(0,a.jsx)(n.code,{children:".json"})," and ",(0,a.jsx)(n.code,{children:".js"})," files. This can be set up with ",(0,a.jsx)(n.code,{children:".sequelizerc"})," file. You just have to provide the path to your ",(0,a.jsx)(n.code,{children:".js"})," file as the ",(0,a.jsx)(n.code,{children:"config"})," option of your exported object:"]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-js",children:"const path = require('path');\n\nmodule.exports = {\n  config: path.resolve('config', 'config.js'),\n};\n"})}),"\n",(0,a.jsxs)(n.p,{children:["Now the Sequelize CLI will load ",(0,a.jsx)(n.code,{children:"config/config.js"})," for getting configuration options."]}),"\n",(0,a.jsxs)(n.p,{children:["An example of ",(0,a.jsx)(n.code,{children:"config/config.js"})," file:"]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-js",children:"const fs = require('fs');\n\nmodule.exports = {\n  development: {\n    username: 'database_dev',\n    password: 'database_dev',\n    database: 'database_dev',\n    host: '127.0.0.1',\n    port: 3306,\n    dialect: 'mysql',\n    dialectOptions: {\n      bigNumberStrings: true,\n    },\n  },\n  test: {\n    username: process.env.CI_DB_USERNAME,\n    password: process.env.CI_DB_PASSWORD,\n    database: process.env.CI_DB_NAME,\n    host: '127.0.0.1',\n    port: 3306,\n    dialect: 'mysql',\n    dialectOptions: {\n      bigNumberStrings: true,\n    },\n  },\n  production: {\n    username: process.env.PROD_DB_USERNAME,\n    password: process.env.PROD_DB_PASSWORD,\n    database: process.env.PROD_DB_NAME,\n    host: process.env.PROD_DB_HOSTNAME,\n    port: process.env.PROD_DB_PORT,\n    dialect: 'mysql',\n    dialectOptions: {\n      bigNumberStrings: true,\n      ssl: {\n        ca: fs.readFileSync(__dirname + '/mysql-ca-main.crt'),\n      },\n    },\n  },\n};\n"})}),"\n",(0,a.jsx)(n.p,{children:"The example above also shows how to add custom dialect options to the configuration."}),"\n",(0,a.jsx)(n.h3,{id:"using-babel",children:"Using Babel"}),"\n",(0,a.jsxs)(n.p,{children:["To enable more modern constructions in your migrations and seeders, you can simply install ",(0,a.jsx)(n.code,{children:"babel-register"})," and require it at the beginning of ",(0,a.jsx)(n.code,{children:".sequelizerc"}),":"]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"# using npm\nnpm i --save-dev babel-register\n# using yarn\nyarn add babel-register --dev\n"})}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-js",children:"// .sequelizerc\n\nrequire('babel-register');\n\nconst path = require('path');\n\nmodule.exports = {\n  config: path.resolve('config', 'config.json'),\n  'models-path': path.resolve('models'),\n  'seeders-path': path.resolve('seeders'),\n  'migrations-path': path.resolve('migrations'),\n};\n"})}),"\n",(0,a.jsxs)(n.p,{children:["Of course, the outcome will depend upon your babel configuration (such as in a ",(0,a.jsx)(n.code,{children:".babelrc"})," file). Learn more at ",(0,a.jsx)(n.a,{href:"https://babeljs.io",children:"babeljs.io"}),"."]}),"\n",(0,a.jsx)(n.h3,{id:"security-tip",children:"Security tip"}),"\n",(0,a.jsx)(n.p,{children:"Use environment variables for config settings. This is because secrets such as passwords should never be part of the source code (and especially not committed to version control)."}),"\n",(0,a.jsx)(n.h3,{id:"storage",children:"Storage"}),"\n",(0,a.jsxs)(n.p,{children:["There are three types of storage that you can use: ",(0,a.jsx)(n.code,{children:"sequelize"}),", ",(0,a.jsx)(n.code,{children:"json"}),", and ",(0,a.jsx)(n.code,{children:"none"}),"."]}),"\n",(0,a.jsxs)(n.ul,{children:["\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"sequelize"})," : stores migrations and seeds in a table on the sequelize database"]}),"\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"json"})," : stores migrations and seeds on a json file"]}),"\n",(0,a.jsxs)(n.li,{children:[(0,a.jsx)(n.code,{children:"none"})," : does not store any migration/seed"]}),"\n"]}),"\n",(0,a.jsx)(n.h4,{id:"migration-storage",children:"Migration Storage"}),"\n",(0,a.jsxs)(n.p,{children:["By default, the CLI will create a table in your database called ",(0,a.jsx)(n.code,{children:"SequelizeMeta"})," containing an entry for each executed migration. To change this behavior, there are three options you can add to the configuration file. Using ",(0,a.jsx)(n.code,{children:"migrationStorage"}),", you can choose the type of storage to be used for migrations. If you choose ",(0,a.jsx)(n.code,{children:"json"}),", you can specify the path of the file using ",(0,a.jsx)(n.code,{children:"migrationStoragePath"})," or the CLI will write to the file ",(0,a.jsx)(n.code,{children:"sequelize-meta.json"}),". If you want to keep the information in the database, using ",(0,a.jsx)(n.code,{children:"sequelize"}),", but want to use a different table, you can change the table name using ",(0,a.jsx)(n.code,{children:"migrationStorageTableName"}),". Also, you can define a different schema for the ",(0,a.jsx)(n.code,{children:"SequelizeMeta"})," table by providing the ",(0,a.jsx)(n.code,{children:"migrationStorageTableSchema"})," property."]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-json",children:'{\n  "development": {\n    "username": "root",\n    "password": null,\n    "database": "database_development",\n    "host": "127.0.0.1",\n    "dialect": "mysql",\n\n    // Use a different storage type. Default: sequelize\n    "migrationStorage": "json",\n\n    // Use a different file name. Default: sequelize-meta.json\n    "migrationStoragePath": "sequelizeMeta.json",\n\n    // Use a different table name. Default: SequelizeMeta\n    "migrationStorageTableName": "sequelize_meta",\n\n    // Use a different schema for the SequelizeMeta table\n    "migrationStorageTableSchema": "custom_schema"\n  }\n}\n'})}),"\n",(0,a.jsxs)(n.p,{children:[(0,a.jsx)(n.strong,{children:"Note:"})," ",(0,a.jsxs)(n.em,{children:["The ",(0,a.jsx)(n.code,{children:"none"})," storage is not recommended as a migration storage. If you decide to use it, be aware of the implications of having no record of what migrations did or didn't run."]})]}),"\n",(0,a.jsx)(n.h4,{id:"seed-storage",children:"Seed Storage"}),"\n",(0,a.jsxs)(n.p,{children:["By default, the CLI will not save any seed that is executed. If you choose to change this behavior (!), you can use ",(0,a.jsx)(n.code,{children:"seederStorage"})," in the configuration file to change the storage type. If you choose ",(0,a.jsx)(n.code,{children:"json"}),", you can specify the path of the file using ",(0,a.jsx)(n.code,{children:"seederStoragePath"})," or the CLI will write to the file ",(0,a.jsx)(n.code,{children:"sequelize-data.json"}),". If you want to keep the information in the database, using ",(0,a.jsx)(n.code,{children:"sequelize"}),", you can specify the table name using ",(0,a.jsx)(n.code,{children:"seederStorageTableName"}),", or it will default to ",(0,a.jsx)(n.code,{children:"SequelizeData"}),"."]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-json",children:'{\n  "development": {\n    "username": "root",\n    "password": null,\n    "database": "database_development",\n    "host": "127.0.0.1",\n    "dialect": "mysql",\n    // Use a different storage. Default: none\n    "seederStorage": "json",\n    // Use a different file name. Default: sequelize-data.json\n    "seederStoragePath": "sequelizeData.json",\n    // Use a different table name. Default: SequelizeData\n    "seederStorageTableName": "sequelize_data"\n  }\n}\n'})}),"\n",(0,a.jsx)(n.h3,{id:"configuration-connection-string",children:"Configuration Connection String"}),"\n",(0,a.jsxs)(n.p,{children:["As an alternative to the ",(0,a.jsx)(n.code,{children:"--config"})," option with configuration files defining your database, you can use the ",(0,a.jsx)(n.code,{children:"--url"})," option to pass in a connection string. For example:"]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-bash",children:"# using npm\nnpx sequelize-cli db:migrate --url 'mysql://root:password@mysql_host.com/database_name'\n# using yarn\nyarn sequelize-cli db:migrate --url 'mysql://root:password@mysql_host.com/database_name'\n"})}),"\n",(0,a.jsxs)(n.p,{children:["If utilizing ",(0,a.jsx)(n.code,{children:"package.json"})," scripts with npm, make sure to use the extra ",(0,a.jsx)(n.code,{children:"--"})," in your command when using flags.\nFor example:"]}),"\n",(0,a.jsx)(n.pre,{children:(0,a.jsx)(n.code,{className:"language-json",children:'// package.json\n\n...\n  "scripts": {\n    "migrate:up": "npx sequelize-cli db:migrate",\n    "migrate:undo": "npx sequelize-cli db:migrate:undo"\n  },\n...\n'})}),"\n",(0,a.jsxs)(n.p,{children:["Use the command like so: ",(0,a.jsx)(n.code,{children:"npm run migrate:up -- --url <url>"})]}),"\n",(0,a.jsx)(n.h3,{id:"programmatic-usage",children:"Programmatic usage"}),"\n",(0,a.jsxs)(n.p,{children:["Sequelize has a sister library called ",(0,a.jsx)(n.a,{href:"https://github.com/sequelize/umzug",children:"umzug"})," for programmatically handling execution and logging of migration tasks."]})]})}function h(e={}){const{wrapper:n}={...(0,t.R)(),...e.components};return n?(0,a.jsx)(n,{...e,children:(0,a.jsx)(c,{...e})}):c(e)}},28453(e,n,i){i.d(n,{R:()=>r,x:()=>o});var s=i(96540);const a={},t=s.createContext(a);function r(e){const n=s.useContext(t);return s.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function o(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(a):e.components||a:r(e.components),s.createElement(t.Provider,{value:n},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.