Skip to content

Commit

Permalink
feat: add transactions view to prisma seeds (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdotta authored Oct 22, 2024
1 parent 524c2ef commit 07b211f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"generate": "prisma generate",
"dev": "pnpm generate && tsup --watch --onSuccess 'pnpm start'",
"start": "node ./dist/index.js",
"db:seed": "tsx ./prisma/seed.ts",
"db:seed": "tsx ./prisma/seed",
"db:seed:dev": "pnpm db:seed dev",
"db:seed:prod": "pnpm db:seed prod",
"db:migrate": "prisma migrate dev"
Expand Down
3 changes: 3 additions & 0 deletions apps/api/prisma/seed.ts → apps/api/prisma/seed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
treasureRuby,
} from "@treasure-dev/tdk-core";
import { arbitrum, arbitrumSepolia, mainnet, sepolia } from "thirdweb/chains";
import upsertTransactionsViewSql from "./transactions-view";

type RemoteEnvironment = "dev" | "prod";
type Environment = "local" | RemoteEnvironment;
Expand Down Expand Up @@ -241,6 +242,8 @@ const createProject = async ({
});
}

await upsertTransactionsViewSql(prisma);

await prisma.$disconnect();
} catch (err) {
console.error(err);
Expand Down
24 changes: 24 additions & 0 deletions apps/api/prisma/seed/transactions-view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Prisma, type PrismaClient } from "@prisma/client";

const upsertTransactionsViewSql = Prisma.sql`
CREATE OR REPLACE VIEW
"smart_account"."transactions" AS (
SELECT
(body ->> 'chain'::text) AS chain,
(body ->> 'chain_id'::text) AS chain_id,
to_timestamp((((body ->> 'block_timestamp'::text))::numeric)::double precision) AS block_timestamp,
(body ->> 'to_address'::text) AS to_address,
(body ->> 'id'::text) AS id,
(body ->> 'from_address'::text) AS from_address,
(body ->> 'transaction_hash'::text) AS transaction_hash,
((body ->> 'block_number'::text))::numeric AS block_number,
((body ->> 'value'::text))::numeric AS value,
((body ->> 'transaction_index'::text))::numeric AS transaction_index
FROM
smart_account.transactions_jsonb
);
`;

export default (prisma: PrismaClient) => {
return prisma.$queryRaw(upsertTransactionsViewSql);
};

0 comments on commit 07b211f

Please sign in to comment.