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

#SQLite

oor/sqlite 只生成 SQLite 风格 SQL 语法,不执行数据库操作。

#导出

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

#whereSql

TypeScript
import { whereSql } from 'oor/sqlite'

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

输出:

TypeScript
{
  sql: '("is_active" = ? AND "title" LIKE ? AND "created_at" BETWEEN ? AND ?)',
  params: [1, 'he%', '2024-01-15T00:00:00.000Z', '2024-01-15T23:59:59.999Z']
}

#querySql

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

会额外补 order、limit、offset。

#map

TypeScript
{
  map: {
    status: { column: 'user.status', type: 'string' },
    age: { column: 'user.age', type: 'number' },
    createdAt: { column: 'user.created_at', type: 'date' }
  }
}

map 决定:

  • 字段映射到哪个列
  • 值如何序列化,例如 boolean、date、json

#SQLite 方言细节

  • 列名使用双引号
  • 参数占位符使用 ?
  • boolean 转为 0 / 1
  • date 默认转为 ISO 字符串

#下一步

  • PG / MySQL
  • Drizzle 插件
  • 导出
  • whereSql
  • querySql
  • map
  • SQLite 方言细节
  • 下一步