2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import { EventEmitter } from 'events';
|
2016-05-18 20:22:50 +10:00
|
|
|
import { each, toArray } from 'lodash'
|
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.
|
|
|
|
function SchemaBuilder(client) {
|
2016-05-18 19:59:24 +10:00
|
|
|
this.client = client
|
2016-03-02 17:07:05 +01:00
|
|
|
this._sequence = []
|
2016-05-18 19:59:24 +10:00
|
|
|
this._debug = client.config && client.config.debug
|
2016-03-02 17:07:05 +01:00
|
|
|
}
|
|
|
|
inherits(SchemaBuilder, EventEmitter)
|
|
|
|
|
|
|
|
// Each of the schema builder methods just add to the
|
|
|
|
// "_sequence" array for consistency.
|
|
|
|
each([
|
|
|
|
'createTable',
|
|
|
|
'createTableIfNotExists',
|
|
|
|
'createSchema',
|
|
|
|
'createSchemaIfNotExists',
|
|
|
|
'dropSchema',
|
|
|
|
'dropSchemaIfExists',
|
|
|
|
'createExtension',
|
|
|
|
'createExtensionIfNotExists',
|
|
|
|
'dropExtension',
|
|
|
|
'dropExtensionIfExists',
|
|
|
|
'table',
|
|
|
|
'alterTable',
|
|
|
|
'hasTable',
|
|
|
|
'hasColumn',
|
|
|
|
'dropTable',
|
|
|
|
'renameTable',
|
|
|
|
'dropTableIfExists',
|
|
|
|
'raw'
|
|
|
|
], function(method) {
|
|
|
|
SchemaBuilder.prototype[method] = function() {
|
|
|
|
if (method === 'table') method = 'alterTable';
|
|
|
|
this._sequence.push({
|
2016-05-17 01:01:34 +10:00
|
|
|
method,
|
2016-03-02 17:07:05 +01:00
|
|
|
args: toArray(arguments)
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
require('../interface')(SchemaBuilder)
|
|
|
|
|
|
|
|
SchemaBuilder.prototype.withSchema = function(schemaName) {
|
|
|
|
this._schema = schemaName;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
SchemaBuilder.prototype.toString = function() {
|
|
|
|
return this.toQuery()
|
|
|
|
}
|
|
|
|
|
|
|
|
SchemaBuilder.prototype.toSQL = function() {
|
|
|
|
return this.client.schemaCompiler(this).toSQL()
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default SchemaBuilder
|