nqhtrung
nqhtrung
DTDrizzle Team
Created by Down on 2/7/2025 in #help
when running postgres in docker, drizzle cannot connect to it, says database doesnt exist
have you map localhost to 127.0.0.1?
7 replies
DTDrizzle Team
Created by nu on 2/5/2025 in #help
Changing postgres timestamp mode (string -> date) isn't reflected in migrations
Date is stored as string in postgres/mysql. The mode: ‘date’ only affects the typescript type. Behind the scene, your data is always saved as string.
4 replies
DTDrizzle Team
Created by Bernardes on 2/7/2025 in #help
Field type error on insert
No description
2 replies
DTDrizzle Team
Created by xxxxx on 2/7/2025 in #help
Update returning with some relation
I believe your question is answered here https://www.answeroverflow.com/m/1177544395306827826 As far as I’m concerned, it’s not possible in Postgres (assumed that’s what you’re using). If it can’t be done via native query, it’s not possible in Drizzle
2 replies
DTDrizzle Team
Created by Down on 2/7/2025 in #help
when running postgres in docker, drizzle cannot connect to it, says database doesnt exist
localhost in docker refers to its own network - the network inside docker container. Instead of using localhost, try to use the container name in docker. Make sure you have all the services in the same network. It’s worth reading up on docker network for these kinds of issues.
7 replies
DTDrizzle Team
Created by Vinny on 3/10/2024 in #help
Error Too many connections with Drizzle + MySQL2
No description
13 replies
DTDrizzle Team
Created by Vinny on 3/10/2024 in #help
Error Too many connections with Drizzle + MySQL2
it’s a problem with hot reload in NextJS. Try using singleton for your connection pool.
13 replies
DTDrizzle Team
Created by nqhtrung on 1/19/2024 in #help
drizzle infers incorrect type of decimal
Seems like it is a bug and has been reported here https://github.com/drizzle-team/drizzle-orm/issues/1290
2 replies
DTDrizzle Team
Created by nqhtrung on 1/10/2024 in #help
Drizzle kit drops and re-adds constraint
I understand that if a schema is not specified, the default one is public, so what's the point of forcing "public" namespace for constraint like this? It would make the migration files huge
2 replies
DTDrizzle Team
Created by nqhtrung on 12/15/2023 in #help
findFirst should return optional object
Hi, thanks for the response. I just discovered that strictNullChecks should be true. My bad 😥 . Everything is good now! Much appreciated!
4 replies
DTDrizzle Team
Created by codefork on 8/18/2023 in #help
Get total row count in select
here is how I get total count of the records returned. I also did some pagination here.
const condition = and(
input?.category ? ilike(products.category, input.category) : undefined,
input?.name ? ilike(products.name, `%${input.name}%`) : undefined
);

const [countResult] = await db
.select({
count: sql`count(*)`.mapWith(Number).as("count"),
})
.from(products)
.where(condition);

const productList = await db.query.products.findMany({
limit: input.perPage,
offset: input.page * input.perPage,
where: condition,
orderBy: products.name,
});

const totalPage = Math.ceil((countResult?.count ?? 0) / input.perPage);

return {
totalPage,
perPage: input.perPage,
data: productList,
};
const condition = and(
input?.category ? ilike(products.category, input.category) : undefined,
input?.name ? ilike(products.name, `%${input.name}%`) : undefined
);

const [countResult] = await db
.select({
count: sql`count(*)`.mapWith(Number).as("count"),
})
.from(products)
.where(condition);

const productList = await db.query.products.findMany({
limit: input.perPage,
offset: input.page * input.perPage,
where: condition,
orderBy: products.name,
});

const totalPage = Math.ceil((countResult?.count ?? 0) / input.perPage);

return {
totalPage,
perPage: input.perPage,
data: productList,
};
7 replies
DTDrizzle Team
Created by sik on 8/13/2023 in #help
What's the best way to deploy migrations on production database?
Sorry if this is not what you needed, but here is how I handle migration on live production I create a docker instance and install the bare necessary dependencies just to run the migration script Here is the Dockerfile
FROM --platform=$BUILDPLATFORM node:alpine AS deps
WORKDIR /app

# We only need these dependencies for the migration script
RUN yarn add drizzle-orm postgres dotenv tsx

FROM --platform=$BUILDPLATFORM node:alpine AS builder

WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

CMD ["yarn", "migration:run"]
FROM --platform=$BUILDPLATFORM node:alpine AS deps
WORKDIR /app

# We only need these dependencies for the migration script
RUN yarn add drizzle-orm postgres dotenv tsx

FROM --platform=$BUILDPLATFORM node:alpine AS builder

WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

CMD ["yarn", "migration:run"]
Here is the script section in my package.json
"scripts": {
...
"migration:run": "tsx ./drizzle/migration.ts",
...
},
"scripts": {
...
"migration:run": "tsx ./drizzle/migration.ts",
...
},
migration.ts is just a ts file where I define my migrate function. Here is my docker compose file.
version: '3.6'

services:
postgres_db:
// docker instance for the db
app:
// docker instance for the main app
migration:
depends_on:
- postgres_db
- app
build:
context: .
dockerfile: Dockerfile.migration // the name of the Dockerfile for migration
environment:
- DB_USER=${DB_USER}
- DB_PASS=${DB_PASS}
- DB_NAME=${DB_NAME}
- DB_PORT=5432
- DB_DOMAIN=postgres_db
- NODE_ENV=production
volumes:
postgres_db:
version: '3.6'

services:
postgres_db:
// docker instance for the db
app:
// docker instance for the main app
migration:
depends_on:
- postgres_db
- app
build:
context: .
dockerfile: Dockerfile.migration // the name of the Dockerfile for migration
environment:
- DB_USER=${DB_USER}
- DB_PASS=${DB_PASS}
- DB_NAME=${DB_NAME}
- DB_PORT=5432
- DB_DOMAIN=postgres_db
- NODE_ENV=production
volumes:
postgres_db:
This way, every time you run the docker compose, you start the db, the app and THEN the migration will run, the container will automatically shut down when the migration succeeds (or fails). Since migrations are safe to run when they have already been run, we can start migrations every time we start the app.
16 replies