knex 0.21.20 → 0.21.21

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.
Files changed (141) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/CONTRIBUTING.md +184 -184
  3. package/LICENSE +22 -22
  4. package/README.md +95 -95
  5. package/bin/cli.js +414 -414
  6. package/bin/utils/cli-config-utils.js +151 -151
  7. package/bin/utils/constants.js +7 -7
  8. package/bin/utils/migrationsLister.js +37 -37
  9. package/knex.js +8 -8
  10. package/lib/client.js +413 -413
  11. package/lib/config-resolver.js +61 -61
  12. package/lib/constants.js +44 -44
  13. package/lib/dialects/mssql/index.js +390 -390
  14. package/lib/dialects/mssql/query/compiler.js +444 -444
  15. package/lib/dialects/mssql/schema/columncompiler.js +103 -103
  16. package/lib/dialects/mssql/schema/compiler.js +59 -59
  17. package/lib/dialects/mssql/schema/tablecompiler.js +245 -245
  18. package/lib/dialects/mssql/transaction.js +97 -97
  19. package/lib/dialects/mysql/index.js +191 -191
  20. package/lib/dialects/mysql/query/compiler.js +142 -142
  21. package/lib/dialects/mysql/schema/columncompiler.js +171 -171
  22. package/lib/dialects/mysql/schema/compiler.js +60 -60
  23. package/lib/dialects/mysql/schema/tablecompiler.js +262 -262
  24. package/lib/dialects/mysql/transaction.js +48 -48
  25. package/lib/dialects/mysql2/index.js +35 -35
  26. package/lib/dialects/mysql2/transaction.js +46 -46
  27. package/lib/dialects/oracle/DEAD_CODE.md +5 -5
  28. package/lib/dialects/oracle/formatter.js +20 -20
  29. package/lib/dialects/oracle/index.js +79 -79
  30. package/lib/dialects/oracle/query/compiler.js +327 -327
  31. package/lib/dialects/oracle/schema/columnbuilder.js +18 -18
  32. package/lib/dialects/oracle/schema/columncompiler.js +139 -139
  33. package/lib/dialects/oracle/schema/compiler.js +81 -81
  34. package/lib/dialects/oracle/schema/tablecompiler.js +165 -165
  35. package/lib/dialects/oracle/schema/trigger.js +126 -126
  36. package/lib/dialects/oracle/utils.js +86 -86
  37. package/lib/dialects/oracledb/index.js +489 -489
  38. package/lib/dialects/oracledb/query/compiler.js +363 -363
  39. package/lib/dialects/oracledb/schema/columncompiler.js +35 -35
  40. package/lib/dialects/oracledb/transaction.js +76 -76
  41. package/lib/dialects/oracledb/utils.js +14 -14
  42. package/lib/dialects/postgres/index.js +319 -319
  43. package/lib/dialects/postgres/query/compiler.js +206 -206
  44. package/lib/dialects/postgres/schema/columncompiler.js +125 -125
  45. package/lib/dialects/postgres/schema/compiler.js +109 -109
  46. package/lib/dialects/postgres/schema/tablecompiler.js +183 -183
  47. package/lib/dialects/redshift/index.js +73 -73
  48. package/lib/dialects/redshift/query/compiler.js +119 -119
  49. package/lib/dialects/redshift/schema/columnbuilder.js +20 -20
  50. package/lib/dialects/redshift/schema/columncompiler.js +60 -60
  51. package/lib/dialects/redshift/schema/compiler.js +14 -14
  52. package/lib/dialects/redshift/schema/tablecompiler.js +123 -123
  53. package/lib/dialects/redshift/transaction.js +18 -18
  54. package/lib/dialects/sqlite3/formatter.js +21 -21
  55. package/lib/dialects/sqlite3/index.js +169 -169
  56. package/lib/dialects/sqlite3/query/compiler.js +222 -222
  57. package/lib/dialects/sqlite3/schema/columncompiler.js +27 -27
  58. package/lib/dialects/sqlite3/schema/compiler.js +49 -49
  59. package/lib/dialects/sqlite3/schema/ddl.js +525 -525
  60. package/lib/dialects/sqlite3/schema/tablecompiler.js +238 -238
  61. package/lib/formatter.js +295 -295
  62. package/lib/functionhelper.js +14 -14
  63. package/lib/helpers.js +92 -92
  64. package/lib/index.js +3 -3
  65. package/lib/interface.js +115 -115
  66. package/lib/knex.js +42 -42
  67. package/lib/logger.js +76 -76
  68. package/lib/migrate/MigrationGenerator.js +82 -82
  69. package/lib/migrate/Migrator.js +611 -611
  70. package/lib/migrate/configuration-merger.js +60 -60
  71. package/lib/migrate/migrate-stub.js +17 -17
  72. package/lib/migrate/migration-list-resolver.js +36 -36
  73. package/lib/migrate/sources/fs-migrations.js +99 -99
  74. package/lib/migrate/stub/cjs.stub +15 -15
  75. package/lib/migrate/stub/coffee.stub +13 -13
  76. package/lib/migrate/stub/eg.stub +14 -14
  77. package/lib/migrate/stub/js.stub +15 -15
  78. package/lib/migrate/stub/knexfile-coffee.stub +34 -34
  79. package/lib/migrate/stub/knexfile-eg.stub +43 -43
  80. package/lib/migrate/stub/knexfile-js.stub +44 -44
  81. package/lib/migrate/stub/knexfile-ls.stub +35 -35
  82. package/lib/migrate/stub/knexfile-ts.stub +44 -44
  83. package/lib/migrate/stub/ls.stub +14 -14
  84. package/lib/migrate/stub/ts.stub +21 -21
  85. package/lib/migrate/table-creator.js +67 -67
  86. package/lib/migrate/table-resolver.js +27 -27
  87. package/lib/query/builder.js +1372 -1372
  88. package/lib/query/compiler.js +889 -889
  89. package/lib/query/constants.js +13 -13
  90. package/lib/query/joinclause.js +263 -263
  91. package/lib/query/methods.js +92 -92
  92. package/lib/query/string.js +190 -190
  93. package/lib/raw.js +188 -188
  94. package/lib/ref.js +39 -39
  95. package/lib/runner.js +285 -285
  96. package/lib/schema/builder.js +82 -82
  97. package/lib/schema/columnbuilder.js +117 -117
  98. package/lib/schema/columncompiler.js +177 -177
  99. package/lib/schema/compiler.js +101 -101
  100. package/lib/schema/helpers.js +51 -51
  101. package/lib/schema/tablebuilder.js +288 -288
  102. package/lib/schema/tablecompiler.js +296 -296
  103. package/lib/seed/Seeder.js +203 -203
  104. package/lib/seed/seed-stub.js +13 -13
  105. package/lib/seed/stub/coffee.stub +9 -9
  106. package/lib/seed/stub/eg.stub +11 -11
  107. package/lib/seed/stub/js.stub +13 -13
  108. package/lib/seed/stub/ls.stub +11 -11
  109. package/lib/seed/stub/ts.stub +13 -13
  110. package/lib/transaction.js +363 -363
  111. package/lib/util/batchInsert.js +59 -59
  112. package/lib/util/delay.js +6 -6
  113. package/lib/util/fake-client.js +9 -9
  114. package/lib/util/finally-mixin.js +13 -13
  115. package/lib/util/fs.js +76 -76
  116. package/lib/util/import-file.js +13 -13
  117. package/lib/util/is-module-type.js +14 -14
  118. package/lib/util/is.js +32 -32
  119. package/lib/util/make-knex.js +338 -338
  120. package/lib/util/nanoid.js +29 -29
  121. package/lib/util/noop.js +1 -1
  122. package/lib/util/parse-connection.js +66 -66
  123. package/lib/util/save-async-stack.js +14 -14
  124. package/lib/util/template.js +52 -52
  125. package/lib/util/timeout.js +29 -29
  126. package/lib/util/timestamp.js +16 -16
  127. package/package.json +1 -1
  128. package/scripts/build.js +125 -125
  129. package/scripts/docker-compose.yml +111 -111
  130. package/scripts/next-release-howto.md +24 -24
  131. package/scripts/release.sh +34 -34
  132. package/scripts/runkit-example.js +34 -34
  133. package/scripts/stress-test/README.txt +18 -18
  134. package/scripts/stress-test/docker-compose.yml +47 -47
  135. package/scripts/stress-test/knex-stress-test.js +196 -196
  136. package/scripts/stress-test/mysql2-random-hanging-every-now-and-then.js +145 -145
  137. package/scripts/stress-test/mysql2-sudden-exit-without-error.js +100 -100
  138. package/scripts/stress-test/reconnect-test-mysql-based-drivers.js +184 -184
  139. package/types/index.d.ts +2249 -2249
  140. package/types/result.d.ts +27 -27
  141. package/types/tables.d.ts +4 -4
@@ -1,177 +1,177 @@
1
- // Column Compiler
2
- // Used for designating column definitions
3
- // during the table "create" / "alter" statements.
4
- // -------
5
- const Raw = require('../raw');
6
- const helpers = require('./helpers');
7
- const groupBy = require('lodash/groupBy');
8
- const first = require('lodash/first');
9
- const has = require('lodash/has');
10
- const tail = require('lodash/tail');
11
- const { isObject } = require('../util/is');
12
-
13
- function ColumnCompiler(client, tableCompiler, columnBuilder) {
14
- this.client = client;
15
- this.tableCompiler = tableCompiler;
16
- this.columnBuilder = columnBuilder;
17
- this._commonBuilder = this.columnBuilder;
18
- this.args = columnBuilder._args;
19
- this.type = columnBuilder._type.toLowerCase();
20
- this.grouped = groupBy(columnBuilder._statements, 'grouping');
21
- this.modified = columnBuilder._modifiers;
22
- this.isIncrements = this.type.indexOf('increments') !== -1;
23
- this.formatter = client.formatter(columnBuilder);
24
- this.sequence = [];
25
- this.modifiers = [];
26
- }
27
-
28
- ColumnCompiler.prototype.pushQuery = helpers.pushQuery;
29
-
30
- ColumnCompiler.prototype.pushAdditional = helpers.pushAdditional;
31
-
32
- ColumnCompiler.prototype.unshiftQuery = helpers.unshiftQuery;
33
-
34
- ColumnCompiler.prototype._defaultMap = {
35
- columnName: function () {
36
- if (!this.isIncrements) {
37
- throw new Error(
38
- `You did not specify a column name for the ${this.type} column.`
39
- );
40
- }
41
- return 'id';
42
- },
43
- };
44
-
45
- ColumnCompiler.prototype.defaults = function (label) {
46
- if (Object.prototype.hasOwnProperty.call(this._defaultMap, label)) {
47
- return this._defaultMap[label].bind(this)();
48
- } else {
49
- throw new Error(
50
- `There is no default for the specified identifier ${label}`
51
- );
52
- }
53
- };
54
-
55
- // To convert to sql, we first go through and build the
56
- // column as it would be in the insert statement
57
- ColumnCompiler.prototype.toSQL = function () {
58
- this.pushQuery(this.compileColumn());
59
- if (this.sequence.additional) {
60
- this.sequence = this.sequence.concat(this.sequence.additional);
61
- }
62
- return this.sequence;
63
- };
64
-
65
- // Compiles a column.
66
- ColumnCompiler.prototype.compileColumn = function () {
67
- return (
68
- this.formatter.wrap(this.getColumnName()) +
69
- ' ' +
70
- this.getColumnType() +
71
- this.getModifiers()
72
- );
73
- };
74
-
75
- // Assumes the autoincrementing key is named `id` if not otherwise specified.
76
- ColumnCompiler.prototype.getColumnName = function () {
77
- const value = first(this.args);
78
- return value || this.defaults('columnName');
79
- };
80
-
81
- ColumnCompiler.prototype.getColumnType = function () {
82
- const type = this[this.type];
83
- return typeof type === 'function' ? type.apply(this, tail(this.args)) : type;
84
- };
85
-
86
- ColumnCompiler.prototype.getModifiers = function () {
87
- const modifiers = [];
88
-
89
- for (let i = 0, l = this.modifiers.length; i < l; i++) {
90
- const modifier = this.modifiers[i];
91
-
92
- //Cannot allow 'nullable' modifiers on increments types
93
- if (!this.isIncrements || (this.isIncrements && modifier === 'comment')) {
94
- if (has(this.modified, modifier)) {
95
- const val = this[modifier].apply(this, this.modified[modifier]);
96
- if (val) modifiers.push(val);
97
- }
98
- }
99
- }
100
-
101
- return modifiers.length > 0 ? ` ${modifiers.join(' ')}` : '';
102
- };
103
-
104
- // Types
105
- // ------
106
-
107
- ColumnCompiler.prototype.increments =
108
- 'integer not null primary key autoincrement';
109
- ColumnCompiler.prototype.bigincrements =
110
- 'integer not null primary key autoincrement';
111
- ColumnCompiler.prototype.integer = ColumnCompiler.prototype.smallint = ColumnCompiler.prototype.mediumint =
112
- 'integer';
113
- ColumnCompiler.prototype.biginteger = 'bigint';
114
- ColumnCompiler.prototype.varchar = function (length) {
115
- return `varchar(${this._num(length, 255)})`;
116
- };
117
- ColumnCompiler.prototype.text = 'text';
118
- ColumnCompiler.prototype.tinyint = 'tinyint';
119
- ColumnCompiler.prototype.floating = function (precision, scale) {
120
- return `float(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
121
- };
122
- ColumnCompiler.prototype.decimal = function (precision, scale) {
123
- if (precision === null) {
124
- throw new Error(
125
- 'Specifying no precision on decimal columns is not supported for that SQL dialect.'
126
- );
127
- }
128
- return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
129
- };
130
- ColumnCompiler.prototype.binary = 'blob';
131
- ColumnCompiler.prototype.bool = 'boolean';
132
- ColumnCompiler.prototype.date = 'date';
133
- ColumnCompiler.prototype.datetime = 'datetime';
134
- ColumnCompiler.prototype.time = 'time';
135
- ColumnCompiler.prototype.timestamp = 'timestamp';
136
- ColumnCompiler.prototype.enu = 'varchar';
137
-
138
- ColumnCompiler.prototype.bit = ColumnCompiler.prototype.json = 'text';
139
-
140
- ColumnCompiler.prototype.uuid = 'char(36)';
141
- ColumnCompiler.prototype.specifictype = (type) => type;
142
-
143
- // Modifiers
144
- // -------
145
-
146
- ColumnCompiler.prototype.nullable = (nullable) =>
147
- nullable === false ? 'not null' : 'null';
148
- ColumnCompiler.prototype.notNullable = function () {
149
- return this.nullable(false);
150
- };
151
- ColumnCompiler.prototype.defaultTo = function (value) {
152
- if (value === void 0) {
153
- return '';
154
- } else if (value === null) {
155
- value = 'null';
156
- } else if (value instanceof Raw) {
157
- value = value.toQuery();
158
- } else if (this.type === 'bool') {
159
- if (value === 'false') value = 0;
160
- value = `'${value ? 1 : 0}'`;
161
- } else if (
162
- (this.type === 'json' || this.type === 'jsonb') &&
163
- isObject(value)
164
- ) {
165
- value = `'${JSON.stringify(value)}'`;
166
- } else {
167
- value = this.client._escapeBinding(value.toString());
168
- }
169
- return `default ${value}`;
170
- };
171
- ColumnCompiler.prototype._num = function (val, fallback) {
172
- if (val === undefined || val === null) return fallback;
173
- const number = parseInt(val, 10);
174
- return isNaN(number) ? fallback : number;
175
- };
176
-
177
- module.exports = ColumnCompiler;
1
+ // Column Compiler
2
+ // Used for designating column definitions
3
+ // during the table "create" / "alter" statements.
4
+ // -------
5
+ const Raw = require('../raw');
6
+ const helpers = require('./helpers');
7
+ const groupBy = require('lodash/groupBy');
8
+ const first = require('lodash/first');
9
+ const has = require('lodash/has');
10
+ const tail = require('lodash/tail');
11
+ const { isObject } = require('../util/is');
12
+
13
+ function ColumnCompiler(client, tableCompiler, columnBuilder) {
14
+ this.client = client;
15
+ this.tableCompiler = tableCompiler;
16
+ this.columnBuilder = columnBuilder;
17
+ this._commonBuilder = this.columnBuilder;
18
+ this.args = columnBuilder._args;
19
+ this.type = columnBuilder._type.toLowerCase();
20
+ this.grouped = groupBy(columnBuilder._statements, 'grouping');
21
+ this.modified = columnBuilder._modifiers;
22
+ this.isIncrements = this.type.indexOf('increments') !== -1;
23
+ this.formatter = client.formatter(columnBuilder);
24
+ this.sequence = [];
25
+ this.modifiers = [];
26
+ }
27
+
28
+ ColumnCompiler.prototype.pushQuery = helpers.pushQuery;
29
+
30
+ ColumnCompiler.prototype.pushAdditional = helpers.pushAdditional;
31
+
32
+ ColumnCompiler.prototype.unshiftQuery = helpers.unshiftQuery;
33
+
34
+ ColumnCompiler.prototype._defaultMap = {
35
+ columnName: function () {
36
+ if (!this.isIncrements) {
37
+ throw new Error(
38
+ `You did not specify a column name for the ${this.type} column.`
39
+ );
40
+ }
41
+ return 'id';
42
+ },
43
+ };
44
+
45
+ ColumnCompiler.prototype.defaults = function (label) {
46
+ if (Object.prototype.hasOwnProperty.call(this._defaultMap, label)) {
47
+ return this._defaultMap[label].bind(this)();
48
+ } else {
49
+ throw new Error(
50
+ `There is no default for the specified identifier ${label}`
51
+ );
52
+ }
53
+ };
54
+
55
+ // To convert to sql, we first go through and build the
56
+ // column as it would be in the insert statement
57
+ ColumnCompiler.prototype.toSQL = function () {
58
+ this.pushQuery(this.compileColumn());
59
+ if (this.sequence.additional) {
60
+ this.sequence = this.sequence.concat(this.sequence.additional);
61
+ }
62
+ return this.sequence;
63
+ };
64
+
65
+ // Compiles a column.
66
+ ColumnCompiler.prototype.compileColumn = function () {
67
+ return (
68
+ this.formatter.wrap(this.getColumnName()) +
69
+ ' ' +
70
+ this.getColumnType() +
71
+ this.getModifiers()
72
+ );
73
+ };
74
+
75
+ // Assumes the autoincrementing key is named `id` if not otherwise specified.
76
+ ColumnCompiler.prototype.getColumnName = function () {
77
+ const value = first(this.args);
78
+ return value || this.defaults('columnName');
79
+ };
80
+
81
+ ColumnCompiler.prototype.getColumnType = function () {
82
+ const type = this[this.type];
83
+ return typeof type === 'function' ? type.apply(this, tail(this.args)) : type;
84
+ };
85
+
86
+ ColumnCompiler.prototype.getModifiers = function () {
87
+ const modifiers = [];
88
+
89
+ for (let i = 0, l = this.modifiers.length; i < l; i++) {
90
+ const modifier = this.modifiers[i];
91
+
92
+ //Cannot allow 'nullable' modifiers on increments types
93
+ if (!this.isIncrements || (this.isIncrements && modifier === 'comment')) {
94
+ if (has(this.modified, modifier)) {
95
+ const val = this[modifier].apply(this, this.modified[modifier]);
96
+ if (val) modifiers.push(val);
97
+ }
98
+ }
99
+ }
100
+
101
+ return modifiers.length > 0 ? ` ${modifiers.join(' ')}` : '';
102
+ };
103
+
104
+ // Types
105
+ // ------
106
+
107
+ ColumnCompiler.prototype.increments =
108
+ 'integer not null primary key autoincrement';
109
+ ColumnCompiler.prototype.bigincrements =
110
+ 'integer not null primary key autoincrement';
111
+ ColumnCompiler.prototype.integer = ColumnCompiler.prototype.smallint = ColumnCompiler.prototype.mediumint =
112
+ 'integer';
113
+ ColumnCompiler.prototype.biginteger = 'bigint';
114
+ ColumnCompiler.prototype.varchar = function (length) {
115
+ return `varchar(${this._num(length, 255)})`;
116
+ };
117
+ ColumnCompiler.prototype.text = 'text';
118
+ ColumnCompiler.prototype.tinyint = 'tinyint';
119
+ ColumnCompiler.prototype.floating = function (precision, scale) {
120
+ return `float(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
121
+ };
122
+ ColumnCompiler.prototype.decimal = function (precision, scale) {
123
+ if (precision === null) {
124
+ throw new Error(
125
+ 'Specifying no precision on decimal columns is not supported for that SQL dialect.'
126
+ );
127
+ }
128
+ return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
129
+ };
130
+ ColumnCompiler.prototype.binary = 'blob';
131
+ ColumnCompiler.prototype.bool = 'boolean';
132
+ ColumnCompiler.prototype.date = 'date';
133
+ ColumnCompiler.prototype.datetime = 'datetime';
134
+ ColumnCompiler.prototype.time = 'time';
135
+ ColumnCompiler.prototype.timestamp = 'timestamp';
136
+ ColumnCompiler.prototype.enu = 'varchar';
137
+
138
+ ColumnCompiler.prototype.bit = ColumnCompiler.prototype.json = 'text';
139
+
140
+ ColumnCompiler.prototype.uuid = 'char(36)';
141
+ ColumnCompiler.prototype.specifictype = (type) => type;
142
+
143
+ // Modifiers
144
+ // -------
145
+
146
+ ColumnCompiler.prototype.nullable = (nullable) =>
147
+ nullable === false ? 'not null' : 'null';
148
+ ColumnCompiler.prototype.notNullable = function () {
149
+ return this.nullable(false);
150
+ };
151
+ ColumnCompiler.prototype.defaultTo = function (value) {
152
+ if (value === void 0) {
153
+ return '';
154
+ } else if (value === null) {
155
+ value = 'null';
156
+ } else if (value instanceof Raw) {
157
+ value = value.toQuery();
158
+ } else if (this.type === 'bool') {
159
+ if (value === 'false') value = 0;
160
+ value = `'${value ? 1 : 0}'`;
161
+ } else if (
162
+ (this.type === 'json' || this.type === 'jsonb') &&
163
+ isObject(value)
164
+ ) {
165
+ value = `'${JSON.stringify(value)}'`;
166
+ } else {
167
+ value = this.client._escapeBinding(value.toString());
168
+ }
169
+ return `default ${value}`;
170
+ };
171
+ ColumnCompiler.prototype._num = function (val, fallback) {
172
+ if (val === undefined || val === null) return fallback;
173
+ const number = parseInt(val, 10);
174
+ return isNaN(number) ? fallback : number;
175
+ };
176
+
177
+ module.exports = ColumnCompiler;
@@ -1,101 +1,101 @@
1
- const { pushQuery, pushAdditional, unshiftQuery } = require('./helpers');
2
-
3
- // The "SchemaCompiler" takes all of the query statements which have been
4
- // gathered in the "SchemaBuilder" and turns them into an array of
5
- // properly formatted / bound query strings.
6
- function SchemaCompiler(client, builder) {
7
- this.builder = builder;
8
- this._commonBuilder = this.builder;
9
- this.client = client;
10
- this.schema = builder._schema;
11
- this.formatter = client.formatter(builder);
12
- this.sequence = [];
13
- }
14
-
15
- function throwOnlyPGError(operationName) {
16
- throw new Error(
17
- `${operationName} is not supported for this dialect (only PostgreSQL supports it currently).`
18
- );
19
- }
20
-
21
- Object.assign(SchemaCompiler.prototype, {
22
- pushQuery: pushQuery,
23
-
24
- pushAdditional: pushAdditional,
25
-
26
- unshiftQuery: unshiftQuery,
27
-
28
- createTable: buildTable('create'),
29
-
30
- createTableIfNotExists: buildTable('createIfNot'),
31
-
32
- createSchema: () => {
33
- throwOnlyPGError('createSchema');
34
- },
35
- createSchemaIfNotExists: () => {
36
- throwOnlyPGError('createSchemaIfNotExists');
37
- },
38
- dropSchema: () => {
39
- throwOnlyPGError('dropSchema');
40
- },
41
- dropSchemaIfExists: () => {
42
- throwOnlyPGError('dropSchemaIfExists');
43
- },
44
-
45
- alterTable: buildTable('alter'),
46
-
47
- dropTablePrefix: 'drop table ',
48
-
49
- dropTable(tableName) {
50
- this.pushQuery(
51
- this.dropTablePrefix +
52
- this.formatter.wrap(prefixedTableName(this.schema, tableName))
53
- );
54
- },
55
-
56
- dropTableIfExists(tableName) {
57
- this.pushQuery(
58
- this.dropTablePrefix +
59
- 'if exists ' +
60
- this.formatter.wrap(prefixedTableName(this.schema, tableName))
61
- );
62
- },
63
-
64
- raw(sql, bindings) {
65
- this.sequence.push(this.client.raw(sql, bindings).toSQL());
66
- },
67
-
68
- toSQL() {
69
- const sequence = this.builder._sequence;
70
- for (let i = 0, l = sequence.length; i < l; i++) {
71
- const query = sequence[i];
72
- this[query.method].apply(this, query.args);
73
- }
74
- return this.sequence;
75
- },
76
- });
77
-
78
- function buildTable(type) {
79
- return function (tableName, fn) {
80
- const builder = this.client.tableBuilder(type, tableName, fn);
81
-
82
- // pass queryContext down to tableBuilder but do not overwrite it if already set
83
- const queryContext = this.builder.queryContext();
84
- if (queryContext !== undefined && builder.queryContext() === undefined) {
85
- builder.queryContext(queryContext);
86
- }
87
-
88
- builder.setSchema(this.schema);
89
- const sql = builder.toSQL();
90
-
91
- for (let i = 0, l = sql.length; i < l; i++) {
92
- this.sequence.push(sql[i]);
93
- }
94
- };
95
- }
96
-
97
- function prefixedTableName(prefix, table) {
98
- return prefix ? `${prefix}.${table}` : table;
99
- }
100
-
101
- module.exports = SchemaCompiler;
1
+ const { pushQuery, pushAdditional, unshiftQuery } = require('./helpers');
2
+
3
+ // The "SchemaCompiler" takes all of the query statements which have been
4
+ // gathered in the "SchemaBuilder" and turns them into an array of
5
+ // properly formatted / bound query strings.
6
+ function SchemaCompiler(client, builder) {
7
+ this.builder = builder;
8
+ this._commonBuilder = this.builder;
9
+ this.client = client;
10
+ this.schema = builder._schema;
11
+ this.formatter = client.formatter(builder);
12
+ this.sequence = [];
13
+ }
14
+
15
+ function throwOnlyPGError(operationName) {
16
+ throw new Error(
17
+ `${operationName} is not supported for this dialect (only PostgreSQL supports it currently).`
18
+ );
19
+ }
20
+
21
+ Object.assign(SchemaCompiler.prototype, {
22
+ pushQuery: pushQuery,
23
+
24
+ pushAdditional: pushAdditional,
25
+
26
+ unshiftQuery: unshiftQuery,
27
+
28
+ createTable: buildTable('create'),
29
+
30
+ createTableIfNotExists: buildTable('createIfNot'),
31
+
32
+ createSchema: () => {
33
+ throwOnlyPGError('createSchema');
34
+ },
35
+ createSchemaIfNotExists: () => {
36
+ throwOnlyPGError('createSchemaIfNotExists');
37
+ },
38
+ dropSchema: () => {
39
+ throwOnlyPGError('dropSchema');
40
+ },
41
+ dropSchemaIfExists: () => {
42
+ throwOnlyPGError('dropSchemaIfExists');
43
+ },
44
+
45
+ alterTable: buildTable('alter'),
46
+
47
+ dropTablePrefix: 'drop table ',
48
+
49
+ dropTable(tableName) {
50
+ this.pushQuery(
51
+ this.dropTablePrefix +
52
+ this.formatter.wrap(prefixedTableName(this.schema, tableName))
53
+ );
54
+ },
55
+
56
+ dropTableIfExists(tableName) {
57
+ this.pushQuery(
58
+ this.dropTablePrefix +
59
+ 'if exists ' +
60
+ this.formatter.wrap(prefixedTableName(this.schema, tableName))
61
+ );
62
+ },
63
+
64
+ raw(sql, bindings) {
65
+ this.sequence.push(this.client.raw(sql, bindings).toSQL());
66
+ },
67
+
68
+ toSQL() {
69
+ const sequence = this.builder._sequence;
70
+ for (let i = 0, l = sequence.length; i < l; i++) {
71
+ const query = sequence[i];
72
+ this[query.method].apply(this, query.args);
73
+ }
74
+ return this.sequence;
75
+ },
76
+ });
77
+
78
+ function buildTable(type) {
79
+ return function (tableName, fn) {
80
+ const builder = this.client.tableBuilder(type, tableName, fn);
81
+
82
+ // pass queryContext down to tableBuilder but do not overwrite it if already set
83
+ const queryContext = this.builder.queryContext();
84
+ if (queryContext !== undefined && builder.queryContext() === undefined) {
85
+ builder.queryContext(queryContext);
86
+ }
87
+
88
+ builder.setSchema(this.schema);
89
+ const sql = builder.toSQL();
90
+
91
+ for (let i = 0, l = sql.length; i < l; i++) {
92
+ this.sequence.push(sql[i]);
93
+ }
94
+ };
95
+ }
96
+
97
+ function prefixedTableName(prefix, table) {
98
+ return prefix ? `${prefix}.${table}` : table;
99
+ }
100
+
101
+ module.exports = SchemaCompiler;
@@ -1,51 +1,51 @@
1
- const tail = require('lodash/tail');
2
- const { isString } = require('../util/is');
3
-
4
- // Push a new query onto the compiled "sequence" stack,
5
- // creating a new formatter, returning the compiler.
6
- function pushQuery(query) {
7
- if (!query) return;
8
- if (isString(query)) {
9
- query = { sql: query };
10
- }
11
- if (!query.bindings) {
12
- query.bindings = this.formatter.bindings;
13
- }
14
- this.sequence.push(query);
15
-
16
- this.formatter = this.client.formatter(this._commonBuilder);
17
- }
18
-
19
- // Used in cases where we need to push some additional column specific statements.
20
- function pushAdditional(fn) {
21
- const child = new this.constructor(
22
- this.client,
23
- this.tableCompiler,
24
- this.columnBuilder
25
- );
26
- fn.call(child, tail(arguments));
27
- this.sequence.additional = (this.sequence.additional || []).concat(
28
- child.sequence
29
- );
30
- }
31
-
32
- // Unshift a new query onto the compiled "sequence" stack,
33
- // creating a new formatter, returning the compiler.
34
- function unshiftQuery(query) {
35
- if (!query) return;
36
- if (isString(query)) {
37
- query = { sql: query };
38
- }
39
- if (!query.bindings) {
40
- query.bindings = this.formatter.bindings;
41
- }
42
- this.sequence.unshift(query);
43
-
44
- this.formatter = this.client.formatter(this._commonBuilder);
45
- }
46
-
47
- module.exports = {
48
- pushAdditional,
49
- pushQuery,
50
- unshiftQuery,
51
- };
1
+ const tail = require('lodash/tail');
2
+ const { isString } = require('../util/is');
3
+
4
+ // Push a new query onto the compiled "sequence" stack,
5
+ // creating a new formatter, returning the compiler.
6
+ function pushQuery(query) {
7
+ if (!query) return;
8
+ if (isString(query)) {
9
+ query = { sql: query };
10
+ }
11
+ if (!query.bindings) {
12
+ query.bindings = this.formatter.bindings;
13
+ }
14
+ this.sequence.push(query);
15
+
16
+ this.formatter = this.client.formatter(this._commonBuilder);
17
+ }
18
+
19
+ // Used in cases where we need to push some additional column specific statements.
20
+ function pushAdditional(fn) {
21
+ const child = new this.constructor(
22
+ this.client,
23
+ this.tableCompiler,
24
+ this.columnBuilder
25
+ );
26
+ fn.call(child, tail(arguments));
27
+ this.sequence.additional = (this.sequence.additional || []).concat(
28
+ child.sequence
29
+ );
30
+ }
31
+
32
+ // Unshift a new query onto the compiled "sequence" stack,
33
+ // creating a new formatter, returning the compiler.
34
+ function unshiftQuery(query) {
35
+ if (!query) return;
36
+ if (isString(query)) {
37
+ query = { sql: query };
38
+ }
39
+ if (!query.bindings) {
40
+ query.bindings = this.formatter.bindings;
41
+ }
42
+ this.sequence.unshift(query);
43
+
44
+ this.formatter = this.client.formatter(this._commonBuilder);
45
+ }
46
+
47
+ module.exports = {
48
+ pushAdditional,
49
+ pushQuery,
50
+ unshiftQuery,
51
+ };