nqhtrung
nqhtrung
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
DTDrizzle Team
Created by codefork on 8/18/2023 in #help
Get total row count in select
I ran into same issue and using extras doesn’t work, I have to use the regular CRUD api
7 replies
DTDrizzle Team
Created by nqhtrung on 8/4/2023 in #help
How to implement interface for table?
This, I’m working in a monorepo where I have a module for Web, one for API and one for shared interface. The workflow is I first create an interface in the shared interface module then use it in both web and api modules. This way I can make sure the api returns the correct type for the web.
5 replies