hyhy
hyhy
Explore posts from servers
TTCTheo's Typesafe Cult
Created by hyhy on 12/30/2023 in #questions
Calling trpc mutations on client components with app router
thanks so much, that's a really dumb error of me lol
21 replies
TTCTheo's Typesafe Cult
Created by hyhy on 12/30/2023 in #questions
Calling trpc mutations on client components with app router
no idea if the episode card loader will work with no data so might need to comment it out in the main app router page
21 replies
TTCTheo's Typesafe Cult
Created by hyhy on 12/30/2023 in #questions
Calling trpc mutations on client components with app router
no worries, let me know if you need a hand
21 replies
TTCTheo's Typesafe Cult
Created by hyhy on 12/30/2023 in #questions
Calling trpc mutations on client components with app router
here's the page router example from the other project, clicking the + icon in the top header opens this pop up
21 replies
TTCTheo's Typesafe Cult
Created by hyhy on 12/30/2023 in #questions
Calling trpc mutations on client components with app router
@Vargas i've put it up at https://github.com/hyhydev/Modulation/blob/main/src/app/_components/episode_form/episode_form.tsx, it's a bit messy as was porting over some page router code from a different project
21 replies
TTCTheo's Typesafe Cult
Created by hyhy on 12/30/2023 in #questions
Calling trpc mutations on client components with app router
Will do, I'll just do a bit of GitHub wrangling to get it public
21 replies
TTCTheo's Typesafe Cult
Created by hyhy on 12/30/2023 in #questions
Calling trpc mutations on client components with app router
thanks for the help
21 replies
TTCTheo's Typesafe Cult
Created by hyhy on 12/30/2023 in #questions
Calling trpc mutations on client components with app router
"use client";

import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { loggerLink, unstable_httpBatchStreamLink } from "@trpc/client";
import { createTRPCReact } from "@trpc/react-query";
import { useState } from "react";

import { type AppRouter } from "~/server/api/root";
import { getUrl, transformer } from "./shared";

export const api = createTRPCReact<AppRouter>();

export function TRPCReactProvider(props: {
children: React.ReactNode;
cookies: string;
}) {
const [queryClient] = useState(() => new QueryClient());

const [trpcClient] = useState(() =>
api.createClient({
transformer,
links: [
loggerLink({
enabled: (op) =>
process.env.NODE_ENV === "development" ||
(op.direction === "down" && op.result instanceof Error),
}),
unstable_httpBatchStreamLink({
url: getUrl(),
headers() {
return {
cookie: props.cookies,
"x-trpc-source": "react",
};
},
}),
],
})
);

return (
<QueryClientProvider client={queryClient}>
<api.Provider client={trpcClient} queryClient={queryClient}>
{props.children}
</api.Provider>
</QueryClientProvider>
);
}
"use client";

import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { loggerLink, unstable_httpBatchStreamLink } from "@trpc/client";
import { createTRPCReact } from "@trpc/react-query";
import { useState } from "react";

import { type AppRouter } from "~/server/api/root";
import { getUrl, transformer } from "./shared";

export const api = createTRPCReact<AppRouter>();

export function TRPCReactProvider(props: {
children: React.ReactNode;
cookies: string;
}) {
const [queryClient] = useState(() => new QueryClient());

const [trpcClient] = useState(() =>
api.createClient({
transformer,
links: [
loggerLink({
enabled: (op) =>
process.env.NODE_ENV === "development" ||
(op.direction === "down" && op.result instanceof Error),
}),
unstable_httpBatchStreamLink({
url: getUrl(),
headers() {
return {
cookie: props.cookies,
"x-trpc-source": "react",
};
},
}),
],
})
);

return (
<QueryClientProvider client={queryClient}>
<api.Provider client={trpcClient} queryClient={queryClient}>
{props.children}
</api.Provider>
</QueryClientProvider>
);
}
as far as I can tell it's unmodified from the create-t3-app setup
21 replies
TTCTheo's Typesafe Cult
Created by hyhy on 12/30/2023 in #questions
Calling trpc mutations on client components with app router
it does, yeah
21 replies
TTCTheo's Typesafe Cult
Created by hyhy on 12/30/2023 in #questions
Calling trpc mutations on client components with app router
create: hyhyProtectedProcedure
.input(
z.object({
name: z.string(),
}),
)
.mutation(async ({ ctx, input }) => {
await ctx.db.insert(episodes).values({
name: input.name,
});
}),
update: hyhyProtectedProcedure
.input(
z.object({
id: z.number(),
name: z.string(),
}),
)
.mutation(async ({ ctx, input }) => {
await ctx.db
.update(episodes)
.set({
name: input.name,
})
.where(eq(episodes.id, input.id));
}),
create: hyhyProtectedProcedure
.input(
z.object({
name: z.string(),
}),
)
.mutation(async ({ ctx, input }) => {
await ctx.db.insert(episodes).values({
name: input.name,
});
}),
update: hyhyProtectedProcedure
.input(
z.object({
id: z.number(),
name: z.string(),
}),
)
.mutation(async ({ ctx, input }) => {
await ctx.db
.update(episodes)
.set({
name: input.name,
})
.where(eq(episodes.id, input.id));
}),
simple mutation in case it's helpful at all
21 replies
TTCTheo's Typesafe Cult
Created by hyhy on 12/15/2023 in #questions
Drizzle Foreign Key Constraints are not allowed
export const relAlbumsEpisodes = mysqlTable("rel_albums_episodes", {
id: serial("id").primaryKey(),
albumId: int("album_id"),
episodeId: int("episode_id"),
createdAt: timestamp("created_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updatedAt").onUpdateNow(),
});

export const relAlbumsEpisodesRelations = relations(
relAlbumsEpisodes,
({ one }) => ({
artist: one(albums, {
fields: [relAlbumsEpisodes.albumId],
references: [albums.id],
}),
genre: one(episodes, {
fields: [relAlbumsEpisodes.episodeId],
references: [episodes.id],
}),
}),
);
export const relAlbumsEpisodes = mysqlTable("rel_albums_episodes", {
id: serial("id").primaryKey(),
albumId: int("album_id"),
episodeId: int("episode_id"),
createdAt: timestamp("created_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp("updatedAt").onUpdateNow(),
});

export const relAlbumsEpisodesRelations = relations(
relAlbumsEpisodes,
({ one }) => ({
artist: one(albums, {
fields: [relAlbumsEpisodes.albumId],
references: [albums.id],
}),
genre: one(episodes, {
fields: [relAlbumsEpisodes.episodeId],
references: [episodes.id],
}),
}),
);
9 replies
TTCTheo's Typesafe Cult
Created by hyhy on 12/15/2023 in #questions
Drizzle Foreign Key Constraints are not allowed
i think this works correctly now but will have to see, it pushed up at least
9 replies
TTCTheo's Typesafe Cult
Created by hyhy on 12/15/2023 in #questions
Drizzle Foreign Key Constraints are not allowed
9 replies
TTCTheo's Typesafe Cult
Created by hyhy on 12/15/2023 in #questions
Drizzle Foreign Key Constraints are not allowed
Assuming I'm either missing something in my config, or defining the relations incorrectly
9 replies
TTCTheo's Typesafe Cult
Created by hyhy on 12/15/2023 in #questions
Drizzle Foreign Key Constraints are not allowed
Relations in my schema in question, for example each episode has a mix:
export const episodes = mysqlTable(
"episode",
{
id: bigint("id", { mode: "number" }).primaryKey().autoincrement(),
name: varchar("name", { length: 256 }),
mixId: bigint("mix_id", { mode: "number" }).references(() => mixes.id),
},
(episode) => ({
nameIndex: index("name_idx").on(episode.name),
}),
);

export const mixes = mysqlTable("mix", {
id: bigint("id", { mode: "number" }).primaryKey().autoincrement(),
});
export const episodes = mysqlTable(
"episode",
{
id: bigint("id", { mode: "number" }).primaryKey().autoincrement(),
name: varchar("name", { length: 256 }),
mixId: bigint("mix_id", { mode: "number" }).references(() => mixes.id),
},
(episode) => ({
nameIndex: index("name_idx").on(episode.name),
}),
);

export const mixes = mysqlTable("mix", {
id: bigint("id", { mode: "number" }).primaryKey().autoincrement(),
});
or a many to many between albums and episodes
export const relAlbumsEpisodes = mysqlTable("rel_albums_episodes", {
id: bigint("id", { mode: "number" }).primaryKey().autoincrement(),
albumId: bigint("album_id", { mode: "number" }).references(() => albums.id),
episodeId: bigint("episode_id", { mode: "number" }).references(() => episodes.id),
});
export const relAlbumsEpisodes = mysqlTable("rel_albums_episodes", {
id: bigint("id", { mode: "number" }).primaryKey().autoincrement(),
albumId: bigint("album_id", { mode: "number" }).references(() => albums.id),
episodeId: bigint("episode_id", { mode: "number" }).references(() => episodes.id),
});
9 replies
TTCTheo's Typesafe Cult
Created by hyhy on 11/1/2023 in #questions
"inArray requires at least 1 value" error deleting files with uploadthing
ok i took a long break but yes this works thank you so much
13 replies
TTCTheo's Typesafe Cult
Created by hyhy on 11/1/2023 in #questions
"inArray requires at least 1 value" error deleting files with uploadthing
i'll give it a try tomorrow
13 replies
TTCTheo's Typesafe Cult
Created by hyhy on 11/1/2023 in #questions
"inArray requires at least 1 value" error deleting files with uploadthing
sorry i missed your message too markr
13 replies
TTCTheo's Typesafe Cult
Created by hyhy on 11/1/2023 in #questions
"inArray requires at least 1 value" error deleting files with uploadthing
oh i totally gave up on this but if it works i will thank you forever
13 replies
TTCTheo's Typesafe Cult
Created by hyhy on 11/1/2023 in #questions
"inArray requires at least 1 value" error deleting files with uploadthing
never solved this one, any help appreciated
13 replies