Skip to content

Commit

Permalink
chore: implementing npm scripts to create and execute migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
artursudnik committed Dec 20, 2022
1 parent 7ac9a68 commit d086536
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 87 deletions.
10 changes: 8 additions & 2 deletions apps/vc-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"write-openapi": "ts-node ./scripts/write-open-api-json.ts && prettier -w docs/openapi.json",
"update-openapi": "npm run write-openapi; git add docs/openapi.json"
"update-openapi": "npm run write-openapi; git add docs/openapi.json",
"migration:generate:pg": "export DB_TYPE=POSTGRES; npm run migration:run && cd src/migrations/pg && typeorm-ts-node-commonjs -d ../../config/ormconfig.ts migration:generate -p",
"migration:generate:sqlite": "export DB_TYPE=SQLITE; npm run migration:run && cd src/migrations/sqlite && typeorm-ts-node-commonjs -d ../../config/ormconfig.ts migration:generate -p",
"migration:run": "typeorm-ts-node-commonjs -d src/config/ormconfig.ts migration:run",
"migration:run:pg": "export DB_TYPE=POSTGRES; typeorm-ts-node-commonjs -d src/config/ormconfig.ts migration:run",
"migration:run:sqlite": "export DB_TYPE=SQLITE; typeorm-ts-node-commonjs -d src/config/ormconfig.ts migration:run"
},
"dependencies": {
"@energyweb/ssi-did": "0.0.1",
Expand All @@ -49,7 +54,8 @@
"@nestjs/axios": "^0.1.0",
"@nestjs/serve-static": "^3.0.0",
"joi": "^17.0.0",
"pg": "^8.0.0"
"pg": "^8.0.0",
"dotenv": "^16.0.0"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
Expand Down
15 changes: 14 additions & 1 deletion apps/vc-api/src/config/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';
import { ConfigService } from '@nestjs/config';
import { DB_TYPES } from './env-vars-validation-schema';
import * as path from 'path';

/**
* Inspired by https://dev.to/webeleon/unit-testing-nestjs-with-typeorm-in-memory-l6m
Expand All @@ -43,12 +44,23 @@ export const typeOrmConfigFactory = (config: ConfigService): TypeOrmModuleOption
}

if (config.get<DB_TYPES>('DB_TYPE') === DB_TYPES.SQLITE) {
const SQLITE_FILE = config.get('SQLITE_FILE');
if (!SQLITE_FILE) {
throw new Error(`SQLITE_FILE not set`);
}

// this is required because migration generation is executed from the migrations subfolder
const databaseFilePath = path.isAbsolute(SQLITE_FILE)
? SQLITE_FILE
: path.resolve(__dirname, '../..', SQLITE_FILE);

return {
type: 'better-sqlite3',
database: config.get('SQLITE_FILE'),
database: databaseFilePath,
dropSchema: config.get('DB_DROP_SCHEMA'),
synchronize: config.get('DB_SYNCHRONIZE'),
migrationsRun: config.get('DB_RUN_MIGRATIONS'),
migrations: [`${path.resolve(__dirname, '../migrations/sqlite')}/*.{ts,js}`],
...commonOptions
};
}
Expand All @@ -64,6 +76,7 @@ export const typeOrmConfigFactory = (config: ConfigService): TypeOrmModuleOption
dropSchema: config.get('DB_DROP_SCHEMA'),
synchronize: config.get('DB_SYNCHRONIZE'),
migrationsRun: config.get('DB_RUN_MIGRATIONS'),
migrations: [`${path.resolve(__dirname, '../migrations/pg')}/*.{ts,js}`],
...commonOptions
};
}
Expand Down
48 changes: 48 additions & 0 deletions apps/vc-api/src/config/ormconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2021, 2022 Energy Web Foundation
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { config } from 'dotenv';
import * as path from 'path';

config({ path: path.resolve(__dirname, '../../.env') });

import { DataSource, DataSourceOptions } from 'typeorm';
import { typeOrmConfigFactory } from './db';
import * as process from 'process';
import { ConfigService } from '@nestjs/config';
import { envVarsValidationSchema } from './env-vars-validation-schema';

const entities = [path.resolve(__dirname, '../**/*.entity.ts')];

const validationResults = envVarsValidationSchema.validate(process.env, {
allowUnknown: true,
abortEarly: false,
stripUnknown: true
});

if (validationResults.error) {
console.log('env variables errors:');
console.log(validationResults.error.details.map((e) => ` ${e.message}`).join('\n'));
console.log('\nexiting');
process.exit(1);
}

module.exports = {
dataSource: new DataSource({
...typeOrmConfigFactory({ get: (key: string, def?: string) => process.env[key] || def } as ConfigService),
entities
} as DataSourceOptions)
};
92 changes: 8 additions & 84 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d086536

Please sign in to comment.