feat: 完成现有小程序接口的全部迁移至新后端
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
# MongoDB Setup
|
||||
|
||||
This reference configures **Prisma ORM 6 MongoDB** applications. Preserve an existing Prisma 6 app or use this path when 6 is explicitly selected. Do not apply Prisma 7 SQL adapters here.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- MongoDB 4.2+
|
||||
- Replica Set configured (required for transactions)
|
||||
- Latest Prisma 6.x release, or your team's pinned Prisma 6 version
|
||||
- A Node.js and TypeScript version supported by the selected Prisma 6 release
|
||||
|
||||
## 1. Schema Configuration
|
||||
|
||||
Use the standard Prisma 6 MongoDB setup with `prisma-client-js`.
|
||||
|
||||
In `prisma/schema.prisma`:
|
||||
|
||||
```prisma
|
||||
datasource db {
|
||||
provider = "mongodb"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
```
|
||||
|
||||
### Driver Adapters
|
||||
|
||||
Do **not** apply the Prisma 7 SQL adapter setup here. MongoDB does not use a SQL `@prisma/adapter-*` package.
|
||||
|
||||
### ID Field Requirement
|
||||
|
||||
MongoDB models **must** have a mapped `_id` field using `@id` and `@map("_id")`, usually of type `String` with `auto()` and `db.ObjectId`.
|
||||
|
||||
```prisma
|
||||
model User {
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
email String @unique
|
||||
name String?
|
||||
posts Post[]
|
||||
}
|
||||
```
|
||||
|
||||
### Relations
|
||||
|
||||
Relations in MongoDB expect IDs to be `db.ObjectId` type.
|
||||
|
||||
```prisma
|
||||
model Post {
|
||||
id String @id @default(auto()) @map("_id") @db.ObjectId
|
||||
author User @relation(fields: [authorId], references: [id])
|
||||
authorId String @db.ObjectId
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Environment Variable
|
||||
|
||||
In `.env`:
|
||||
|
||||
```env
|
||||
DATABASE_URL="mongodb+srv://user:password@cluster.mongodb.net/mydb?retryWrites=true&w=majority"
|
||||
```
|
||||
|
||||
## Migrations vs Introspection
|
||||
|
||||
- **No Migrations**: MongoDB is schema-less. `prisma migrate` commands **do not work**.
|
||||
- **db push**: Use `prisma db push` to sync indexes and constraints.
|
||||
- **db pull**: Use `prisma db pull` to generate schema from existing data (sampling).
|
||||
|
||||
## Client setup
|
||||
|
||||
Keep existing pinned versions. For a new explicitly selected Prisma 6 setup:
|
||||
|
||||
```bash
|
||||
npm install --save-dev prisma@6
|
||||
npm install @prisma/client@6 dotenv
|
||||
```
|
||||
|
||||
Generate with the matching CLI, then initialize the client without a SQL adapter:
|
||||
|
||||
```typescript
|
||||
import 'dotenv/config'
|
||||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
const prisma = new PrismaClient()
|
||||
```
|
||||
|
||||
Use a read-only model query to verify connectivity. Review any index changes before applying `db push` to an existing database.
|
||||
|
||||
## Common Issues
|
||||
|
||||
### "Transactions not supported"
|
||||
Ensure your MongoDB instance is a **Replica Set**. Standalone instances do not support transactions. Atlas clusters are replica sets by default.
|
||||
|
||||
### "Invalid ObjectID"
|
||||
Ensure fields referencing IDs are decorated with `@db.ObjectId` if the target is an ObjectID.
|
||||
|
||||
## References
|
||||
|
||||
- [Prisma 6 MongoDB connector](https://www.prisma.io/docs/orm/v6/overview/databases/mongodb)
|
||||
@@ -0,0 +1,50 @@
|
||||
# Prisma Client Setup
|
||||
|
||||
Generate and instantiate **Prisma ORM 7** Client for SQL providers. For MongoDB, follow [Prisma 6 MongoDB setup](v6-mongodb.md). For an existing Prisma 6 SQL app, preserve its client setup rather than copying these examples.
|
||||
|
||||
## 1. Install dependencies
|
||||
|
||||
```bash
|
||||
npm install --save-dev prisma@7
|
||||
npm install @prisma/client@7 dotenv
|
||||
```
|
||||
|
||||
For an existing application, retain its pinned releases. For a new Prisma 7 application, use compatible CLI, client, and adapter versions and check the [Prisma 7 runtime requirements](https://www.prisma.io/docs/guides/upgrade-prisma-orm/v7#prerequisites).
|
||||
|
||||
## 2. Add generator block
|
||||
|
||||
In `prisma/schema.prisma`:
|
||||
|
||||
```prisma
|
||||
generator client {
|
||||
provider = "prisma-client"
|
||||
output = "../generated"
|
||||
}
|
||||
```
|
||||
|
||||
`prisma-client` requires an explicit `output` path and does not generate into `node_modules` by default.
|
||||
|
||||
## 3. Generate Prisma Client
|
||||
|
||||
```bash
|
||||
npx prisma generate
|
||||
```
|
||||
|
||||
Re-run `prisma generate` after every schema change to keep the client in sync.
|
||||
|
||||
## 4. Instantiate Prisma Client
|
||||
|
||||
```typescript
|
||||
import 'dotenv/config'
|
||||
import { PrismaClient } from '../generated/client'
|
||||
import { PrismaPg } from '@prisma/adapter-pg'
|
||||
|
||||
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
|
||||
const prisma = new PrismaClient({ adapter })
|
||||
```
|
||||
|
||||
If you change the generator `output`, update the import path to match. For the SQL provider workflow, replace `PrismaPg` with the adapter for your database.
|
||||
|
||||
## 5. Use a single instance
|
||||
|
||||
Each `PrismaClient` instance creates a connection pool. Reuse a single instance per app process to avoid exhausting database connections.
|
||||
@@ -0,0 +1,94 @@
|
||||
# CockroachDB Setup
|
||||
|
||||
Configure **Prisma ORM 7** with CockroachDB. For an existing Prisma 6 app, keep its configuration and use the [Prisma 6 docs](https://www.prisma.io/docs/orm/v6); the configuration and adapter examples below are for 7.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- CockroachDB cluster
|
||||
|
||||
## 1. Schema Configuration
|
||||
|
||||
In `prisma/schema.prisma`:
|
||||
|
||||
```prisma
|
||||
datasource db {
|
||||
provider = "cockroachdb"
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client"
|
||||
output = "../generated"
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Config Configuration
|
||||
|
||||
In `prisma.config.ts`:
|
||||
|
||||
```typescript
|
||||
import 'dotenv/config'
|
||||
import { defineConfig, env } from 'prisma/config'
|
||||
|
||||
export default defineConfig({
|
||||
schema: 'prisma/schema.prisma',
|
||||
datasource: {
|
||||
url: env('DATABASE_URL'),
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
## 3. Environment Variable
|
||||
|
||||
In `.env`:
|
||||
|
||||
```env
|
||||
DATABASE_URL="postgresql://user:password@host:26257/db?sslmode=verify-full"
|
||||
```
|
||||
|
||||
Note: CockroachDB uses the PostgreSQL wire protocol, so the URL often looks like postgresql, but the provider **MUST** be `cockroachdb` in the schema to handle specific CRDB features correctly.
|
||||
|
||||
## Driver Adapter
|
||||
|
||||
Use a driver adapter for the standard SQL workflow. CockroachDB is PostgreSQL-compatible, so use the PostgreSQL adapter.
|
||||
|
||||
1. Install adapter and driver:
|
||||
```bash
|
||||
npm install @prisma/adapter-pg@7 pg
|
||||
```
|
||||
|
||||
2. Instantiate Prisma Client with the adapter:
|
||||
```typescript
|
||||
import 'dotenv/config'
|
||||
import { PrismaClient } from '../generated/client'
|
||||
import { PrismaPg } from '@prisma/adapter-pg'
|
||||
|
||||
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
|
||||
const prisma = new PrismaClient({ adapter })
|
||||
```
|
||||
|
||||
## ID Generation
|
||||
|
||||
CockroachDB uses `BigInt` or `UUID` for IDs efficiently.
|
||||
|
||||
```prisma
|
||||
model User {
|
||||
id BigInt @id @default(autoincrement()) // Uses unique_rowid()
|
||||
}
|
||||
```
|
||||
|
||||
Or using string UUIDs:
|
||||
|
||||
```prisma
|
||||
model User {
|
||||
id String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
|
||||
}
|
||||
```
|
||||
|
||||
## Common Issues
|
||||
|
||||
### Schema Introspection
|
||||
Always use `provider = "cockroachdb"` to ensure correct type mapping during `db pull`.
|
||||
|
||||
## References
|
||||
|
||||
- [Prisma 7 CockroachDB documentation](https://www.prisma.io/docs/orm/v7/core-concepts/supported-databases/cockroachdb)
|
||||
@@ -0,0 +1,132 @@
|
||||
# MySQL Setup
|
||||
|
||||
Configure **Prisma ORM 7** with MySQL and MariaDB. For an existing Prisma 6 app, keep its configuration and use the [Prisma 6 docs](https://www.prisma.io/docs/orm/v6); the configuration and adapter examples below are for 7.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- MySQL or MariaDB database
|
||||
- Connection string
|
||||
|
||||
## 1. Schema Configuration
|
||||
|
||||
In `prisma/schema.prisma`:
|
||||
|
||||
```prisma
|
||||
datasource db {
|
||||
provider = "mysql"
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client"
|
||||
output = "../generated"
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Config Configuration
|
||||
|
||||
In `prisma.config.ts`:
|
||||
|
||||
```typescript
|
||||
import 'dotenv/config'
|
||||
import { defineConfig, env } from 'prisma/config'
|
||||
|
||||
export default defineConfig({
|
||||
schema: 'prisma/schema.prisma',
|
||||
datasource: {
|
||||
url: env('DATABASE_URL'),
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
## 3. Environment Variable
|
||||
|
||||
In `.env`:
|
||||
|
||||
```env
|
||||
DATABASE_URL="mysql://user:password@localhost:3306/mydb"
|
||||
```
|
||||
|
||||
### Connection String Format
|
||||
|
||||
```
|
||||
mysql://USER:PASSWORD@HOST:PORT/DATABASE
|
||||
```
|
||||
|
||||
- **USER**: Database user
|
||||
- **PASSWORD**: Password
|
||||
- **HOST**: Hostname
|
||||
- **PORT**: Port (default 3306)
|
||||
- **DATABASE**: Database name
|
||||
|
||||
## Driver Adapter
|
||||
|
||||
Use a driver adapter for the standard SQL workflow.
|
||||
|
||||
1. Install adapter and driver:
|
||||
```bash
|
||||
npm install @prisma/adapter-mariadb@7 mariadb
|
||||
```
|
||||
|
||||
2. Set `MYSQL_USER`, `MYSQL_PASSWORD`, and `MYSQL_DATABASE` in the application environment to match the CLI URL, and use the same host and port. Instantiate Prisma Client with the adapter:
|
||||
|
||||
```typescript
|
||||
import 'dotenv/config'
|
||||
import { PrismaClient } from '../generated/client'
|
||||
import { PrismaMariaDb } from '@prisma/adapter-mariadb'
|
||||
|
||||
const adapter = new PrismaMariaDb({
|
||||
host: 'localhost',
|
||||
port: 3306,
|
||||
connectionLimit: 5,
|
||||
user: process.env.MYSQL_USER,
|
||||
password: process.env.MYSQL_PASSWORD,
|
||||
database: process.env.MYSQL_DATABASE,
|
||||
})
|
||||
|
||||
const prisma = new PrismaClient({ adapter })
|
||||
```
|
||||
|
||||
### Text protocol option
|
||||
|
||||
If you need the MariaDB driver's text protocol instead of the default binary `execute()` path, enable `useTextProtocol` explicitly:
|
||||
|
||||
```typescript
|
||||
import { PrismaClient } from '../generated/client'
|
||||
import { PrismaMariaDb } from '@prisma/adapter-mariadb'
|
||||
|
||||
const adapter = new PrismaMariaDb(process.env.DATABASE_URL!, {
|
||||
useTextProtocol: true,
|
||||
})
|
||||
|
||||
const prisma = new PrismaClient({ adapter })
|
||||
```
|
||||
|
||||
Use this only when you specifically need text-protocol compatibility for your MariaDB setup.
|
||||
|
||||
## PlanetScale Setup
|
||||
|
||||
Check whether foreign key constraints are enabled on the PlanetScale database. When they are disabled, use `relationMode = "prisma"` and add indexes for relation fields. Do not override an existing database that has foreign keys enabled.
|
||||
|
||||
In `prisma/schema.prisma`:
|
||||
|
||||
```prisma
|
||||
datasource db {
|
||||
provider = "mysql"
|
||||
relationMode = "prisma" // Emulate foreign keys in Prisma
|
||||
}
|
||||
```
|
||||
|
||||
## Common Issues
|
||||
|
||||
### "Too many connections"
|
||||
|
||||
Set `connectionLimit` on `PrismaMariaDb`, as in the example above. The Prisma 6 `connection_limit` URL parameter does not configure the Prisma 7 adapter pool.
|
||||
|
||||
### JSON Support
|
||||
MySQL 5.7+ supports JSON. MariaDB 10.2+ supports JSON (as an alias for LONGTEXT with check constraints). Prisma handles this, but verify your version.
|
||||
|
||||
## References
|
||||
|
||||
- [Prisma 7 connection pool settings](https://www.prisma.io/docs/orm/v7/prisma-client/setup-and-configuration/databases-connections/connection-pool)
|
||||
- [PlanetScale foreign key configuration](https://www.prisma.io/docs/orm/v7/overview/databases/planetscale)
|
||||
- [Prisma 7 MySQL and MariaDB documentation](https://www.prisma.io/docs/orm/v7/core-concepts/supported-databases/mysql)
|
||||
@@ -0,0 +1,97 @@
|
||||
# PostgreSQL Setup
|
||||
|
||||
Configure **Prisma ORM 7** with PostgreSQL. For an existing Prisma 6 app, keep its configuration and use the [Prisma 6 docs](https://www.prisma.io/docs/orm/v6); the configuration and adapter examples below are for 7.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- PostgreSQL database (local or cloud)
|
||||
- Connection string
|
||||
|
||||
## 1. Schema Configuration
|
||||
|
||||
In `prisma/schema.prisma`:
|
||||
|
||||
```prisma
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client"
|
||||
output = "../generated"
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Config Configuration
|
||||
|
||||
In `prisma.config.ts`:
|
||||
|
||||
```typescript
|
||||
import 'dotenv/config'
|
||||
import { defineConfig, env } from 'prisma/config'
|
||||
|
||||
export default defineConfig({
|
||||
schema: 'prisma/schema.prisma',
|
||||
datasource: {
|
||||
url: env('DATABASE_URL'),
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
## 3. Environment Variable
|
||||
|
||||
In `.env`:
|
||||
|
||||
```env
|
||||
DATABASE_URL="postgresql://user:password@localhost:5432/mydb?schema=public"
|
||||
```
|
||||
|
||||
### Connection String Format
|
||||
|
||||
```
|
||||
postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA
|
||||
```
|
||||
|
||||
- **USER**: Database user
|
||||
- **PASSWORD**: Password (URL encoded if special chars)
|
||||
- **HOST**: Hostname (localhost, IP, or domain)
|
||||
- **PORT**: Port (default 5432)
|
||||
- **DATABASE**: Database name
|
||||
- **SCHEMA**: Schema name (default `public`)
|
||||
|
||||
## Driver Adapter
|
||||
|
||||
Use a driver adapter for the standard SQL workflow.
|
||||
|
||||
1. Install adapter and driver:
|
||||
```bash
|
||||
npm install @prisma/adapter-pg@7 pg
|
||||
```
|
||||
|
||||
2. Instantiate Prisma Client with the adapter:
|
||||
```typescript
|
||||
import 'dotenv/config'
|
||||
import { PrismaClient } from '../generated/client'
|
||||
import { PrismaPg } from '@prisma/adapter-pg'
|
||||
|
||||
const adapter = new PrismaPg({ connectionString: process.env.DATABASE_URL })
|
||||
const prisma = new PrismaClient({ adapter })
|
||||
```
|
||||
|
||||
## Common Issues
|
||||
|
||||
### "Can't reach database server"
|
||||
- Check host and port
|
||||
- Check firewall settings
|
||||
- Ensure database is running
|
||||
|
||||
### "Authentication failed"
|
||||
- Check user/password
|
||||
- Special characters in password must be URL-encoded
|
||||
|
||||
### "Schema does not exist"
|
||||
- Ensure `?schema=public` (or your schema) is in the URL
|
||||
|
||||
## References
|
||||
|
||||
- [Prisma 7 PostgreSQL documentation](https://www.prisma.io/docs/orm/v7/core-concepts/supported-databases/postgresql)
|
||||
@@ -0,0 +1,32 @@
|
||||
# Prisma Postgres with Prisma ORM 7
|
||||
|
||||
Use this reference for an existing or explicitly selected **Prisma ORM 7** app. Obtain or reuse the database through [prisma-postgres-setup](../../prisma-postgres-setup/SKILL.md).
|
||||
|
||||
## Node.js connections
|
||||
|
||||
Use the PostgreSQL TCP connection supplied by Prisma Console or the integration and follow [PostgreSQL setup](v7-postgresql.md). Use a pooled or direct connection according to the intended operation. A `prisma+postgres://` Accelerate URL is not a TCP connection string for `PrismaPg`.
|
||||
|
||||
## Edge connections
|
||||
|
||||
For a runtime without PostgreSQL TCP support, Prisma Postgres provides an HTTP/WebSocket driver. With a Prisma 7 client:
|
||||
|
||||
```bash
|
||||
npm install @prisma/adapter-ppg@7 @prisma/ppg
|
||||
```
|
||||
|
||||
```typescript
|
||||
import { PrismaClient } from '../generated/client'
|
||||
import { PrismaPostgresAdapter } from '@prisma/adapter-ppg'
|
||||
|
||||
const prisma = new PrismaClient({
|
||||
adapter: new PrismaPostgresAdapter({
|
||||
connectionString: process.env.DATABASE_URL!,
|
||||
}),
|
||||
})
|
||||
```
|
||||
|
||||
The serverless driver takes a direct Prisma Postgres connection string as its credential and communicates over HTTP/WebSockets. It does not open a TCP connection. Load secrets through the host's supported mechanism and set the Prisma 7 generator's `runtime` to that deployment target.
|
||||
|
||||
These adapter examples do not configure Prisma 8. Its runtime guidance belongs to the installed `prisma-8` skill.
|
||||
|
||||
See the [Prisma Postgres serverless driver documentation](https://www.prisma.io/docs/postgres/database/serverless-driver) for connection selection and supported runtimes.
|
||||
@@ -0,0 +1,114 @@
|
||||
# SQLite Setup
|
||||
|
||||
Configure **Prisma ORM 7** with SQLite. For an existing Prisma 6 app, keep its configuration and use the [Prisma 6 docs](https://www.prisma.io/docs/orm/v6); the configuration and adapter examples below are for 7.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- None (file-based)
|
||||
|
||||
## 1. Schema Configuration
|
||||
|
||||
In `prisma/schema.prisma`:
|
||||
|
||||
```prisma
|
||||
datasource db {
|
||||
provider = "sqlite"
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client"
|
||||
output = "../generated"
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Config Configuration
|
||||
|
||||
In `prisma.config.ts`:
|
||||
|
||||
```typescript
|
||||
import 'dotenv/config'
|
||||
import { defineConfig, env } from 'prisma/config'
|
||||
|
||||
export default defineConfig({
|
||||
schema: 'prisma/schema.prisma',
|
||||
datasource: {
|
||||
url: env('DATABASE_URL'),
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
## 3. Environment Variable
|
||||
|
||||
In `.env`:
|
||||
|
||||
```env
|
||||
DATABASE_URL="file:./dev.db"
|
||||
```
|
||||
|
||||
### Connection String Format
|
||||
|
||||
```
|
||||
file:PATH
|
||||
```
|
||||
|
||||
- **PATH**: Relative path to the database file. Check `prisma.config.ts` if you need to confirm how your app resolves it.
|
||||
|
||||
## Driver Adapter
|
||||
|
||||
Use a driver adapter for the standard SQL workflow.
|
||||
|
||||
1. Install adapter and driver:
|
||||
```bash
|
||||
npm install @prisma/adapter-better-sqlite3@7 better-sqlite3
|
||||
```
|
||||
|
||||
2. Instantiate Prisma Client with the adapter:
|
||||
```typescript
|
||||
import 'dotenv/config'
|
||||
import { PrismaClient } from '../generated/client'
|
||||
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3'
|
||||
|
||||
const adapter = new PrismaBetterSqlite3({
|
||||
url: process.env.DATABASE_URL!,
|
||||
})
|
||||
|
||||
const prisma = new PrismaClient({ adapter })
|
||||
```
|
||||
|
||||
## Using Driver Adapter (LibSQL / Turso)
|
||||
|
||||
For edge compatibility or Turso:
|
||||
|
||||
1. Install:
|
||||
```bash
|
||||
npm install @prisma/adapter-libsql@7 @libsql/client
|
||||
```
|
||||
|
||||
2. Instantiate:
|
||||
```typescript
|
||||
import 'dotenv/config'
|
||||
import { PrismaClient } from '../generated/client'
|
||||
import { PrismaLibSql } from '@prisma/adapter-libsql'
|
||||
|
||||
const adapter = new PrismaLibSql({
|
||||
url: process.env.TURSO_DATABASE_URL,
|
||||
authToken: process.env.TURSO_AUTH_TOKEN,
|
||||
})
|
||||
const prisma = new PrismaClient({ adapter })
|
||||
```
|
||||
|
||||
## Limitations
|
||||
|
||||
- **Enums**: Prisma supports enum fields, but SQLite does not enforce enum values at the database level.
|
||||
- **No Scalar Lists**: `String[]` is not supported directly.
|
||||
- **Concurrency**: Write operations lock the file.
|
||||
|
||||
## Common Issues
|
||||
|
||||
### "Database file not found"
|
||||
|
||||
Check the resolved file path for both the CLI and the runtime adapter; relative paths can resolve differently. Use an explicit path to the intended file rather than silently creating a second database.
|
||||
|
||||
## References
|
||||
|
||||
- [Prisma 7 SQLite documentation](https://www.prisma.io/docs/orm/v7/core-concepts/supported-databases/sqlite)
|
||||
@@ -0,0 +1,101 @@
|
||||
# SQL Server Setup
|
||||
|
||||
Configure **Prisma ORM 7** with SQL Server. For an existing Prisma 6 app, keep its configuration and use the [Prisma 6 docs](https://www.prisma.io/docs/orm/v6); the configuration and adapter examples below are for 7.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- SQL Server 2017, 2019, 2022, or Azure SQL
|
||||
- TCP/IP enabled
|
||||
|
||||
## 1. Schema Configuration
|
||||
|
||||
In `prisma/schema.prisma`:
|
||||
|
||||
```prisma
|
||||
datasource db {
|
||||
provider = "sqlserver"
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client"
|
||||
output = "../generated"
|
||||
}
|
||||
```
|
||||
|
||||
## 2. Config Configuration
|
||||
|
||||
In `prisma.config.ts`:
|
||||
|
||||
```typescript
|
||||
import 'dotenv/config'
|
||||
import { defineConfig, env } from 'prisma/config'
|
||||
|
||||
export default defineConfig({
|
||||
schema: 'prisma/schema.prisma',
|
||||
datasource: {
|
||||
url: env('DATABASE_URL'),
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
## 3. Environment Variable
|
||||
|
||||
In `.env`:
|
||||
|
||||
```env
|
||||
DATABASE_URL="sqlserver://localhost:1433;database=mydb;user=sa;password=Password123;encrypt=true;trustServerCertificate=true"
|
||||
```
|
||||
|
||||
### Connection String Format
|
||||
|
||||
```
|
||||
sqlserver://HOST:PORT;database=DB;user=USER;password=PASS;encrypt=true;trustServerCertificate=true
|
||||
```
|
||||
|
||||
- **encrypt**: Required for Azure (true).
|
||||
- **trustServerCertificate**: True for self-signed certs (local dev).
|
||||
|
||||
## Driver Adapter
|
||||
|
||||
Use a driver adapter for the standard SQL workflow.
|
||||
|
||||
1. Install adapter and driver:
|
||||
```bash
|
||||
npm install @prisma/adapter-mssql@7 mssql
|
||||
```
|
||||
|
||||
2. Set `SQLSERVER_USER` and `SQLSERVER_PASSWORD` in the application environment to match the CLI URL, and use the same server, port, database, and TLS options. The following example is for local development with a self-signed certificate; use certificate verification for hosted databases. Instantiate Prisma Client with the adapter:
|
||||
|
||||
```typescript
|
||||
import 'dotenv/config'
|
||||
import { PrismaClient } from '../generated/client'
|
||||
import { PrismaMssql } from '@prisma/adapter-mssql'
|
||||
|
||||
const adapter = new PrismaMssql({
|
||||
server: 'localhost',
|
||||
port: 1433,
|
||||
database: 'mydb',
|
||||
user: process.env.SQLSERVER_USER,
|
||||
password: process.env.SQLSERVER_PASSWORD,
|
||||
options: {
|
||||
encrypt: true,
|
||||
trustServerCertificate: true,
|
||||
},
|
||||
})
|
||||
|
||||
const prisma = new PrismaClient({ adapter })
|
||||
```
|
||||
|
||||
## Common Issues
|
||||
|
||||
### "Login failed for user"
|
||||
- SQL Server auth vs Windows auth. Prisma typically uses SQL Server authentication (username/password).
|
||||
- Ensure TCP/IP is enabled in SQL Server Configuration Manager.
|
||||
|
||||
### "Table not found" (dbo schema)
|
||||
|
||||
Check that the connection URL's `schema` parameter (normally `dbo`) matches the intended schema. Preserve an existing multi-schema configuration and model mappings.
|
||||
|
||||
## References
|
||||
|
||||
- [Prisma 7 SQL Server documentation](https://www.prisma.io/docs/orm/v7/core-concepts/supported-databases/sql-server)
|
||||
Reference in New Issue
Block a user