Zamiel
Zamiel
Explore posts from servers
DTDrizzle Team
Created by Zamiel on 1/1/2024 in #help
How to perform multiple queries?
I have multiple queries that I would like to do at the same time in order to save network traffic back and forth N times. I see that the "Batch" section of the docs applies to LibSQL and to D1. But I am using Postgres. Do I have any options in Postgres? (Besides just writing the SQL query raw I guess, which I want to hopefully avoid.)
19 replies
DTDrizzle Team
Created by Zamiel on 12/18/2023 in #help
How do I use NOW() in Drizzle?
Can someone explain how to write the following query in Drizzle?
UPDATE users
SET datetime_last_login = NOW()
WHERE id = $1
UPDATE users
SET datetime_last_login = NOW()
WHERE id = $1
6 replies
DTDrizzle Team
Created by Zamiel on 11/27/2023 in #help
How to check if a row exists in Drizzle?
According to this StackOverflow post: https://stackoverflow.com/questions/7471625/fastest-check-if-row-exists-in-postgresql The most performant way to check if a row exists is as follows:
SELECT EXISTS(SELECT 1 FROM foo WHERE id = 123)
SELECT EXISTS(SELECT 1 FROM foo WHERE id = 123)
How do you accomplish this in Drizzle?
6 replies
DTDrizzle Team
Created by Zamiel on 11/27/2023 in #help
What are the Drizzle conventions for giving names to Drizzle return types?
Hello all, I have a question about Drizzle conventions. Do people give names to the return types of their Drizzle calls? For example, consider the following code:
import { eq } from "drizzle-orm";
import { usersTable } from "../databaseSchema";
import { db } from "../db";

export type User = NonNullable<Awaited<ReturnType<typeof users.get>>>;

export const users = {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
get: async (username: string) => {
const rows = await db
.select({
id: usersTable.id,
username: usersTable.username,
passwordHash: usersTable.passwordHash,
})
.from(usersTable)
.where(eq(usersTable.username, username))
.limit(1);

return rows[0];
},
};
import { eq } from "drizzle-orm";
import { usersTable } from "../databaseSchema";
import { db } from "../db";

export type User = NonNullable<Awaited<ReturnType<typeof users.get>>>;

export const users = {
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
get: async (username: string) => {
const rows = await db
.select({
id: usersTable.id,
username: usersTable.username,
passwordHash: usersTable.passwordHash,
})
.from(usersTable)
.where(eq(usersTable.username, username))
.limit(1);

return rows[0];
},
};
- Here, we use the ReturnType utility type from TypeScript to satisfy DRY. (If we wanted to violate DRY, we could repeat all the user fields inside of a dedicated User TypeScript interface.) - However, we now have a problem where Intellisense does not work properly. In other words, if we mouse over user in the following code:
const user = users.get("foo");
const user = users.get("foo");
We get the full enumerated object inside of a union with undeifned, instead of the much easier to read and understand User | undefined. Does anyone know if there are Drizzle conventions to get smarter Intellisense here?
40 replies
DTDrizzle Team
Created by Zamiel on 8/14/2023 in #help
What is the Drizzle convention for storing the `db` object?
In the "Quick Start" guide, it showcases connecting to the database at the root of a module. However, in real code, this would likely be in a file called "db.ts", and it would not be at the root of the module, since the connection string would not yet be available. This is my naïve attempt to show what a more real-life code would look like:
export let db: PostgresJsDatabase;

export function databaseInit(): void {
const config = getDatabaseConfig();
const client = postgres(config);
db = drizzle(client);
}
export let db: PostgresJsDatabase;

export function databaseInit(): void {
const config = getDatabaseConfig();
const client = postgres(config);
db = drizzle(client);
}
And then, elsewhere in the code:
const user = await db.select().from(usersTable);
const user = await db.select().from(usersTable);
But here, I export a "let" variable, which flags ESLint and suggests that I am architecting my code incorrectly. Is there a more conventional way to handle the "db" object?
57 replies
DTDrizzle Team
Created by Zamiel on 8/12/2023 in #help
Does Drizzle ORM support CommonJS (CJS)?
No description
67 replies