iolyd
iolyd
DTDrizzle Team
Created by iolyd on 6/6/2024 in #help
Drizzle-zod errors
What is supposed to be the highest version of drizzle-orm supported by drizzle-zod's latest release? I'm facing a breaking error when defining validation schemas using createInsertSchema with drizzle-orm@latest and it persists even when trying to regress all the way to @0.30.9, where I stopped trying older versions.
TypeError: Cannot read properties of undefined (reading 'Symbol(drizzle:Columns)')
at getTableColumns (redacted/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/drizzle-orm/utils.js:108:15)
at Module.c (redacted/node_modules/.pnpm/[email protected][email protected]_@[email protected]_@[email protected][email protected][email protected]/node_modules/drizzle-zod/index.mjs:1:431)
at redacted/src/lib/crud/validation/auth.ts:29:28
at async instantiateModule (redacted/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected][email protected]_/node_modules/vite/dist/node/chunks/dep-BKbDVx1T.js:56231:9)
TypeError: Cannot read properties of undefined (reading 'Symbol(drizzle:Columns)')
at getTableColumns (redacted/node_modules/.pnpm/[email protected]_@[email protected]_@[email protected][email protected]/node_modules/drizzle-orm/utils.js:108:15)
at Module.c (redacted/node_modules/.pnpm/[email protected][email protected]_@[email protected]_@[email protected][email protected][email protected]/node_modules/drizzle-zod/index.mjs:1:431)
at redacted/src/lib/crud/validation/auth.ts:29:28
at async instantiateModule (redacted/node_modules/.pnpm/[email protected]_@[email protected][email protected][email protected][email protected][email protected]_/node_modules/vite/dist/node/chunks/dep-BKbDVx1T.js:56231:9)
4 replies
DTDrizzle Team
Created by iolyd on 1/20/2024 in #help
Less verbose generic types for helper functions
Trying to implement various helper functions to join or filter data, I keep facing the need to add quite verbose generic types whenever I want to handle subqueries and/or selections alongside tables. Are there type helpers I'm unaware of that could make things a bit more straightforward? Here's an example illustrating what I mean:
export function withTranslations<
T extends AnyTable<TableConfig> | PgSelect | Subquery,
TT extends (AnyTable<TableConfig> | PgSelect | Subquery) & {
[K in keyof TranslationLangColumn]: AnyColumn;
},
PK extends T extends Table
? ValueOf<T['_']['columns']>
: T extends PgSelect
? ValueOf<T['_']['selectedFields']>
: T extends Subquery
? ValueOf<T['_']['selectedFields']>
: never,
FK extends TT extends Table
? ValueOf<TT['_']['columns']>
: TT extends PgSelect
? ValueOf<TT['_']['selectedFields']>
: TT extends Subquery
? ValueOf<TT['_']['selectedFields']>
: never,
>(
selection: T,
translationsSelection: TT,
relation: [PK, FK] | ((selection: T, translationsSelection: TT) => [PK, FK])
) {
//...
}
export function withTranslations<
T extends AnyTable<TableConfig> | PgSelect | Subquery,
TT extends (AnyTable<TableConfig> | PgSelect | Subquery) & {
[K in keyof TranslationLangColumn]: AnyColumn;
},
PK extends T extends Table
? ValueOf<T['_']['columns']>
: T extends PgSelect
? ValueOf<T['_']['selectedFields']>
: T extends Subquery
? ValueOf<T['_']['selectedFields']>
: never,
FK extends TT extends Table
? ValueOf<TT['_']['columns']>
: TT extends PgSelect
? ValueOf<TT['_']['selectedFields']>
: TT extends Subquery
? ValueOf<TT['_']['selectedFields']>
: never,
>(
selection: T,
translationsSelection: TT,
relation: [PK, FK] | ((selection: T, translationsSelection: TT) => [PK, FK])
) {
//...
}
I could implement some type helpers of my own, but was wondering if anything was already included in drizzle-orm
10 replies
DTDrizzle Team
Created by iolyd on 11/23/2023 in #help
Malformed migration file
For some reason, when running migrations on the latest versions of drizzle-kit and drizzle-orm, it fails with data is malformed on most of my previous migrations' metadata:
migrations/meta/0002_snapshot.json data is malformed
migrations/meta/0003_snapshot.json data is malformed
migrations/meta/0004_snapshot.json data is malformed
migrations/meta/0005_snapshot.json data is malformed
migrations/meta/0006_snapshot.json data is malformed
migrations/meta/0007_snapshot.json data is malformed
migrations/meta/0008_snapshot.json data is malformed
migrations/meta/0009_snapshot.json data is malformed
migrations/meta/0010_snapshot.json data is malformed
migrations/meta/0011_snapshot.json data is malformed
migrations/meta/0012_snapshot.json data is malformed
migrations/meta/0013_snapshot.json data is malformed
migrations/meta/0014_snapshot.json data is malformed
migrations/meta/0015_snapshot.json data is malformed
migrations/meta/0016_snapshot.json data is malformed
migrations/meta/0017_snapshot.json data is malformed
migrations/meta/0018_snapshot.json data is malformed
migrations/meta/0019_snapshot.json data is malformed
migrations/meta/0002_snapshot.json data is malformed
migrations/meta/0003_snapshot.json data is malformed
migrations/meta/0004_snapshot.json data is malformed
migrations/meta/0005_snapshot.json data is malformed
migrations/meta/0006_snapshot.json data is malformed
migrations/meta/0007_snapshot.json data is malformed
migrations/meta/0008_snapshot.json data is malformed
migrations/meta/0009_snapshot.json data is malformed
migrations/meta/0010_snapshot.json data is malformed
migrations/meta/0011_snapshot.json data is malformed
migrations/meta/0012_snapshot.json data is malformed
migrations/meta/0013_snapshot.json data is malformed
migrations/meta/0014_snapshot.json data is malformed
migrations/meta/0015_snapshot.json data is malformed
migrations/meta/0016_snapshot.json data is malformed
migrations/meta/0017_snapshot.json data is malformed
migrations/meta/0018_snapshot.json data is malformed
migrations/meta/0019_snapshot.json data is malformed
The logs are pretty bare, is there a way to get more detail? I updated both orm and kit and changed a few things across my app's types and queries (spanning multiple commits) before trying to run a migration so going back to the previous versions is not really an option as it would imply undoing all that :/
5 replies
DTDrizzle Team
Created by iolyd on 11/2/2023 in #help
Get subquery columns in a way similar to `getTableColumns`
Keeping track manually of all subquery fields can quickly become annoying, and I'm looking for a simple helper to allow specifying fields with a pattern similar to what getTableColumns enables. Has anyone implemented a small helper that's somewhat "battle-tested"? Here's what I have at the moment, but I'm not trusting it that much due to how naive it is and due to the necessary typecasting.
export function getSubqueryColumns<S extends ColumnsSelection, A extends string>(
query: WithSubqueryWithSelection<S, A> | SubqueryWithSelection<S, A>
): (typeof query)['_']['selectedFields'] {
return query[SubqueryConfig as unknown as string].selection;
}
export function getSubqueryColumns<S extends ColumnsSelection, A extends string>(
query: WithSubqueryWithSelection<S, A> | SubqueryWithSelection<S, A>
): (typeof query)['_']['selectedFields'] {
return query[SubqueryConfig as unknown as string].selection;
}
12 replies
DTDrizzle Team
Created by iolyd on 10/19/2023 in #help
Typing a helper function parameter to be a table with required column(s)
I'm having a hard time typing some helper functions where I need some params to accept any table as long as it implements certain required columns (key and type). Here's what i'm aiming for:
export function withTranslation<T extends AnyTable, TT extends AnyTable</* How to type this to enforce certain columns, e.g. "locale"? */>>(
event: RequestEvent | ServerLoadEvent,
table: T,
translationsTable: TT,
join: { field: ValueOf<T['_']['columns']>; reference: ValueOf<TT['_']['columns']> }
) {
return dbpool.$with('tt').as((db) =>
db
.select({
...getTableColumns(table),
...getTableColumns(translationsTable),
})
.from(table)
.leftJoin(
translationsTable,
and(
eq(translationsTable.locale, event.locals.locale),
// ^^^^^^Property "locale" does not exist on type TT
eq(join.field, join.reference))
)
);
}
export function withTranslation<T extends AnyTable, TT extends AnyTable</* How to type this to enforce certain columns, e.g. "locale"? */>>(
event: RequestEvent | ServerLoadEvent,
table: T,
translationsTable: TT,
join: { field: ValueOf<T['_']['columns']>; reference: ValueOf<TT['_']['columns']> }
) {
return dbpool.$with('tt').as((db) =>
db
.select({
...getTableColumns(table),
...getTableColumns(translationsTable),
})
.from(table)
.leftJoin(
translationsTable,
and(
eq(translationsTable.locale, event.locals.locale),
// ^^^^^^Property "locale" does not exist on type TT
eq(join.field, join.reference))
)
);
}
Anyone have experience with this?
11 replies
DTDrizzle Team
Created by iolyd on 9/19/2023 in #help
Json object aggregate
In a multilingual app, I am using relational queries (but I could also just work with normal selects) to retrieve rows and their related translation strings. My schema has a bunch of tables like so:
export const projects = pgTable('projects', {
id: nanoid('id').default(generateNanoid()).primaryKey(),
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow().notNull(),
publishedAt: timestamp('published_at', { withTimezone: true }),
typeId: integer('type_id').references(() => projectTypes.id, {
onDelete: 'set null',
onUpdate: 'cascade',
}),
// ...
});
type Project = InferSelectModel<typeof projects>;
export const projectsTranslations = pgTable('projects_t',
{
id: nanoid('id').references(() => projects.id, {
onDelete: 'cascade',
onUpdate: 'cascade',
}).notNull(),
locale: locale('locale').references(() => locales.locale, {
onDelete: 'cascade',
onUpdate: 'cascade',
}).notNull(),
title: text('title').notNull(),
summary: text('summary'),
description: text('description'),
},
(table) => {
return {
pk: primaryKey(table.id, table.locale),
unq: unique().on(table.locale, table.title),
};
}
);
type ProjectTranslation = InferSelectModel<typeof projectsTranslations>;
export const projects = pgTable('projects', {
id: nanoid('id').default(generateNanoid()).primaryKey(),
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull(),
updatedAt: timestamp('updated_at', { withTimezone: true }).defaultNow().notNull(),
publishedAt: timestamp('published_at', { withTimezone: true }),
typeId: integer('type_id').references(() => projectTypes.id, {
onDelete: 'set null',
onUpdate: 'cascade',
}),
// ...
});
type Project = InferSelectModel<typeof projects>;
export const projectsTranslations = pgTable('projects_t',
{
id: nanoid('id').references(() => projects.id, {
onDelete: 'cascade',
onUpdate: 'cascade',
}).notNull(),
locale: locale('locale').references(() => locales.locale, {
onDelete: 'cascade',
onUpdate: 'cascade',
}).notNull(),
title: text('title').notNull(),
summary: text('summary'),
description: text('description'),
},
(table) => {
return {
pk: primaryKey(table.id, table.locale),
unq: unique().on(table.locale, table.title),
};
}
);
type ProjectTranslation = InferSelectModel<typeof projectsTranslations>;
I'm looking for a query that would return the data formatted as:
const selectedProjects: (Project & {translations: Record<Locale, Translation undefined>})[] = ...
const selectedProjects: (Project & {translations: Record<Locale, Translation undefined>})[] = ...
but I'm struggling to get anything else than:
const selectedProjecets: (Project & {translations: Translation[]})[] = ...
const selectedProjecets: (Project & {translations: Translation[]})[] = ...
18 replies
DTDrizzle Team
Created by iolyd on 8/30/2023 in #help
Select as db column names instead of js property names
Is there a helper to make select queries that return db column names, i.e. snake-cased instead of the camelCased schema names? I figure I can implement pretty easily, just wanted to know if there's already something provided by drizzle?
3 replies