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

#快速开始

下面只看两个最常见的入口:核心层和 Drizzle。

#1. 核心层

TypeScript
import { makeNode, schema } from 'oor'
import { querySql } from 'oor/sqlite'
import { z } from 'zod'

const query = schema({
  status: z.string(),
  createdAt: z.date()
})

const input = query.parse({
  statusIn: ['active', 'pending'],
  createdAtDay: '2026-04-08',
  page: '1',
  size: '20',
  sort: 'createdAt',
  order: 'desc'
})

const node = makeNode(input)

const sql = querySql(
  input,
  undefined,
  {
    map: {
      status: { column: 'status', type: 'string' },
      createdAt: { column: 'created_at', type: 'date' }
    }
  }
)

这里做了三件事:

  • schema 校验并归一化输入
  • makeNode 生成条件树
  • querySql 生成 SQL 语法对象

#2. Drizzle

TypeScript
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'
import { withTable } from 'oor/drizzle'

const users = sqliteTable('users', {
  id: integer('id').primaryKey({ autoIncrement: true }),
  name: text('name').notNull(),
  status: text('status'),
  isDeleted: integer('is_deleted', { mode: 'boolean' }).notNull().default(false),
  createdAt: integer('created_at', { mode: 'timestamp_ms' }).notNull(),
  updatedAt: integer('updated_at', { mode: 'timestamp_ms' }).notNull()
})

const userApi = withTable(db, users, {
  soft: {
    mode: 'flag',
    field: 'isDeleted',
    del: true,
    keep: false
  },
  page: { size: 20 },
  sort: { field: 'createdAt', by: 'desc' }
})

直接查询或写入:

TypeScript
await userApi.query({ status: 'active' })
await userApi.page({ page: 2 })
await userApi.update({ status: 'locked' }, { id: 1 })

写操作必须带条件。

#下一步

  • Query Flow
  • Suffix 规则
  • Condition
  • Drizzle 插件
  • 1. 核心层
  • 2. Drizzle
  • 下一步