|
1 |
| -# sequelize/cli [](https://travis-ci.org/sequelize/cli) [](https://greenkeeper.io/) |
| 1 | +# sequelize/cli [](https://badge.fury.io/js/sequelize-cli) [](https://travis-ci.org/sequelize/cli) [](https://greenkeeper.io/) |
2 | 2 |
|
3 | 3 | The Sequelize Command Line Interface (CLI)
|
4 | 4 |
|
5 |
| -## Sequelize Support |
6 |
| - |
7 |
| -In current v3 release CLI generate migration/models which follow Sequelize v3 format, CLI will work with Sequelize v4 in most cases but migration/model skeleton is still generated to support v3. |
8 |
| - |
9 |
| -Full support for Sequelize v4 will be coming soon with [Sequelize CLI v4](https://github.com/sequelize/cli/issues/441) |
| 5 | +## Table of Contents |
| 6 | +- [Installation](#installation) |
| 7 | +- [Contributing](#contributing) |
| 8 | +- [Sequelize Support](#sequelize-support) |
| 9 | +- [Documentation](#documentation) |
10 | 10 |
|
11 | 11 | ## Installation
|
12 | 12 |
|
13 |
| -Install this globally and you'll have access to the `sequelize` command anywhere on your system. |
| 13 | +### Globally |
| 14 | +Install CLI globally with |
14 | 15 |
|
15 | 16 | ```bash
|
16 | 17 | $ npm install -g sequelize-cli
|
17 | 18 | ```
|
18 | 19 |
|
19 |
| -or install it locally to your `node_modules` folder |
| 20 | +Now you can run CLI using following command anywhere |
| 21 | + |
| 22 | +```bash |
| 23 | +$ sequelize |
| 24 | +``` |
| 25 | + |
| 26 | +### Locally |
| 27 | +Install CLI locally to your `node_modules` folder with |
20 | 28 |
|
21 | 29 | ```bash
|
22 | 30 | $ npm install --save sequelize-cli
|
23 | 31 | ```
|
24 |
| -## Global Install Usage |
| 32 | + |
| 33 | +You should be able to run CLI with |
25 | 34 |
|
26 | 35 | ```bash
|
27 |
| -$ sequelize |
| 36 | +$ node_modules/.bin/sequelize |
28 | 37 | ```
|
29 | 38 |
|
| 39 | +### Usage |
30 | 40 | ```
|
31 | 41 | Sequelize CLI [Node: 6.11.2, CLI: 3.0.0, ORM: 4.8.0]
|
32 | 42 |
|
@@ -54,238 +64,20 @@ Options:
|
54 | 64 | --help Show help [boolean]
|
55 | 65 | ```
|
56 | 66 |
|
57 |
| -## Local Install Usage |
58 |
| - |
59 |
| -``` |
60 |
| -$ node_modules/.bin/sequelize [--HARMONY-FLAGS] |
61 |
| -``` |
62 |
| - |
63 |
| -## Options |
64 |
| - |
65 |
| -The manuals will show all the flags and options which are available for the respective tasks. |
66 |
| -If you find yourself in a situation where you always define certain flags in order to |
67 |
| -make the CLI compliant to your project, you can move those definitions also into a file called |
68 |
| -`.sequelizerc`. The file will get `require`d if available and can therefore be either a JSON file |
69 |
| -or a Node.JS script that exports a hash. |
70 |
| - |
71 |
| -### Example for a Node.JS script |
72 |
| - |
73 |
| -```js |
74 |
| -var path = require('path') |
75 |
| - |
76 |
| -module.exports = { |
77 |
| - 'config': path.resolve('config', 'database.json'), |
78 |
| - 'migrations-path': path.resolve('db', 'migrate') |
79 |
| -} |
80 |
| -``` |
81 |
| - |
82 |
| -This will configure the CLI to always treat `config/database.json` as config file and |
83 |
| -`db/migrate` as the directory for migrations. |
84 |
| - |
85 |
| -### Configuration file |
86 |
| - |
87 |
| -By default the CLI will try to use the file `config/config.js` and `config/config.json`. You can modify that path either via the `--config` flag or via the option mentioned earlier. Here is how a configuration file might look like (this is the one that `sequelize init` generates): |
88 |
| - |
89 |
| -```json |
90 |
| -{ |
91 |
| - "development": { |
92 |
| - "username": "root", |
93 |
| - "password": null, |
94 |
| - "database": "database_development", |
95 |
| - "host": "127.0.0.1", |
96 |
| - "dialect": "mysql" |
97 |
| - }, |
98 |
| - "test": { |
99 |
| - "username": "root", |
100 |
| - "password": null, |
101 |
| - "database": "database_test", |
102 |
| - "host": "127.0.0.1", |
103 |
| - "dialect": "mysql" |
104 |
| - }, |
105 |
| - "production": { |
106 |
| - "username": "root", |
107 |
| - "password": null, |
108 |
| - "database": "database_production", |
109 |
| - "host": "127.0.0.1", |
110 |
| - "dialect": "mysql" |
111 |
| - } |
112 |
| -} |
113 |
| -``` |
114 |
| - |
115 |
| -The properties can also be combined to a `url`: |
116 |
| - |
117 |
| -```json |
118 |
| -{ |
119 |
| - "development": { |
120 |
| - "url": "mysql://root:password@mysql_host.com/database_name", |
121 |
| - "dialect": "mysql" |
122 |
| - } |
123 |
| -} |
124 |
| -``` |
125 |
| - |
126 |
| -In case of a JS file it obviously needs to `module.exports` the object. |
127 |
| -Optionally, it's possible to put all the configuration to the `url` option. The format is explained in the section below. |
128 |
| - |
129 |
| -### Configuration Connection String |
130 |
| - |
131 |
| -As an alternative to the `--config` option with configuration files defining your database, you can |
132 |
| -use the `--url` option to pass in a connection string. For example: |
133 |
| - |
134 |
| -`sequelize db:migrate --url 'mysql://root:password@mysql_host.com/database_name'` |
135 |
| - |
136 |
| -### Configuration Connection Environment Variable |
137 |
| - |
138 |
| -Another possibility is to store the URL in an environment variable and to tell |
139 |
| -the CLI to lookup a certain variable during runtime. Let's assume you have an |
140 |
| -environment variable called `DB_CONNECTION_STRING` which stores the value |
141 |
| -`mysql://root:password@mysql_host.com/database_name`. In order to make the CLI |
142 |
| -use it, you have to use declare it in your config file: |
143 |
| - |
144 |
| -```json |
145 |
| -{ |
146 |
| - "production": { |
147 |
| - "use_env_variable": "DB_CONNECTION_STRING" |
148 |
| - } |
149 |
| -} |
150 |
| -``` |
151 |
| - |
152 |
| -With v2.0.0 of the CLI you can also just directly access the environment variables inside the `config/config.js`: |
| 67 | +## Contributing |
153 | 68 |
|
154 |
| -```js |
155 |
| -module.exports = { |
156 |
| - "production": { |
157 |
| - "hostname": process.env.DB_HOSTNAME |
158 |
| - } |
159 |
| -} |
160 |
| -``` |
161 |
| - |
162 |
| -### Storage |
163 |
| - |
164 |
| -There are three types of storage that you can use: `sequelize`, `json`, and `none`. |
165 |
| - |
166 |
| -- `sequelize` : stores migrations and seeds in a table on the sequelize database |
167 |
| -- `json` : stores migrations and seeds on a json file |
168 |
| -- `none` : does not store any migration/seed |
169 |
| - |
170 |
| - |
171 |
| -#### Migration |
172 |
| - |
173 |
| -By default the CLI will create a table in your database called `SequelizeMeta` containing an entry |
174 |
| -for each executed migration. To change this behavior, there are three options you can add to the |
175 |
| -configuration file. Using `migrationStorage`, you can choose the type of storage to be used for |
176 |
| -migrations. If you choose `json`, you can specify the path of the file using `migrationStoragePath` |
177 |
| -or the CLI will write to the file `sequelize-meta.json`. If you want to keep the information in the |
178 |
| -database, using `sequelize`, but want to use a different table, you can change the table name using |
179 |
| -`migrationStorageTableName`. |
180 |
| - |
181 |
| -```json |
182 |
| -{ |
183 |
| - "development": { |
184 |
| - "username": "root", |
185 |
| - "password": null, |
186 |
| - "database": "database_development", |
187 |
| - "host": "127.0.0.1", |
188 |
| - "dialect": "mysql", |
189 |
| - |
190 |
| - // Use a different storage type. Default: sequelize |
191 |
| - "migrationStorage": "json", |
192 |
| - |
193 |
| - // Use a different file name. Default: sequelize-meta.json |
194 |
| - "migrationStoragePath": "sequelizeMeta.json", |
195 |
| - |
196 |
| - // Use a different table name. Default: SequelizeMeta |
197 |
| - "migrationStorageTableName": "sequelize_meta" |
198 |
| - } |
199 |
| -} |
200 |
| -``` |
201 |
| - |
202 |
| -NOTE: The `none` storage is not recommended as a migration storage. If you decide to use it, be |
203 |
| -aware of the implications of having no record of what migrations did or didn't run. |
204 |
| - |
205 |
| - |
206 |
| -#### Seed |
207 |
| - |
208 |
| -By default the CLI will not save any seed that is executed. If you choose to change this behavior (!), |
209 |
| -you can use `seederStorage` in the configuration file to change the storage type. If you choose `json`, |
210 |
| -you can specify the path of the file using `seederStoragePath` or the CLI will write to the file |
211 |
| -`sequelize-data.json`. If you want to keep the information in the database, using `sequelize`, you can |
212 |
| -specify the table name using `seederStorageTableName`, or it will default to `SequelizeData`. |
213 |
| - |
214 |
| -```json |
215 |
| -{ |
216 |
| - "development": { |
217 |
| - "username": "root", |
218 |
| - "password": null, |
219 |
| - "database": "database_development", |
220 |
| - "host": "127.0.0.1", |
221 |
| - "dialect": "mysql", |
222 |
| - // Use a different storage. Default: none |
223 |
| - "seederStorage": "json", |
224 |
| - // Use a different file name. Default: sequelize-data.json |
225 |
| - "seederStoragePath": "sequelizeData.json", |
226 |
| - // Use a different table name. Default: SequelizeData |
227 |
| - "seederStorageTableName": "sequelize_data" |
228 |
| - } |
229 |
| -} |
230 |
| -``` |
| 69 | +Sequelize CLI is always looking for contributions. You can help us with fixing bugs, reporting bugs or improving documentation. |
231 | 70 |
|
| 71 | +Please read [contributing documentation](CONTRIBUTING.md) |
232 | 72 |
|
233 |
| -### Dialect specific options |
234 |
| - |
235 |
| -In order to pass options to the underlying database connectors, you can add the property `dialectOptions` |
236 |
| -to your configuration like this: |
237 |
| - |
238 |
| -```js |
239 |
| -var fs = require('fs'); |
240 |
| - |
241 |
| -module.exports = { |
242 |
| - development: { |
243 |
| - dialect: 'mysql', |
244 |
| - dialectOptions: { |
245 |
| - ssl: { |
246 |
| - ca: fs.readFileSync(__dirname + '/mysql-ca.crt') |
247 |
| - } |
248 |
| - } |
249 |
| - } |
250 |
| -}; |
251 |
| -``` |
252 |
| - |
253 |
| -### Schema migration |
254 |
| - |
255 |
| -Sequelize CLI continue to use schema from `v2` and fully compatible with `v2`. If you are still using old schema from pre `v2`, use `v2` to upgrade to current schema with `db:migrate:old_schema` |
256 |
| - |
257 |
| -#### Timestamps |
258 |
| - |
259 |
| -Since v2.8.0 the CLI supports a adding timestamps to the schema for saving the executed migrations. You can opt-in for timestamps by running the following command: |
260 |
| - |
261 |
| -```bash |
262 |
| -$ sequelize db:migrate:schema:timestamps:add |
263 |
| -``` |
264 |
| - |
265 |
| -### The migration schema |
266 |
| - |
267 |
| -The CLI uses [umzug](https://github.com/sequelize/umzug) and its migration schema. This means a migration has to look like this: |
268 |
| - |
269 |
| -```js |
270 |
| -"use strict"; |
271 |
| - |
272 |
| -module.exports = { |
273 |
| - up: function(queryInterface, Sequelize, done) { |
274 |
| - done(); |
275 |
| - }, |
276 |
| - |
277 |
| - down: function(queryInterface) { |
278 |
| - return new Promise(function (resolve, reject) { |
279 |
| - resolve(); |
280 |
| - }); |
281 |
| - } |
282 |
| -}; |
283 |
| -``` |
| 73 | +## Sequelize Support |
284 | 74 |
|
285 |
| -Please note that you can either return a Promise or call the third argument of the function once your asynchronous logic was executed. If you pass something to the callback function (the `done` function) it will be treated as erroneous execution. |
| 75 | +CLI v3 fully supports Sequelize v3. Support for Sequelize v4 is still experimental. |
286 | 76 |
|
287 |
| -Additional note: If you need to access the sequelize instance, you can easily do that via `queryInterface.sequelize`. For example `queryInterface.sequelize.query('CREATE TABLE mytable();')`. |
| 77 | +Full support for Sequelize v4 is coming soon with [Sequelize CLI v4](https://github.com/sequelize/cli/issues/441) |
288 | 78 |
|
289 |
| -## Documentation and FAQ |
| 79 | +## Documentation |
290 | 80 |
|
291 |
| -More documentation around migrations [here](http://docs.sequelizejs.com/manual/tutorial/migrations.html). You can find FAQ section [here](https://github.com/sequelize/cli/blob/master/FAQ.md) |
| 81 | +- [Detailed CLI documentation](docs/README.md) |
| 82 | +- [Frequently Asked Questions](docs/FAQ.md) |
| 83 | +- [Query Interface API and Migrations](http://docs.sequelizejs.com/manual/tutorial/migrations.html) |
0 commit comments