/--ts-talk

Primary LanguageTypeScript

The "HOW" and "WHY" of TypeScript

This repo contains resources from my talk on Tuesday, Auguest 16 2022 for the OCK WebDevs UserGroup (part of the Techlahoma Foundation).

Resources:

All of the examples in this repo (and many more!) can be found here: https://github.com/dzharii/awesome-typescript

HIGHLIGHTS

Examples:

To run an example, first fork this repository and cd into the directory. Then, follow the instructions for the example below.

Example 1: Prisma + tRPC + Postgres + Zod

Prerequisites: You will need docker installed and running, and preferably a VSCode based editor. Credit for the sample SQL server goes to José David Arévalo (you can find out more in that directory's README.md).

Run the sql server:

cd ./docker_postgres_with_data
docker-compose up -d
cd ../prisma

Install deps and create env variables:

yarn install
touch .env
echo DB_URL='"postgresql://postgres:postgres@localhost:5438/postgres?schema=public"' >> ./.env

Infer the schema from the SQL server:

npx prisma db pull

Now you should see schema.prisma has been updated to include an auto-generated schema!

Generate router (tRPC), validator (Zod), and database resolvers (Prisma):

npx prisma generate

You should now see two new directories:

  • src/trpc: Contains routers and schemas for your new type-safe API
  • src/zod: Contains generated Zod validator functions

Bonus Points

  • Run npx tsc to compile your new code to JavaScript, and you'll see a new folder: lib/ containing the generated javascript codebase!