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

#Drizzle Integration

oor/drizzle 是当前最直接的使用方式。

它把 suffix、condition、分页、软删、时间字段收拢成一个单表 API。

#导出

TypeScript
import { withDB, withTable } from 'oor/drizzle'

#withTable

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(),
  age: integer('age'),
  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' },
  timestamps: {
    createdAt: 'createdAt',
    updatedAt: 'updatedAt'
  }
})

#读接口

TypeScript
await userApi.query({ status: 'active' })

await userApi.where({
  link: 'OR',
  items: [
    { field: 'status', op: 'Equal', value: 'active' },
    { field: 'status', op: 'Equal', value: 'pending' }
  ]
})

await userApi.page({ page: 2, size: 10 })

#写接口

TypeScript
await userApi.insert({ name: 'ada' })
await userApi.update({ status: 'locked' }, { id: 1 })
await userApi.updateNode(
  { status: 'vip' },
  {
    link: 'OR',
    items: [
      { field: 'id', op: 'Equal', value: 1 },
      { field: 'id', op: 'Equal', value: 2 }
    ]
  }
)

await userApi.delete({ id: 1 })
await userApi.deleteNode(node)
await userApi.hardDelete({ id: 1 })
  • delete 优先走 soft delete
  • hardDelete 才是真删
  • 写操作必须带条件
  • 空条件会直接抛错

#withDB

TypeScript
import { withDB } from 'oor/drizzle'

const dbApi = withDB(db, {
  page: { size: 15 }
})

const userApi = dbApi.withTable(users, {
  sort: { field: 'createdAt', by: 'desc' }
})

const postApi = dbApi.withTable(posts, {
  page: { size: 30 }
})

分页默认值优先级:

  1. query input 里的 size
  2. table config 的 page.size
  3. db config 的 page.size
  4. 默认值 15

#timestamps

TypeScript
{
  timestamps: {
    createdAt: 'createdAt',
    updatedAt: 'updatedAt'
  }
}
  • insert 会自动补 createdAt / updatedAt
  • update 会自动刷新 updatedAt
  • 手动传值时会保留手动值

#strict 与 fields

TypeScript
const userApi = withTable(db, users, meta, {
  strict: true
})

开启后,未知字段或不支持的操作会直接报错。

TypeScript
const fields = userApi.fields()

fields() 会返回字段名、列名、类型和可空信息。

#下一步

  • 回到 快速开始
  • 回到 简介
  • 导出
  • withTable
  • 读接口
  • 写接口
  • withDB
  • timestamps
  • strict 与 fields
  • 下一步