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
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