lost22git / test-fastify

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dev app

init app

mkdir test-fastify && cd test-fastify

pnpm init -y

install fastify

pnpm i fastify

install typescript

pnpm i -D typescript @types/node

pnpm exec tsc --init

tsconfig.json

{
    "compileOptions": {
        "target": "ES2021",
        "module": "ES2022",
        "moduleResolution": "node"
    }
}

package.json

{
    "scripts": {
        "build": "tsc -p tsconfig.json",
        "start": "node index.js"
    }
    "type": "module",
}

install prisma

pnpm i prisma

init prisma

pnpm exec prisma init --datasource-provider sqlite

edit ./prisma/schema.prisma file

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = env("DATABASE_URL")
}

model Fighter {
  id         Int       @id @default(autoincrement())
  name       String    @unique
  skill      String
  created_at DateTime  @default(now())
  updated_at DateTime?

  @@map("fighter")
}

edit .env file

# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB and CockroachDB.
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="file:./fighter.db"

sync schema to db and generate migrate scripts

pnpm exec prisma migrate dev --name init-fighter-schema

generate prisma client code

pnpm exec prisma generate

use PrismaClient in your ts code

import { PrismaClient } from '@prisma/client'

const prisma = new PrismaClient()

install typebox and plugin for fastify

pnpm i @sinclair/typebox @fastify/type-provider-typebox

install cors plugin

pnpm i @fastify/cors

install swagger plugin

pnpm i @fastify/swagger

pnpm i @fastify/swagger-ui

build and start app

pnpm build && pnpm start

About

License:MIT License


Languages

Language:TypeScript 100.0%