knex/lib/schema/builder.js

116 lines
3.0 KiB
JavaScript
Raw Normal View History

const { EventEmitter } = require('events');
const toArray = require('lodash/toArray');
const assign = require('lodash/assign');
const { addQueryContext } = require('../util/helpers');
const saveAsyncStack = require('../util/save-async-stack');
const {
augmentWithBuilderInterface,
} = require('../builder-interface-augmenter');
2016-03-02 17:07:05 +01:00
// Constructor for the builder instance, typically called from
// `knex.builder`, accepting the current `knex` instance,
// and pulling out the `client` and `grammar` from the current
// knex instance.
class SchemaBuilder extends EventEmitter {
constructor(client) {
super();
this.client = client;
this._sequence = [];
2018-04-25 23:11:24 +02:00
if (client.config) {
this._debug = client.config.debug;
saveAsyncStack(this, 4);
}
}
withSchema(schemaName) {
this._schema = schemaName;
return this;
}
toString() {
return this.toQuery();
2018-04-25 23:11:24 +02:00
}
toSQL() {
return this.client.schemaCompiler(this).toSQL();
}
async generateDdlCommands() {
return await this.client.schemaCompiler(this).generateDdlCommands();
}
}
2016-03-02 17:07:05 +01:00
// Each of the schema builder methods just add to the
// "_sequence" array for consistency.
[
'createTable',
'createTableIfNotExists',
'createTableLike',
2021-10-20 22:23:29 +02:00
'createView',
'createViewOrReplace',
'createMaterializedView',
'refreshMaterializedView',
'dropView',
'dropViewIfExists',
'dropMaterializedView',
'dropMaterializedViewIfExists',
'createSchema',
'createSchemaIfNotExists',
'dropSchema',
'dropSchemaIfExists',
'createExtension',
'createExtensionIfNotExists',
'dropExtension',
'dropExtensionIfExists',
'table',
'alterTable',
2021-10-20 22:23:29 +02:00
'view',
'alterView',
'hasTable',
'hasColumn',
'dropTable',
'renameTable',
2021-10-20 22:23:29 +02:00
'renameView',
'dropTableIfExists',
'raw',
2020-04-19 00:40:23 +02:00
].forEach(function (method) {
SchemaBuilder.prototype[method] = function () {
if (method === 'createTableIfNotExists') {
this.client.logger.warn(
[
'Use async .hasTable to check if table exists and then use plain .createTable. Since ',
'.createTableIfNotExists actually just generates plain "CREATE TABLE IF NOT EXIST..." ',
'query it will not work correctly if there are any alter table queries generated for ',
'columns afterwards. To not break old migrations this function is left untouched for now',
', but it should not be used when writing new code and it is removed from documentation.',
].join('')
);
}
if (method === 'table') method = 'alterTable';
2021-10-20 22:23:29 +02:00
if (method === 'view') method = 'alterView';
this._sequence.push({
method,
args: toArray(arguments),
});
return this;
};
});
2016-03-02 17:07:05 +01:00
SchemaBuilder.extend = (methodName, fn) => {
if (
Object.prototype.hasOwnProperty.call(SchemaBuilder.prototype, methodName)
) {
throw new Error(
`Can't extend SchemaBuilder with existing method ('${methodName}').`
);
}
assign(SchemaBuilder.prototype, { [methodName]: fn });
};
augmentWithBuilderInterface(SchemaBuilder);
Add queryContext to schema and query builders (#2314) * feat(query-builder): add hookContext for wrapIdentifier * refactor: use isUndefined * test(transaction): test passing of hookContext * feat(runnner): pass context to postProcessResponse * test(runner): test postProcessResponse for raw responses * test(raw): test passing of hookContext * feat: add hookContext to Raw and SchemaBuilder * test(transaction): fix test for hookContext * chore: fix lint error * fix: check for hookContext before calling it * test(transaction): fix hookContext test * chore: remove whitespace * test(hookContext): test cloning of context object * refactor: hookContext -> queryContext * minor: use more descriptive variable name i.e. refactor: `context` => `queryContext` * fix: remove unnecessary checks for query builder * fix(Raw): pass query builder to formatter * fix(SchemaCompiler): pass schema builder to formatter * refactor: add addQueryContext helper * feat: add queryContext to TableBuilder and ColumnBuilder * fix(TableCompiler): pass table builder to formatter * fix(ColumnCompiler): pass column builder to formatter * fix(pushQuery): fix passing builder to formatter * test(Schema|Table|ColumnCompiler): test passing queryContext * fix(SchemaCompiler): pass queryContext to TableCompiler * fix(TableCompiler): pass queryContext to ColumnCompiler * test: add queryContext tests for all schema dialects * test(TableCompiler): test overwriting queryContext from SchemaCompiler * test(Raw): test passing queryContext to wrapIdentifier * tests: run all the tests
2018-02-01 23:41:01 +01:00
addQueryContext(SchemaBuilder);
2016-03-02 17:07:05 +01:00
module.exports = SchemaBuilder;