potato
potato
Explore posts from servers
DTDrizzle Team
Created by potato on 11/11/2023 in #help
Can't figure out this simple one-to-many relationship
Hey, so i have two tables, a patient one and a village one. I just want to define a simple one-to-many, one village for many patients, what am I missing here? My villageRelations is throwing type errors export const patient = mysqlTable("patient", { id: serial("id").primaryKey(), firstName: varchar("firstName", { length: 256 }).notNull(), middleName: varchar("middleName", { length: 256 }), lastName: varchar("lastName", { length: 256 }).notNull(), dob: date("dob", { mode: "date" }).notNull(), sex: varchar("sex", { length: 32 }), villageId: int("villageId"), createdAt: datetime("createdAt", { mode: "date", fsp: 3 }) .default(sqlCURRENT_TIMESTAMP(3)) .notNull(), updatedAt: datetime("updatedAt", { mode: "date", fsp: 3 }) .default(sqlCURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)) .notNull(), }); export const patientRelations = relations(patient, ({ one }) => ({ village: one(village, { fields: [patient.villageId], // Referencing villageId in patient table references: [village.id], // Referencing id in village table }), })); and export const village = mysqlTable("village", { id: serial("id").primaryKey(), name: varchar("name", { length: 256 }).notNull(), region: varchar("region", { length: 256 }), population: int("population"), createdAt: datetime("createdAt", { mode: "date", fsp: 3 }) .default(sqlCURRENT_TIMESTAMP(3)) .notNull(), updatedAt: datetime("updatedAt", { mode: "date", fsp: 3 }) .default(sqlCURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)) .notNull(), }); export const villageRelations = relations(village, ({ many }) => ({ patients: many(patient, { fields: [patient.villageId], // Referencing villageId in patient table references: [village.id], // Referencing id in village table }), }));
2 replies
TTCTheo's Typesafe Cult
Created by potato on 5/15/2023 in #questions
What is the best way to handle a caching layer?
Hey, I am currently using upstash redis to cache one of my queries. However since using a cache in my router folder, I have to create a temporary interface to make typescript happy with the output as I cannot infer the types from the router. Just wondering, what is the best approach for something like this. Cheers!
// clientRouter.ts

interface cacheOutput {
paginatedClientsObject: {
id: string;
name: string;
description: string;
created_at: Date;
author_id: string;
}[];
rowCount: number;
}

const redisClient = new Redis(env.REDIS_URL);
const clientRepository = new SortedItemRepository('sorted-clients', redisClient);

export const clientRouter = createTRPCRouter({
getAllPaginated: protectedProcedure
.input(z.object({ skip: z.number(), take: z.number() }))
.query(async ({ ctx, input }) => {
const redisCache = await clientRepository.getById(`paginatedClients-${input.skip}`);
if (redisCache?.data) {
console.log('DATA FROM CACHE');
const cacheOutput = JSON.parse(redisCache.data as string) as cacheOutput;
return cacheOutput;
} else {
const paginatedClientsandRows = await ctx.prisma.$transaction(async (tx) => {
const rowCount = await tx.client.count();
const paginatedClients = await tx.client.findMany({
skip: input.skip * input.take,
take: input.take,
});
const paginatedClientsObject = paginatedClients.map((client) => ({
id: client.id,
name: client.name,
description: client.description,
created_at: client.createdAt,
author_id: client.authorId,
}));
return { paginatedClientsObject, rowCount };
});
await clientRepository.set({
id: `paginatedClients-${input.skip}`,
data: JSON.stringify(paginatedClientsandRows),
});
console.log('DATA FROM QUERY', paginatedClientsandRows);
return paginatedClientsandRows;
}
}),
// clientRouter.ts

interface cacheOutput {
paginatedClientsObject: {
id: string;
name: string;
description: string;
created_at: Date;
author_id: string;
}[];
rowCount: number;
}

const redisClient = new Redis(env.REDIS_URL);
const clientRepository = new SortedItemRepository('sorted-clients', redisClient);

export const clientRouter = createTRPCRouter({
getAllPaginated: protectedProcedure
.input(z.object({ skip: z.number(), take: z.number() }))
.query(async ({ ctx, input }) => {
const redisCache = await clientRepository.getById(`paginatedClients-${input.skip}`);
if (redisCache?.data) {
console.log('DATA FROM CACHE');
const cacheOutput = JSON.parse(redisCache.data as string) as cacheOutput;
return cacheOutput;
} else {
const paginatedClientsandRows = await ctx.prisma.$transaction(async (tx) => {
const rowCount = await tx.client.count();
const paginatedClients = await tx.client.findMany({
skip: input.skip * input.take,
take: input.take,
});
const paginatedClientsObject = paginatedClients.map((client) => ({
id: client.id,
name: client.name,
description: client.description,
created_at: client.createdAt,
author_id: client.authorId,
}));
return { paginatedClientsObject, rowCount };
});
await clientRepository.set({
id: `paginatedClients-${input.skip}`,
data: JSON.stringify(paginatedClientsandRows),
});
console.log('DATA FROM QUERY', paginatedClientsandRows);
return paginatedClientsandRows;
}
}),
2 replies
TTCTheo's Typesafe Cult
Created by potato on 5/13/2023 in #questions
Syntax help for useQuery with no inputs
3 replies
DTDrizzle Team
Created by potato on 4/10/2023 in #help
[BUG] drizzle-kit doesn't like imports outside of a module
npx drizzle-kit generate:pg drizzle-kit: v0.17.4 drizzle-orm: v0.23.10 C:\Users\Abdul\Documents\GitHub\bodyshop-mgmt\src\env.mjs:2 import { z } from "zod"; ^^^^^^ SyntaxError: Cannot use import statement outside a module at Object.compileFunction (node:vm:360:18) at wrapSafe (node:internal/modules/cjs/loader:1088:15) at Module._compile (node:internal/modules/cjs/loader:1123:27) at Module._compile (C:\Users\Abdul\Documents\GitHub\bodyshop-mgmt\node_modules\drizzle-kit\index.js:20706:30) at Module._extensions..js (node:internal/modules/cjs/loader:1213:10) at Object.newLoader [as .mjs] (C:\Users\Abdul\Documents\GitHub\bodyshop-mgmt\node_modules\drizzle-kit\index.js:20710:13) at Module.load (node:internal/modules/cjs/loader:1037:32) at Module._load (node:internal/modules/cjs/loader:878:12) at Module.require (node:internal/modules/cjs/loader:1061:19) at require (node:internal/modules/cjs/helpers:103:18)
24 replies
DTDrizzle Team
Created by potato on 4/10/2023 in #help
drizzle-kit throwing await is not available in the configured target environment
Hi, when using the generate:pg command i am getting this error: 'Top-level await is not available in the configured target environment ("node14")'
5 replies