volks
volks
Explore posts from servers
DTDrizzle Team
Created by volks on 5/29/2023 in #help
Postgres's Serial column type doesn't automatically have a default
Hello, when using postgres's serial types, and setting them as primary keys, there is an issue currently that does not mark them as has default. What I mean is, from the docs The data types smallserial, serial and bigserial are not true types, but merely a notational convenience for creating unique identifier columns (similar to the AUTO_INCREMENT property supported by some other databases), the Serial types already have a default implementation, but if you use them as is in Drizzle, they aren't regarded as having a default
type NewTest = InferModel<typeof test, 'insert'>;
^? type NewTest = {
id: number; <-- Should be nullable
}

export const test = pgTable("test", {
id: smallserial('id').primaryKey(),
});
type NewTest = InferModel<typeof test, 'insert'>;
^? type NewTest = {
id: number; <-- Should be nullable
}

export const test = pgTable("test", {
id: smallserial('id').primaryKey(),
});
I'd expect it to be nullable for inserts as serials have default
6 replies
DTDrizzle Team
Created by volks on 5/13/2023 in #help
Self referencing nullable ID field not assigneable in a `eq` statement
I am not sure if I encountered another edge case but basically I have a self-referencing table where the reference can be nullable
export const carrierTasks = pgTable('carrier_tasks', {
id: uuid('id').primaryKey().defaultRandom(),
nextTaskId: uuid('next_task_id').references((): AnyPgColumn => carrierTasks.id),
isCurrent: boolean('is_current').default(false).notNull(),
});
export const carrierTasks = pgTable('carrier_tasks', {
id: uuid('id').primaryKey().defaultRandom(),
nextTaskId: uuid('next_task_id').references((): AnyPgColumn => carrierTasks.id),
isCurrent: boolean('is_current').default(false).notNull(),
});
await db.transaction(async (tx) => {
const currentTasks = await tx
.delete(carrierTasks)
.where(eq(carrierTasks.isCurrent, true))
.returning({ nextTaskId: carrierTasks.nextTaskId })

const currentTask = currentTasks?.[0];
if (!currentTask) {
throw new Error("Carrier task not found or not in current state");
}

await tx
.update(carrierTasks)
.set({ isCurrent: true })
.where(eq(carrierTasks.id, currentTask.nextTaskId))
await db.transaction(async (tx) => {
const currentTasks = await tx
.delete(carrierTasks)
.where(eq(carrierTasks.isCurrent, true))
.returning({ nextTaskId: carrierTasks.nextTaskId })

const currentTask = currentTasks?.[0];
if (!currentTask) {
throw new Error("Carrier task not found or not in current state");
}

await tx
.update(carrierTasks)
.set({ isCurrent: true })
.where(eq(carrierTasks.id, currentTask.nextTaskId))
2 replies
DTDrizzle Team
Created by volks on 5/10/2023 in #help
Raw sql nullable types, sql<Type | undefined>
const result = await db.select({
customField: sql<Type | null>`...`
const result = await db.select({
customField: sql<Type | null>`...`
This gets inferred to SQL<any> Is there a way to have a nullable type when using raw sql?
9 replies
DTDrizzle Team
Created by volks on 5/8/2023 in #help
Support for multiple with statements?
Is it possible to have multiple with statements?
const carriers = await tx
.with(carrierTaskCount)
.with(idleCarrierDriverCount)
const carriers = await tx
.with(carrierTaskCount)
.with(idleCarrierDriverCount)
typescript: Property 'with' does not exist on type '{ select: { (): PgSelectBuilder<undefined, "db">; <TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection, "db">; }; }'.
typescript: Property 'with' does not exist on type '{ select: { (): PgSelectBuilder<undefined, "db">; <TSelection extends SelectedFields>(fields: TSelection): PgSelectBuilder<TSelection, "db">; }; }'.
2 replies
TtRPC
Created by volks on 5/7/2023 in #❓-help
Set Request Headers for individual requests?
Hello everyone, I have a simple question, can you somehow add headers to the individual requests instead of doing it once during setup of the tRPC? I would like to have custom per request header that I cannot know ahead of time.
2 replies
DTDrizzle Team
Created by volks on 5/6/2023 in #help
Custom Type interpreted as String
Hello everyone, I am having some issues with getting Drizzle to work with PostGIS, I am trying to have support for PostGIS's Point geometry. This is what I have setup
export type Point = {
longitude: number;
latitude: number;
};

export const pointDB = customType<
{
data: Point;
driverData: string;
}
>({
dataType() {
return 'GEOMETRY(POINT, 4326)';
},
toDriver(value: Point): string {
return `SRID=4326;POINT(${value.longitude} ${value.latitude})`;
},
fromDriver(value: string): Point {
const matches = value.match(/POINT\((?<longitude>[\d.-]+) (?<latitude>[\d.-]+)\)/);
const { longitude, latitude } = matches.groups;

return {
longitude: parseFloat(longitude),
latitude: parseFloat(latitude),
};
},
});
export type Point = {
longitude: number;
latitude: number;
};

export const pointDB = customType<
{
data: Point;
driverData: string;
}
>({
dataType() {
return 'GEOMETRY(POINT, 4326)';
},
toDriver(value: Point): string {
return `SRID=4326;POINT(${value.longitude} ${value.latitude})`;
},
fromDriver(value: string): Point {
const matches = value.match(/POINT\((?<longitude>[\d.-]+) (?<latitude>[\d.-]+)\)/);
const { longitude, latitude } = matches.groups;

return {
longitude: parseFloat(longitude),
latitude: parseFloat(latitude),
};
},
});
But the data returned stays a string
carriers: Array(1)0:
id: "db34d18d-b4d0-42e2-8121-29d47ad18327"
location: "POINT(44.769368 20.4631)"
carriers: Array(1)0:
id: "db34d18d-b4d0-42e2-8121-29d47ad18327"
location: "POINT(44.769368 20.4631)"
This is my query
const carriers = await db.select({
id: activeCarriers.employeeId,
location: sql<Point>`st_astext(active_carriers.location) as location`,
})
.from(activeCarriers)
return { carriers }
const carriers = await db.select({
id: activeCarriers.employeeId,
location: sql<Point>`st_astext(active_carriers.location) as location`,
})
.from(activeCarriers)
return { carriers }
This is happening across packages in monorepo, not sure if that could cause the type inference to fail
73 replies
TtRPC
Created by volks on 4/18/2023 in #❓-help
Integrate third-party added endpoints into the router definition?
Its would be cool but not too important, so I was wondering whether or not it would be possible to have dummy router configs without any actual routers. I am using SuperTokens for my Auth and it adds several endpoints to my server (/auth/signup, /auth/login and so on), and it would be cool to somehow add these into tRPC so that I could ditch custom fetch calls.
9 replies
TtRPC
Created by volks on 2/6/2023 in #❓-help
Cannot read properties of undefined (reading 'data') of res.error.data, when trpc errors out
Hello everyone, I am using @trpc/react-query alongside trpc for express, and I am experiencing a crash when I have an error
debug: Server listening on port: 3000 at development mode
error: ERROR OCCURED: '[\n' +
' {\n' +
' "code": "invalid_type",\n' +
' "expected": "object",\n' +
' "received": "undefined",\n' +
' "path": [],\n' +
' "message": "Required"\n' +
' }\n' +
']'
error: ERROR OCCURED: "Cannot read properties of undefined (reading 'data')"
/work/company/monorepo/node_modules/@trpc/server/dist/config-200d5226.js:63
const data = res.error.data;
^

TypeError: Cannot read properties of undefined (reading 'data')
at /work/company/monorepo/node_modules/@trpc/server/dist/config-200d5226.js:63:36
at Array.map (<anonymous>)
at Object.getHTTPStatusCode (/work/company/monorepo/node_modules/@trpc/server/dist/config-200d5226.js:61:38)
at endResponse (/work/company/monorepo/node_modules/@trpc/server/dist/resolveHTTPResponse-e94bc3b5.js:46:29)
at Object.resolveHTTPResponse (/work/company/monorepo/node_modules/@trpc/server/dist/resolveHTTPResponse-e94bc3b5.js:194:16)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Object.nodeHTTPRequestHandler (/work/company/monorepo/node_modules/@trpc/server/dist/nodeHTTPRequestHandler-b0d356ac.js:52:20)
at async /work/company/monorepo/node_modules/@trpc/server/dist/adapters/express.js:16:9
[nodemon] app crashed - waiting for file changes before starting...
debug: Server listening on port: 3000 at development mode
error: ERROR OCCURED: '[\n' +
' {\n' +
' "code": "invalid_type",\n' +
' "expected": "object",\n' +
' "received": "undefined",\n' +
' "path": [],\n' +
' "message": "Required"\n' +
' }\n' +
']'
error: ERROR OCCURED: "Cannot read properties of undefined (reading 'data')"
/work/company/monorepo/node_modules/@trpc/server/dist/config-200d5226.js:63
const data = res.error.data;
^

TypeError: Cannot read properties of undefined (reading 'data')
at /work/company/monorepo/node_modules/@trpc/server/dist/config-200d5226.js:63:36
at Array.map (<anonymous>)
at Object.getHTTPStatusCode (/work/company/monorepo/node_modules/@trpc/server/dist/config-200d5226.js:61:38)
at endResponse (/work/company/monorepo/node_modules/@trpc/server/dist/resolveHTTPResponse-e94bc3b5.js:46:29)
at Object.resolveHTTPResponse (/work/company/monorepo/node_modules/@trpc/server/dist/resolveHTTPResponse-e94bc3b5.js:194:16)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Object.nodeHTTPRequestHandler (/work/company/monorepo/node_modules/@trpc/server/dist/nodeHTTPRequestHandler-b0d356ac.js:52:20)
at async /work/company/monorepo/node_modules/@trpc/server/dist/adapters/express.js:16:9
[nodemon] app crashed - waiting for file changes before starting...
3 replies
TtRPC
Created by volks on 10/12/2022 in #❓-help
Losing the type between the client and server
Hello everyone, I am new to tRPC and the magic of types in typescript so I am looking for ideas as to what is happening, the client is receiving any type instead of a string
24 replies