Skip to content

Commit

Permalink
fix: added WithTypeOrmQuery type to core module and renamed enableUui…
Browse files Browse the repository at this point in the history
…dv4 to enablePostgresUuidExtension
  • Loading branch information
sksadjad committed Nov 24, 2023
1 parent d7c1237 commit 9bfb597
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/data-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"typeorm-postgres:migration:run": "pnpm run typeorm -- migration:run -c migration-postgres"
},
"dependencies": {
"@sphereon/ssi-sdk.core": "workspace:*",
"@sphereon/ssi-types": "workspace:*",
"@veramo/core": "4.2.0",
"class-validator": "^0.14.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/data-store/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export { AbstractContactStore } from './contact/AbstractContactStore'
export { AbstractIssuanceBrandingStore } from './issuanceBranding/AbstractIssuanceBrandingStore'
export { IssuanceBrandingStore } from './issuanceBranding/IssuanceBrandingStore'
export { StatusListStore } from './statusList/StatusListStore'
export { DataStoreMigrations, enableUuidv4 } from './migrations'
export { DataStoreMigrations } from './migrations'
export * from './types'
export * from './utils/contact/MappingUtils'

Expand Down
2 changes: 1 addition & 1 deletion packages/data-store/src/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { DataStoreMigrations } from './generic'
export { enableUuidv4 } from './postgres/uuid'
export { createQueryRunnerAdapter } from './postgres/utils'
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { MigrationInterface, QueryRunner } from 'typeorm'
import { enableUuidv4 } from './uuid'
import { createQueryRunnerAdapter } from './utils'
import { enablePostgresUuidExtension } from '@sphereon/ssi-sdk.core'

export class CreateContacts1659463079428 implements MigrationInterface {
name = 'CreateContacts1659463079428'

public async up(queryRunner: QueryRunner): Promise<void> {
await enableUuidv4(queryRunner)
await enablePostgresUuidExtension(createQueryRunnerAdapter(queryRunner))
await queryRunner.query(
`CREATE TABLE "BaseConfigEntity" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "client_id" character varying(255), "client_secret" character varying(255), "scopes" text, "issuer" character varying(255), "redirect_url" text, "dangerously_allow_insecure_http_requests" boolean, "client_auth_method" text, "identifier" character varying(255), "session_id" character varying(255), "type" character varying NOT NULL, "connectionId" uuid, CONSTRAINT "REL_BaseConfig_connectionId" UNIQUE ("connectionId"), CONSTRAINT "PK_BaseConfigEntity_id" PRIMARY KEY ("id"))`
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { MigrationInterface, QueryRunner } from 'typeorm'
import { enableUuidv4 } from './uuid'
import { createQueryRunnerAdapter } from './utils'
import { enablePostgresUuidExtension } from '@sphereon/ssi-sdk.core'

export class CreateIssuanceBranding1685628974232 implements MigrationInterface {
name = 'CreateIssuanceBranding1685628974232'

public async up(queryRunner: QueryRunner): Promise<void> {
await enableUuidv4(queryRunner)
await enablePostgresUuidExtension(createQueryRunnerAdapter(queryRunner))
await queryRunner.query(
`CREATE TABLE "ImageDimensions" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "width" integer NOT NULL, "height" integer NOT NULL, CONSTRAINT "PK_ImageDimensions_id" PRIMARY KEY ("id"))`
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { MigrationInterface, QueryRunner } from 'typeorm'
import { enableUuidv4 } from './uuid'
import { createQueryRunnerAdapter } from './utils'
import { enablePostgresUuidExtension } from '@sphereon/ssi-sdk.core'

export class CreateContacts1690925872592 implements MigrationInterface {
name = 'CreateContacts1690925872592'

public async up(queryRunner: QueryRunner): Promise<void> {
await enableUuidv4(queryRunner)
await enablePostgresUuidExtension(createQueryRunnerAdapter(queryRunner))
await queryRunner.query(`ALTER TABLE "CorrelationIdentifier" DROP CONSTRAINT "FK_CorrelationIdentifier_identityId"`)
await queryRunner.query(`ALTER TABLE "IdentityMetadata" DROP CONSTRAINT "FK_IdentityMetadata_identityId"`)
await queryRunner.query(`ALTER TABLE "Identity" DROP CONSTRAINT "FK_Identity_contactId"`)
Expand Down
8 changes: 8 additions & 0 deletions packages/data-store/src/migrations/postgres/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { QueryRunner } from 'typeorm'
import { WithTypeOrmQuery } from '@sphereon/ssi-sdk.core'

export function createQueryRunnerAdapter(queryRunner: QueryRunner): WithTypeOrmQuery {
return (query: string, parameters?: any[]) => {
return queryRunner.query(query, parameters);
};
}
12 changes: 0 additions & 12 deletions packages/data-store/src/migrations/postgres/uuid.ts

This file was deleted.

16 changes: 16 additions & 0 deletions packages/ssi-sdk-core/src/utils/database.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
export const flattenArray = <T>(args: { items: Array<T | Array<T>> }): Array<T> => args.items.flat() as Array<T>

export const flattenMigrations = <T>(args: { migrations: Array<T | Array<T>> }): Array<T> => args.migrations.flat() as Array<T>

export type WithTypeOrmQuery =
((query: string, parameters?: any[]) => Promise<any>) |
((query: string, parameters?: any[], useStructuredResult?: boolean) => Promise<any>)


export const enablePostgresUuidExtension = async (query: WithTypeOrmQuery) => {
try {
await query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`);
} catch (error) {
console.log(
`Please enable the uuid-ossp.control extension in your PostgreSQL installation. It enables generating V4 UUID and can be found in the postgresql-contrib package`
);
throw error;
}
}
5 changes: 4 additions & 1 deletion pnpm-lock.yaml

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

0 comments on commit 9bfb597

Please sign in to comment.