Skip to content

Commit

Permalink
Add pgInstallSchemaFromString helper
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Aug 17, 2023
1 parent 2853a7c commit 66f689b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export {

export {
installSchemaFromString,
pgInstallSchemaFromString,
} from './lib/schema-helper.js';

export {
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export {

export {
installSchemaFromString,
pgInstallSchemaFromString,
} from './lib/schema-helper.js';

export {
Expand Down
31 changes: 21 additions & 10 deletions lib/schema-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,33 @@ function getTablesFromString (createTablesString) {
});
}

/**
* @param {import('./advanced-types.js').DefineUmzeptionContexts['pg']} context
* @param {string|Promise<string>} createTablesString
* @returns {Promise<void>}
*/
export async function pgInstallSchemaFromString (context, createTablesString) {
const tables = getTablesFromString(await createTablesString);

await context.value.transact(async client => {
for (const table of tables) {
await client.query(table);
}
});
}

/**
* @param {string|Promise<string>} createTablesString
* @returns {import('umzug').MigrationFn<import('./advanced-types.js').AnyUmzeptionContext>}
*/
export function installSchemaFromString (createTablesString) {
return async ({ context }) => {
if (context.type !== 'pg') {
throw new Error(`Unsupported context type: ${context.type}`);
switch (context.type) {
case 'pg':
await pgInstallSchemaFromString(context, createTablesString);
break;
default:
throw new Error(`Unsupported context type: ${context.type}`);
}

const tables = getTablesFromString(await createTablesString);

await context.value.transact(async client => {
for (const table of tables) {
await client.query(table);
}
});
};
}

0 comments on commit 66f689b

Please sign in to comment.