await client.query( `SELECT * FROM users WHERE id = $1`, ['123'] )
await client.query( `SELECT * FROM users WHERE id = $1`, ['123'] )
interface User { id: number; name: string; age: number; } knex<User>('users') .where('id', '123') .first();
interface User { id: number; name: string; age: number; } knex<User>('users') .where('id', '123') .first();
// schema.prisma generator client { provider = "prisma-client-js" } datasource db { provider = "postgresql" url = env("DATABASE_URL") } model User { user_id String @id email String @unique full_name String bank_accounts BankAccount[] created_at DateTime @default(now()) updated_at DateTime @updatedAt @@map(name: "users") // Table name in your DB }
// schema.prisma generator client { provider = "prisma-client-js" } datasource db { provider = "postgresql" url = env("DATABASE_URL") } model User { user_id String @id email String @unique full_name String bank_accounts BankAccount[] created_at DateTime @default(now()) updated_at DateTime @updatedAt @@map(name: "users") // Table name in your DB }
prisma db push
β
prisma db pull
β
// User.ts @Entity() export class User { @PrimaryGeneratedColumn("uuid") user_id: number; @Column() firstName: string; @Column() lastName: string; @Column() age: number; }
// User.ts @Entity() export class User { @PrimaryGeneratedColumn("uuid") user_id: number; @Column() firstName: string; @Column() lastName: string; @Column() age: number; }
server > dist > node_modules > prisma schema.prisma > src > test .env .env.local .gitignore .npmrc .nvmrc .prettierrc.yml package.json package-lock.json README.md tsconfig.json
server > dist > node_modules > prisma schema.prisma > src > test .env .env.local .gitignore .npmrc .nvmrc .prettierrc.yml package.json package-lock.json README.md tsconfig.json
prisma generate
server > dist > node_modules > .bin > .prisma > client index.d.ts index.js ... > ... > prisma schema.prisma > src > test ...
server > dist > node_modules > .bin > .prisma > client index.d.ts index.js ... > ... > prisma schema.prisma > src > test ...
π findFirst()
π findMany()
π updateOne()
π etcβ¦
'helper' functions:
prismaClient.$queryRaw('<SQL>')
All TypeScript types corresponding to our entities.
π BankAccount
:
A row in our table.
π BankAccountCreateInput
:
What we have to set when creating a bank_account.
π BankAccountWhereInput
What we can use in a 'where' condition.
import express from 'express' // 1- Import it import { PrismaClient } from '@prisma/client' // = import from '.prisma/client // 2- Instantiate it const prisma = new PrismaClient() const app = express() app.get('/bank-accounts', async (req, res) => { // 3- Use it const bankAccounts = await prisma.bankAccount.findMany({ where: { active: true }, include: { user: true }, orderBy: { created_at: 'desc' }, }) res.json(bankAccounts) })
import express from 'express' // 1- Import it import { PrismaClient } from '@prisma/client' // = import from '.prisma/client // 2- Instantiate it const prisma = new PrismaClient() const app = express() app.get('/bank-accounts', async (req, res) => { // 3- Use it const bankAccounts = await prisma.bankAccount.findMany({ where: { active: true }, include: { user: true }, orderBy: { created_at: 'desc' }, }) res.json(bankAccounts) })
DATABASE_URL=postgresql://test-user:plop@localhost:5432/freely-test
TRUNCATE TABLE \"${dbSchemaName}\".\"${tablename}\" CASCADE;
More info here.
prisma format
> formats + runs some checksprisma db push
> updates local database structure + updates Prisma clientprisma migrate dev --name "Adding bank_accounts table"
server > dist > node_modules > prisma > migrations > 20210715144607_adding_bank_accounts_table migration.sql schema.prisma > src ...
server > dist > node_modules > prisma > migrations > 20210715144607_adding_bank_accounts_table migration.sql schema.prisma > src ...
scripts: { "db-preprod-up": "dotenv -e .env.preprod -- npx prisma migrate deploy", }
scripts: { "db-preprod-up": "dotenv -e .env.preprod -- npx prisma migrate deploy", }
prisma studio
A minimalist admin UI to navigate throughout your data.
ββ TypeScript-first.
ββ Dynamic project.
A real team behind it, regular releases, huge community on Slack (~46,000 people), Prisma Day conference.
(Disclaimer: Like any other tool, we donβt know if it will still be that dynamic in a few years!)
ββ A wide and well-written documentation.