Noahh
Noahh
DTDrizzle Team
Created by Noahh on 8/23/2023 in #help
Missing @opentelemetry/api module at runtime when upgrading to 0.28.4
Just upgraded drizzle-orm to 0.28.4, and running my project (after a successful typecheck) I get the error Cannot find module '@opentelemetry/api'. Here is the full stack trace:
Require stack:
- C:\Users\...\node_modules\.pnpm\[email protected][email protected]\node_modules\drizzle-orm\index-b1dbb7ec.cjs
- C:\Users\...\node_modules\.pnpm\[email protected][email protected]\node_modules\drizzle-orm\postgres-js\index.cjs
- C:\Users\...\dist\global\services\drizzle.service.js
- C:\Users\ngreg\...\dist\modules\users\users.service.js
- C:\Users\ngreg\...\dist\app.module.js
- C:\Users\...\dist\main.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1072:15)
at Function.Module._load (node:internal/modules/cjs/loader:925:27)
at Module.require (node:internal/modules/cjs/loader:1139:19)
at require (node:internal/modules/helpers:121:18)
at Object.<anonymous> (C:\Users\...\node_modules\.pnpm\[email protected][email protected]\node_modules\drizzle-orm\index-b1dbb7ec.cjs:3:1)
at Module._compile (node:internal/modules/cjs/loader:1257:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1311:10)
at Module.load (node:internal/modules/cjs/loader:1115:32)
at Function.Module._load (node:internal/modules/cjs/loader:962:12)
at Module.require (node:internal/modules/cjs/loader:1139:19)
Require stack:
- C:\Users\...\node_modules\.pnpm\[email protected][email protected]\node_modules\drizzle-orm\index-b1dbb7ec.cjs
- C:\Users\...\node_modules\.pnpm\[email protected][email protected]\node_modules\drizzle-orm\postgres-js\index.cjs
- C:\Users\...\dist\global\services\drizzle.service.js
- C:\Users\ngreg\...\dist\modules\users\users.service.js
- C:\Users\ngreg\...\dist\app.module.js
- C:\Users\...\dist\main.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1072:15)
at Function.Module._load (node:internal/modules/cjs/loader:925:27)
at Module.require (node:internal/modules/cjs/loader:1139:19)
at require (node:internal/modules/helpers:121:18)
at Object.<anonymous> (C:\Users\...\node_modules\.pnpm\[email protected][email protected]\node_modules\drizzle-orm\index-b1dbb7ec.cjs:3:1)
at Module._compile (node:internal/modules/cjs/loader:1257:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1311:10)
at Module.load (node:internal/modules/cjs/loader:1115:32)
at Function.Module._load (node:internal/modules/cjs/loader:962:12)
at Module.require (node:internal/modules/cjs/loader:1139:19)
For reference I'm using PNPM and CJS
55 replies
DTDrizzle Team
Created by Noahh on 7/5/2023 in #help
PostgreSQL RQB truncation
I have a fairly nested query using the RQB and I'm running into issues where PostgreSQL truncates the identifiers used to 64 characters (as it normally does). In wondering if it makes sense/if there is a way to make the identifiers used in the RQB always fit into the 64 character limit because the truncation results in duplicate identifiers and makes the query fail. I am happy to send an example schema/error if that makes anything easier.
1 replies
DTDrizzle Team
Created by Noahh on 6/22/2023 in #help
Filtering findMany using related records
I have a restaurants table that has a one-to-one relation with an addresses table. I want to pull all of the restaurants and their address within specific lat/long bounds, but I'm struggling to figure out how to do that. A simplified version of what I want/have so far
await this.drizzleService.db.query.restaurants.findMany({
where: (table) => and(between(/* what could I use here to specify table.address.latitude */, minLat, maxLat)),
with: {
address: true
}
});
await this.drizzleService.db.query.restaurants.findMany({
where: (table) => and(between(/* what could I use here to specify table.address.latitude */, minLat, maxLat)),
with: {
address: true
}
});
I'm trying to figure out if this is somethin Drizzle can do or if I need to break into using sql`` to do it
19 replies
DTDrizzle Team
Created by Noahh on 6/17/2023 in #help
drizzle-kit generate:pg throws ERR_INVALID_RETURN_PROPERTY_VALUE
I have my drizzle.config.ts file unchanged (besides adding driver) from before the recent update (see below), but after updating to latest I am getting an error: TypeError [ERR_INVALID_RETURN_PROPERTY_VALUE]: Expected a url string to be returned for the "url" from the "drizzle-kit/loader.mjs 'resolve'" function but got instance of String.
import { Config } from 'drizzle-kit';

export default {
schema: './src/db/schema/*',
out: './migrations',
driver: 'pg',
} satisfies Config;
import { Config } from 'drizzle-kit';

export default {
schema: './src/db/schema/*',
out: './migrations',
driver: 'pg',
} satisfies Config;
drizzle-kit: v0.19.1
drizzle-orm: v0.27.0
drizzle-kit: v0.19.1
drizzle-orm: v0.27.0
11 replies
DTDrizzle Team
Created by Noahh on 5/22/2023 in #help
Duplicate relations when using `with`
I'm running into a problem where some duplicate values are being returned through a relation with with. Basically, I'm getting
[
{
"id":"317078bd-6265-4156-986c-085bdf297765",
"modifierGroups":[
{
"menuItemId":"317078bd-6265-4156-986c-085bdf297765",
"modifierGroupId":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"order":0,
"modifierGroup":{
"id":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"modifiers":[
{
"modifierGroupId":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"modifierId":"6e43c680-7c41-4840-9ff8-2597d98257ed",
"order":0,
"modifier":{
"id":"6e43c680-7c41-4840-9ff8-2597d98257ed",
"ingredient":{
"id":"8bb2caab-b758-4cb7-9012-f9e543cf4eb4"
},
"item":null
}
}
]
}
},
{
"menuItemId":"317078bd-6265-4156-986c-085bdf297765",
"modifierGroupId":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"order":0,
"modifierGroup":{
"id":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"modifiers":[
{
"modifierGroupId":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"modifierId":"6e43c680-7c41-4840-9ff8-2597d98257ed",
"order":0,
"modifier":{
"id":"6e43c680-7c41-4840-9ff8-2597d98257ed",
"ingredient":{
"id":"8bb2caab-b758-4cb7-9012-f9e543cf4eb4"
},
"item":null
}
}
]
}
}
]
}
]
[
{
"id":"317078bd-6265-4156-986c-085bdf297765",
"modifierGroups":[
{
"menuItemId":"317078bd-6265-4156-986c-085bdf297765",
"modifierGroupId":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"order":0,
"modifierGroup":{
"id":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"modifiers":[
{
"modifierGroupId":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"modifierId":"6e43c680-7c41-4840-9ff8-2597d98257ed",
"order":0,
"modifier":{
"id":"6e43c680-7c41-4840-9ff8-2597d98257ed",
"ingredient":{
"id":"8bb2caab-b758-4cb7-9012-f9e543cf4eb4"
},
"item":null
}
}
]
}
},
{
"menuItemId":"317078bd-6265-4156-986c-085bdf297765",
"modifierGroupId":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"order":0,
"modifierGroup":{
"id":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"modifiers":[
{
"modifierGroupId":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"modifierId":"6e43c680-7c41-4840-9ff8-2597d98257ed",
"order":0,
"modifier":{
"id":"6e43c680-7c41-4840-9ff8-2597d98257ed",
"ingredient":{
"id":"8bb2caab-b758-4cb7-9012-f9e543cf4eb4"
},
"item":null
}
}
]
}
}
]
}
]
instead of
[
{
"id":"317078bd-6265-4156-986c-085bdf297765",
"modifierGroups":[
{
"menuItemId":"317078bd-6265-4156-986c-085bdf297765",
"modifierGroupId":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"order":0,
"modifierGroup":{
"id":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"modifiers":[
{
"modifierGroupId":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"modifierId":"6e43c680-7c41-4840-9ff8-2597d98257ed",
"order":0,
"modifier":{
"id":"6e43c680-7c41-4840-9ff8-2597d98257ed",
"ingredient":{
"id":"8bb2caab-b758-4cb7-9012-f9e543cf4eb4"
},
"item":null
}
}
]
}
}
]
}
]
[
{
"id":"317078bd-6265-4156-986c-085bdf297765",
"modifierGroups":[
{
"menuItemId":"317078bd-6265-4156-986c-085bdf297765",
"modifierGroupId":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"order":0,
"modifierGroup":{
"id":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"modifiers":[
{
"modifierGroupId":"0fd458f3-128a-4787-8253-4d288efcf7c4",
"modifierId":"6e43c680-7c41-4840-9ff8-2597d98257ed",
"order":0,
"modifier":{
"id":"6e43c680-7c41-4840-9ff8-2597d98257ed",
"ingredient":{
"id":"8bb2caab-b758-4cb7-9012-f9e543cf4eb4"
},
"item":null
}
}
]
}
}
]
}
]
You can see that all of the ids are duplicates. I know I ran into this plenty when I was doing my own joined queries, but am I doing something wrong here? My query is
context.query.menuItems
.findMany({
with: {
modifierGroups: {
with: {
modifierGroup: {
with: {
modifiers: {
with: {
modifier: {
with: {
ingredient: true,
item: true,
},
},
},
orderBy: modifierGroupModifiers.order,
},
},
},
},
orderBy: menuItemModifierGroups.order,
}
},
})
context.query.menuItems
.findMany({
with: {
modifierGroups: {
with: {
modifierGroup: {
with: {
modifiers: {
with: {
modifier: {
with: {
ingredient: true,
item: true,
},
},
},
orderBy: modifierGroupModifiers.order,
},
},
},
},
orderBy: menuItemModifierGroups.order,
}
},
})
Any help is appreciated, I'm hoping to have this functional sometime tomorrow :) Thanks in advance!
26 replies
DTDrizzle Team
Created by Noahh on 5/9/2023 in #help
Using with NestJS and Zod
I'm trying to use drizzle-zod and nestjs-zod together to create Zod schemas and classes for insert and select. I mostly got it to work, even with Swagger! However, I'm running into a problem with one of the create schemas. My table is defined:
export const addresses = pgTable('addresses', {
id: uuid('id').defaultRandom().primaryKey(),
name: text('name'),
placeId: text('place_id'),
firstLine: text('first_line').notNull(),
secondLine: text('second_line'),
city: text('city').notNull(),
state: text('state').notNull(),
zipCode: text('zip_code').notNull(),
country: text('country').notNull(),
latitude: decimal('latitude', {
precision: 8,
scale: 6,
}).notNull(),
longitude: decimal('longitude', {
precision: 9,
scale: 6,
}).notNull(),
instructions: text('instructions'),
createdAt: timestamp('created_at').defaultNow(),
});
export const addresses = pgTable('addresses', {
id: uuid('id').defaultRandom().primaryKey(),
name: text('name'),
placeId: text('place_id'),
firstLine: text('first_line').notNull(),
secondLine: text('second_line'),
city: text('city').notNull(),
state: text('state').notNull(),
zipCode: text('zip_code').notNull(),
country: text('country').notNull(),
latitude: decimal('latitude', {
precision: 8,
scale: 6,
}).notNull(),
longitude: decimal('longitude', {
precision: 9,
scale: 6,
}).notNull(),
instructions: text('instructions'),
createdAt: timestamp('created_at').defaultNow(),
});
Then I create the Zod schema for inserting
const createAddressSchema = createInsertSchema(addresses);
const createAddressSchema = createInsertSchema(addresses);
And then I turn that into a class
import { createZodDto } from 'nestjs-zod';

export class NewAddress extends createZodDto(createAddressSchema) {}
import { createZodDto } from 'nestjs-zod';

export class NewAddress extends createZodDto(createAddressSchema) {}
However, when I try to do something like
const address = await context
.insert(addresses)
.values({} as NewAddress) // Not actually what I'm doing, just doing it to get the error
.returning({ id: addresses.id });
const address = await context
.insert(addresses)
.values({} as NewAddress) // Not actually what I'm doing, just doing it to get the error
.returning({ id: addresses.id });
I get Property 'firstLine' is optional in type 'NewAddress' but required in type '{ firstLine: string | SQL<unknown> | Placeholder<string, any>;.... Looking at the generated class, every property has ? after it, and I'm not sure if that's a drizzle-zod problem or a nestjs-zod problem. Any information would be awesome, thanks! So far I'm loving using Drizzle
2 replies
DTDrizzle Team
Created by Noahh on 5/9/2023 in #help
Typing columns based on Table
I'm trying to make a generic paginateQuery function that would let me pass in the table and then an array of only columns from that table would be accepted. Is there any way to do this with the Drizzle types? I feel like I have gotten close a few times but nothing fully gets there. Thanks!
const getFilterSql = <TColumn extends AnyPgColumn>(
filter: string,
filterableColumns: TColumn[],
) => {
if (!filter || filter.trim() === '') return or();

return or(...filterableColumns.map((c) => ilike(c, `%${filter}%`)));
};


export const paginatedQuery = <TTable extends AnyPgTable>(
context: PgDatabase<NodePgQueryResultHKT>,
table: TTable,
paginationDto: PaginationDto,
filterableColumns: (/* Not too sure what to put here */)[],
defaultWhere?: SQL,
) => {
return context
.select()
.from(table)
.where(
and(defaultWhere, getFilterSql(paginationDto.filter, filterableColumns)), // TypeScript gives an error here
)
.offset(paginationDto.page * paginationDto.pageSize)
.limit(paginationDto.pageSize);
};
const getFilterSql = <TColumn extends AnyPgColumn>(
filter: string,
filterableColumns: TColumn[],
) => {
if (!filter || filter.trim() === '') return or();

return or(...filterableColumns.map((c) => ilike(c, `%${filter}%`)));
};


export const paginatedQuery = <TTable extends AnyPgTable>(
context: PgDatabase<NodePgQueryResultHKT>,
table: TTable,
paginationDto: PaginationDto,
filterableColumns: (/* Not too sure what to put here */)[],
defaultWhere?: SQL,
) => {
return context
.select()
.from(table)
.where(
and(defaultWhere, getFilterSql(paginationDto.filter, filterableColumns)), // TypeScript gives an error here
)
.offset(paginationDto.page * paginationDto.pageSize)
.limit(paginationDto.pageSize);
};
4 replies
DTDrizzle Team
Created by Noahh on 5/8/2023 in #help
Does onConflictDoUpdate work with composite primary keys?
I have a model defined below:
export const userDevices = pgTable(
'user_devices',
{
id: text('id').notNull(),
userId: uuid('user_id')
.notNull()
.references(() => users.id, {
onDelete: 'cascade',
}),
...
},
(table) => ({
idUserIdPk: primaryKey(table.id, table.userId),
}),
);
export const userDevices = pgTable(
'user_devices',
{
id: text('id').notNull(),
userId: uuid('user_id')
.notNull()
.references(() => users.id, {
onDelete: 'cascade',
}),
...
},
(table) => ({
idUserIdPk: primaryKey(table.id, table.userId),
}),
);
I want to upsert a row into this table but I'm not sure how to set target to the composite primary key. Is this possible?
return this.drizzleService.db
.insert(userDevices)
.values(data)
.onConflictDoUpdate({
target: /* What to do here? */,
set: data,
});
return this.drizzleService.db
.insert(userDevices)
.values(data)
.onConflictDoUpdate({
target: /* What to do here? */,
set: data,
});
3 replies