Skip to content

Commit

Permalink
refactor(utils): extract toArray (#1127)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shinigami92 committed Apr 26, 2024
1 parent a194af2 commit 2a1a4ab
Show file tree
Hide file tree
Showing 21 changed files with 88 additions and 69 deletions.
3 changes: 2 additions & 1 deletion src/operations/extensions/createExtension.ts
@@ -1,4 +1,5 @@
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type {
DropOptions,
IfNotExistsOption,
Expand All @@ -22,7 +23,7 @@ export function createExtension(mOptions: MigrationOptions): CreateExtension {
const _create: CreateExtension = (_extensions, options = {}) => {
const { ifNotExists, schema } = options;

const extensions = Array.isArray(_extensions) ? _extensions : [_extensions];
const extensions = toArray(_extensions);
const ifNotExistsStr = ifNotExists ? ' IF NOT EXISTS' : '';
const schemaStr = schema ? ` SCHEMA ${mOptions.literal(schema)}` : '';

Expand Down
3 changes: 2 additions & 1 deletion src/operations/extensions/dropExtension.ts
@@ -1,4 +1,5 @@
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type { DropOptions } from '../generalTypes';
import type { StringExtension } from './shared';

Expand All @@ -11,7 +12,7 @@ export function dropExtension(mOptions: MigrationOptions): DropExtension {
const _drop: DropExtension = (_extensions, options = {}) => {
const { ifExists, cascade } = options;

const extensions = Array.isArray(_extensions) ? _extensions : [_extensions];
const extensions = toArray(_extensions);
const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const cascadeStr = cascade ? ' CASCADE' : '';

Expand Down
7 changes: 4 additions & 3 deletions src/operations/grants/grantOnSchemas.ts
@@ -1,12 +1,13 @@
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type { Name, Reversible } from '../generalTypes';
import { revokeOnSchemas } from './revokeOnSchemas';
import type {
RevokeOnObjectsOptions,
SchemaPrivilege,
WithGrantOption,
} from './shared';
import { asArray, asRolesStr } from './shared';
import { asRolesStr } from './shared';

export interface OnlyGrantOnSchemasOptions {
privileges: SchemaPrivilege | SchemaPrivilege[] | 'ALL';
Expand All @@ -32,8 +33,8 @@ export function grantOnSchemas(mOptions: MigrationOptions): GrantOnSchemas {
withGrantOption,
}) => {
const rolesStr = asRolesStr(roles, mOptions);
const schemasStr = asArray(schemas).map(mOptions.literal).join(', ');
const privilegesStr = asArray(privileges).map(String).join(', ');
const schemasStr = toArray(schemas).map(mOptions.literal).join(', ');
const privilegesStr = toArray(privileges).map(String).join(', ');
const withGrantOptionStr = withGrantOption ? ' WITH GRANT OPTION' : '';

return `GRANT ${privilegesStr} ON SCHEMA ${schemasStr} TO ${rolesStr}${withGrantOptionStr};`;
Expand Down
5 changes: 3 additions & 2 deletions src/operations/grants/grantOnTables.ts
@@ -1,4 +1,5 @@
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type { Reversible } from '../generalTypes';
import { revokeOnTables } from './revokeOnTables';
import type {
Expand All @@ -7,7 +8,7 @@ import type {
RevokeOnObjectsOptions,
SomeTablesOptions,
} from './shared';
import { asArray, asRolesStr, asTablesStr } from './shared';
import { asRolesStr, asTablesStr } from './shared';

export type GrantOnSomeTablesOptions = CommonGrantOnTablesOptions &
SomeTablesOptions;
Expand All @@ -29,7 +30,7 @@ export function grantOnTables(mOptions: MigrationOptions): GrantOnTables {
const _grantOnTables: GrantOnTables = (options) => {
const { privileges, roles, withGrantOption } = options;
const rolesStr = asRolesStr(roles, mOptions);
const privilegesStr = asArray(privileges).map(String).join(', ');
const privilegesStr = toArray(privileges).map(String).join(', ');
const tablesStr = asTablesStr(options, mOptions);
const withGrantOptionStr = withGrantOption ? ' WITH GRANT OPTION' : '';

Expand Down
6 changes: 3 additions & 3 deletions src/operations/grants/grantRoles.ts
@@ -1,9 +1,9 @@
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type { Name } from '../generalTypes';
import type { RevokeRolesOptions } from './revokeRoles';
import { revokeRoles } from './revokeRoles';
import type { WithAdminOption } from './shared';
import { asArray } from './shared';

export type GrantRolesOptions = WithAdminOption & RevokeRolesOptions;

Expand All @@ -17,8 +17,8 @@ export type GrantRoles = GrantRolesFn & { reverse: GrantRolesFn };

export function grantRoles(mOptions: MigrationOptions): GrantRoles {
const _grantRoles: GrantRoles = (rolesFrom, rolesTo, options) => {
const rolesFromStr = asArray(rolesFrom).map(mOptions.literal).join(', ');
const rolesToStr = asArray(rolesTo).map(mOptions.literal).join(', ');
const rolesFromStr = toArray(rolesFrom).map(mOptions.literal).join(', ');
const rolesToStr = toArray(rolesTo).map(mOptions.literal).join(', ');
const withAdminOptionStr =
options && options.withAdminOption ? ' WITH ADMIN OPTION' : '';

Expand Down
7 changes: 4 additions & 3 deletions src/operations/grants/revokeOnSchemas.ts
@@ -1,7 +1,8 @@
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type { OnlyGrantOnSchemasOptions } from './grantOnSchemas';
import type { RevokeOnObjectsOptions } from './shared';
import { asArray, asRolesStr } from './shared';
import { asRolesStr } from './shared';

export type RevokeOnSchemasOptions = OnlyGrantOnSchemasOptions &
RevokeOnObjectsOptions;
Expand All @@ -19,8 +20,8 @@ export function revokeOnSchemas(mOptions: MigrationOptions): RevokeOnSchemas {
cascade,
}) => {
const rolesStr = asRolesStr(roles, mOptions);
const schemasStr = asArray(schemas).map(mOptions.literal).join(', ');
const privilegesStr = asArray(privileges).map(String).join(', ');
const schemasStr = toArray(schemas).map(mOptions.literal).join(', ');
const privilegesStr = toArray(privileges).map(String).join(', ');
const onlyGrantOptionStr = onlyGrantOption ? ' GRANT OPTION FOR' : '';
const cascadeStr = cascade ? ' CASCADE' : '';

Expand Down
5 changes: 3 additions & 2 deletions src/operations/grants/revokeOnTables.ts
@@ -1,11 +1,12 @@
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type {
AllTablesOptions,
CommonOnTablesOptions,
RevokeOnObjectsOptions,
SomeTablesOptions,
} from './shared';
import { asArray, asRolesStr, asTablesStr } from './shared';
import { asRolesStr, asTablesStr } from './shared';

export type RevokeOnTablesOptions = CommonOnTablesOptions &
(AllTablesOptions | SomeTablesOptions) &
Expand All @@ -19,7 +20,7 @@ export function revokeOnTables(mOptions: MigrationOptions): RevokeOnTables {
const _revokeOnTables: RevokeOnTables = (options) => {
const { privileges, roles, onlyGrantOption, cascade } = options;
const rolesStr = asRolesStr(roles, mOptions);
const privilegesStr = asArray(privileges).map(String).join(', ');
const privilegesStr = toArray(privileges).map(String).join(', ');
const tablesStr = asTablesStr(options, mOptions);
const onlyGrantOptionStr = onlyGrantOption ? ' GRANT OPTION FOR' : '';
const cascadeStr = cascade ? ' CASCADE' : '';
Expand Down
6 changes: 3 additions & 3 deletions src/operations/grants/revokeRoles.ts
@@ -1,7 +1,7 @@
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type { CascadeOption, Name } from '../generalTypes';
import type { OnlyAdminOption } from './shared';
import { asArray } from './shared';

export type RevokeRolesOptions = OnlyAdminOption & CascadeOption;

Expand All @@ -13,8 +13,8 @@ export type RevokeRoles = (

export function revokeRoles(mOptions: MigrationOptions): RevokeRoles {
const _revokeRoles: RevokeRoles = (roles, rolesFrom, options) => {
const rolesStr = asArray(roles).map(mOptions.literal).join(', ');
const rolesToStr = asArray(rolesFrom).map(mOptions.literal).join(', ');
const rolesStr = toArray(roles).map(mOptions.literal).join(', ');
const rolesToStr = toArray(rolesFrom).map(mOptions.literal).join(', ');
const onlyAdminOptionStr =
options && options.onlyAdminOption ? ' ADMIN OPTION FOR' : '';
const cascadeStr = options && options.cascade ? ' CASCADE' : '';
Expand Down
9 changes: 3 additions & 6 deletions src/operations/grants/shared.ts
@@ -1,4 +1,5 @@
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type { CascadeOption, Name } from '../generalTypes';

export interface WithAdminOption {
Expand Down Expand Up @@ -47,10 +48,6 @@ export interface AllTablesOptions {

export type RevokeOnObjectsOptions = OnlyGrantOption & CascadeOption;

export function asArray<T>(item: T | T[]): T[] {
return Array.isArray(item) ? item : [item];
}

export function isAllTablesOptions(
options: AllTablesOptions | SomeTablesOptions
): options is AllTablesOptions {
Expand All @@ -61,7 +58,7 @@ export function asRolesStr(
roles: Name | Name[],
mOptions: MigrationOptions
): string {
return asArray(roles)
return toArray(roles)
.map((role) => (role === 'PUBLIC' ? role : mOptions.literal(role)))
.join(', ');
}
Expand All @@ -72,5 +69,5 @@ export function asTablesStr(
): string {
return isAllTablesOptions(options)
? `ALL TABLES IN SCHEMA ${mOptions.literal(options.schema)}`
: asArray(options.tables).map(mOptions.literal).join(', ');
: toArray(options.tables).map(mOptions.literal).join(', ');
}
11 changes: 3 additions & 8 deletions src/operations/indexes/createIndex.ts
@@ -1,4 +1,5 @@
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type { Name, Reversible } from '../generalTypes';
import type { DropIndexOptions } from './dropIndex';
import { dropIndex } from './dropIndex';
Expand Down Expand Up @@ -47,10 +48,7 @@ export function createIndex(mOptions: MigrationOptions): CreateIndex {
ifNotExists - optionally create index
options.method - [ btree | hash | gist | spgist | gin ]
*/
const columns = Array.isArray(rawColumns)
? rawColumns.slice()
: [rawColumns];

const columns = toArray(rawColumns);
if (options.opclass) {
mOptions.logger.warn(
"Using opclass is deprecated. You should use it as part of column definition e.g. pgm.createIndex('table', [['column', 'opclass', 'ASC']])"
Expand Down Expand Up @@ -82,10 +80,7 @@ export function createIndex(mOptions: MigrationOptions): CreateIndex {
const method = options.method ? ` USING ${options.method}` : '';
const where = options.where ? ` WHERE ${options.where}` : '';
const include = options.include
? ` INCLUDE (${(Array.isArray(options.include)
? options.include
: [options.include]
)
? ` INCLUDE (${toArray(options.include)
.map(mOptions.literal)
.join(', ')})`
: '';
Expand Down
5 changes: 2 additions & 3 deletions src/operations/indexes/dropIndex.ts
@@ -1,4 +1,5 @@
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type { DropOptions, Name } from '../generalTypes';
import type { IndexColumn } from './shared';
import { generateIndexName } from './shared';
Expand All @@ -21,9 +22,7 @@ export function dropIndex(mOptions: MigrationOptions): DropIndex {
const _drop: DropIndex = (tableName, rawColumns, options = {}) => {
const { concurrently, ifExists, cascade } = options;

const columns = Array.isArray(rawColumns)
? rawColumns.slice()
: [rawColumns];
const columns = toArray(rawColumns);
const concurrentlyStr = concurrently ? ' CONCURRENTLY' : '';
const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const indexName = generateIndexName(
Expand Down
5 changes: 2 additions & 3 deletions src/operations/materializedViews/createMaterializedView.ts
@@ -1,4 +1,5 @@
import type { MigrationOptions } from '../../types';
import { toArray } from '../../utils';
import type {
DropOptions,
IfNotExistsOption,
Expand Down Expand Up @@ -39,9 +40,7 @@ export function createMaterializedView(
data,
} = options;

const columnNames = (Array.isArray(columns) ? columns : [columns])
.map(mOptions.literal)
.join(', ');
const columnNames = toArray(columns).map(mOptions.literal).join(', ');
const withOptions = Object.keys(storageParameters)
.map(storageParameterStr(storageParameters))
.join(', ');
Expand Down
4 changes: 3 additions & 1 deletion src/operations/policies/shared.ts
@@ -1,3 +1,5 @@
import { toArray } from '../../utils';

export interface PolicyOptions {
role?: string | string[];

Expand All @@ -7,7 +9,7 @@ export interface PolicyOptions {
}

export function makeClauses({ role, using, check }: PolicyOptions): string[] {
const roles = (Array.isArray(role) ? role : [role]).join(', ');
const roles = toArray(role).join(', ');
const clauses: string[] = [];

if (roles) {
Expand Down
14 changes: 4 additions & 10 deletions src/operations/roles/shared.ts
@@ -1,4 +1,4 @@
import { escapeValue } from '../../utils';
import { escapeValue, toArray } from '../../utils';
import type { Value } from '../generalTypes';

export interface RoleOptions {
Expand Down Expand Up @@ -80,23 +80,17 @@ export function formatRoleOptions(roleOptions: RoleOptions = {}): string {
}

if (roleOptions.inRole) {
const inRole = Array.isArray(roleOptions.inRole)
? roleOptions.inRole.join(',')
: roleOptions.inRole;
const inRole = toArray(roleOptions.inRole).join(', ');
options.push(`IN ROLE ${inRole}`);
}

if (roleOptions.role) {
const role = Array.isArray(roleOptions.role)
? roleOptions.role.join(',')
: roleOptions.role;
const role = toArray(roleOptions.role).join(', ');
options.push(`ROLE ${role}`);
}

if (roleOptions.admin) {
const admin = Array.isArray(roleOptions.admin)
? roleOptions.admin.join(',')
: roleOptions.admin;
const admin = toArray(roleOptions.admin).join(', ');
options.push(`ADMIN ${admin}`);
}

Expand Down
18 changes: 7 additions & 11 deletions src/operations/tables/shared.ts
@@ -1,5 +1,5 @@
import type { Literal, MigrationOptions } from '../../types';
import { applyType, escapeValue, makeComment } from '../../utils';
import { applyType, escapeValue, makeComment, toArray } from '../../utils';
import type { FunctionParamType } from '../functions';
import type { IfNotExistsOption, Name, Value } from '../generalTypes';
import { parseSequenceOptions, type SequenceOptions } from '../sequences';
Expand Down Expand Up @@ -337,17 +337,15 @@ export function parseConstraints(
}

if (unique) {
const uniqueArray: Array<Name | Name[]> = Array.isArray(unique)
? unique
: [unique];
const uniqueArray: Array<Name | Name[]> = toArray(unique);
const isArrayOfArrays = uniqueArray.some((uniqueSet) =>
Array.isArray(uniqueSet)
);

(
(isArrayOfArrays ? uniqueArray : [uniqueArray]) as Array<Name | Name[]>
).forEach((uniqueSet) => {
const cols = Array.isArray(uniqueSet) ? uniqueSet : [uniqueSet];
const cols = toArray(uniqueSet);
const name = literal(optionName || `${tableName}_uniq_${cols.join('_')}`);

constraints.push(
Expand All @@ -358,19 +356,17 @@ export function parseConstraints(

if (primaryKey) {
const name = literal(optionName || `${tableName}_pkey`);
const key = (Array.isArray(primaryKey) ? primaryKey : [primaryKey])
.map(literal)
.join(', ');
const key = toArray(primaryKey).map(literal).join(', ');

constraints.push(`CONSTRAINT ${name} PRIMARY KEY (${key})`);
}

if (foreignKeys) {
(Array.isArray(foreignKeys) ? foreignKeys : [foreignKeys]).forEach((fk) => {
toArray(foreignKeys).forEach((fk) => {
const { columns, referencesConstraintName, referencesConstraintComment } =
fk;

const cols = Array.isArray(columns) ? columns : [columns];
const cols = toArray(columns);
const name = literal(
referencesConstraintName ||
optionName ||
Expand Down Expand Up @@ -434,7 +430,7 @@ export function parseLike(
name: 'INCLUDING' | 'EXCLUDING',
options?: Like | Like[]
) =>
(Array.isArray(options) ? options : [options])
toArray(options)
.filter((option): option is Like => option !== undefined)
.map((option) => ` ${name} ${option}`)
.join('');
Expand Down
7 changes: 2 additions & 5 deletions src/operations/triggers/createTrigger.ts
@@ -1,5 +1,5 @@
import type { MigrationOptions } from '../../types';
import { escapeValue } from '../../utils';
import { escapeValue, toArray } from '../../utils';
import type { FunctionOptions } from '../functions';
import { createFunction, dropFunction } from '../functions';
import type { DropOptions, Name, Reversible, Value } from '../generalTypes';
Expand Down Expand Up @@ -43,10 +43,7 @@ export function createTrigger(mOptions: MigrationOptions): CreateTrigger {

let { when, level = 'STATEMENT', function: functionName } = triggerOptions;

const operations = Array.isArray(operation)
? operation.join(' OR ')
: operation;

const operations = toArray(operation).join(' OR ');
if (constraint) {
when = 'AFTER';
}
Expand Down

0 comments on commit 2a1a4ab

Please sign in to comment.