O
OOR
文档
OOR
文档
简介
Getting Started
安装
快速开始
Query Flow
Core Concepts
Suffix
Schema
Condition Tree
Adapters
SQLite
PostgreSQL / MySQL
Elasticsearch
Integrations
Drizzle Integration
ES Client

#PG / MySQL

oor/pg 和 oor/mysql 跟 oor/sqlite 用同一套 API。

TypeScript
import { whereSql, whereNode, querySql, queryNode } from 'oor/pg'
import { whereSql, whereNode, querySql, queryNode } from 'oor/mysql'

主要差别只在方言细节。

方言列名占位符booleandate
pg"$1true / false保持原值
mysql`?0 / 1保持 Date

#PostgreSQL

TypeScript
import { querySql } from 'oor/pg'

const query = querySql(
  {
    idIn: [1, 2, 3],
    page: 2,
    size: 5,
    sort: 'createdAt',
    order: 'desc'
  },
  undefined,
  {
    map: {
      id: { column: 'user.id', type: 'number' },
      createdAt: { column: 'user.created_at', type: 'date' }
    }
  }
)

#MySQL

TypeScript
import { whereSql } from 'oor/mysql'

const where = whereSql(
  {
    active: true,
    titleStartWith: 'he',
    createdAtDay: '2024-01-15'
  },
  undefined,
  {
    map: {
      active: { column: 'post.is_active', type: 'boolean' },
      title: { column: 'post.title', type: 'string' },
      createdAt: { column: 'post.created_at', type: 'date' }
    }
  }
)

#严格模式

TypeScript
whereSql(
  { missing: 1 },
  undefined,
  {
    strict: true,
    map: {
      id: { column: 'id', type: 'number' }
    }
  }
)

字段不存在或字段类型不支持该操作时,会直接报错。

#下一步

  • Elasticsearch
  • Drizzle 插件
  • PostgreSQL
  • MySQL
  • 严格模式
  • 下一步