mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-09-07 18:55:42 +00:00
telebirr out in the payment
This commit is contained in:
@@ -1,19 +1,128 @@
|
||||
import { registerAs } from "@nestjs/config";
|
||||
import { TypeOrmModuleOptions } from "@nestjs/typeorm";
|
||||
import { join, dirname } from "path";
|
||||
import {
|
||||
DefaultPosition,
|
||||
DefaultUnit,
|
||||
EmployeePosition,
|
||||
Employee,
|
||||
OrganizationConfiguration,
|
||||
GlobalOrganizationConfiguration,
|
||||
OrganizationType,
|
||||
Organization,
|
||||
PositionConfiguration,
|
||||
PositionPermission,
|
||||
PositionTypeConfiguration,
|
||||
PositionTypePermission,
|
||||
PositionType,
|
||||
Position,
|
||||
Project,
|
||||
UnitSetting,
|
||||
GlobalUnitConfiguration,
|
||||
Unit,
|
||||
EmployeeSignature,
|
||||
EmployeeStamp,
|
||||
RecordFooter,
|
||||
RecordHeader,
|
||||
Seal,
|
||||
AccountConfiguration,
|
||||
Application,
|
||||
DocumentaryRequirement,
|
||||
Permission,
|
||||
RolePermission,
|
||||
Role,
|
||||
Session,
|
||||
UserCredential,
|
||||
UserDocument,
|
||||
UserRole,
|
||||
UserVerification,
|
||||
User,
|
||||
Notification,
|
||||
NotificationEvent,
|
||||
NotificationPlaceholder,
|
||||
NotificationReceiverField,
|
||||
NotificationReceiver,
|
||||
NotificationTemplate,
|
||||
} from "@tria-plc/iamapi-common";
|
||||
import { OrganizationSetting } from "@tria-plc/iamapi-common/entities/iam/organization-structure/organization-setting.entity";
|
||||
import { APPLICATION_SEARCH_PATH } from "./ensure-postgres-schemas";
|
||||
|
||||
const iamEntities = [
|
||||
DefaultPosition,
|
||||
DefaultUnit,
|
||||
EmployeePosition,
|
||||
Employee,
|
||||
OrganizationConfiguration,
|
||||
GlobalOrganizationConfiguration,
|
||||
OrganizationSetting,
|
||||
OrganizationType,
|
||||
Organization,
|
||||
PositionConfiguration,
|
||||
PositionPermission,
|
||||
PositionTypeConfiguration,
|
||||
PositionTypePermission,
|
||||
PositionType,
|
||||
Position,
|
||||
Project,
|
||||
UnitSetting,
|
||||
GlobalUnitConfiguration,
|
||||
Unit,
|
||||
EmployeeSignature,
|
||||
EmployeeStamp,
|
||||
RecordFooter,
|
||||
RecordHeader,
|
||||
Seal,
|
||||
AccountConfiguration,
|
||||
Application,
|
||||
DocumentaryRequirement,
|
||||
Permission,
|
||||
RolePermission,
|
||||
Role,
|
||||
Session,
|
||||
UserCredential,
|
||||
UserDocument,
|
||||
UserRole,
|
||||
UserVerification,
|
||||
User,
|
||||
Notification,
|
||||
NotificationEvent,
|
||||
NotificationPlaceholder,
|
||||
NotificationReceiverField,
|
||||
NotificationReceiver,
|
||||
NotificationTemplate,
|
||||
];
|
||||
|
||||
const iamMigrationsGlob = join(
|
||||
dirname(require.resolve("@tria-plc/iamapi-common/package.json")),
|
||||
"dist/db/migrations/*.js",
|
||||
);
|
||||
const freightMigrationsGlob = join(__dirname, "../migrations/*.js");
|
||||
|
||||
export default registerAs(
|
||||
"database",
|
||||
(): TypeOrmModuleOptions => ({
|
||||
(): TypeOrmModuleOptions => {
|
||||
return {
|
||||
type: "postgres",
|
||||
host: process.env.DB_HOST ?? "localhost",
|
||||
port: parseInt(process.env.DB_PORT ?? "5433", 10),
|
||||
username: process.env.DB_USER ?? "postgres",
|
||||
password: process.env.DB_PASSWORD ?? "",
|
||||
database: process.env.DB_NAME ?? "edr_freight",
|
||||
entities: [__dirname + "/../**/*.entity.{ts,js}"],
|
||||
migrations: [__dirname + "/../../migrations/*.{ts,js}"],
|
||||
// Never enable synchronize in production. Use migrations.
|
||||
synchronize: process.env.NODE_ENV === "development",
|
||||
schema: "public",
|
||||
extra: {
|
||||
options: `-c search_path=${APPLICATION_SEARCH_PATH}`,
|
||||
},
|
||||
entities: [__dirname + "/../**/*.entity.{ts,js}", ...iamEntities],
|
||||
autoLoadEntities: true,
|
||||
migrations: [
|
||||
// IAM schema + tables must be created before freight migrations
|
||||
iamMigrationsGlob,
|
||||
freightMigrationsGlob,
|
||||
],
|
||||
migrationsRun: true,
|
||||
// Schema changes via migrations only (synchronize breaks ITMLS backfill on existing rows).
|
||||
synchronize: false,
|
||||
logging: process.env.NODE_ENV === "development",
|
||||
}),
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
50
apps/edr-freight-api/src/config/ensure-postgres-schemas.ts
Normal file
50
apps/edr-freight-api/src/config/ensure-postgres-schemas.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { DataSource, DataSourceOptions } from "typeorm";
|
||||
|
||||
/** Schemas required before TypeORM migrations and entity access. */
|
||||
export const APPLICATION_SCHEMAS = [
|
||||
"public",
|
||||
"iam",
|
||||
"freight",
|
||||
"audit",
|
||||
] as const;
|
||||
|
||||
export const APPLICATION_SEARCH_PATH = APPLICATION_SCHEMAS.join(",");
|
||||
|
||||
/**
|
||||
* TypeORM creates the migrations table before any migration runs. If `public` was
|
||||
* dropped, current_schema() is null and CREATE TABLE migrations fails.
|
||||
* IAM/freight migrations assume their schemas already exist.
|
||||
*/
|
||||
export async function ensurePostgresSchemas(
|
||||
options: DataSourceOptions,
|
||||
): Promise<void> {
|
||||
const bootstrap = new DataSource({
|
||||
...options,
|
||||
entities: [],
|
||||
migrations: [],
|
||||
migrationsRun: false,
|
||||
synchronize: false,
|
||||
});
|
||||
|
||||
await bootstrap.initialize();
|
||||
|
||||
for (const schema of APPLICATION_SCHEMAS) {
|
||||
if (schema === "public") {
|
||||
await bootstrap.query(`CREATE SCHEMA IF NOT EXISTS public`);
|
||||
await bootstrap.query(`GRANT ALL ON SCHEMA public TO public`);
|
||||
await bootstrap.query(`GRANT CREATE ON SCHEMA public TO public`);
|
||||
} else {
|
||||
await bootstrap.query(`CREATE SCHEMA IF NOT EXISTS "${schema}"`);
|
||||
await bootstrap.query(`GRANT USAGE ON SCHEMA "${schema}" TO public`);
|
||||
await bootstrap.query(
|
||||
`GRANT CREATE ON SCHEMA "${schema}" TO public`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
await bootstrap.query(
|
||||
`SET search_path TO ${APPLICATION_SEARCH_PATH}`,
|
||||
);
|
||||
|
||||
await bootstrap.destroy();
|
||||
}
|
||||
Reference in New Issue
Block a user