eXecuteye
eXecuteye
DTDrizzle Team
Created by Kevin on 8/21/2024 in #help
ZOD - how to createUpdateSchema?
i use this:
const updateSchema = createInsertSchema(tableName).pick({ name: true, ... }).partial()
const updateSchema = createInsertSchema(tableName).pick({ name: true, ... }).partial()
3 replies
DTDrizzle Team
Created by yasserconnect on 7/5/2024 in #help
use limit or undefined
passing a 0 does apply the limit since 0.32.1 (as highlighted in the release notes) -> it returns 0 rows https://github.com/drizzle-team/drizzle-orm/releases/tag/0.32.1 So this workaround with passing 0 does not work anymore and we also cannot pass null or undefined to "ignore" the limit...
12 replies
DTDrizzle Team
Created by yasserconnect on 7/5/2024 in #help
use limit or undefined
This approach does not work anymore from 0.32.1 onwards...
12 replies
DTDrizzle Team
Created by BubbleTrouble on 8/4/2024 in #help
Get type from select object ?
I go about it this way:
export const userSelectSchema = createSelectSchema(userTable).pick({ id: true, firstName: true, lastName: true });

export const userSelect = {
id: userTable.id,
firstName: userTable.firstName,
lastName: userTable.lastName,
};

export type User = z.infer<typeof userSelectSchema>;
export const userSelectSchema = createSelectSchema(userTable).pick({ id: true, firstName: true, lastName: true });

export const userSelect = {
id: userTable.id,
firstName: userTable.firstName,
lastName: userTable.lastName,
};

export type User = z.infer<typeof userSelectSchema>;
you can further improve on this by providing the select with the correct type. The following is a type that i put together to get Select/Columns/With typing for by providing a table
// Query type helpers (need to be defined here to avoid circular reference)
type TablesWithRelations = ExtractTablesWithRelations<typeof schema>;

// Select type helpers (need to be defined here to avoid circular reference)
/** Helper types to apply typing to predefined select object (detailedTableSelect: {id: table.id} satisfies DrizzleSelectType<"table">) */
export type DrizzleSelectType<TableName extends keyof TablesWithRelations> = Partial<{
[K in keyof ExtractTablesWithRelations<typeof schema>[TableName]["columns"]]: ExtractTablesWithRelations<typeof schema>[TableName]["columns"][K];
}>;

/** Helper types to apply typing to predefined queryColumns object (detailedTableQueryColumns: {id: true} satisfies QueryColumnType<"table">) */
export type DrizzleQueryColumnType<TableName extends keyof TablesWithRelations> = DBQueryConfig<
"many",
true,
ExtractTablesWithRelations<typeof schema>,
ExtractTablesWithRelations<typeof schema>[TableName]
>["columns"];

/** Helper types to apply typing to predefined queryWith object (detailedTableQueryWith: {id: true} satisfies QueryWithType<"table">) */
export type DrizzleQueryWithType<TableName extends keyof TablesWithRelations> = DBQueryConfig<
"many",
true,
ExtractTablesWithRelations<typeof schema>,
ExtractTablesWithRelations<typeof schema>[TableName]
>["with"];
// Query type helpers (need to be defined here to avoid circular reference)
type TablesWithRelations = ExtractTablesWithRelations<typeof schema>;

// Select type helpers (need to be defined here to avoid circular reference)
/** Helper types to apply typing to predefined select object (detailedTableSelect: {id: table.id} satisfies DrizzleSelectType<"table">) */
export type DrizzleSelectType<TableName extends keyof TablesWithRelations> = Partial<{
[K in keyof ExtractTablesWithRelations<typeof schema>[TableName]["columns"]]: ExtractTablesWithRelations<typeof schema>[TableName]["columns"][K];
}>;

/** Helper types to apply typing to predefined queryColumns object (detailedTableQueryColumns: {id: true} satisfies QueryColumnType<"table">) */
export type DrizzleQueryColumnType<TableName extends keyof TablesWithRelations> = DBQueryConfig<
"many",
true,
ExtractTablesWithRelations<typeof schema>,
ExtractTablesWithRelations<typeof schema>[TableName]
>["columns"];

/** Helper types to apply typing to predefined queryWith object (detailedTableQueryWith: {id: true} satisfies QueryWithType<"table">) */
export type DrizzleQueryWithType<TableName extends keyof TablesWithRelations> = DBQueryConfig<
"many",
true,
ExtractTablesWithRelations<typeof schema>,
ExtractTablesWithRelations<typeof schema>[TableName]
>["with"];
This has som caveats though... (eg: Columns/With types only work for findMany) Hope this helps
3 replies
DTDrizzle Team
Created by eXecuteye on 6/20/2024 in #help
drizzle postgis geometry always geometry(point)
I have looked a bit more into it and it seems that the geometry field is really only meant for point as sqlType for geometry seems to be hardcoded to point and the mapFrom/ToDriverValue function only supports 2 dimensional points for now...
export class PgGeometryObject<T extends ColumnBaseConfig<'json', 'PgGeometryObject'>> extends PgColumn<T> {
static readonly [entityKind]: string = 'PgGeometryObject';

getSQLType(): string {
return 'geometry(point)';
}

override mapFromDriverValue(value: string): { x: number; y: number } {
const parsed = parseEWKB(value);
return { x: parsed[0], y: parsed[1] };
}

override mapToDriverValue(value: { x: number; y: number }): string {
return `point(${value.x} ${value.y})`;
}
}
export class PgGeometryObject<T extends ColumnBaseConfig<'json', 'PgGeometryObject'>> extends PgColumn<T> {
static readonly [entityKind]: string = 'PgGeometryObject';

getSQLType(): string {
return 'geometry(point)';
}

override mapFromDriverValue(value: string): { x: number; y: number } {
const parsed = parseEWKB(value);
return { x: parsed[0], y: parsed[1] };
}

override mapToDriverValue(value: { x: number; y: number }): string {
return `point(${value.x} ${value.y})`;
}
}
3 replies
DTDrizzle Team
Created by eXecuteye on 6/17/2024 in #help
drizzle-kit generate not working with unhelpful error message
Update: after trying for way too long i found the issue... Having files in the schema folder that are not relevant to db tables (and therefore not exported in the schemas/index.ts file) caused this issue... In my case it was a utils file and ta types file inside the schema folder... After moving them outside of the schema folder, the issue did not occur again... I hope this helps if anyone else runs into this issue... It would be nice if the error message for this could be a bit more indicative to the issue causing it...
2 replies
DTDrizzle Team
Created by Hiro on 6/16/2024 in #help
Drizzle not generating, and can't migrate
If you make such an issue please provide some context on the issue...
10 replies
DTDrizzle Team
Created by Entsllor on 5/2/2024 in #help
[BUG?] Handling Postgresql schemas when using enums
Ran into the same issue today
2 replies