Skip to content

Commit efb9dfc

Browse files
committed
Added support for reading config file in the migration sql generation
1 parent e7f16db commit efb9dfc

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/packages/pongo/src/commandLine/configFile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const sampleConfig = (collectionNames: string[] = ['users']) => {
2525
const types = collectionNames
2626
.map(
2727
(name) =>
28-
`type ${formatTypeName(name)} = { name: string; description: string; date: Date }`,
28+
`export type ${formatTypeName(name)} = { name: string; description: string; date: Date }`,
2929
)
3030
.join('\n');
3131

src/packages/pongo/src/commandLine/migrate.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface MigrateRunOptions {
1818
interface MigrateSqlOptions {
1919
print?: boolean;
2020
write?: string;
21+
config?: string;
2122
collection: string[];
2223
}
2324

@@ -29,7 +30,7 @@ migrateCommand
2930
.command('run')
3031
.description('Run database migrations')
3132
.option(
32-
'-cs, --connectionString <string>',
33+
'-cs, --connection-string <string>',
3334
'Connection string for the database',
3435
)
3536
.option(
@@ -51,7 +52,8 @@ migrateCommand
5152

5253
if (!connectionString) {
5354
console.error(
54-
'Error: Connection string is required. Provide it either as a "--connectionString" parameter or through the DB_CONNECTION_STRING environment variable.',
55+
'Error: Connection string is required. Provide it either as a "--connection-string" parameter or through the DB_CONNECTION_STRING environment variable.' +
56+
'\nFor instance: --connection-string postgresql://postgres:postgres@localhost:5432/postgres',
5557
);
5658
process.exit(1);
5759
}
@@ -94,24 +96,34 @@ migrateCommand
9496
},
9597
[] as string[],
9698
)
99+
.option('-f, --config <path>', 'Path to configuration file with Pongo config')
97100
.option('--print', 'Print the SQL to the console (default)', true)
98101
//.option('--write <filename>', 'Write the SQL to a specified file')
99-
.action((options: MigrateSqlOptions) => {
102+
.action(async (options: MigrateSqlOptions) => {
100103
const { collection } = options;
101104

102-
if (!collection) {
105+
let collectionNames: string[];
106+
107+
if (options.config) {
108+
const config = await loadConfigFile(options.config);
109+
110+
collectionNames = config.collections.map((c) => c.name);
111+
} else if (collection) {
112+
collectionNames = collection;
113+
} else {
103114
console.error(
104-
'Error: You need to provide at least one collection name is required. Provide it either as a "col" parameter.',
115+
'Error: You need to provide at least one collection name. Provide it either through "--config" file or as a "--collection" parameter.',
105116
);
106117
process.exit(1);
107118
}
119+
108120
const coreMigrations = migrationTableSchemaComponent.migrations({
109121
connector: 'PostgreSQL:pg',
110122
});
111123
const migrations = [
112124
...coreMigrations,
113-
...collection.flatMap((collectionsName) =>
114-
pongoCollectionSchemaComponent(collectionsName).migrations({
125+
...collectionNames.flatMap((collectionName) =>
126+
pongoCollectionSchemaComponent(collectionName).migrations({
115127
connector: 'PostgreSQL:pg', // TODO: Provide connector here
116128
}),
117129
),

0 commit comments

Comments
 (0)