Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add provider integration database schema #593

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions apps/armory/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ armory/build:
# === Code format ===

armory/format:
npx nx format:write --projects ${ARMORY_PROJECT_NAME}
npx nx format:write --projects ${ARMORY_PROJECT_NAME}
npx prisma format --schema ${ARMORY_DATABASE_SCHEMA}

armory/lint:
npx nx lint ${ARMORY_PROJECT_NAME} -- --fix

armory/format/check:
npx nx format:check --projects ${ARMORY_PROJECT_NAME}
npx nx format:check --projects ${ARMORY_PROJECT_NAME}
npx prisma format --schema ${ARMORY_DATABASE_SCHEMA}

armory/lint/check:
npx nx lint ${ARMORY_PROJECT_NAME}
Expand Down
2 changes: 2 additions & 0 deletions apps/policy-engine/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ policy-engine/lint/check:

policy-engine/format:
npx nx format:write --projects ${POLICY_ENGINE_PROJECT_NAME}
npx prisma format --schema ${POLICY_ENGINE_DATABASE_SCHEMA}

policy-engine/format/check:
npx nx format:check --projects ${POLICY_ENGINE_PROJECT_NAME}
npx prisma format --schema ${POLICY_ENGINE_DATABASE_SCHEMA}

# === Database ===

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ datasource db {
}

model Engine {
id String @id
id String @id
masterKey String? @map("master_key")
adminApiKey String? @map("admin_api_key")

Expand Down
2 changes: 2 additions & 0 deletions apps/vault/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ vault/build:

vault/format:
npx nx format:write --projects ${VAULT_PROJECT_NAME}
npx prisma format --schema ${VAULT_DATABASE_SCHEMA}

vault/lint:
npx nx lint ${VAULT_PROJECT_NAME} -- --fix

vault/format/check:
npx nx format:check --projects ${VAULT_PROJECT_NAME}
npx prisma format --schema ${VAULT_DATABASE_SCHEMA}

vault/lint/check:
npx nx lint ${VAULT_PROJECT_NAME}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
-- CreateTable
CREATE TABLE "provider_wallet" (
"id" TEXT NOT NULL,
"label" TEXT,
"client_id" TEXT NOT NULL,
"provider" TEXT NOT NULL,
"external_id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,

CONSTRAINT "provider_wallet_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "provider_account" (
"id" TEXT NOT NULL,
"label" TEXT,
"client_id" TEXT NOT NULL,
"provider" TEXT NOT NULL,
"external_id" TEXT NOT NULL,
"wallet_id" TEXT NOT NULL,
"network_id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,

CONSTRAINT "provider_account_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "provider_address" (
"id" TEXT NOT NULL,
"client_id" TEXT NOT NULL,
"provider" TEXT NOT NULL,
"external_id" TEXT NOT NULL,
"account_id" TEXT NOT NULL,
"address" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,

CONSTRAINT "provider_address_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "provider_address_book" (
"id" TEXT NOT NULL,
"client_id" TEXT NOT NULL,
"connection_id" TEXT NOT NULL,
"provider" TEXT NOT NULL,
"external_id" TEXT NOT NULL,
"external_classification" TEXT,
"address" TEXT NOT NULL,
"asset_id" TEXT,
"network_id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,

CONSTRAINT "provider_address_book_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "provider_connection" (
"id" TEXT NOT NULL,
"client_id" TEXT NOT NULL,
"provider" TEXT NOT NULL,
"url" TEXT NOT NULL,
"label" TEXT,
"credentials" JSONB NOT NULL,
"status" TEXT NOT NULL,
"_integrity" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMP(3) NOT NULL,
"revoked_at" TIMESTAMP(3),

CONSTRAINT "provider_connection_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "provider_wallet_connection" (
"client_id" TEXT NOT NULL,
"connection_id" TEXT NOT NULL,
"wallet_id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "provider_wallet_connection_pkey" PRIMARY KEY ("client_id","connection_id","wallet_id")
);

-- CreateTable
CREATE TABLE "provider_sync" (
"id" TEXT NOT NULL,
"client_id" TEXT NOT NULL,
"connection_id" TEXT NOT NULL,
"status" TEXT NOT NULL,
"error_name" TEXT,
"error_message" TEXT,
"error_trace_id" TEXT,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"completed_at" TIMESTAMP(3),

CONSTRAINT "provider_sync_pkey" PRIMARY KEY ("id")
);

-- CreateTable
CREATE TABLE "provider_transfer" (
"id" TEXT NOT NULL,
"client_id" TEXT NOT NULL,
"source_wallet_id" TEXT,
"source_account_id" TEXT,
"source_address_id" TEXT,
"provider" TEXT NOT NULL,
"external_id" TEXT NOT NULL,
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

CONSTRAINT "provider_transfer_pkey" PRIMARY KEY ("id")
);

-- AddForeignKey
ALTER TABLE "provider_account" ADD CONSTRAINT "provider_account_wallet_id_fkey" FOREIGN KEY ("wallet_id") REFERENCES "provider_wallet"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "provider_address" ADD CONSTRAINT "provider_address_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "provider_account"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "provider_address_book" ADD CONSTRAINT "provider_address_book_connection_id_fkey" FOREIGN KEY ("connection_id") REFERENCES "provider_connection"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "provider_wallet_connection" ADD CONSTRAINT "provider_wallet_connection_connection_id_fkey" FOREIGN KEY ("connection_id") REFERENCES "provider_connection"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "provider_wallet_connection" ADD CONSTRAINT "provider_wallet_connection_wallet_id_fkey" FOREIGN KEY ("wallet_id") REFERENCES "provider_wallet"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "provider_sync" ADD CONSTRAINT "provider_sync_connection_id_fkey" FOREIGN KEY ("connection_id") REFERENCES "provider_connection"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "provider_transfer" ADD CONSTRAINT "provider_transfer_source_wallet_id_fkey" FOREIGN KEY ("source_wallet_id") REFERENCES "provider_wallet"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "provider_transfer" ADD CONSTRAINT "provider_transfer_source_account_id_fkey" FOREIGN KEY ("source_account_id") REFERENCES "provider_account"("id") ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE "provider_transfer" ADD CONSTRAINT "provider_transfer_source_address_id_fkey" FOREIGN KEY ("source_address_id") REFERENCES "provider_address"("id") ON DELETE SET NULL ON UPDATE CASCADE;
149 changes: 142 additions & 7 deletions apps/vault/src/shared/module/persistence/schema/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
generator client {
provider = "prisma-client-js"
provider = "prisma-client-js"
binaryTargets = ["native", "debian-openssl-1.1.x", "debian-openssl-3.0.x"]
// Output into a separate subdirectory so multiple schemas can be used in a
// monorepo.
//
// Reference: https://github.com/nrwl/nx-recipes/tree/main/nestjs-prisma
output = "../../../../../../../node_modules/@prisma/client/vault"
output = "../../../../../../../node_modules/@prisma/client/vault"
}

datasource db {
Expand All @@ -14,7 +14,7 @@ datasource db {
}

model Vault {
id String @id
id String @id
masterKey String? @map("master_key")
adminApiKey String? @map("admin_api_key")

Expand All @@ -24,10 +24,145 @@ model Vault {
// TODO: (@wcalderipe, 12/03/23) use hstore extension for better performance.
// See https://www.postgresql.org/docs/9.1/hstore.html
model KeyValue {
key String @id
clientId String? @map("client_id")
collection String
value String
key String @id
clientId String? @map("client_id")
collection String
value String

@@map("key_value")
}

model ProviderWallet {
id String @id
label String?
clientId String @map("client_id")
provider String
externalId String @map("external_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")

accounts ProviderAccount[]
connections ProviderWalletConnection[]
transfers ProviderTransfer[] @relation("SourceWallet")

@@map("provider_wallet")
}

model ProviderAccount {
id String @id
label String?
clientId String @map("client_id")
provider String
externalId String @map("external_id")
walletId String @map("wallet_id")
networkId String @map("network_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")

wallet ProviderWallet @relation(fields: [walletId], references: [id])
addresses ProviderAddress[]
transfers ProviderTransfer[] @relation("SourceAccount")

@@map("provider_account")
}

model ProviderAddress {
id String @id
clientId String @map("client_id")
provider String
externalId String @map("external_id")
accountId String @map("account_id")
address String
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")

account ProviderAccount @relation(fields: [accountId], references: [id])
transfers ProviderTransfer[] @relation("SourceAddress")

@@map("provider_address")
}

model ProviderAddressBook {
id String @id
clientId String @map("client_id")
connectionId String @map("connection_id")
provider String
externalId String @map("external_id")
externalClassification String? @map("external_classification")
address String
assetId String? @map("asset_id")
networkId String @map("network_id")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")

connection ProviderConnection @relation(fields: [connectionId], references: [id])

@@map("provider_address_book")
}

model ProviderConnection {
id String @id
clientId String @map("client_id")
provider String
url String
label String?
credentials Json
status String
integrity String @map("_integrity")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prisma doesn't let me name the column with an _ prefix.

Error: Prisma schema validation - (validate wasm)
Error code: P1012
error: Error validating: This line is not a valid field or attribute definition.
  -->  apps/vault/src/shared/module/persistence/schema/schema.prisma:111
   |
110 |   status      string
111 |   _integrity    string
112 |   createdAt   DateTime  @default(now()) @map("created_at")
   |

createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
revokedAt DateTime? @map("revoked_at")

counterparties ProviderAddressBook[]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"addressBookEntries" is the other option, but I found the term long. Happy to change it back.

connections ProviderWalletConnection[]
syncs ProviderSync[]

@@map("provider_connection")
}

model ProviderWalletConnection {
clientId String @map("client_id")
connectionId String @map("connection_id")
walletId String @map("wallet_id")
createdAt DateTime @default(now()) @map("created_at")

connection ProviderConnection @relation(fields: [connectionId], references: [id])
wallet ProviderWallet @relation(fields: [walletId], references: [id])

@@id([clientId, connectionId, walletId])
@@map("provider_wallet_connection")
}

model ProviderSync {
id String @id
clientId String @map("client_id")
connectionId String @map("connection_id")
status String
errorName String? @map("error_name")
errorMessage String? @map("error_message")
errorTraceId String? @map("error_trace_id")
createdAt DateTime @default(now()) @map("created_at")
completedAt DateTime? @map("completed_at")

// Relations
connection ProviderConnection @relation(fields: [connectionId], references: [id])

@@map("provider_sync")
}

model ProviderTransfer {
id String @id
clientId String @map("client_id")
sourceWalletId String? @map("source_wallet_id")
sourceAccountId String? @map("source_account_id")
sourceAddressId String? @map("source_address_id")
provider String
externalId String @map("external_id")
createdAt DateTime @default(now()) @map("created_at")

wallet ProviderWallet? @relation("SourceWallet", fields: [sourceWalletId], references: [id])
account ProviderAccount? @relation("SourceAccount", fields: [sourceAccountId], references: [id])
address ProviderAddress? @relation("SourceAddress", fields: [sourceAddressId], references: [id])

@@map("provider_transfer")
}
Loading