Comparing version 0.95.12-rc3 to 0.95.12-rc4
@@ -31,2 +31,4 @@ const { Pool, TimeoutError } = require('tarn'); | ||
const { POOL_CONFIG_OPTIONS } = require('./constants'); | ||
const ViewBuilder = require('./schema/viewbuilder.js'); | ||
const ViewCompiler = require('./schema/viewcompiler.js'); | ||
@@ -106,2 +108,6 @@ const debug = require('debug')('knex:client'); | ||
viewBuilder(type, viewBuilder, fn) { | ||
return new ViewBuilder(this, type, viewBuilder, fn); | ||
} | ||
tableCompiler(tableBuilder) { | ||
@@ -111,2 +117,6 @@ return new TableCompiler(this, tableBuilder); | ||
viewCompiler(viewCompiler) { | ||
return new ViewCompiler(this, viewCompiler); | ||
} | ||
columnBuilder(tableBuilder, type, args) { | ||
@@ -113,0 +123,0 @@ return new ColumnBuilder(this, tableBuilder, type, args); |
@@ -7,2 +7,3 @@ // CockroachDB Client | ||
const TableCompiler = require('./crdb-tablecompiler'); | ||
const ViewCompiler = require('./crdb-viewcompiler'); | ||
@@ -25,2 +26,6 @@ // Always initialize with the "QueryBuilder" and "QueryCompiler" | ||
viewCompiler() { | ||
return new ViewCompiler(this, ...arguments); | ||
} | ||
_parseVersion(versionString) { | ||
@@ -27,0 +32,0 @@ return versionString.split(' ')[2]; |
@@ -14,2 +14,3 @@ // MSSQL Client | ||
const TableCompiler = require('./schema/mssql-tablecompiler'); | ||
const ViewCompiler = require('./schema/mssql-viewcompiler'); | ||
const ColumnCompiler = require('./schema/mssql-columncompiler'); | ||
@@ -116,2 +117,5 @@ const QueryBuilder = require('../../query/querybuilder'); | ||
viewCompiler() { | ||
return new ViewCompiler(this, ...arguments); | ||
} | ||
queryBuilder() { | ||
@@ -118,0 +122,0 @@ const b = new QueryBuilder(this); |
@@ -17,2 +17,9 @@ // MySQL Schema Compiler | ||
dropViewIfExists(viewName) { | ||
const name = this.formatter.wrap(prefixedTableName(this.schema, viewName)); | ||
this.pushQuery( | ||
`if object_id('${name}', 'V') is not null DROP VIEW ${name}` | ||
); | ||
} | ||
// Rename a table on the schema. | ||
@@ -29,2 +36,12 @@ renameTable(tableName, to) { | ||
renameView(viewTable, to) { | ||
this.pushQuery( | ||
`exec sp_rename ${this.client.parameter( | ||
prefixedTableName(this.schema, viewTable), | ||
this.builder, | ||
this.bindingsHolder | ||
)}, ${this.client.parameter(to, this.builder, this.bindingsHolder)}` | ||
); | ||
} | ||
// Check whether a table exists on the query. | ||
@@ -31,0 +48,0 @@ hasTable(tableName) { |
@@ -15,2 +15,4 @@ // MySQL Client | ||
const { makeEscape } = require('../../util/string'); | ||
const ViewCompiler = require('./schema/mysql-viewcompiler'); | ||
const ViewBuilder = require('./schema/mysql-viewbuilder'); | ||
@@ -37,2 +39,10 @@ // Always initialize with the "QueryBuilder" and "QueryCompiler" | ||
viewCompiler() { | ||
return new ViewCompiler(this, ...arguments); | ||
} | ||
viewBuilder() { | ||
return new ViewBuilder(this, ...arguments); | ||
} | ||
columnCompiler() { | ||
@@ -39,0 +49,0 @@ return new ColumnCompiler(this, ...arguments); |
@@ -19,2 +19,6 @@ // MySQL Schema Compiler | ||
renameView(from, to) { | ||
this.renameTable(from, to); | ||
} | ||
// Check whether a table exists on the query. | ||
@@ -21,0 +25,0 @@ hasTable(tableName) { |
@@ -80,7 +80,20 @@ // Oracle Schema Compiler | ||
dropTableIfExists(tableName) { | ||
this.dropObject(tableName, 'table'); | ||
} | ||
dropViewIfExists(viewName) { | ||
this.dropObject(viewName, 'view'); | ||
} | ||
dropObject(objectName, type) { | ||
const prefix = this.schema ? `"${this.schema}".` : ''; | ||
let errorCode = -942; | ||
if (type === 'materialized view') { | ||
// https://stackoverflow.com/a/1801453 | ||
errorCode = -12003; | ||
} | ||
this.pushQuery( | ||
utils.wrapSqlWithCatch( | ||
`drop table ${prefix}${this.formatter.wrap(tableName)}`, | ||
-942 | ||
`drop ${type} ${prefix}${this.formatter.wrap(objectName)}`, | ||
errorCode | ||
) | ||
@@ -90,6 +103,22 @@ ); | ||
// removing the sequence that was possibly generated by increments() column | ||
this._dropRelatedSequenceIfExists(tableName); | ||
this._dropRelatedSequenceIfExists(objectName); | ||
} | ||
refreshMaterializedView(viewName) { | ||
return this.pushQuery({ | ||
sql: `BEGIN DBMS_MVIEW.REFRESH('${ | ||
this.schemaNameRaw ? this.schemaNameRaw + '.' : '' | ||
}${viewName}'); END;`, | ||
}); | ||
} | ||
dropMaterializedView(viewName) { | ||
this._dropView(viewName, false, true); | ||
} | ||
dropMaterializedViewIfExists(viewName) { | ||
this.dropObject(viewName, 'materialized view'); | ||
} | ||
} | ||
module.exports = SchemaCompiler_Oracle; |
@@ -18,2 +18,4 @@ // Oracledb Client | ||
} = require('./utils'); | ||
const ViewCompiler = require('./schema/oracledb-viewcompiler'); | ||
const ViewBuilder = require('./schema/oracledb-viewbuilder'); | ||
const Transaction = require('./transaction'); | ||
@@ -73,2 +75,10 @@ const Client_Oracle = require('../oracle'); | ||
viewBuilder() { | ||
return new ViewBuilder(this, ...arguments); | ||
} | ||
viewCompiler() { | ||
return new ViewCompiler(this, ...arguments); | ||
} | ||
formatter(builder) { | ||
@@ -75,0 +85,0 @@ return new Formatter(this, builder); |
@@ -12,2 +12,4 @@ // PostgreSQL | ||
const TableCompiler = require('./schema/pg-tablecompiler'); | ||
const ViewCompiler = require('./schema/pg-viewcompiler'); | ||
const ViewBuilder = require('./schema/pg-viewbuilder'); | ||
const SchemaCompiler = require('./schema/pg-compiler'); | ||
@@ -48,2 +50,10 @@ const { makeEscape } = require('../../util/string'); | ||
viewCompiler() { | ||
return new ViewCompiler(this, ...arguments); | ||
} | ||
viewBuilder() { | ||
return new ViewBuilder(this, ...arguments); | ||
} | ||
_driver() { | ||
@@ -50,0 +60,0 @@ return require('pg'); |
@@ -113,4 +113,25 @@ // PostgreSQL Schema Compiler | ||
} | ||
renameView(from, to) { | ||
this.pushQuery( | ||
this.alterViewPrefix + | ||
`${this.formatter.wrap(from)} rename to ${this.formatter.wrap(to)}` | ||
); | ||
} | ||
refreshMaterializedView(viewName) { | ||
this.pushQuery({ | ||
sql: `refresh materialized view ${this.formatter.wrap(viewName)}`, | ||
}); | ||
} | ||
dropMaterializedView(viewName) { | ||
this._dropView(viewName, false, true); | ||
} | ||
dropMaterializedViewIfExists(viewName) { | ||
this._dropView(viewName, true, true); | ||
} | ||
} | ||
module.exports = SchemaCompiler_PG; |
@@ -12,2 +12,3 @@ // Redshift | ||
const SchemaCompiler = require('./schema/redshift-compiler'); | ||
const ViewCompiler = require('./schema/redshift-viewcompiler'); | ||
@@ -39,2 +40,6 @@ class Client_Redshift extends Client_PG { | ||
viewCompiler() { | ||
return new ViewCompiler(this, ...arguments); | ||
} | ||
_driver() { | ||
@@ -41,0 +46,0 @@ return require('pg'); |
@@ -15,2 +15,3 @@ // SQLite3 | ||
const TableCompiler = require('./schema/sqlite-tablecompiler'); | ||
const ViewCompiler = require('./schema/sqlite-viewcompiler'); | ||
const SQLite3_DDL = require('./schema/ddl'); | ||
@@ -47,2 +48,6 @@ const Formatter = require('../../formatter'); | ||
viewCompiler(builder, formatter) { | ||
return new ViewCompiler(this, builder, formatter); | ||
} | ||
columnCompiler() { | ||
@@ -49,0 +54,0 @@ return new ColumnCompiler(this, ...arguments); |
@@ -49,2 +49,10 @@ const { EventEmitter } = require('events'); | ||
'createTableLike', | ||
'createView', | ||
'createViewOrReplace', | ||
'createMaterializedView', | ||
'refreshMaterializedView', | ||
'dropView', | ||
'dropViewIfExists', | ||
'dropMaterializedView', | ||
'dropMaterializedViewIfExists', | ||
'createSchema', | ||
@@ -60,2 +68,4 @@ 'createSchemaIfNotExists', | ||
'alterTable', | ||
'view', | ||
'alterView', | ||
'hasTable', | ||
@@ -65,2 +75,3 @@ 'hasColumn', | ||
'renameTable', | ||
'renameView', | ||
'dropTableIfExists', | ||
@@ -82,2 +93,3 @@ 'raw', | ||
if (method === 'table') method = 'alterTable'; | ||
if (method === 'view') method = 'alterView'; | ||
this._sequence.push({ | ||
@@ -84,0 +96,0 @@ method, |
@@ -55,2 +55,36 @@ const { | ||
dropView(viewName) { | ||
this._dropView(viewName, false, false); | ||
} | ||
dropViewIfExists(viewName) { | ||
this._dropView(viewName, true, false); | ||
} | ||
dropMaterializedView(viewName) { | ||
throw new Error('materialized views are not supported by this dialect.'); | ||
} | ||
dropMaterializedViewIfExists(viewName) { | ||
throw new Error('materialized views are not supported by this dialect.'); | ||
} | ||
renameView(from, to) { | ||
throw new Error( | ||
'rename view is not supported by this dialect (instead drop then create another view).' | ||
); | ||
} | ||
refreshMaterializedView() { | ||
throw new Error('materialized views are not supported by this dialect.'); | ||
} | ||
_dropView(viewName, ifExists, materialized) { | ||
this.pushQuery( | ||
(materialized ? this.dropMaterializedViewPrefix : this.dropViewPrefix) + | ||
(ifExists ? 'if exists ' : '') + | ||
this.formatter.wrap(prefixedTableName(this.schema, viewName)) | ||
); | ||
} | ||
raw(sql, bindings) { | ||
@@ -82,2 +116,5 @@ this.sequence.push(this.client.raw(sql, bindings).toSQL()); | ||
SchemaCompiler.prototype.dropTablePrefix = 'drop table '; | ||
SchemaCompiler.prototype.dropViewPrefix = 'drop view '; | ||
SchemaCompiler.prototype.dropMaterializedViewPrefix = 'drop materialized view '; | ||
SchemaCompiler.prototype.alterViewPrefix = 'alter view '; | ||
@@ -89,2 +126,9 @@ SchemaCompiler.prototype.alterTable = buildTable('alter'); | ||
SchemaCompiler.prototype.createView = buildView('create'); | ||
SchemaCompiler.prototype.createViewOrReplace = buildView('createOrReplace'); | ||
SchemaCompiler.prototype.createMaterializedView = buildView( | ||
'createMaterializedView' | ||
); | ||
SchemaCompiler.prototype.alterView = buildView('alter'); | ||
SchemaCompiler.prototype.pushQuery = pushQuery; | ||
@@ -94,18 +138,18 @@ SchemaCompiler.prototype.pushAdditional = pushAdditional; | ||
function buildTable(type) { | ||
function build(builder) { | ||
// pass queryContext down to tableBuilder but do not overwrite it if already set | ||
const queryContext = this.builder.queryContext(); | ||
if (queryContext !== undefined && builder.queryContext() === undefined) { | ||
builder.queryContext(queryContext); | ||
} | ||
function build(builder) { | ||
// pass queryContext down to tableBuilder but do not overwrite it if already set | ||
const queryContext = this.builder.queryContext(); | ||
if (queryContext !== undefined && builder.queryContext() === undefined) { | ||
builder.queryContext(queryContext); | ||
} | ||
builder.setSchema(this.schema); | ||
const sql = builder.toSQL(); | ||
builder.setSchema(this.schema); | ||
const sql = builder.toSQL(); | ||
for (let i = 0, l = sql.length; i < l; i++) { | ||
this.sequence.push(sql[i]); | ||
} | ||
for (let i = 0, l = sql.length; i < l; i++) { | ||
this.sequence.push(sql[i]); | ||
} | ||
} | ||
function buildTable(type) { | ||
if (type === 'createLike') { | ||
@@ -129,2 +173,9 @@ return function (tableName, tableNameLike, fn) { | ||
function buildView(type) { | ||
return function (viewName, fn) { | ||
const builder = this.client.viewBuilder(type, viewName, fn); | ||
build.call(this, builder); | ||
}; | ||
} | ||
function prefixedTableName(prefix, table) { | ||
@@ -131,0 +182,0 @@ return prefix ? `${prefix}.${table}` : table; |
{ | ||
"name": "knex", | ||
"version": "0.95.12-rc3", | ||
"version": "0.95.12-rc4", | ||
"description": "A batteries-included SQL query & schema builder for PostgresSQL, MySQL, CockroachDB, MSSQL and SQLite3", | ||
@@ -5,0 +5,0 @@ "main": "knex", |
@@ -47,2 +47,28 @@ ## Upgrading to new knex.js versions | ||
* IDE autocomplete may stop working if you are using JavaScript (not TypeScript). There are reports for autocomplete still working correctly if knex is used this way: | ||
```js | ||
const knex = require('knex').knex({ | ||
//connection parameters | ||
}); | ||
``` | ||
It also works when using ESM imports: | ||
```js | ||
import { knex } from 'knex' | ||
const kn = knex({ | ||
//connection parameters | ||
}) | ||
``` | ||
For usage as param it can be addressed like this: | ||
```js | ||
/** | ||
* @param {import("knex").Knex} db | ||
*/ | ||
function up(db) { | ||
// Your code | ||
} | ||
``` | ||
* Syntax for QueryBuilder augmentation changed. Previously it looked like this: | ||
@@ -49,0 +75,0 @@ |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
759952
174
20082