Skip to content

Commit

Permalink
swap trpc transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Nov 29, 2023
1 parent c5b3fe1 commit 6c9ddd6
Show file tree
Hide file tree
Showing 8 changed files with 2,044 additions and 72 deletions.
1 change: 0 additions & 1 deletion packages/store-indexer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
"fastify": "^4.21.0",
"postgres": "^3.3.5",
"rxjs": "7.5.5",
"superjson": "^1.12.4",
"viem": "1.14.0",
"zod": "^3.21.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/store-sync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
"postgres": "^3.3.5",
"rxjs": "7.5.5",
"sql.js": "^1.8.0",
"superjson": "^1.12.4",
"viem": "1.14.0",
"wagmi": "^1.4.7",
"zod": "^3.21.4",
"zustand": "^4.3.7"
},
Expand Down
8 changes: 4 additions & 4 deletions packages/store-sync/src/postgres/columnTypes.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { customType } from "drizzle-orm/pg-core";
import superjson from "superjson";
import { Address, ByteArray, bytesToHex, getAddress, Hex, hexToBytes } from "viem";
import { deserialize, serialize } from "wagmi";

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const asJson = <TData>(name: string) =>
customType<{ data: TData; driverData: string }>({
dataType() {
// TODO: move to json column type? if we do, we'll prob wanna choose something other than superjson since it adds one level of depth (json/meta keys)
// TODO: move to json column type?
return "text";
},
toDriver(data: TData): string {
return superjson.stringify(data);
return serialize(data);
},
fromDriver(driverData: string): TData {
return superjson.parse(driverData);
return deserialize(driverData);
},
})(name);

Expand Down
6 changes: 3 additions & 3 deletions packages/store-sync/src/sqlite/columnTypes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { customType } from "drizzle-orm/sqlite-core";
import superjson from "superjson";
import { Address, Hex, getAddress } from "viem";
import { deserialize, serialize } from "wagmi";

// TODO: migrate these to same patterns in postgres/columnTypes

Expand All @@ -11,10 +11,10 @@ export const json = <TData>(name: string) =>
return "text";
},
toDriver(data: TData): string {
return superjson.stringify(data);
return serialize(data);
},
fromDriver(driverData: string): TData {
return superjson.parse(driverData);
return deserialize(driverData);
},
})(name);

Expand Down
4 changes: 2 additions & 2 deletions packages/store-sync/src/trpc-indexer/createAppRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { z } from "zod";
import { QueryAdapter } from "./common";
import { isHex } from "viem";
import { initTRPC } from "@trpc/server";
import superjson from "superjson";
import { transformer } from "./transformer";

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function createAppRouter() {
const t = initTRPC.context<{ queryAdapter: QueryAdapter }>().create({
transformer: superjson,
transformer,
});

return t.router({
Expand Down
4 changes: 2 additions & 2 deletions packages/store-sync/src/trpc-indexer/createIndexerClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createTRPCProxyClient, httpBatchLink, CreateTRPCProxyClient } from "@trpc/client";
import superjson from "superjson";
import type { AppRouter } from "./createAppRouter";
import { transformer } from "./transformer";

type CreateIndexerClientOptions = {
/**
Expand All @@ -17,7 +17,7 @@ type CreateIndexerClientOptions = {
*/
export function createIndexerClient({ url }: CreateIndexerClientOptions): CreateTRPCProxyClient<AppRouter> {
return createTRPCProxyClient<AppRouter>({
transformer: superjson,
transformer,
links: [httpBatchLink({ url })],
});
}
7 changes: 7 additions & 0 deletions packages/store-sync/src/trpc-indexer/transformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { DataTransformerOptions } from "@trpc/server";
import { serialize, deserialize } from "wagmi";

export const transformer: DataTransformerOptions = {
serialize: (data: any) => JSON.parse(serialize(data)),
deserialize: (data: any) => deserialize(JSON.stringify(data)),
};

0 comments on commit 6c9ddd6

Please sign in to comment.