piedra
piedra
DTDrizzle Team
Created by Pistonmaster on 12/4/2023 in #help
Question about dynamic query building
19 replies
DTDrizzle Team
Created by Pistonmaster on 12/4/2023 in #help
Question about dynamic query building
sure, that would be in the docs repo, right?
19 replies
DTDrizzle Team
Created by Pistonmaster on 12/4/2023 in #help
Question about dynamic query building
I'm having the same doubt as the OP, so just to be clear, doing
let query = db.select().from(table).$dynamic();

query
.where(eq(table.col1, 'foo'))
.where(ilike(table.col1, 'bar%'));

const results = await query;
let query = db.select().from(table).$dynamic();

query
.where(eq(table.col1, 'foo'))
.where(ilike(table.col1, 'bar%'));

const results = await query;
would execute
select * from "table" where "col1" ilike 'bar%';
select * from "table" where "col1" ilike 'bar%';
If that is correct, the docs indeed never mention that using a dynamic query allows multiple calls to where that are merged automatically, but it's the impression one gets after reading the docs the first time since that's the example that is given. What do you think could help bring some clarity to the docs? would an alert like the one here (https://orm.drizzle.team/docs/sql-schema-declaration) about the export keyword work? Thanks!
19 replies
DTDrizzle Team
Created by piedra on 10/7/2023 in #help
how to disambiguate query relations
I think I figured it out
const baseFields = {
id: uuid('id').primaryKey().defaultRandom(),
createdAt: timestamp('created_at').notNull().defaultNow(),
};

export const users = pgTable('users', {
...baseFields,

email: varchar('email', { length: 256 }).unique().notNull(),
firstName: varchar('first_name', { length: 256 }).notNull(),
lastName: varchar('last_name', { length: 256 }).notNull(),
active: boolean('active').notNull(),
birthdate: date('birthdate').notNull(),
sendsDaily: boolean('sends_daily').notNull(),
joinedAt: date('joined_at').notNull(),
coins: integer('coins').notNull(),
});

export const usersRelations = relations(users, ({ many }) => ({
sentCoinsTransactions: many(coinsTransactions, {
relationName: 'sentCoinsTransactions',
}),
receivedCoinsTransactions: many(coinsTransactions, {
relationName: 'receivedCoinsTransactions',
}),
}));

export const coinsTransactions = pgTable('coins_transactions', {
...baseFields,

fromUserId: uuid('from_user_id')
.notNull()
.references(() => users.id),
toUserId: uuid('to_user_id')
.notNull()
.references(() => users.id),
message: text('message').notNull(),
amount: integer('amount').notNull(),
});

export const coinsTransactionsRelations = relations(
coinsTransactions,
({ one }) => ({
fromUser: one(users, {
fields: [coinsTransactions.fromUserId],
references: [users.id],
relationName: 'sentCoinsTransactions',
}),
toUser: one(users, {
fields: [coinsTransactions.toUserId],
references: [users.id],
relationName: 'receivedCoinsTransactions',
}),
})
);
const baseFields = {
id: uuid('id').primaryKey().defaultRandom(),
createdAt: timestamp('created_at').notNull().defaultNow(),
};

export const users = pgTable('users', {
...baseFields,

email: varchar('email', { length: 256 }).unique().notNull(),
firstName: varchar('first_name', { length: 256 }).notNull(),
lastName: varchar('last_name', { length: 256 }).notNull(),
active: boolean('active').notNull(),
birthdate: date('birthdate').notNull(),
sendsDaily: boolean('sends_daily').notNull(),
joinedAt: date('joined_at').notNull(),
coins: integer('coins').notNull(),
});

export const usersRelations = relations(users, ({ many }) => ({
sentCoinsTransactions: many(coinsTransactions, {
relationName: 'sentCoinsTransactions',
}),
receivedCoinsTransactions: many(coinsTransactions, {
relationName: 'receivedCoinsTransactions',
}),
}));

export const coinsTransactions = pgTable('coins_transactions', {
...baseFields,

fromUserId: uuid('from_user_id')
.notNull()
.references(() => users.id),
toUserId: uuid('to_user_id')
.notNull()
.references(() => users.id),
message: text('message').notNull(),
amount: integer('amount').notNull(),
});

export const coinsTransactionsRelations = relations(
coinsTransactions,
({ one }) => ({
fromUser: one(users, {
fields: [coinsTransactions.fromUserId],
references: [users.id],
relationName: 'sentCoinsTransactions',
}),
toUser: one(users, {
fields: [coinsTransactions.toUserId],
references: [users.id],
relationName: 'receivedCoinsTransactions',
}),
})
);
It would be good to document this on the site, is it ok to create a PR for the docs?
2 replies