Community程式設計與開發github.com

mindrally/typeorm

使用 TypeORM 進行開發的準則,TypeORM 是一個功能完整的 ORM,支援多種資料庫,適用於 TypeScript 和 JavaScript。

typeorm 是什麼?

此技能提供一套完整的 TypeORM 開發準則,涵蓋實體定義、關聯映射、查詢構建與遷移管理等核心主題。TypeORM 支援 MySQL、PostgreSQL、SQLite、MongoDB 等多種資料庫,讓開發者以一致的方式操作不同後端。此技能適用於 Claude Code、Cursor 等 AI 編碼助手,能協助撰寫類型安全、高效能的資料庫程式碼,並遵循最佳實務如索引優化、連線管理和錯誤處理。透過內建的 Active Record 與 Data Mapper 模式,開發者可根據專案需求選擇最合適的架構風格。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/mindrally/skills/tree/main/skills/typeorm

Installed? Explore more 程式設計與開發 skills: steipete/bluebubbles, steipete/eightctl, steipete/blucli · View all 6 →

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

typeorm 是做什麼的?

使用 TypeORM 進行開發的準則,TypeORM 是一個功能完整的 ORM,支援多種資料庫,適用於 TypeScript 和 JavaScript。

概述

TypeORM 是一種物件關係映射工具,可在 TypeScript 和 JavaScript 應用程式中,簡化資料庫操作與物件模型之間的轉換。本技能提供的準則涵蓋:

  • 定義實體和欄位
  • 建立關聯(一對一、一對多、多對多)
  • 使用查詢建構器與原生 SQL
  • 執行資料遷移
  • 最佳實務與效能優化

支援的資料庫

TypeORM 支援多種關聯式與文件式資料庫,包括但不限於:

  • MySQL / MariaDB
  • PostgreSQL
  • SQLite
  • Microsoft SQL Server
  • Oracle
  • MongoDB(文件式)
  • CockroachDB

常用的 TypeORM 命令

建立實體

npx typeorm entity:create src/entity/User

執行遷移

npx typeorm migration:run

回滾遷移

npx typeorm migration:revert

實體範例

import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column({ unique: true })
  email: string;

  @Column({ type: 'timestamp', default: () => 'CURRENT_TIMESTAMP' })
  createdAt: Date;
}

關聯範例

import { Entity, PrimaryGeneratedColumn, Column, OneToMany } from 'typeorm';
import { Post } from './Post';

@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @OneToMany(() => Post, (post) => post.user)
  posts: Post[];
}

查詢範例

import { getRepository } from 'typeorm';
import { User } from './entity/User';

const userRepository = getRepository(User);
const users = await userRepository.find({ where: { isActive: true }, relations: ['posts'] });

最佳實務

  • 始終為頻繁查詢的欄位建立索引
  • 使用 Data Mapper 模式進行複雜業務邏輯的隔離
  • 避免 N+1 查詢問題,善用 relations 或 join 選項
  • 將資料庫連線設定儲存在環境變數中
  • 定期執行遷移以保持資料庫結構與實體同步

更多資訊

更多詳細資訊,請參閱 TypeORM 官方文件 。

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

mindrally/accessibility-a11y

Implement web accessibility (a11y) best practices following WCAG guidelines to create inclusive, accessible user interfaces.

mindrally/chrome-extension-development

Expert guidelines for Chrome extension development with Manifest V3, covering security, performance, and best practices. Use when building browser extensions, creating popup UIs, implementing content scripts, working with Chrome APIs, managing extension permissions, or publishing to Chrome Web Store.

mindrally/computer-vision-opencv

Expert guidance for computer vision development using OpenCV, PyTorch, and modern deep learning techniques for image and video processing.

mindrally/data-analysis-jupyter

Expert guidance for data analysis, visualization, and Jupyter Notebook development with pandas, matplotlib, seaborn, and numpy.

mindrally/deep-learning-pytorch

Expert guidance for deep learning, transformers, diffusion models, and LLM development with PyTorch, Transformers, Diffusers, and Gradio.

mindrally/expo-react-native-typescript

Expert in Expo React Native TypeScript mobile development with best practices

mindrally/fastapi-python

Expert in FastAPI Python development with best practices for APIs and async operations

mindrally/framer-motion

Expert guidelines for building performant animations with Framer Motion/Motion library in React applications

mindrally/htmx

HTMX development guidelines for building dynamic web applications with minimal JavaScript using HTML attributes.

mindrally/jwt-security

Guidelines for implementing JWT authentication with security best practices for token creation, validation, and storage

mindrally/machine-learning

Machine learning development with JAX, functional programming patterns, and high-performance computing.

mindrally/mysql-best-practices

MySQL development best practices for schema design, query optimization, and database administration

mindrally/nextjs-react-typescript

Expert in TypeScript, Node.js, Next.js App Router, React, Shadcn UI, Radix UI and Tailwind

mindrally/nextjs-typescript-tailwindcss-supabase

Full-stack Next.js 14 development with TypeScript, TailwindCSS, and Supabase for building production-ready web applications.

mindrally/odoo-development

Expert guidance for Odoo ERP development including Python ORM, XML views, and module architecture

mindrally/puppeteer-automation

Expert guidance for browser automation using Puppeteer with best practices for web scraping, testing, screenshot capture, and JavaScript execution in headless Chrome.

mindrally/redis-best-practices

Redis development best practices for caching, data structures, and high-performance key-value operations

mindrally/scrapy-web-scraping

Expert guidance for building web scrapers and crawlers using the Scrapy Python framework with best practices for spider development, data extraction, and pipeline management.

mindrally/scss-best-practices

SCSS/Sassy CSS best practices and coding guidelines for maintainable, scalable stylesheets

mindrally/web-scraping

Expert in web scraping and data extraction with Python tools

相關技能