Skip to content

Commit

Permalink
BREAKING: New syntax for installSchemaFromString
Browse files Browse the repository at this point in the history
  • Loading branch information
voxpelli committed Aug 17, 2023
1 parent a4e9e5e commit 214405f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ import { installSchemaFromString } from 'umzeption';
export const umzeptionConfig = {
dependencies: ['@yikesable/abc'],
glob: ['migrations/*.js'],
// installSchemaFromString accepts a string or a Promise resolving to a string
installSchema: installSchemaFromString(
readFile(new URL('create-tables.sql', import.meta.url), 'utf8')
),
installSchema: async ({ context }) => {
const tables = await readFile(new URL('create-tables.sql', import.meta.url), 'utf8');
return installSchemaFromString(context, tables);
},
};
```
Expand Down
27 changes: 13 additions & 14 deletions lib/schema-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ function getTablesFromString (createTablesString) {

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

await context.value.transact(async client => {
for (const table of tables) {
Expand All @@ -27,17 +27,16 @@ export async function pgInstallSchemaFromString (context, createTablesString) {
}

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

0 comments on commit 214405f

Please sign in to comment.