///
import SqlDDLSync = require("@fxjs/sql-ddl-sync")
import { addPromiseInterface } from "./utils";
//lets you attach further metadata to column definition
//e.g. 'references product(id) on delete cascade'
var getColumnMetadata = function(property: FxOrmSqlDDLSync__Column.Property): "" | string {
return property.hasOwnProperty('addSQL') ? property.addSQL : "";
};
// duplicated from sql-ddl-sync Sync closure
class MigrationDSL implements FxOrmPlugin__MigrationDSL.MigrationDSL {
driver: FxOrmSqlDDLSync__Driver.Driver
Dialect: FxOrmSqlDDLSync__Dialect.Dialect
constructor (driver: FxOrmSqlDDLSync__Driver.Driver) {
this.driver = driver;
this.Dialect = SqlDDLSync.dialect(driver.dialect);
this.Dialect.escapeId = driver.query.escapeId;
}
/**
*
* @param collection collection table name
* @param name column name
* @param property property descriptor
* @param Dialect
* @param driver
*/
createColumn (
collection: FxOrmSqlDDLSync.TableName,
name: string,
property: FxOrmSqlDDLSync__Column.Property,
Dialect: FxOrmSqlDDLSync__Dialect.Dialect,
driver: FxOrmSqlDDLSync__Driver.Driver
): false | FxOrmSqlDDLSync__Column.OpResult__CreateColumn {
var type = Dialect.getType(collection, property, driver);
if (type === false) {
return false;
}
if (typeof type == "string") {
type = { value : type };
}
var meta = getColumnMetadata(property);
return {
value : `${Dialect.escapeId(name)} ${type.value} ${meta}`,
before : type.before
};
};
// ----- Migration DSL functions
// duplicated and altered from sql-ddl-sync Sync closure
createTable (
collectionName: string,
properties: FxOrmPlugin__MigrationDSL.Properties__createTable,
cb: FxOrmSqlDDLSync.ExecutionCallback
) {
const columns = [];
let keys = [];
for (let k in properties) {
const col = this.createColumn(collectionName, k, properties[k], this.Dialect, this.driver);
if (col === false) {
return cb(new Error("Unknown type for property '" + k + "'"));
}
// `primary` is deprecated in favour of `key`
if (properties[k].key || properties[k].primary) {
keys.push(k);
}
if (typeof this.Dialect.processKeys == "function") {
keys = this.Dialect.processKeys(keys);
}
columns.push(col.value);
}
this.Dialect.createCollection(this.driver, collectionName, columns, keys, cb);
}
addColumn (
collectionName: FxOrmSqlDDLSync.TableName,
properties: FxOrmPlugin__MigrationDSL.Properties__addColumn,
cb: FxOrmSqlDDLSync.ExecutionCallback
) {
var columnName = Object.keys(properties)[0]
var column = this.createColumn(collectionName, columnName, properties[columnName], this.Dialect, this.driver);
if (column)
this.Dialect.addCollectionColumn(this.driver, collectionName, column.value, null, cb);
}
renameColumn (
collectionName: FxOrmSqlDDLSync.TableName,
oldName: string,
newName: string,
cb: FxOrmSqlDDLSync.ExecutionCallback
) {
this.Dialect.renameCollectionColumn(this.driver, collectionName, oldName, newName, cb);
}
addIndex (
indexName: string,
options: FxOrmPlugin__MigrationDSL.Options__addIndex,
cb: FxOrmSqlDDLSync.ExecutionCallback
) {
this.Dialect.addIndex(this.driver, indexName, options.unique, options.table, options.columns, cb);
}
dropIndex (
collectionName: FxOrmSqlDDLSync.TableName,
indexName: string,
cb: FxOrmSqlDDLSync.ExecutionCallback
) {
this.Dialect.removeIndex(this.driver, indexName, collectionName, cb);
}
dropColumn (
collectionName: FxOrmSqlDDLSync.TableName,
columnName: string,
cb: FxOrmSqlDDLSync.ExecutionCallback
) {
this.Dialect.dropCollectionColumn(this.driver, collectionName, columnName, cb);
}
dropTable (
collectionName: FxOrmSqlDDLSync.TableName,
cb: FxOrmSqlDDLSync.ExecutionCallback
) {
this.Dialect.dropCollection(this.driver, collectionName, cb);
}
addPrimaryKey (
collectionName: FxOrmSqlDDLSync.TableName,
columnName: string,
cb: FxOrmSqlDDLSync.ExecutionCallback
) {
this.Dialect.addPrimaryKey(this.driver, collectionName, columnName, cb);
}
dropPrimaryKey (
collectionName: FxOrmSqlDDLSync.TableName,
columnName: string,
cb: FxOrmSqlDDLSync.ExecutionCallback
) {
this.Dialect.dropPrimaryKey(this.driver, collectionName, columnName, cb);
}
addForeignKey (
collectionName: FxOrmSqlDDLSync.TableName,
options: FxOrmPlugin__MigrationDSL.Options__addForeignKey,
cb: FxOrmSqlDDLSync.ExecutionCallback
) {
this.Dialect.addForeignKey(this.driver, collectionName, options, cb);
}
dropForeignKey (
collectionName: FxOrmSqlDDLSync.TableName,
columnName: string,
cb: FxOrmSqlDDLSync.ExecutionCallback
) {
this.Dialect.dropForeignKey(this.driver, collectionName, columnName, cb);
}
hasTable (
collectionName: FxOrmSqlDDLSync.TableName,
cb: FxOrmSqlDDLSync.ExecutionCallback
) {
this.Dialect.hasCollection(this.driver, collectionName, cb);
}
getColumns (
collectionName: FxOrmSqlDDLSync.TableName,
cb: FxOrmSqlDDLSync.ExecutionCallback
) {
this.Dialect.getCollectionProperties(this.driver, collectionName, cb);
}
execQuery (
query: string,
args: (string|number|Date|Class_Buffer|any)[],
cb: FxOrmSqlDDLSync.ExecutionCallback
) {
this.driver.execQuery(query, args, cb);
}
// comment out for now
// renameTable (
// oldCollectionName,
// newCollectionName,
// cb: FxOrmSqlDDLSync.ExecutionCallback
// ) {
// this.Dialect.renameTable(this.driver, oldCollectionName, newCollectionName, cb);
// }
}
;[
"createTable",
"addColumn",
"renameColumn",
"addIndex",
"dropIndex",
"dropColumn",
"dropTable",
"addPrimaryKey",
"addForeignKey",
"dropPrimaryKey",
"dropForeignKey",
"hasTable",
"getColumns",
"execQuery"
].forEach(methodName => {
MigrationDSL.prototype[methodName] = addPromiseInterface(MigrationDSL.prototype[methodName])
});
export = MigrationDSL