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

refactor/use neon http pg client #306

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions apps/maestro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@
"@axelarjs/ui": "workspace:*",
"@axelarjs/utils": "workspace:*",
"@hookform/resolvers": "^3.3.4",
"@neondatabase/serverless": "^0.9.0",
"@sentry/nextjs": "^7.108.0",
"@tanstack/react-query": "^5.28.6",
"@trpc/client": "11.0.0-next.320",
"@trpc/next": "11.0.0-next.320",
"@trpc/react-query": "11.0.0-next.320",
"@trpc/server": "11.0.0-next.320",
"@vercel/kv": "^1.0.1",
"@vercel/postgres": "^0.7.2",
"@web3modal/wagmi": "^4.1.1",
"drizzle-orm": "^0.29.5",
"drizzle-orm": "^0.30.4",
"lucide-react": "^0.265.0",
"next": "^14.1.4",
"nextjs-cors": "^2.2.0",
Expand Down
19 changes: 17 additions & 2 deletions apps/maestro/src/lib/drizzle/client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
import { sql } from "@vercel/postgres";
import { drizzle } from "drizzle-orm/vercel-postgres";
import { neon } from "@neondatabase/serverless";
import type { BatchItem as _BatchItem } from "drizzle-orm/batch";
import { drizzle } from "drizzle-orm/neon-http";

import * as schema from "./schema";

const sql = neon(process.env.POSTGRES_URL!);

const dbClient = drizzle(sql, { schema });

export type DBClient = typeof dbClient;

export default dbClient;

export type BatchItem = _BatchItem<"pg">;
export type BatchItems = [BatchItem, ...BatchItem[]];

/**
* Type-guard to check if drizzle batch is valid.
*
* @param batch - The batch to check
* @returns true if the batch is valid
*/
export const isValidBatch = (batch: BatchItem[]): batch is BatchItems =>
batch.length > 1;
127 changes: 65 additions & 62 deletions apps/maestro/src/services/db/postgres/MaestroPostgresClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,69 +97,72 @@ export default class MaestroPostgresClient {
async recordRemoteInterchainTokenDeployments(
values: NewRemoteInterchainTokenInput[]
) {
await this.db.transaction(async (tx) => {
const existingTokens = await tx.query.remoteInterchainTokens.findMany({
where: (table, { eq }) => eq(table.tokenId, values[0].tokenId),
});

const updateValues = existingTokens
.map(
(t) =>
[
t,
sanitizeObject(
values.find(
(v) =>
v.axelarChainId === t.axelarChainId &&
v.tokenId === t.tokenId
) ?? {}
),
] as const
)
.filter(([, v]) => Boolean(v));

const insertValues = values.filter((v) => {
const id = `${v.axelarChainId}:${v.tokenAddress}`;
return !existingTokens.some((t) => t.id === id);
});

if (updateValues.length > 0) {
for (const [existingToken, updateValue] of updateValues) {
await tx
.update(remoteInterchainTokens)
.set({
id: updateValue.id ?? existingToken.id,
tokenManagerAddress:
updateValue.tokenManagerAddress ??
existingToken.tokenManagerAddress,
deploymentMessageId:
updateValue.deploymentMessageId ??
existingToken.deploymentMessageId,
deploymentStatus:
updateValue.deploymentStatus ?? existingToken.deploymentStatus,
tokenManagerType:
updateValue.tokenManagerType ?? existingToken.tokenManagerType,
tokenAddress:
updateValue.tokenAddress ?? existingToken.tokenAddress,
updatedAt: new Date(),
})
.where(eq(remoteInterchainTokens.id, existingToken.id));
}
}

if (!insertValues.length) {
return;
}

await tx.insert(remoteInterchainTokens).values(
insertValues.map((v) => ({
...v,
id: `${v.axelarChainId}:${v.tokenAddress}`,
createdAt: new Date(),
updatedAt: new Date(),
}))
);
const existingTokens = await this.db.query.remoteInterchainTokens.findMany({
where: (table, { eq }) => eq(table.tokenId, values[0].tokenId),
});

const updateValues = existingTokens
.map(
(t) =>
[
t,
sanitizeObject(
values.find(
(v) =>
v.axelarChainId === t.axelarChainId && v.tokenId === t.tokenId
) ?? {}
),
] as const
)
.filter(([, v]) => Boolean(v));

const insertValues = values.filter((v) => {
const id = `${v.axelarChainId}:${v.tokenAddress}`;
return !existingTokens.some((t) => t.id === id);
});

if (updateValues.length > 0) {
const [head, ...tail] = updateValues.map(([existingToken, updateValue]) =>
this.db
.update(remoteInterchainTokens)
.set({
id: updateValue.id ?? existingToken.id,
tokenManagerAddress:
updateValue.tokenManagerAddress ??
existingToken.tokenManagerAddress,
deploymentMessageId:
updateValue.deploymentMessageId ??
existingToken.deploymentMessageId,
deploymentStatus:
updateValue.deploymentStatus ?? existingToken.deploymentStatus,
tokenManagerType:
updateValue.tokenManagerType ?? existingToken.tokenManagerType,
tokenAddress:
updateValue.tokenAddress ?? existingToken.tokenAddress,
updatedAt: new Date(),
})
.where(eq(remoteInterchainTokens.id, existingToken.id))
);

// only batch if there are more than one update operation,
// otherise run the single update
const update = tail.length ? this.db.batch([head, ...tail]) : head;

await update;
}

if (!insertValues.length) {
return;
}

await this.db.insert(remoteInterchainTokens).values(
insertValues.map((v) => ({
...v,
id: `${v.axelarChainId}:${v.tokenAddress}`,
createdAt: new Date(),
updatedAt: new Date(),
}))
);
}

/**
Expand Down
59 changes: 22 additions & 37 deletions pnpm-lock.yaml

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

Loading