From 9bfb597c378d3ca269cabcc001dc672f56a1be0a Mon Sep 17 00:00:00 2001 From: sksadjad Date: Fri, 24 Nov 2023 18:40:15 +0100 Subject: [PATCH] fix: added WithTypeOrmQuery type to core module and renamed enableUuidv4 to enablePostgresUuidExtension --- packages/data-store/package.json | 1 + packages/data-store/src/index.ts | 2 +- packages/data-store/src/migrations/index.ts | 2 +- .../postgres/1659463079428-CreateContacts.ts | 5 +++-- .../1685628974232-CreateIssuanceBranding.ts | 5 +++-- .../postgres/1690925872592-CreateContacts.ts | 5 +++-- .../data-store/src/migrations/postgres/utils.ts | 8 ++++++++ .../data-store/src/migrations/postgres/uuid.ts | 12 ------------ packages/ssi-sdk-core/src/utils/database.ts | 16 ++++++++++++++++ pnpm-lock.yaml | 5 ++++- 10 files changed, 40 insertions(+), 21 deletions(-) create mode 100644 packages/data-store/src/migrations/postgres/utils.ts delete mode 100644 packages/data-store/src/migrations/postgres/uuid.ts diff --git a/packages/data-store/package.json b/packages/data-store/package.json index f96410110..fad29cbe1 100644 --- a/packages/data-store/package.json +++ b/packages/data-store/package.json @@ -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", diff --git a/packages/data-store/src/index.ts b/packages/data-store/src/index.ts index 889b3b6ca..8c03dad94 100644 --- a/packages/data-store/src/index.ts +++ b/packages/data-store/src/index.ts @@ -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' diff --git a/packages/data-store/src/migrations/index.ts b/packages/data-store/src/migrations/index.ts index cc3ccde60..e107873b2 100644 --- a/packages/data-store/src/migrations/index.ts +++ b/packages/data-store/src/migrations/index.ts @@ -1,2 +1,2 @@ export { DataStoreMigrations } from './generic' -export { enableUuidv4 } from './postgres/uuid' +export { createQueryRunnerAdapter } from './postgres/utils' diff --git a/packages/data-store/src/migrations/postgres/1659463079428-CreateContacts.ts b/packages/data-store/src/migrations/postgres/1659463079428-CreateContacts.ts index 691e87642..e15f19e9e 100644 --- a/packages/data-store/src/migrations/postgres/1659463079428-CreateContacts.ts +++ b/packages/data-store/src/migrations/postgres/1659463079428-CreateContacts.ts @@ -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 { - 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"))` ) diff --git a/packages/data-store/src/migrations/postgres/1685628974232-CreateIssuanceBranding.ts b/packages/data-store/src/migrations/postgres/1685628974232-CreateIssuanceBranding.ts index 969615420..e7ed6d476 100644 --- a/packages/data-store/src/migrations/postgres/1685628974232-CreateIssuanceBranding.ts +++ b/packages/data-store/src/migrations/postgres/1685628974232-CreateIssuanceBranding.ts @@ -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 { - 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"))` ) diff --git a/packages/data-store/src/migrations/postgres/1690925872592-CreateContacts.ts b/packages/data-store/src/migrations/postgres/1690925872592-CreateContacts.ts index b928cb5cc..a53c68a95 100644 --- a/packages/data-store/src/migrations/postgres/1690925872592-CreateContacts.ts +++ b/packages/data-store/src/migrations/postgres/1690925872592-CreateContacts.ts @@ -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 { - 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"`) diff --git a/packages/data-store/src/migrations/postgres/utils.ts b/packages/data-store/src/migrations/postgres/utils.ts new file mode 100644 index 000000000..9aaeefe7b --- /dev/null +++ b/packages/data-store/src/migrations/postgres/utils.ts @@ -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); + }; +} diff --git a/packages/data-store/src/migrations/postgres/uuid.ts b/packages/data-store/src/migrations/postgres/uuid.ts deleted file mode 100644 index aabfaecce..000000000 --- a/packages/data-store/src/migrations/postgres/uuid.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { QueryRunner } from 'typeorm' - -export async function enableUuidv4(queryRunner: QueryRunner) { - try { - await queryRunner.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 - } -} diff --git a/packages/ssi-sdk-core/src/utils/database.ts b/packages/ssi-sdk-core/src/utils/database.ts index 2d4336c08..366312beb 100644 --- a/packages/ssi-sdk-core/src/utils/database.ts +++ b/packages/ssi-sdk-core/src/utils/database.ts @@ -1,3 +1,19 @@ export const flattenArray = (args: { items: Array> }): Array => args.items.flat() as Array export const flattenMigrations = (args: { migrations: Array> }): Array => args.migrations.flat() as Array + +export type WithTypeOrmQuery = + ((query: string, parameters?: any[]) => Promise) | + ((query: string, parameters?: any[], useStructuredResult?: boolean) => Promise) + + +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; + } +} diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9c2526e90..7d54c6237 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -357,6 +357,9 @@ importers: packages/data-store: dependencies: + '@sphereon/ssi-sdk.core': + specifier: workspace:* + version: link:../ssi-sdk-core '@sphereon/ssi-types': specifier: workspace:* version: link:../ssi-types @@ -6309,7 +6312,7 @@ packages: '@octokit/request-error': 3.0.3 '@octokit/types': 9.3.2 is-plain-object: 5.0.0 - node-fetch: 2.6.7 + node-fetch: 2.7.0 universal-user-agent: 6.0.0 transitivePeerDependencies: - encoding