Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using 'function' keyword #283

Merged
merged 1 commit into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions lib/operations/domains.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { template, applyType, escapeValue } from "../utils";

export const dropDomain = (domainName, { ifExists, cascade } = {}) => {
export function dropDomain(domainName, { ifExists, cascade } = {}) {
const ifExistsStr = ifExists ? " IF EXISTS" : "";
const cascadeStr = cascade ? " CASCADE" : "";
return template`DROP DOMAIN${ifExistsStr} "${domainName}"${cascadeStr};`;
};
}

export const createDomain = typeShorthands => {
export function createDomain(typeShorthands) {
const _create = (domainName, type, options = {}) => {
const {
default: defaultValue,
Expand Down Expand Up @@ -46,9 +46,9 @@ export const createDomain = typeShorthands => {
_create.reverse = (domainName, type, options) =>
dropDomain(domainName, options);
return _create;
};
}

export const alterDomain = (domainName, options) => {
export function alterDomain(domainName, options) {
const {
default: defaultValue,
notNull,
Expand Down Expand Up @@ -76,10 +76,11 @@ export const alterDomain = (domainName, options) => {
return `${actions
.map(action => template`ALTER DOMAIN "${domainName}" ${action}`)
.join(";\n")};`;
};
}

export const renameDomain = (domainName, newDomainName) =>
template`ALTER DOMAIN "${domainName}" RENAME TO "${newDomainName}";`;
export function renameDomain(domainName, newDomainName) {
return template`ALTER DOMAIN "${domainName}" RENAME TO "${newDomainName}";`;
}

const undoRename = (domainName, newDomainName) =>
renameDomain(newDomainName, domainName);
Expand Down
8 changes: 4 additions & 4 deletions lib/operations/extensions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from "lodash";
import { template } from "../utils";

export const createExtension = (extensions, { ifNotExists, schema } = {}) => {
export function createExtension(extensions, { ifNotExists, schema } = {}) {
if (!_.isArray(extensions)) extensions = [extensions]; // eslint-disable-line no-param-reassign
return _.map(
extensions,
Expand All @@ -10,9 +10,9 @@ export const createExtension = (extensions, { ifNotExists, schema } = {}) => {
ifNotExists ? " IF NOT EXISTS" : ""
} "${extension}"${schema ? ` SCHEMA "${schema}"` : ""};`
);
};
}

export const dropExtension = (extensions, { ifExists, cascade } = {}) => {
export function dropExtension(extensions, { ifExists, cascade } = {}) {
if (!_.isArray(extensions)) extensions = [extensions]; // eslint-disable-line no-param-reassign
return _.map(
extensions,
Expand All @@ -21,7 +21,7 @@ export const dropExtension = (extensions, { ifExists, cascade } = {}) => {
cascade ? " CASCADE" : ""
};`
);
};
}

// setup reverse functions
createExtension.reverse = dropExtension;
26 changes: 12 additions & 14 deletions lib/operations/functions.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { template, escapeValue, formatParams } from "../utils";

export const dropFunction = typeShorthands => (
functionName,
functionParams = [],
{ ifExists, cascade } = {}
) => {
const ifExistsStr = ifExists ? " IF EXISTS" : "";
const cascadeStr = cascade ? " CASCADE" : "";
const paramsStr = formatParams(functionParams, typeShorthands);
return template`DROP FUNCTION${ifExistsStr} "${functionName}"${paramsStr}${cascadeStr};`;
};
export function dropFunction(typeShorthands) {
return (functionName, functionParams = [], { ifExists, cascade } = {}) => {
const ifExistsStr = ifExists ? " IF EXISTS" : "";
const cascadeStr = cascade ? " CASCADE" : "";
const paramsStr = formatParams(functionParams, typeShorthands);
return template`DROP FUNCTION${ifExistsStr} "${functionName}"${paramsStr}${cascadeStr};`;
};
}

export const createFunction = typeShorthands => {
export function createFunction(typeShorthands) {
const _create = (
functionName,
functionParams = [],
Expand Down Expand Up @@ -60,9 +58,9 @@ export const createFunction = typeShorthands => {
_create.reverse = dropFunction(typeShorthands);

return _create;
};
}

export const renameFunction = typeShorthands => {
export function renameFunction(typeShorthands) {
const _rename = (oldFunctionName, functionParams = [], newFunctionName) => {
const paramsStr = formatParams(functionParams, typeShorthands);
return template`ALTER FUNCTION "${oldFunctionName}"${paramsStr} RENAME TO "${newFunctionName}";`;
Expand All @@ -72,4 +70,4 @@ export const renameFunction = typeShorthands => {
_rename(newFunctionName, functionParams, oldFunctionName);

return _rename;
};
}
8 changes: 4 additions & 4 deletions lib/operations/indexes.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function generateColumnsString(columns) {
: generateColumnString(columns);
}

export const createIndex = (tableName, columns, options = {}) => {
export function createIndex(tableName, columns, options = {}) {
/*
columns - the column, columns, or expression to create the index on

Expand All @@ -48,17 +48,17 @@ export const createIndex = (tableName, columns, options = {}) => {
const opclass = options.opclass ? ` ${options.opclass}` : "";

return template`CREATE ${unique} INDEX ${concurrently} "${indexName}" ON "${tableName}"${method} (${columnsString}${opclass})${where};`;
};
}

export const dropIndex = (tableName, columns, options = {}) => {
export function dropIndex(tableName, columns, options = {}) {
const { concurrently, ifExists, cascade } = options;
const concurrentlyStr = concurrently ? " CONCURRENTLY" : "";
const ifExistsStr = ifExists ? " IF EXISTS" : "";
const indexName = generateIndexName(tableName, columns, options);
const cascadeStr = cascade ? " CASCADE" : "";

return `DROP INDEX${concurrentlyStr}${ifExistsStr} "${indexName}"${cascadeStr};`;
};
}

// setup reverse functions
createIndex.reverse = dropIndex;
32 changes: 16 additions & 16 deletions lib/operations/operators.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ export const dropOperator = (operatorName, options = {}) => {
return `DROP OPERATOR${ifExistsStr} ${operatorNameStr}(${leftStr}, ${rightStr})${cascadeStr};`;
};

export const createOperatorFamily = (operatorFamilyName, indexMethod) => {
export function createOperatorFamily(operatorFamilyName, indexMethod) {
const operatorFamilyNameStr = schemalize(operatorFamilyName);
return `CREATE OPERATOR FAMILY ${operatorFamilyNameStr} USING ${indexMethod};`;
};
}

export const dropOperatorFamily = (
export function dropOperatorFamily(
operatorFamilyName,
indexMethod,
{ ifExists, cascade } = {}
) => {
) {
const operatorFamilyNameStr = schemalize(operatorFamilyName);
const ifExistsStr = ifExists ? " IF EXISTS" : "";
const cascadeStr = cascade ? " CASCADE" : "";
return `DROP OPERATOR FAMILY ${ifExistsStr} ${operatorFamilyNameStr} USING ${indexMethod}${cascadeStr};`;
};
}

const operatorMap = typeShorthands => ({
type = "",
Expand Down Expand Up @@ -118,16 +118,16 @@ export const addToOperatorFamily = changeOperatorFamily(
removeFromOperatorFamily
);

export const renameOperatorFamily = (
export function renameOperatorFamily(
oldOperatorFamilyName,
indexMethod,
newOperatorFamilyName
) => {
) {
const oldOperatorFamilyNameStr = schemalize(oldOperatorFamilyName);
const newOperatorFamilyNameStr = schemalize(newOperatorFamilyName);

return `ALTER OPERATOR FAMILY ${oldOperatorFamilyNameStr} USING ${indexMethod} RENAME TO ${newOperatorFamilyNameStr};`;
};
}

const undoRenameOperatorFamily = (
oldOperatorFamilyName,
Expand All @@ -140,19 +140,19 @@ const undoRenameOperatorFamily = (
oldOperatorFamilyName
);

export const dropOperatorClass = (
export function dropOperatorClass(
operatorClassName,
indexMethod,
{ ifExists, cascade } = {}
) => {
) {
const operatorClassNameStr = schemalize(operatorClassName);
const ifExistsStr = ifExists ? " IF EXISTS" : "";
const cascadeStr = cascade ? " CASCADE" : "";

return `DROP OPERATOR CLASS ${ifExistsStr} ${operatorClassNameStr} USING ${indexMethod}${cascadeStr};`;
};
}

export const createOperatorClass = typeShorthands => {
export function createOperatorClass(typeShorthands) {
const _create = (
operatorClassName,
type,
Expand Down Expand Up @@ -181,18 +181,18 @@ export const createOperatorClass = typeShorthands => {
options
) => dropOperatorClass(operatorClassName, indexMethod, options);
return _create;
};
}

export const renameOperatorClass = (
export function renameOperatorClass(
oldOperatorClassName,
indexMethod,
newOperatorClassName
) => {
) {
const oldOperatorClassNameStr = schemalize(oldOperatorClassName);
const newOperatorClassNameStr = schemalize(newOperatorClassName);

return `ALTER OPERATOR CLASS ${oldOperatorClassNameStr} USING ${indexMethod} RENAME TO ${newOperatorClassNameStr};`;
};
}

const undoRenameOperatorClass = (
oldOperatorClassName,
Expand Down
4 changes: 2 additions & 2 deletions lib/operations/other.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { t } from "../utils";

// eslint-disable-next-line import/prefer-default-export
export const sql = (...args) => {
export function sql(...args) {
// applies some very basic templating using the utils.p
let s = t(...args);
// add trailing ; if not present
if (s.lastIndexOf(";") !== s.length - 1) {
s += ";";
}
return s;
};
}
17 changes: 9 additions & 8 deletions lib/operations/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const makeClauses = ({ role, using, check }) => {
return clauses;
};

export const createPolicy = (tableName, policyName, options = {}) => {
export function createPolicy(tableName, policyName, options = {}) {
const createOptions = {
...options,
role: options.role || "PUBLIC"
Expand All @@ -26,20 +26,21 @@ export const createPolicy = (tableName, policyName, options = {}) => {
];
const clausesStr = clauses.join(" ");
return template`CREATE POLICY "${policyName}" ON "${tableName}" ${clausesStr};`;
};
}

export const alterPolicy = (tableName, policyName, options = {}) => {
export function alterPolicy(tableName, policyName, options = {}) {
const clausesStr = makeClauses(options).join(" ");
return template`ALTER POLICY "${policyName}" ON "${tableName}" ${clausesStr};`;
};
}

export const dropPolicy = (tableName, policyName, { ifExists } = {}) => {
export function dropPolicy(tableName, policyName, { ifExists } = {}) {
const ifExistsStr = ifExists ? " IF EXISTS" : "";
return template`DROP POLICY${ifExistsStr} "${policyName}" ON "${tableName}";`;
};
}

export const renamePolicy = (tableName, policyName, newPolicyName) =>
template`ALTER POLICY "${policyName}" ON "${tableName}" RENAME TO "${newPolicyName}";`;
export function renamePolicy(tableName, policyName, newPolicyName) {
return template`ALTER POLICY "${policyName}" ON "${tableName}" RENAME TO "${newPolicyName}";`;
}

const undoRename = (tableName, policyName, newPolicyName) =>
renamePolicy(tableName, newPolicyName, policyName);
Expand Down
18 changes: 10 additions & 8 deletions lib/operations/roles.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const formatRoleOptions = (roleOptions = {}) => {
return options.join(" ");
};

export const createRole = (roleName, roleOptions = {}) => {
export function createRole(roleName, roleOptions = {}) {
const options = formatRoleOptions({
...roleOptions,
superuser: roleOptions.superuser || false,
Expand All @@ -72,18 +72,20 @@ export const createRole = (roleName, roleOptions = {}) => {
});
const optionsStr = options ? ` WITH ${options}` : "";
return template`CREATE ROLE "${roleName}"${optionsStr};`;
};
}

export const dropRole = (roleName, { ifExists } = {}) =>
template`DROP ROLE${ifExists ? " IF EXISTS" : ""} "${roleName}";`;
export function dropRole(roleName, { ifExists } = {}) {
return template`DROP ROLE${ifExists ? " IF EXISTS" : ""} "${roleName}";`;
}

export const alterRole = (roleName, roleOptions = {}) => {
export function alterRole(roleName, roleOptions = {}) {
const options = formatRoleOptions(roleOptions);
return options ? template`ALTER ROLE "${roleName}" WITH ${options};` : "";
};
}

export const renameRole = (oldRoleName, newRoleName) =>
template`ALTER ROLE "${oldRoleName}" RENAME TO "${newRoleName}";`;
export function renameRole(oldRoleName, newRoleName) {
return template`ALTER ROLE "${oldRoleName}" RENAME TO "${newRoleName}";`;
}

const undoRename = (oldRoleName, newRoleName) =>
renameRole(newRoleName, oldRoleName);
Expand Down
16 changes: 7 additions & 9 deletions lib/operations/schemas.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { template } from "../utils";

export const dropSchema = (schemaName, { ifExists, cascade } = {}) => {
export function dropSchema(schemaName, { ifExists, cascade } = {}) {
const ifExistsStr = ifExists ? " IF EXISTS" : "";
const cascadeStr = cascade ? " CASCADE" : "";
return template`DROP SCHEMA${ifExistsStr} "${schemaName}"${cascadeStr};`;
};
}

export const createSchema = (
schemaName,
{ ifNotExists, authorization } = {}
) => {
export function createSchema(schemaName, { ifNotExists, authorization } = {}) {
const ifNotExistsStr = ifNotExists ? " IF NOT EXISTS" : "";
const authorizationStr = authorization
? ` AUTHORIZATION ${authorization}`
: "";
return template`CREATE SCHEMA${ifNotExistsStr} "${schemaName}"${authorizationStr};`;
};
}

export const renameSchema = (schemaName, newSchemaName) =>
template`ALTER SCHEMA "${schemaName}" RENAME TO "${newSchemaName}";`;
export function renameSchema(schemaName, newSchemaName) {
return template`ALTER SCHEMA "${schemaName}" RENAME TO "${newSchemaName}";`;
}

const undoRename = (schemaName, newSchemaName) =>
renameSchema(newSchemaName, schemaName);
Expand Down
Loading