Skip to content

feat: Kysely upgrade #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ FROM node:20-alpine AS base
ENV HUSKY=0
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN npm i -g corepack@latest
RUN corepack enable

FROM base AS build
Expand Down
4 changes: 2 additions & 2 deletions Procfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
release: PGSSLMODE=no-verify npm run knex:prod:migrate:latest
web: npm run start:prod
release: PGSSLMODE=no-verify pnpm run kysely:prod:migrate:latest
web: node dist/src/main.js
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ $ pnpm install

```bash
# development
$ pnpm run start
$ pnpm start

# watch mode
$ npm run start:dev
$ pnpm start:dev

# debug mode
$ pnpm run start:debug
$ pnpm start:debug

# production mode
$ pnpm run start:prod
$ pnpm start:prod
```

### Running the app with docker :whale:
Expand Down Expand Up @@ -74,14 +74,11 @@ src

### Database and ORM

For database we have used PostgreSQL. And for ORM we have used [Knex.js](https://knexjs.org) with [ObjectionJS](https://vincit.github.io/objection.js).
For the database we use **PostgreSQL**. For interacting with the database, we now use [Kysely](https://kysely.dev/), a modern, type-safe SQL query builder for TypeScript.

Knex is an awesome query builder which is closer to SQL. Objection also a thin wrapper around Knex.
We now also handle migrations using [kysely-ctl](https://github.com/kysely-org/kysely-ctl), the official CLI tool for Kysely. Migration files are written in TypeScript and reside in the `src/database/migrations` directory.

For all database migrations please use `snake_case` conversion when creating tables or columns.
In PostgreSQL it is natural to work with `snake_case` when writing queries.

Knex will automatically map your `snake_case` names into `camelCase` on application side. Dont use `snake_case` in JS side. Instead always use `camelCase`. Read [more](https://vincit.github.io/objection.js/recipes/snake-case-to-camel-case-conversion.html)
Kysely will automatically map your `snake_case` names into `camelCase` on application side. Don't use `snake_case` in JS side. Instead always use `camelCase`. Read [more](https://kysely-org.github.io/kysely-apidoc/classes/CamelCasePlugin.html)

> Please change the database name in docker-compose file and .env

Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ services:
ports:
- 3000:3000
- 9229:9229
working_dir: /usr/src/app
working_dir: /app
volumes:
- .:/usr/src/app
- node_modules:/usr/src/app/node_modules
- dist:/usr/src/app/dist
depends_on:
- db
command: npm run start:debug
command: pnpm run start:debug

db:
image: postgres:latest
Expand Down
26 changes: 0 additions & 26 deletions knexfile.ts

This file was deleted.

21 changes: 21 additions & 0 deletions kysely.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { defineConfig, getKnexTimestampPrefix } from 'kysely-ctl';
import { Pool } from 'pg';

export default defineConfig({
dialect: 'pg',
dialectConfig: {
pool: new Pool({
connectionString: process.env.DATABASE_URL,
}),
},
migrations: {
migrationFolder: './src/database/migrations',
allowJS: true,
getMigrationPrefix: getKnexTimestampPrefix,
},
seeds: {
allowJS: true,
getSeedPrefix: getKnexTimestampPrefix,
seedFolder: './src/database/seeds',
},
});
58 changes: 29 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "wavezync-nest-starter",
"version": "0.0.1",
"version": "2.0.0",
"description": "WaveZync Nest Starter",
"author": "Kasun Vithanage <alankasun@gmail.com> (https://kasvith.me)",
"author": "Kasun Vithanage <kasun@wavezync.com> (https://kasvith.me)",
"private": false,
"repository": {
"type": "git",
"url": "https://github.com/wavezync/nestjs-starter"
"url": "https://github.com/wavezync/nestjs-starter.git"
},
"license": "MIT",
"scripts": {
Expand All @@ -23,33 +23,32 @@
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json",
"knex": "knex",
"knex:migrate:latest": "knex migrate:latest",
"knex:migrate:up": "knex migrate:up",
"knex:migrate:down": "knex migrate:down",
"knex:migrate:rollback": "knex migrate:rollback",
"knex:migrate:rollback:all": "knex migrate:rollback --all",
"knex:prod": "knex --knexfile dist/knexfile.js",
"knex:prod:migrate:latest": "pnpm run knex:prod migrate:latest",
"knex:prod:migrate:up": "pnpm run knex:prod migrate:up",
"knex:prod:migrate:down": "pnpm run knex:prod migrate:down",
"knex:prod:migrate:rollback": "pnpm run knex:prod migrate:rollback",
"knex:prod:migrate:rollback:all": "pnpm run knex:prod migrate:rollback --all",
"kysely": "kysely",
"kysely:migrate:latest": "kysely migrate:latest",
"kysely:migrate:up": "kysely migrate:up",
"kysely:migrate:down": "kysely migrate:down",
"kysely:migrate:rollback:all": "kysely migrate:rollback --all",
"kysely:prod": "kysely --config dist/kysely.config.js",
"kysely:prod:migrate:latest": "pnpm run kysely:prod migrate:latest",
"kysely:prod:migrate:up": "pnpm run kysely:prod migrate:up",
"kysely:prod:migrate:down": "pnpm run kysely:prod migrate:down",
"kysely:prod:migrate:rollback:all": "pnpm run kysely:prod migrate:rollback --all",
"doc:generate": "npx compodoc -p tsconfig.json -n \"Documentation\"",
"doc:serve": "npx compodoc -p tsconfig.json -n \"Documentation\" -s"
},
"dependencies": {
"@apollo/server": "^4.10.0",
"@nanogiants/nestjs-swagger-api-exception-decorator": "^1.6.11",
"@nestjs/apollo": "^12.1.0",
"@nestjs/common": "^10.3.3",
"@nestjs/config": "^3.2.0",
"@nestjs/core": "^10.3.3",
"@nestjs/graphql": "^12.1.1",
"@nestjs/jwt": "^10.2.0",
"@nestjs/platform-express": "^10.3.3",
"@nestjs/swagger": "^7.3.0",
"@nestjs/terminus": "^10.2.3",
"@nestjs/apollo": "^13.0.2",
"@nestjs/common": "^11.0.9",
"@nestjs/config": "^4.0.0",
"@nestjs/core": "^11.0.9",
"@nestjs/devtools-integration": "^0.2.0",
"@nestjs/graphql": "^13.0.2",
"@nestjs/jwt": "^11.0.0",
"@nestjs/platform-express": "^11.0.9",
"@nestjs/swagger": "^11.0.3",
"@nestjs/terminus": "^11.0.0",
"bcrypt": "^5.1.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
Expand All @@ -58,9 +57,8 @@
"find-config": "^1.0.0",
"graphql": "^16.8.1",
"helmet": "^7.1.0",
"knex": "^3.1.0",
"kysely": "^0.27.5",
"nestjs-pino": "^4.0.0",
"objection": "^3.1.4",
"pg": "^8.11.3",
"pino-http": "^9.0.0",
"pino-pretty": "^10.3.1",
Expand All @@ -72,13 +70,14 @@
},
"devDependencies": {
"@compodoc/compodoc": "^1.1.23",
"@nestjs/cli": "^10.3.2",
"@nestjs/schematics": "^10.1.1",
"@nestjs/testing": "^10.3.3",
"@nestjs/cli": "^11.0.2",
"@nestjs/schematics": "^11.0.0",
"@nestjs/testing": "^11.0.9",
"@types/bcrypt": "^5.0.2",
"@types/express": "^4.17.21",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.24",
"@types/pg": "^8.11.2",
"@types/supertest": "^6.0.2",
"@types/uuid": "^9.0.8",
"@typescript-eslint/eslint-plugin": "^7.1.1",
Expand All @@ -87,6 +86,7 @@
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.7.0",
"kysely-ctl": "^0.11.1",
"prettier": "^3.2.5",
"supertest": "^6.3.4",
"ts-jest": "^29.1.2",
Expand Down
Loading