rake-db 2.10.12 → 2.10.14

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/dist/index.d.ts CHANGED
@@ -37,6 +37,8 @@ type TableChangeData = Record<string, RakeDbAst.ChangeTableItem.Column | RakeDbA
37
37
 
38
38
  type DropMode = 'CASCADE' | 'RESTRICT';
39
39
  type TableOptions = {
40
+ createIfNotExists?: boolean;
41
+ dropIfExists?: boolean;
40
42
  dropMode?: DropMode;
41
43
  comment?: string;
42
44
  noPrimaryKey?: boolean;
@@ -102,6 +104,8 @@ declare class Migration<CT extends ColumnTypesBase> {
102
104
  *
103
105
  * `dropTable` accepts the same arguments, it will drop the table when migrating and create a table when rolling back.
104
106
  *
107
+ * To create an empty table, the callback with columns may be omitted.
108
+ *
105
109
  * When creating a table within a specific schema, write the table name with schema name: `'schemaName.tableName'`.
106
110
  *
107
111
  * Returns object `{ table: TableInterface }` that allows to insert records right after creating a table.
@@ -110,6 +114,12 @@ declare class Migration<CT extends ColumnTypesBase> {
110
114
  *
111
115
  * ```ts
112
116
  * type TableOptions = {
117
+ * // create the table only if it not exists already
118
+ * createIfNotExists?: boolean;
119
+ *
120
+ * // drop the table only if it exists
121
+ * dropIfExists?: boolean;
122
+ *
113
123
  * // used when reverting a `createTable`
114
124
  * dropMode?: 'CASCADE' | 'RESTRICT';
115
125
  *
@@ -164,30 +174,30 @@ declare class Migration<CT extends ColumnTypesBase> {
164
174
  * @param tableName - name of the table to create
165
175
  * @param fn - create table callback
166
176
  */
167
- createTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, fn: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
177
+ createTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, fn?: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
168
178
  /**
169
179
  * See {@link createTable}
170
180
  *
171
181
  * @param tableName - name of the table to create
172
- * @param options - create table options
182
+ * @param options - {@link TableOptions}
173
183
  * @param fn - create table callback
174
184
  */
175
- createTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, options: TableOptions, fn: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
185
+ createTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, options: TableOptions, fn?: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
176
186
  /**
177
187
  * Drop the table, create it on rollback. See {@link createTable}.
178
188
  *
179
189
  * @param tableName - name of the table to drop
180
190
  * @param fn - create table callback
181
191
  */
182
- dropTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, fn: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
192
+ dropTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, fn?: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
183
193
  /**
184
194
  * Drop the table, create it on rollback. See {@link createTable}.
185
195
  *
186
196
  * @param tableName - name of the table to drop
187
- * @param options - create table options
197
+ * @param options - {@link TableOptions}
188
198
  * @param fn - create table callback
189
199
  */
190
- dropTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, options: TableOptions, fn: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
200
+ dropTable<Table extends string, Shape extends ColumnsShape>(tableName: Table, options: TableOptions, fn?: ColumnsShapeCallback<CT, Shape>): Promise<CreateTableResult<Table, Shape>>;
191
201
  /**
192
202
  * `changeTable` accepts a table name, optional options, and a special callback with column changes.
193
203
  *
@@ -794,6 +804,8 @@ declare namespace RakeDbAst {
794
804
  name: string;
795
805
  shape: ColumnsShape;
796
806
  noPrimaryKey: NoPrimaryKeyOption;
807
+ createIfNotExists?: boolean;
808
+ dropIfExists?: boolean;
797
809
  dropMode?: DropMode;
798
810
  comment?: string;
799
811
  } & TableData;
package/dist/index.js CHANGED
@@ -609,12 +609,7 @@ const createTable$1 = async (migration, up, tableName, options, fn) => {
609
609
  tableMethods
610
610
  );
611
611
  types[orchidCore.snakeCaseKey] = snakeCase;
612
- const shape = pqb.getColumnTypes(
613
- types,
614
- fn,
615
- (_a = migration.options.baseTable) == null ? void 0 : _a.nowSQL,
616
- language
617
- );
612
+ const shape = !fn ? orchidCore.emptyObject : pqb.getColumnTypes(types, fn, (_a = migration.options.baseTable) == null ? void 0 : _a.nowSQL, language);
618
613
  const tableData = pqb.getTableData();
619
614
  const ast = makeAst$2(
620
615
  up,
@@ -624,7 +619,7 @@ const createTable$1 = async (migration, up, tableName, options, fn) => {
624
619
  options,
625
620
  migration.options.noPrimaryKey
626
621
  );
627
- validatePrimaryKey(ast);
622
+ fn && validatePrimaryKey(ast);
628
623
  const queries = astToQueries$1(ast, snakeCase, language);
629
624
  for (const _b of queries) {
630
625
  const _c = _b, { then } = _c, query = __objRest$1(_c, ["then"]);
@@ -705,7 +700,7 @@ const astToQueries$1 = (ast, snakeCase, language) => {
705
700
  }
706
701
  if (ast.action === "drop") {
707
702
  queries.push({
708
- text: `DROP TABLE ${quoteWithSchema(ast)}${ast.dropMode ? ` ${ast.dropMode}` : ""}`
703
+ text: `DROP TABLE${ast.dropIfExists ? " IF EXISTS" : ""} ${quoteWithSchema(ast)}${ast.dropMode ? ` ${ast.dropMode}` : ""}`
709
704
  });
710
705
  return queries;
711
706
  }
@@ -761,7 +756,7 @@ const astToQueries$1 = (ast, snakeCase, language) => {
761
756
  );
762
757
  queries.push(
763
758
  {
764
- text: `CREATE TABLE ${quoteWithSchema(ast)} (${lines.join(",")}
759
+ text: `CREATE TABLE${ast.createIfNotExists ? " IF NOT EXISTS" : ""} ${quoteWithSchema(ast)} (${lines.join(",")}
765
760
  )`,
766
761
  values
767
762
  },
@@ -1372,12 +1367,12 @@ const createMigrationInterface = (tx, up, config) => {
1372
1367
  };
1373
1368
  class Migration {
1374
1369
  createTable(tableName, cbOrOptions, cb) {
1375
- const options = typeof cbOrOptions === "function" ? {} : cbOrOptions;
1370
+ const options = !cbOrOptions || typeof cbOrOptions === "function" ? {} : cbOrOptions;
1376
1371
  const fn = cb || cbOrOptions;
1377
1372
  return createTable$1(this, this.up, tableName, options, fn);
1378
1373
  }
1379
1374
  dropTable(tableName, cbOrOptions, cb) {
1380
- const options = typeof cbOrOptions === "function" ? {} : cbOrOptions;
1375
+ const options = !cbOrOptions || typeof cbOrOptions === "function" ? {} : cbOrOptions;
1381
1376
  const fn = cb || cbOrOptions;
1382
1377
  return createTable$1(this, !this.up, tableName, options, fn);
1383
1378
  }