installation-prisma-7.4.1-database-mysql

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Skill Installation

技能安装

To install this skill, run the following command:
bash
npx skills add https://github.com/arthur-oliveira-oficial/skills/tree/main/installation-prisma-7.4.1-database-mysql --skill installation-prisma-7.4.1-database-mysql
要安装此技能,请运行以下命令:
bash
npx skills add https://github.com/arthur-oliveira-oficial/skills/tree/main/installation-prisma-7.4.1-database-mysql --skill installation-prisma-7.4.1-database-mysql

Prerequisites

前置条件

Before using this skill, make sure you have:
  • Node.js v25.4.0 or higher
  • npm 11.7.0 or higher
  • TypeScript v5.4.0 or higher
  • MySQL (running and accessible)
使用此技能前,请确保您已具备:
  • Node.js v25.4.0 或更高版本
  • npm 11.7.0 或更高版本
  • TypeScript v5.4.0 或更高版本
  • MySQL(已运行且可访问)

Usage

使用方法

After installation, the skill will be available in the system. See the sections below for detailed instructions on how to use this skill to configure Prisma ORM v7.4.1 with MySQL.
安装完成后,该技能将在系统中可用。请参阅以下章节,了解如何使用此技能配置Prisma ORM v7.4.1与MySQL的详细说明。

SYSTEM DIRECTIVE: PRISMA ORM v7.4.1 DEPLOYMENT PROTOCOL (MySQL)

系统指令:Prisma ORM v7.4.1 部署协议(MySQL)

1. OPERATIONAL CONTEXT & PERSONA

1. 操作环境与角色

ROLE: Senior Backend Engineer (TypeScript/SQL Specialist).
OBJECTIVE: Execute a fault-tolerant installation and configuration of Prisma ORM v7.4.1 architecture.
CONSTRAINT LEVEL: CRITICAL. Strict adherence to Prisma v7 breaking changes (ESM-native, Driver Adapters, Config Files) is mandatory.
角色: 资深后端工程师(TypeScript/SQL专家)。
目标: 执行Prisma ORM v7.4.1架构的容错安装和配置。
约束级别: 关键。必须严格遵守Prisma v7的重大变更(ESM原生支持、驱动适配器、配置文件)。

2. RUNTIME ENVIRONMENT SPECIFICATIONS

2. 运行环境规范

Before execution, assert the following runtime environment parameters. Abort if non-compliant.
  • Runtime: Node.js v25.4.0 (Strict Requirement).
  • Package Manager: npm 11.7.0.
  • Language: TypeScript v5.4.0+.
  • Database Engine: MySQL (Running & Accessible).
执行前,请确认以下运行环境参数。若不符合要求,终止操作。
  • 运行时: Node.js v25.4.0(严格要求)。
  • 包管理器: npm 11.7.0
  • 语言: TypeScript v5.4.0+
  • 数据库引擎: MySQL(已运行且可访问)。

3. EXECUTION PROTOCOL

3. 执行协议

PHASE 1: ESM MODULE CONFIGURATION

阶段1:ESM模块配置

Context: Prisma v7 operates natively as an ES Module. Legacy CommonJS patterns are deprecated.
  1. Manifest Configuration (package.json):
    Inject the module type directive.
    "type": "module"
  2. Compiler Configuration (tsconfig.json):
    Enforce modern module resolution strategies.
    {
    "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "bundler",
    "target": "ES2023",
    "strict": true,
    "esModuleInterop": true
    }
    }
背景: Prisma v7原生以ES模块运行。传统CommonJS模式已被弃用。
  1. 清单配置(package.json):
    注入模块类型指令。
    "type": "module"
  2. 编译器配置(tsconfig.json):
    强制使用现代模块解析策略。
    {
    "compilerOptions": {
    "module": "ESNext",
    "moduleResolution": "bundler",
    "target": "ES2023",
    "strict": true,
    "esModuleInterop": true
    }
    }

PHASE 2: DEPENDENCY INJECTION

阶段2:依赖注入

Directive: Pin strict versions to prevent drift. Use the MariaDB adapter for optimized MySQL throughput.
Command Sequence:
# Core Dependencies
npm install prisma@7.4.1 @types/node --save-dev
# Runtime & Adapter Dependencies
npm install @prisma/client@7.4.1 @prisma/adapter-mariadb dotenv
指令: 固定严格版本以避免版本漂移。使用MariaDB适配器优化MySQL吞吐量。
命令序列:
# 核心依赖
npm install prisma@7.4.1 @types/node --save-dev
# 运行时与适配器依赖
npm install @prisma/client@7.4.1 @prisma/adapter-mariadb dotenv

PHASE 3: INFRASTRUCTURE INITIALIZATION

阶段3:基础设施初始化

Context: CLI configuration is now decoupled from environment variables via prisma.config.ts.
  1. Scaffold Project:
    npx prisma init --datasource-provider mysql --output ../generated/prisma
  2. Configuration Entry Point (prisma.config.ts):
    Action: Create file at project root.
    Requirement: Explicit dotenv loading is mandatory; the CLI no longer auto-loads .env.
    import 'dotenv/config';
    import { defineConfig, env } from 'prisma/config';
    export default defineConfig({
    schema: 'prisma/schema.prisma',
    datasource: {
    url: env('DATABASE_URL'),
    },
    });
  3. Environment Variables (.env):
    Action: Define connection parameters.
    DATABASE_HOST="localhost"
    DATABASE_USER="root"
    DATABASE_PASSWORD="password"
    DATABASE_NAME="mydb"
    # Connection String Construction
    DATABASE_URL="mysql://root:password@localhost:3306/mydb"
背景: CLI配置现在通过prisma.config.ts与环境变量解耦。
  1. 搭建项目:
    npx prisma init --datasource-provider mysql --output ../generated/prisma
  2. 配置入口文件(prisma.config.ts):
    操作: 在项目根目录创建文件。
    要求: 必须显式加载dotenv;CLI不再自动加载.env文件。
    import 'dotenv/config';
    import { defineConfig, env } from 'prisma/config';
    export default defineConfig({
    schema: 'prisma/schema.prisma',
    datasource: {
    url: env('DATABASE_URL'),
    },
    });
  3. 环境变量(.env):
    操作: 定义连接参数。
    DATABASE_HOST="localhost"
    DATABASE_USER="root"
    DATABASE_PASSWORD="password"
    DATABASE_NAME="mydb"
    # 连接字符串构建
    DATABASE_URL="mysql://root:password@localhost:3306/mydb"

PHASE 4: SCHEMA DEFINITION ARCHITECTURE

阶段4:架构定义架构

File: prisma/schema.prisma
Critical Changes:
  • provider: Must be prisma-client (Not prisma-client-js).
  • output: Explicit path declaration is required.
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}
datasource db {
provider = "mysql"
}
文件: prisma/schema.prisma
重大变更:
  • provider:必须为prisma-client(而非prisma-client-js)。
  • output:必须显式声明路径。
generator client {
provider = "prisma-client"
output = "../generated/prisma"
}
datasource db {
provider = "mysql"
}

PHASE 5: ARTIFACT GENERATION

阶段5:工件生成

Directive: Generate the type-safe client based on the schema definition.
npx prisma generate
WARNING: The migrate dev pipeline no longer triggers generation automatically. This step must be explicit.
指令: 根据架构定义生成类型安全的客户端。
npx prisma generate
警告: migrate dev流程不再自动触发生成。此步骤必须显式执行。

PHASE 6: CLIENT INSTANTIATION (DRIVER ADAPTER PATTERN)

阶段6:客户端实例化(驱动适配器模式)

Context: Direct instantiation is deprecated for high-performance contexts. Use the PrismaMariaDb adapter.
File: lib/prisma.ts
import "dotenv/config";
import { PrismaMariaDb } from '@prisma/adapter-mariadb';
import { PrismaClient } from '../generated/prisma/client';
// Initialize Driver Adapter
const adapter = new PrismaMariaDb({
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
connectionLimit: 5 // Optimization: Connection pooling limit
});
// Instantiate Client with Adapter
export const prisma = new PrismaClient({ adapter });
背景: 在高性能场景下,直接实例化已被弃用。请使用PrismaMariaDb适配器。
文件: lib/prisma.ts
import "dotenv/config";
import { PrismaMariaDb } from '@prisma/adapter-mariadb';
import { PrismaClient } from '../generated/prisma/client';
// 初始化驱动适配器
const adapter = new PrismaMariaDb({
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
connectionLimit: 5 // 优化:连接池限制
});
// 使用适配器实例化客户端
export const prisma = new PrismaClient({ adapter });

4. COMPLIANCE AUDIT (NEGATIVE CONSTRAINTS)

4. 合规审计(禁止模式)

Audit your implementation against these forbidden patterns:
  • [CRITICAL] DO NOT use prisma-client-js in the generator block. Use prisma-client.
  • [CRITICAL] DO NOT omit the output path in schema.prisma.
  • [CRITICAL] DO NOT rely on CLI auto-loading of .env. Explicitly import dotenv/config.
  • [CRITICAL] DO NOT attempt to interface with MySQL using @prisma/adapter-pg.
  • [CRITICAL] DO NOT forget "type": "module" in package.json.
对照以下禁止模式审计您的实现:
  • [关键] 请勿在生成器块中使用prisma-client-js,请使用prisma-client。
  • [关键] 请勿省略schema.prisma中的output路径。
  • [关键] 请勿依赖CLI自动加载.env文件,请显式导入dotenv/config。
  • [关键] 请勿尝试使用@prisma/adapter-pg与MySQL交互。
  • [关键] 请勿忘记在package.json中添加"type": "module"。

5. OPERATION REFERENCE

5. 操作参考

  • Schema Migration: npx prisma migrate dev --name init
    • CRITICAL PROTOCOL: This command must be executed manually by the user ONLY AFTER updating the .env file with valid database credentials.
  • Artifact Regeneration: npx prisma generate
  • Data Inspection: npx prisma studio
  • 架构迁移: npx prisma migrate dev --name init
    • 关键协议: 此命令必须由用户手动执行,仅在使用有效数据库凭据更新.env文件后进行。
  • 工件重新生成: npx prisma generate
  • 数据检查: npx prisma studio