From 9bfb597c378d3ca269cabcc001dc672f56a1be0a Mon Sep 17 00:00:00 2001 From: sksadjad Date: Fri, 24 Nov 2023 18:40:15 +0100 Subject: [PATCH 1/6] 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 From 5161837faa55b7632cbaa66200d6875ae3534569 Mon Sep 17 00:00:00 2001 From: sksadjad Date: Fri, 24 Nov 2023 18:47:07 +0100 Subject: [PATCH 2/6] fix: export enablePostgresUuidExtension and WithTypeOrmQuery to core exports --- packages/ssi-sdk-core/src/utils/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/ssi-sdk-core/src/utils/index.ts b/packages/ssi-sdk-core/src/utils/index.ts index 16630ae8c..a23c58bd6 100644 --- a/packages/ssi-sdk-core/src/utils/index.ts +++ b/packages/ssi-sdk-core/src/utils/index.ts @@ -1,4 +1,4 @@ export * from './encoding' -export { flattenArray, flattenMigrations } from './database' +export { enablePostgresUuidExtension, flattenArray, flattenMigrations, WithTypeOrmQuery } from './database' export { getImageMediaType, getImageDimensions, downloadImage } from './image' export * from './vc' From 3654a8a070bed87fd9cab66184603cce4c298a05 Mon Sep 17 00:00:00 2001 From: sksadjad Date: Sat, 25 Nov 2023 10:57:25 +0100 Subject: [PATCH 3/6] fix: refactored usages of enablePostgresUuidExtension to accept queryRunner as the main param --- .../postgres/1659463079428-CreateContacts.ts | 3 +-- .../1685628974232-CreateIssuanceBranding.ts | 3 +-- .../postgres/1690925872592-CreateContacts.ts | 3 +-- .../data-store/src/migrations/postgres/utils.ts | 8 -------- packages/ssi-sdk-core/src/utils/database.ts | 13 ++++++------- packages/ssi-sdk-core/src/utils/index.ts | 2 +- 6 files changed, 10 insertions(+), 22 deletions(-) delete mode 100644 packages/data-store/src/migrations/postgres/utils.ts diff --git a/packages/data-store/src/migrations/postgres/1659463079428-CreateContacts.ts b/packages/data-store/src/migrations/postgres/1659463079428-CreateContacts.ts index e15f19e9e..712a8a264 100644 --- a/packages/data-store/src/migrations/postgres/1659463079428-CreateContacts.ts +++ b/packages/data-store/src/migrations/postgres/1659463079428-CreateContacts.ts @@ -1,12 +1,11 @@ import { MigrationInterface, QueryRunner } from 'typeorm' -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 enablePostgresUuidExtension(createQueryRunnerAdapter(queryRunner)) + await enablePostgresUuidExtension(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 e7ed6d476..d1ad2922d 100644 --- a/packages/data-store/src/migrations/postgres/1685628974232-CreateIssuanceBranding.ts +++ b/packages/data-store/src/migrations/postgres/1685628974232-CreateIssuanceBranding.ts @@ -1,12 +1,11 @@ import { MigrationInterface, QueryRunner } from 'typeorm' -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 enablePostgresUuidExtension(createQueryRunnerAdapter(queryRunner)) + await enablePostgresUuidExtension(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 a53c68a95..2550cdd1d 100644 --- a/packages/data-store/src/migrations/postgres/1690925872592-CreateContacts.ts +++ b/packages/data-store/src/migrations/postgres/1690925872592-CreateContacts.ts @@ -1,12 +1,11 @@ import { MigrationInterface, QueryRunner } from 'typeorm' -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 enablePostgresUuidExtension(createQueryRunnerAdapter(queryRunner)) + await enablePostgresUuidExtension(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 deleted file mode 100644 index 9aaeefe7b..000000000 --- a/packages/data-store/src/migrations/postgres/utils.ts +++ /dev/null @@ -1,8 +0,0 @@ -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/ssi-sdk-core/src/utils/database.ts b/packages/ssi-sdk-core/src/utils/database.ts index 366312beb..4ccc52abe 100644 --- a/packages/ssi-sdk-core/src/utils/database.ts +++ b/packages/ssi-sdk-core/src/utils/database.ts @@ -2,14 +2,13 @@ export const flattenArray = (args: { items: Array> }): 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) => { +// It should accept queryRunner from the typeorm +export const enablePostgresUuidExtension = async (queryRunner: any) => { + if (!queryRunner.query) { + throw new Error("You should pass a QueryRunner object to this function.") + } try { - await query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`); + 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` diff --git a/packages/ssi-sdk-core/src/utils/index.ts b/packages/ssi-sdk-core/src/utils/index.ts index a23c58bd6..25af8c5c0 100644 --- a/packages/ssi-sdk-core/src/utils/index.ts +++ b/packages/ssi-sdk-core/src/utils/index.ts @@ -1,4 +1,4 @@ export * from './encoding' -export { enablePostgresUuidExtension, flattenArray, flattenMigrations, WithTypeOrmQuery } from './database' +export { enablePostgresUuidExtension, flattenArray, flattenMigrations } from './database' export { getImageMediaType, getImageDimensions, downloadImage } from './image' export * from './vc' From 80112eca96026d09cc22b89f3651252559542e44 Mon Sep 17 00:00:00 2001 From: sksadjad Date: Sat, 25 Nov 2023 11:03:16 +0100 Subject: [PATCH 4/6] fix: document added --- packages/data-store/src/migrations/index.ts | 1 - packages/ssi-sdk-core/src/utils/database.ts | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/data-store/src/migrations/index.ts b/packages/data-store/src/migrations/index.ts index e107873b2..daad5a1dc 100644 --- a/packages/data-store/src/migrations/index.ts +++ b/packages/data-store/src/migrations/index.ts @@ -1,2 +1 @@ export { DataStoreMigrations } from './generic' -export { createQueryRunnerAdapter } from './postgres/utils' diff --git a/packages/ssi-sdk-core/src/utils/database.ts b/packages/ssi-sdk-core/src/utils/database.ts index 4ccc52abe..48a406362 100644 --- a/packages/ssi-sdk-core/src/utils/database.ts +++ b/packages/ssi-sdk-core/src/utils/database.ts @@ -2,7 +2,9 @@ export const flattenArray = (args: { items: Array> }): Array export const flattenMigrations = (args: { migrations: Array> }): Array => args.migrations.flat() as Array -// It should accept queryRunner from the typeorm +/** + * It should accept queryRunner from the typeorm + */ export const enablePostgresUuidExtension = async (queryRunner: any) => { if (!queryRunner.query) { throw new Error("You should pass a QueryRunner object to this function.") From cb5d8cb2d9b5f89ced3957e3127d197190de03c2 Mon Sep 17 00:00:00 2001 From: sksadjad Date: Mon, 27 Nov 2023 10:40:05 +0100 Subject: [PATCH 5/6] fix: added ssi-sdk.core to data-store tsconfig plus added exposed query function to enablePostgresUuidExtension signature --- packages/data-store/tsconfig.json | 2 ++ packages/ssi-sdk-core/src/utils/database.ts | 21 ++++++++++----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/packages/data-store/tsconfig.json b/packages/data-store/tsconfig.json index 861e525c4..42efb4cff 100644 --- a/packages/data-store/tsconfig.json +++ b/packages/data-store/tsconfig.json @@ -10,6 +10,8 @@ }, "references": [ { + "path": "../ssi-sdk-core" + }, { "path": "../ssi-types" } ] diff --git a/packages/ssi-sdk-core/src/utils/database.ts b/packages/ssi-sdk-core/src/utils/database.ts index 48a406362..6dd05e3f0 100644 --- a/packages/ssi-sdk-core/src/utils/database.ts +++ b/packages/ssi-sdk-core/src/utils/database.ts @@ -2,19 +2,18 @@ export const flattenArray = (args: { items: Array> }): Array export const flattenMigrations = (args: { migrations: Array> }): Array => args.migrations.flat() as Array -/** - * It should accept queryRunner from the typeorm - */ -export const enablePostgresUuidExtension = async (queryRunner: any) => { - if (!queryRunner.query) { - throw new Error("You should pass a QueryRunner object to this function.") - } +type QueryRunnerType = { + query(query: string, parameters: any[] | undefined, useStructuredResult: true): Promise + query(query: string, parameters?: any[]): Promise +} + +export const enablePostgresUuidExtension = async (queryRunner: QueryRunnerType) => { try { - await queryRunner.query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`); + await queryRunner.query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`) } catch (error) { - console.log( + console.error( `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; + ) + throw error } } From 98b588fd9a718eb1d34be0cf3f5955d582a3e7b6 Mon Sep 17 00:00:00 2001 From: sksadjad Date: Mon, 27 Nov 2023 10:46:15 +0100 Subject: [PATCH 6/6] doc: added comment about the accepting type of enablePostgresUuidExtension in ssi-sdk.core --- packages/ssi-sdk-core/src/utils/database.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/ssi-sdk-core/src/utils/database.ts b/packages/ssi-sdk-core/src/utils/database.ts index 6dd05e3f0..68e1db58d 100644 --- a/packages/ssi-sdk-core/src/utils/database.ts +++ b/packages/ssi-sdk-core/src/utils/database.ts @@ -7,6 +7,9 @@ type QueryRunnerType = { query(query: string, parameters?: any[]): Promise } +/** + * It should accept the type QueryRunner from the typeorm + */ export const enablePostgresUuidExtension = async (queryRunner: QueryRunnerType) => { try { await queryRunner.query(`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`)