rake-db 1.3.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env +3 -0
- package/.env.local +1 -0
- package/README.md +1 -545
- package/db.ts +16 -0
- package/dist/index.d.ts +94 -0
- package/dist/index.esm.js +190 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +201 -0
- package/dist/index.js.map +1 -0
- package/jest-setup.ts +3 -0
- package/migrations/20221009210157_first.ts +8 -0
- package/migrations/20221009210200_second.ts +5 -0
- package/package.json +55 -41
- package/rollup.config.js +3 -0
- package/src/commands/createOrDrop.test.ts +145 -0
- package/src/commands/createOrDrop.ts +107 -0
- package/src/commands/generate.test.ts +133 -0
- package/src/commands/generate.ts +85 -0
- package/src/commands/migrateOrRollback.test.ts +118 -0
- package/src/commands/migrateOrRollback.ts +108 -0
- package/src/common.test.ts +281 -0
- package/src/common.ts +224 -0
- package/src/index.ts +2 -0
- package/src/migration/change.ts +20 -0
- package/src/migration/changeTable.test.ts +417 -0
- package/src/migration/changeTable.ts +375 -0
- package/src/migration/createTable.test.ts +269 -0
- package/src/migration/createTable.ts +169 -0
- package/src/migration/migration.test.ts +341 -0
- package/src/migration/migration.ts +296 -0
- package/src/migration/migrationUtils.ts +281 -0
- package/src/rakeDb.ts +29 -0
- package/src/test-utils.ts +45 -0
- package/tsconfig.json +12 -0
- package/dist/lib/createAndDrop.d.ts +0 -2
- package/dist/lib/createAndDrop.js +0 -63
- package/dist/lib/defaults.d.ts +0 -2
- package/dist/lib/defaults.js +0 -5
- package/dist/lib/errorCodes.d.ts +0 -4
- package/dist/lib/errorCodes.js +0 -7
- package/dist/lib/generate.d.ts +0 -1
- package/dist/lib/generate.js +0 -99
- package/dist/lib/help.d.ts +0 -2
- package/dist/lib/help.js +0 -24
- package/dist/lib/init.d.ts +0 -2
- package/dist/lib/init.js +0 -276
- package/dist/lib/migrate.d.ts +0 -4
- package/dist/lib/migrate.js +0 -189
- package/dist/lib/migration.d.ts +0 -37
- package/dist/lib/migration.js +0 -159
- package/dist/lib/schema/changeTable.d.ts +0 -23
- package/dist/lib/schema/changeTable.js +0 -109
- package/dist/lib/schema/column.d.ts +0 -31
- package/dist/lib/schema/column.js +0 -201
- package/dist/lib/schema/createTable.d.ts +0 -10
- package/dist/lib/schema/createTable.js +0 -53
- package/dist/lib/schema/foreignKey.d.ts +0 -11
- package/dist/lib/schema/foreignKey.js +0 -53
- package/dist/lib/schema/index.d.ts +0 -3
- package/dist/lib/schema/index.js +0 -54
- package/dist/lib/schema/primaryKey.d.ts +0 -9
- package/dist/lib/schema/primaryKey.js +0 -24
- package/dist/lib/schema/table.d.ts +0 -43
- package/dist/lib/schema/table.js +0 -110
- package/dist/lib/schema/timestamps.d.ts +0 -3
- package/dist/lib/schema/timestamps.js +0 -9
- package/dist/lib/utils.d.ts +0 -26
- package/dist/lib/utils.js +0 -114
- package/dist/rake-db.d.ts +0 -2
- package/dist/rake-db.js +0 -34
- package/dist/types.d.ts +0 -94
- package/dist/types.js +0 -40
package/.env
ADDED
package/.env.local
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
DATABASE_URL=postgres://rake-db:@localhost:5432/rake-db
|
package/README.md
CHANGED
|
@@ -1,547 +1,3 @@
|
|
|
1
1
|
# rake-db
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
- Nice syntax similar to RoR migrations
|
|
5
|
-
- It can automatically rollback migrations in common cases
|
|
6
|
-
|
|
7
|
-
Restrictions:
|
|
8
|
-
- Currently it supports only Typescript migrations
|
|
9
|
-
- Only Postgres is supported
|
|
10
|
-
|
|
11
|
-
Can create, drop, migrate and rollback database, and can generate migrations.
|
|
12
|
-
|
|
13
|
-
Has an option to generate names of columns, indices and foreign keys in camelCase.
|
|
14
|
-
|
|
15
|
-
## Table of Contents
|
|
16
|
-
* [Get started](#get-started)
|
|
17
|
-
* [Commands](#commands)
|
|
18
|
-
* [Versioning](#versioning)
|
|
19
|
-
* [Up and down and change](#migration-directions)
|
|
20
|
-
* [All methods and options](#all-methods-and-options)
|
|
21
|
-
- [Create table](#create-table)
|
|
22
|
-
- [Create join table](#create-join-table)
|
|
23
|
-
- [Change table](#change-table)
|
|
24
|
-
- [Drop table](#drop-table)
|
|
25
|
-
- [Drop join table](#drop-join-table)
|
|
26
|
-
- [Rename table](#rename-table)
|
|
27
|
-
- [Change table comment](#change-table-comment)
|
|
28
|
-
- [Add column](#add-column)
|
|
29
|
-
- [Change column](#change-column)
|
|
30
|
-
- [Change column default](#change-column-default)
|
|
31
|
-
- [Change column null](#change-column-null)
|
|
32
|
-
- [Change column comment](#change-column-comment)
|
|
33
|
-
- [Rename column](#rename-column)
|
|
34
|
-
- [Column exists](#column-exists)
|
|
35
|
-
- [Add timestamps](#add-timestamps)
|
|
36
|
-
- [Drop timestamps](#drop-timestamps)
|
|
37
|
-
- [Add foreign key](#add-foreign-key)
|
|
38
|
-
- [Drop foreign key](#drop-foreign-key)
|
|
39
|
-
- [Add index](#add-index)
|
|
40
|
-
- [Drop index](#drop-index)
|
|
41
|
-
|
|
42
|
-
## Get started
|
|
43
|
-
|
|
44
|
-
Install:
|
|
45
|
-
|
|
46
|
-
```sh
|
|
47
|
-
npm i rake-db
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
`rake-db` has peer dependencies on `ts-node` and `typescript`, if you don't have them already in your project need to install:
|
|
51
|
-
|
|
52
|
-
```sh
|
|
53
|
-
npm i ts-node typescript
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
Add a script into `package.json`:
|
|
57
|
-
```json
|
|
58
|
-
{
|
|
59
|
-
"scripts": {
|
|
60
|
-
"db": "rake-db"
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
And now commands become available (`npm run` command can be replaced with `yarn`):
|
|
66
|
-
|
|
67
|
-
```sh
|
|
68
|
-
npm run db init
|
|
69
|
-
npm run db g create_users name:text password:text
|
|
70
|
-
npm run db migrate
|
|
71
|
-
```
|
|
72
|
-
|
|
73
|
-
For quick start run:
|
|
74
|
-
```sh
|
|
75
|
-
npm run db init
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
It will ask you for configs and credentials, then it will create directory for migrations, will create or modify existing `.env` file, will create databases.
|
|
79
|
-
|
|
80
|
-
After completing you'll have such `.env` file, or put values manually:
|
|
81
|
-
```.env
|
|
82
|
-
# Required unless `DATABASES` set
|
|
83
|
-
DATABASE_URL=postgres://username:password@localhost:5432/db-name
|
|
84
|
-
|
|
85
|
-
# Optional: you can define any variables for multiple databases
|
|
86
|
-
DATABASE_URL_TEST=postgres://username:password@localhost:5432/db-name-test
|
|
87
|
-
|
|
88
|
-
# Optional: by default camel case is set to `false`
|
|
89
|
-
DATABASE_CAMEL_CASE=true
|
|
90
|
-
|
|
91
|
-
# Optional: by default is set to `migrations`
|
|
92
|
-
MIGRATIONS_PATH=migrations
|
|
93
|
-
|
|
94
|
-
# Optional unless `DATABASE_URL` is set, this defines a list of databases to migrate at once
|
|
95
|
-
DATABASES=DATABASE_URL,DATABASE_URL_TEST
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
If you specified multiple databases this command will migrate them all at once:
|
|
99
|
-
```sh
|
|
100
|
-
npm run db migrate
|
|
101
|
-
```
|
|
102
|
-
|
|
103
|
-
## Commands
|
|
104
|
-
|
|
105
|
-
Command | Description
|
|
106
|
-
--- | ---
|
|
107
|
-
init | creates migrations directory, sets up env config, creates databases
|
|
108
|
-
create | creates databases
|
|
109
|
-
drop | drops databases
|
|
110
|
-
g, generate | generates migration file, see below
|
|
111
|
-
migrate | migrate all pending migrations in all databases
|
|
112
|
-
rollback | rollback the last migrated in all databases
|
|
113
|
-
no or unknown | prints help
|
|
114
|
-
|
|
115
|
-
Generate arguments:
|
|
116
|
-
- (required) first argument is migration name
|
|
117
|
-
* create_* template for create table
|
|
118
|
-
* change_* template for change table
|
|
119
|
-
* add_* template for add columns
|
|
120
|
-
* remove_* template for remove columns
|
|
121
|
-
* drop_* template for drop table
|
|
122
|
-
|
|
123
|
-
- other arguments considered as columns with types:
|
|
124
|
-
```sh
|
|
125
|
-
npm run db g create_table name:text createdAt:date
|
|
126
|
-
```
|
|
127
|
-
|
|
128
|
-
## Versioning
|
|
129
|
-
|
|
130
|
-
`rake-db` is using special table `schemaMigrations` to store info about which migrations where applied, table will be created unless exists before running migration.
|
|
131
|
-
|
|
132
|
-
Migrations files are generated into `db/migrate` directory:
|
|
133
|
-
|
|
134
|
-
```
|
|
135
|
-
db/
|
|
136
|
-
migrate/
|
|
137
|
-
20200216005003_create_table.ts
|
|
138
|
-
20200223142823_change_table.ts
|
|
139
|
-
```
|
|
140
|
-
|
|
141
|
-
## Writing migration
|
|
142
|
-
|
|
143
|
-
Let's create table:
|
|
144
|
-
|
|
145
|
-
```bash
|
|
146
|
-
npm run db g create_entities name:text
|
|
147
|
-
```
|
|
148
|
-
|
|
149
|
-
It will create such migration file:
|
|
150
|
-
```typescript
|
|
151
|
-
import { Migration } from 'rake-db'
|
|
152
|
-
|
|
153
|
-
export const change = (db: Migration, up: boolean) => {
|
|
154
|
-
db.createTable('entities', (t) => {
|
|
155
|
-
t.text('name')
|
|
156
|
-
t.timestamps()
|
|
157
|
-
})
|
|
158
|
-
}
|
|
159
|
-
```
|
|
160
|
-
|
|
161
|
-
`db` argument inherited from [pg-adapter](https://www.npmjs.com/package/pg-adapter) instance,
|
|
162
|
-
so it has all methods which `pg-adapter` library has, can perform queries.
|
|
163
|
-
|
|
164
|
-
Second argument `up` is boolean which is true for migrate and `false` for rollback.
|
|
165
|
-
|
|
166
|
-
```typescript
|
|
167
|
-
import { Migration } from 'rake-db'
|
|
168
|
-
|
|
169
|
-
export const change = (db: Migration, up: boolean) => {
|
|
170
|
-
db.createTable('entities', (t) => {
|
|
171
|
-
t.text('name')
|
|
172
|
-
t.timestamps()
|
|
173
|
-
})
|
|
174
|
-
|
|
175
|
-
if (up) {
|
|
176
|
-
const data = [{ name: 'one' }, { name: 'two' }]
|
|
177
|
-
db.exec(`INSERT INTO entities(name) VALUES ${
|
|
178
|
-
data.map(row => `(${
|
|
179
|
-
[row.name].map(db.quote).join(', ')
|
|
180
|
-
})`).join(', ')
|
|
181
|
-
}`)
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
This will fill table with data only when migrating up.
|
|
187
|
-
|
|
188
|
-
All migrations are running in transaction so if query contains error - not too bad,
|
|
189
|
-
table won't be created, you can fix error and run again.
|
|
190
|
-
|
|
191
|
-
## Migration directions
|
|
192
|
-
|
|
193
|
-
You can define `export const up` for migrate, `export const down` for rollback or `export const change` for both.
|
|
194
|
-
|
|
195
|
-
## All methods and options
|
|
196
|
-
|
|
197
|
-
In all further examples `db` is a first argument of `change` or `up` or `down`:
|
|
198
|
-
```js
|
|
199
|
-
import { Migration } from 'rake-db'
|
|
200
|
-
|
|
201
|
-
export const change = (db: Migration, up: boolean) => {
|
|
202
|
-
}
|
|
203
|
-
// or
|
|
204
|
-
export const up = (db: Migration) => {
|
|
205
|
-
}
|
|
206
|
-
// or
|
|
207
|
-
export const down = (db: Migration) => {
|
|
208
|
-
}
|
|
209
|
-
```
|
|
210
|
-
|
|
211
|
-
### Create table
|
|
212
|
-
|
|
213
|
-
```js
|
|
214
|
-
db.createTable(
|
|
215
|
-
'table_name',
|
|
216
|
-
{ // optional parameters
|
|
217
|
-
id: false, // skip id column which is created by default
|
|
218
|
-
comment: 'table comment', // add comment to table
|
|
219
|
-
},
|
|
220
|
-
(t) => {
|
|
221
|
-
// See "Add column" for available options
|
|
222
|
-
t.column('column_name', 'column_type', { ...columnOptions })
|
|
223
|
-
t.string('text_column', { ...columnOptions })
|
|
224
|
-
|
|
225
|
-
// column is chainable with methods:
|
|
226
|
-
t.column('name')
|
|
227
|
-
.required() // NOT NULL
|
|
228
|
-
.default('raw sql') // set default value - RAW SQL!
|
|
229
|
-
.default("'string default'") // quote strings manually in default
|
|
230
|
-
.type('integer') // can set type
|
|
231
|
-
.index() // add default index
|
|
232
|
-
.index({ ...indexOptions }) // see addIndex section for options
|
|
233
|
-
.unique() // add unique index
|
|
234
|
-
.unique({ ...indexOptions }) // can accept options
|
|
235
|
-
.comment('this is awesome column') // add comment to column
|
|
236
|
-
.length(42) // can set length, for varchar for example
|
|
237
|
-
.precision(10).scale(5) // precision and scale for numeric types
|
|
238
|
-
.collate('value'), // see COLLATE in postgres docs
|
|
239
|
-
.using('value'), // see USING in postgres docs
|
|
240
|
-
.references('table', 'column') // add REFERENCES statement
|
|
241
|
-
.onUpdate('cascade') // ON UPDATE for REFERENCES
|
|
242
|
-
.onDelete('cascade') // ON DELETE for REFERENCES
|
|
243
|
-
|
|
244
|
-
// See "Add index"
|
|
245
|
-
t.index('column_name', { unique: true })
|
|
246
|
-
|
|
247
|
-
// See "Add timestamps"
|
|
248
|
-
t.timestamps()
|
|
249
|
-
|
|
250
|
-
// set composite primary key
|
|
251
|
-
f.primaryKey(['column1', 'column2'])
|
|
252
|
-
|
|
253
|
-
// See "Add foreign key"
|
|
254
|
-
t.foreignKey({
|
|
255
|
-
table: 'some',
|
|
256
|
-
column: 'some_id',
|
|
257
|
-
references: 'id'
|
|
258
|
-
})
|
|
259
|
-
|
|
260
|
-
t.execute('custom sql inside of CREATE TABLE ()')
|
|
261
|
-
|
|
262
|
-
// type helpers, the same as t.column(name, *type*, options)
|
|
263
|
-
t.bigint('column_name', ColumnOptions)
|
|
264
|
-
t.bigserial('column_name', ColumnOptions)
|
|
265
|
-
t.boolean('column_name', ColumnOptions)
|
|
266
|
-
t.date('column_name', ColumnOptions)
|
|
267
|
-
t.decimal('column_name', ColumnOptions)
|
|
268
|
-
t.float('column_name', ColumnOptions)
|
|
269
|
-
t.integer('column_name', ColumnOptions)
|
|
270
|
-
t.text('column_name', ColumnOptions)
|
|
271
|
-
t.smallint('column_name', ColumnOptions)
|
|
272
|
-
t.string('column_name', ColumnOptions)
|
|
273
|
-
t.time('column_name', ColumnOptions)
|
|
274
|
-
t.timestamp('column_name', ColumnOptions)
|
|
275
|
-
t.timestamptz('column_name', ColumnOptions)
|
|
276
|
-
t.binary('column_name', ColumnOptions)
|
|
277
|
-
t.serial('column_name', ColumnOptions)
|
|
278
|
-
t.json('column_name', ColumnOptions)
|
|
279
|
-
t.jsonb('column_name', ColumnOptions)
|
|
280
|
-
},
|
|
281
|
-
)
|
|
282
|
-
```
|
|
283
|
-
|
|
284
|
-
### Create join table
|
|
285
|
-
|
|
286
|
-
If you prefer plural table names skip this section, as this method not supports it for now.
|
|
287
|
-
|
|
288
|
-
```js
|
|
289
|
-
db.createJoinTable(
|
|
290
|
-
'orange',
|
|
291
|
-
'apple',
|
|
292
|
-
{ // optional parameters
|
|
293
|
-
// by default table name will be sorted join of two table names, i.e "apples_oranges"
|
|
294
|
-
tableName: string,
|
|
295
|
-
// by default true, will add index to ensure unique id pairs
|
|
296
|
-
unique: boolean,
|
|
297
|
-
// by default is true, will set REFERENCES for both columns
|
|
298
|
-
references: boolean,
|
|
299
|
-
columnOptions: {
|
|
300
|
-
// by default id columns are NOT NULL
|
|
301
|
-
null: boolean,
|
|
302
|
-
// if set to true will create index for each column
|
|
303
|
-
index: boolean
|
|
304
|
-
// you can provide and other column options too
|
|
305
|
-
},
|
|
306
|
-
},
|
|
307
|
-
(t) => { // optional callback with same methods as in createTable
|
|
308
|
-
}
|
|
309
|
-
)
|
|
310
|
-
```
|
|
311
|
-
|
|
312
|
-
Example:
|
|
313
|
-
```js
|
|
314
|
-
db.createJoinTable('orange', 'apple')
|
|
315
|
-
```
|
|
316
|
-
Will result in such sql:
|
|
317
|
-
```sql
|
|
318
|
-
CREATE TABLE "apple_orange" (
|
|
319
|
-
"apple_id" integer NOT NULL REFERENCES "apple" ("id"),
|
|
320
|
-
"orange_id" integer NOT NULL REFERENCES "orange" ("id")
|
|
321
|
-
);
|
|
322
|
-
CREATE UNIQUE INDEX "apple_orange_unique_index" ON "apple_orange" ("apple_id", "orange_id");
|
|
323
|
-
```
|
|
324
|
-
|
|
325
|
-
### Change table
|
|
326
|
-
|
|
327
|
-
```js
|
|
328
|
-
db.changeTable('table_name', (t) => {
|
|
329
|
-
// adding new column, index, foreign key - all the same as in createTable
|
|
330
|
-
t.column('name', 'type', { ...options })
|
|
331
|
-
|
|
332
|
-
// column is chainable just as in createTable
|
|
333
|
-
t.string('text_column').required().primaryKey().references('other_table', 'column')
|
|
334
|
-
|
|
335
|
-
t.change('column_name', {
|
|
336
|
-
type: 'integer', // change column type
|
|
337
|
-
default: 42, // set new default
|
|
338
|
-
default: null, // remove default
|
|
339
|
-
null: false, // set NOT NULL
|
|
340
|
-
index: true | IndexOptions, // add index for column, for options see "Add index"
|
|
341
|
-
comment: 'column comment', // add comment
|
|
342
|
-
})
|
|
343
|
-
|
|
344
|
-
t.rename('old_column_name', 'new_column_name') // rename column
|
|
345
|
-
|
|
346
|
-
t.comment('column_name', 'comment') // add comment to the column
|
|
347
|
-
|
|
348
|
-
t.default('column_name', 'new default value') // change default value
|
|
349
|
-
t.default('column_name', null) // remove default value
|
|
350
|
-
|
|
351
|
-
// Drop primary key
|
|
352
|
-
f.dropPrimaryKey(['column1', 'column2'])
|
|
353
|
-
|
|
354
|
-
// Add composite primary key
|
|
355
|
-
f.primaryKey(['column1', 'column2'])
|
|
356
|
-
|
|
357
|
-
t.drop('column_name', 'type', ColumnOptions) // drop column, type and options are for rolling back
|
|
358
|
-
|
|
359
|
-
t.dropIndex('index_name', IndexOptions) // drop index, see "Add index" for options
|
|
360
|
-
|
|
361
|
-
t.dropForeignKey(ForeignKeyOptions) // drop foreign key, see "Add foreign key" for options
|
|
362
|
-
})
|
|
363
|
-
```
|
|
364
|
-
|
|
365
|
-
### Drop table
|
|
366
|
-
|
|
367
|
-
```js
|
|
368
|
-
db.dropTable('table_name')
|
|
369
|
-
|
|
370
|
-
// to make migration reversible provide the same callback as for createTable:
|
|
371
|
-
db.dropTable('table_name', (t) => {
|
|
372
|
-
t.string('column')
|
|
373
|
-
})
|
|
374
|
-
```
|
|
375
|
-
|
|
376
|
-
### Drop join table
|
|
377
|
-
|
|
378
|
-
```js
|
|
379
|
-
// the same parameters as in addJoinTable
|
|
380
|
-
db.dropJoinTable('apple', 'orange')
|
|
381
|
-
```
|
|
382
|
-
|
|
383
|
-
### Rename table
|
|
384
|
-
|
|
385
|
-
```js
|
|
386
|
-
db.renameTable('old_name', 'new_name')
|
|
387
|
-
```
|
|
388
|
-
|
|
389
|
-
### Change table comment
|
|
390
|
-
|
|
391
|
-
Looks like this method is irreversible, but who use comments at all? :)
|
|
392
|
-
```js
|
|
393
|
-
db.changeTableComment('table_name', 'new table comment')
|
|
394
|
-
```
|
|
395
|
-
|
|
396
|
-
### Table exists
|
|
397
|
-
|
|
398
|
-
```js
|
|
399
|
-
if (await !db.tableExists('table')) {
|
|
400
|
-
await db.createTable('table')
|
|
401
|
-
}
|
|
402
|
-
```
|
|
403
|
-
|
|
404
|
-
### Add column
|
|
405
|
-
|
|
406
|
-
```js
|
|
407
|
-
db.addColumn('table', 'column_name', 'type', {
|
|
408
|
-
// all fields are optional
|
|
409
|
-
primaryKey: boolean, // if column should be a primary key
|
|
410
|
-
default: 'default value', // set default
|
|
411
|
-
null: false, // add NOT NULL statement
|
|
412
|
-
index: boolean | IndexOptions // add index for the column, for options see "Add index"
|
|
413
|
-
comment: string, // add comment on the column
|
|
414
|
-
mode: string, // RESTRICT or CASCADE to use in rollback
|
|
415
|
-
unique: boolean, // add unique index
|
|
416
|
-
length: number | string, // specify type size, for varchar for example
|
|
417
|
-
precision: number | string, // for numeric
|
|
418
|
-
scale: number | string, // for numeric
|
|
419
|
-
collate: string, // for completeness, see COLLATE in postgres docs
|
|
420
|
-
using: string, // for completeness as well
|
|
421
|
-
})
|
|
422
|
-
```
|
|
423
|
-
|
|
424
|
-
### Change column
|
|
425
|
-
|
|
426
|
-
```js
|
|
427
|
-
db.changeColumn('table_name', 'column_name', {
|
|
428
|
-
type: 'integer', // change column type
|
|
429
|
-
default: 42, // set new default
|
|
430
|
-
default: null, // remove default
|
|
431
|
-
null: false, // set NOT NULL
|
|
432
|
-
index: true | IndexOptions, // add index for column, for options see "Add index"
|
|
433
|
-
comment: 'column comment', // add comment
|
|
434
|
-
})
|
|
435
|
-
```
|
|
436
|
-
|
|
437
|
-
### Change column default
|
|
438
|
-
|
|
439
|
-
```js
|
|
440
|
-
db.changeColumnDefault('table', 'column', 'new default value')
|
|
441
|
-
db.changeColumnDefault('table', 'column', null) // to remove default
|
|
442
|
-
```
|
|
443
|
-
|
|
444
|
-
### Change column null
|
|
445
|
-
|
|
446
|
-
```js
|
|
447
|
-
db.changeColumnNull('table', 'column', boolean) // true for NOT NULL
|
|
448
|
-
```
|
|
449
|
-
|
|
450
|
-
### Change column comment
|
|
451
|
-
|
|
452
|
-
```js
|
|
453
|
-
db.changeColumnComment('table', 'column', 'comment')
|
|
454
|
-
```
|
|
455
|
-
|
|
456
|
-
### Rename column
|
|
457
|
-
|
|
458
|
-
```js
|
|
459
|
-
db.renameColumn('table', 'old_column_name', 'new_column_name')
|
|
460
|
-
```
|
|
461
|
-
|
|
462
|
-
### Column exists
|
|
463
|
-
|
|
464
|
-
```js
|
|
465
|
-
if (!db.columnExists('table', 'column'))
|
|
466
|
-
db.addColumn('table', 'column', 'type')
|
|
467
|
-
```
|
|
468
|
-
|
|
469
|
-
### Add timestamps
|
|
470
|
-
|
|
471
|
-
This will add `updated_at` and `created_at`, both with default 'now()'
|
|
472
|
-
|
|
473
|
-
```js
|
|
474
|
-
db.addTimestamps('table', options) // See "Add column" for options
|
|
475
|
-
```
|
|
476
|
-
|
|
477
|
-
### Drop timestamps
|
|
478
|
-
|
|
479
|
-
```js
|
|
480
|
-
db.dropTimestamps('table', options)
|
|
481
|
-
```
|
|
482
|
-
|
|
483
|
-
### Add primary key
|
|
484
|
-
|
|
485
|
-
Add composite primary key, will create constraint with name `table_pkey`
|
|
486
|
-
|
|
487
|
-
Optionally can take second argument for custom constraint name
|
|
488
|
-
|
|
489
|
-
```js
|
|
490
|
-
db.addPrimaryKey('table', ['column1', 'column2'])
|
|
491
|
-
db.addPrimaryKey('table', ['column1', 'column2'], 'customConstraintName')
|
|
492
|
-
```
|
|
493
|
-
|
|
494
|
-
### Drop primary key
|
|
495
|
-
|
|
496
|
-
The same arguments as when adding primary key so migration is reversible
|
|
497
|
-
|
|
498
|
-
```js
|
|
499
|
-
db.dropPrimaryKey(['column1', 'column2'])
|
|
500
|
-
db.dropPrimaryKey(['column1', 'column2'], 'customConstraintName')
|
|
501
|
-
```
|
|
502
|
-
|
|
503
|
-
### Add foreign key
|
|
504
|
-
|
|
505
|
-
```js
|
|
506
|
-
db.addForeignKey('table', {
|
|
507
|
-
// required
|
|
508
|
-
column: 'some_id', // a foreign key column
|
|
509
|
-
table: 'other_table', // table to connect this column to
|
|
510
|
-
references: 'column_in_other_table', // column in other table to connect
|
|
511
|
-
|
|
512
|
-
// optional
|
|
513
|
-
name: 'custom_fkey_name', // custom name of constraint
|
|
514
|
-
onUpdate: 'CASCADE', // action to perform on update
|
|
515
|
-
onDelete: 'CASCADE', // action to perform on delete
|
|
516
|
-
index: true | IndexOptions // add index on the column
|
|
517
|
-
})
|
|
518
|
-
```
|
|
519
|
-
|
|
520
|
-
### Drop foreign key
|
|
521
|
-
|
|
522
|
-
```js
|
|
523
|
-
db.dropForeignKey('table', ForeignKeyOptions)
|
|
524
|
-
```
|
|
525
|
-
|
|
526
|
-
### Add index
|
|
527
|
-
|
|
528
|
-
```js
|
|
529
|
-
db.addIndex('table', 'column', {
|
|
530
|
-
name: string, // index name
|
|
531
|
-
unique: boolean, // whether the index should be unique
|
|
532
|
-
expression: number | string, // for this syntax: CREATE INDEX name ON table ( column( expression ) )
|
|
533
|
-
order: string, // index order: ASC, DESC, NULLS FIRST/LAST
|
|
534
|
-
using: string, // index method
|
|
535
|
-
including: string, // for INCLUDING statement
|
|
536
|
-
with: string, // for WITH statement
|
|
537
|
-
tablespace: string, // for TABLESPACE
|
|
538
|
-
where: string, // filter rows for the index with query
|
|
539
|
-
mode: string, // RESTRICT | CASCADE for rollback
|
|
540
|
-
})
|
|
541
|
-
```
|
|
542
|
-
|
|
543
|
-
### Drop index
|
|
544
|
-
|
|
545
|
-
```js
|
|
546
|
-
db.dropIndex('table', 'column', options)
|
|
547
|
-
```
|
|
3
|
+
Docs are coming soon
|
package/db.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { config } from 'dotenv';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { rakeDb } from './src/rakeDb';
|
|
4
|
+
|
|
5
|
+
config({ path: path.resolve(process.cwd(), '.env.local') });
|
|
6
|
+
config();
|
|
7
|
+
|
|
8
|
+
const connectionString = process.env.DATABASE_URL;
|
|
9
|
+
if (!connectionString) {
|
|
10
|
+
throw new Error('DATABASE_URL is missing in .env');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
rakeDb(
|
|
14
|
+
{ connectionString },
|
|
15
|
+
{ migrationsPath: path.resolve(process.cwd(), 'migrations') },
|
|
16
|
+
);
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { MaybeArray, AdapterOptions, ColumnTypes, EmptyObject, RawExpression, ColumnType, TransactionAdapter, IndexColumnOptions, IndexOptions, ForeignKeyOptions, ColumnsShape } from 'pqb';
|
|
2
|
+
|
|
3
|
+
declare type MigrationConfig = {
|
|
4
|
+
migrationsPath: string;
|
|
5
|
+
migrationsTable: string;
|
|
6
|
+
requireTs(path: string): void;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
declare const createDb: (arg: MaybeArray<AdapterOptions>, config: MigrationConfig) => Promise<void>;
|
|
10
|
+
declare const dropDb: (arg: MaybeArray<AdapterOptions>) => Promise<void>;
|
|
11
|
+
|
|
12
|
+
declare function add(item: ColumnType, options?: {
|
|
13
|
+
dropMode?: DropMode;
|
|
14
|
+
}): ChangeItem;
|
|
15
|
+
declare function add(emptyObject: EmptyObject): EmptyObject;
|
|
16
|
+
declare function add(items: Record<string, ColumnType>, options?: {
|
|
17
|
+
dropMode?: DropMode;
|
|
18
|
+
}): Record<string, ChangeItem>;
|
|
19
|
+
declare type ChangeOptions = {
|
|
20
|
+
usingUp?: RawExpression;
|
|
21
|
+
usingDown?: RawExpression;
|
|
22
|
+
};
|
|
23
|
+
declare type ChangeArg = ColumnType | ['default', unknown | RawExpression] | ['nullable', boolean] | ['comment', string | null];
|
|
24
|
+
declare type TableChangeMethods = typeof tableChangeMethods;
|
|
25
|
+
declare const tableChangeMethods: {
|
|
26
|
+
add: typeof add;
|
|
27
|
+
drop: typeof add;
|
|
28
|
+
change(from: ChangeArg, to: ChangeArg, options?: ChangeOptions): ChangeItem;
|
|
29
|
+
default(value: unknown | RawExpression): ChangeArg;
|
|
30
|
+
nullable(): ChangeArg;
|
|
31
|
+
nonNullable(): ChangeArg;
|
|
32
|
+
comment(name: string | null): ChangeArg;
|
|
33
|
+
rename(name: string): ChangeItem;
|
|
34
|
+
};
|
|
35
|
+
declare type ChangeItem = [
|
|
36
|
+
action: 'add' | 'drop',
|
|
37
|
+
item: ColumnType,
|
|
38
|
+
options?: {
|
|
39
|
+
dropMode?: DropMode;
|
|
40
|
+
}
|
|
41
|
+
] | [action: 'change', from: ChangeArg, to: ChangeArg, options?: ChangeOptions] | ['rename', string];
|
|
42
|
+
declare type TableChanger = ColumnTypes & TableChangeMethods;
|
|
43
|
+
declare type TableChangeData = Record<string, ChangeItem | EmptyObject>;
|
|
44
|
+
|
|
45
|
+
declare type DropMode = 'CASCADE' | 'RESTRICT';
|
|
46
|
+
declare type TableOptions = {
|
|
47
|
+
dropMode?: DropMode;
|
|
48
|
+
comment?: string;
|
|
49
|
+
};
|
|
50
|
+
declare type ColumnsShapeCallback = (t: ColumnTypes) => ColumnsShape;
|
|
51
|
+
declare type ChangeTableOptions = {
|
|
52
|
+
comment?: string | [string, string] | null;
|
|
53
|
+
};
|
|
54
|
+
declare type ChangeTableCallback = (t: TableChanger) => TableChangeData;
|
|
55
|
+
declare type JoinTableOptions = {
|
|
56
|
+
tableName?: string;
|
|
57
|
+
comment?: string;
|
|
58
|
+
dropMode?: DropMode;
|
|
59
|
+
};
|
|
60
|
+
declare class Migration extends TransactionAdapter {
|
|
61
|
+
up: boolean;
|
|
62
|
+
constructor(tx: TransactionAdapter, up: boolean);
|
|
63
|
+
createTable(tableName: string, options: TableOptions, fn: ColumnsShapeCallback): Promise<void>;
|
|
64
|
+
createTable(tableName: string, fn: ColumnsShapeCallback): Promise<void>;
|
|
65
|
+
dropTable(tableName: string, options: TableOptions, fn: ColumnsShapeCallback): Promise<void>;
|
|
66
|
+
dropTable(tableName: string, fn: ColumnsShapeCallback): Promise<void>;
|
|
67
|
+
createJoinTable(tables: string[], options?: JoinTableOptions, fn?: ColumnsShapeCallback): Promise<void>;
|
|
68
|
+
createJoinTable(tables: string[], fn?: ColumnsShapeCallback): Promise<void>;
|
|
69
|
+
dropJoinTable(tables: string[], options?: JoinTableOptions, fn?: ColumnsShapeCallback): Promise<void>;
|
|
70
|
+
dropJoinTable(tables: string[], fn?: ColumnsShapeCallback): Promise<void>;
|
|
71
|
+
changeTable(tableName: string, options: ChangeTableOptions, fn?: ChangeTableCallback): Promise<void>;
|
|
72
|
+
changeTable(tableName: string, fn: ChangeTableCallback): Promise<void>;
|
|
73
|
+
renameTable(from: string, to: string): Promise<void>;
|
|
74
|
+
addColumn(tableName: string, columnName: string, fn: (t: ColumnTypes) => ColumnType): Promise<void>;
|
|
75
|
+
dropColumn(tableName: string, columnName: string, fn: (t: ColumnTypes) => ColumnType): Promise<void>;
|
|
76
|
+
addIndex(tableName: string, columns: MaybeArray<string | IndexColumnOptions>, options?: IndexOptions): Promise<void>;
|
|
77
|
+
dropIndex(tableName: string, columns: MaybeArray<string | IndexColumnOptions>, options?: IndexOptions): Promise<void>;
|
|
78
|
+
addForeignKey(tableName: string, columns: [string, ...string[]], foreignTable: string, foreignColumns: [string, ...string[]], options?: ForeignKeyOptions): Promise<void>;
|
|
79
|
+
dropForeignKey(tableName: string, columns: [string, ...string[]], foreignTable: string, foreignColumns: [string, ...string[]], options?: ForeignKeyOptions): Promise<void>;
|
|
80
|
+
addPrimaryKey(tableName: string, columns: string[], options?: {
|
|
81
|
+
name?: string;
|
|
82
|
+
}): Promise<void>;
|
|
83
|
+
dropPrimaryKey(tableName: string, columns: string[], options?: {
|
|
84
|
+
name?: string;
|
|
85
|
+
}): Promise<void>;
|
|
86
|
+
renameColumn(tableName: string, from: string, to: string): Promise<void>;
|
|
87
|
+
tableExists(tableName: string): Promise<boolean>;
|
|
88
|
+
columnExists(tableName: string, columnName: string): Promise<boolean>;
|
|
89
|
+
constraintExists(constraintName: string): Promise<boolean>;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
declare const change: (fn: (db: Migration, up: boolean) => Promise<void>) => void;
|
|
93
|
+
|
|
94
|
+
export { change, createDb, dropDb };
|