TanStackT
TanStack7mo ago
13 replies
wispy-olive

Fields coming back as snake case from DB

I've been following the tanstackdb-electric-starter pretty heavily for my project and just noticed a weird issue.

When my electric collection retrieves fields, it retrieves them snake cased (
hello_world
)

I am following the same convention as the starter project, using drizzle and zod schema for my collection's schemas.

export const selectMessageSchema = createSelectSchema(messages, {
    content: z.string().min(1),
});
export function createElectricChatCollection(
    args: CreateChatCollectionArgs = { id: "messages" },
) {
    const table = "messages";
    const id = args.id;
    const where = buildWhereClause(args);

    return createCollection(
        electricCollectionOptions({
            id: id,
            shapeOptions: {
                url: new URL("api/electric", env.VITE_STOA_URL).toString(),
                params: {
                    table,
                    ...(where ? { where } : {}),
                },
                parser: {
                    timestamptz: (date: string) => {
                        return new Date(date);
                    },
                },
            },
            getKey: (item) => item.id,
            schema: selectMessageSchema,
            onInsert: async ({ transaction }) => {
                const { modified: newMessage } = transaction.mutations[0];
                const result = await orpc.clientChat.create.call({
                    clientId: newMessage.clientId,
                    senderId: newMessage.senderId,
                    content: newMessage.content,
                });
                return { txid: result.txId };
            },
        }),
    );
}


It's not like a huge deal, but type-safety is gone now
image.png
GitHub
Real-time sync for Postgres. Contribute to electric-sql/electric development by creating an account on GitHub.
electric/examples/tanstack-db-web-starter at main · electric-sql/e...
Was this page helpful?