适用于 Postgresql、CockroachDB、Materialize 数据库的简单、轻量级 ORM。
该项目基于 ormv 重构,目前处于开发阶段
-
支持为 JSON、JSONB 类型字段建模,提供 JSON 数据结构与类型的深层校验;
-
支持为 JSONB 类型自动创建独立的自增序列,为嵌套数据查询提供唯一索引;
-
采用数据模型与数据库连接池分离设计,支持集成部署或独立部署两种可选方案。
-
在独立部署模式下,可改善 Serverless 环境下的冷启动速度,并提高数据库连接池的复用率;
-
为多租户、多数据库、分布式场景提供更灵活的模型切换与复用支持;
-
使用简洁、直观、可嵌套的数据模型,通过类型函数声明,确保字段命名安全、无冲突;
-
支持创建虚拟表模型,将已有的单个或多个模型,通过映射、裁切为新的虚拟模型;
-
支持扩展自定义运算符函数、自定义数据类型验证器;
-
支持可扩展的查询任务流,可实现 SQL 请求拦截、转发、改写等定制化需求;
-
支持将数据模型中数据结构、索引、自增序列等信息同步至表实体;
-
保留类似 SQL 的语法特征,使用函数链风格的查询表达式,简洁、直观、易于读写;
npm install typeqimport { Client, Schema, Model, operator } from "typepg";
import pgclient from "@typeq/pg";
const { integer, string, boolean, email, jsonb } = Schema.types;
/** 定义表结构模型 */
const schema = new Schema({
id: integer({ primaryKey: true }),
uid: integer,
keywords: {
state: boolean,
area: string,
createdAt: timestamp,
},
list: [
{
id: integer({ sequence: "list.$.id" }),
state: boolean,
address: [
{
id: integer({ sequence: "list.$.address.$.id" }),
name: string,
createdAt: timestamp({ default: "now()" }),
updatedAt: timestamp({ default: "now()" }),
},
],
test: {
state: boolean,
name: string,
},
createdAt: timestamp({ default: "now()" }),
updatedAt: timestamp({ default: "now()" }),
},
],
area: string({ min: 10, max: 100 }),
state: boolean({ default: true }),
modes: jsonb,
email: email,
createdAt: timestamp({ default: "now()" }),
updatedAt: timestamp({ default: "now()" }),
});
const tasks = new Model("tasks", schema);
/** 连接数据库 */
const client = new Client(
"default",
pgclient({
host: "localhost",
database: "demo",
user: "postgres",
password: "******",
port: 5432,
})
);
const { $as, $in } = operator; // 查询操作符
/** 插入数据 */
const [item] = await tasks(options).insert(data).return("id");
/** 查询查询 */
const result = await tasks
.select("id", "email", "keywords", $as("uid", "tid"))
.where({ id: $in(50, 51) })
.and({ keywords: {} })
.or({ id: 5 })
.order({ uid: "desc" })
.limit(20);
/** 更新数据 */
const result = await tasks.update({ id: 1 }).set(data).return("id");
/** 删除数据 */
const result = await tasks.delete({ id: 1 });配置参数与 pg 模块兼容,更多参数细节请参考 node-postgres 文档
import pgclient from "typepg/pgclient";
const client = pgclient({
host: string, // 主机名
database: string, // 数据库名
username: string, // 用户名
password: string, // 密码
port: number, // 端口
logger: boolean, // 打印 sql 日志,可选
connectionString: string, // 字符串连接参数,可选
});const schema = new Schema({ [field: name]: Type });
const model = new Model(name: string, schema: Schema);- name - 实体表名称
- schema - 模式实例
无参数时会尝试创建新的数据表,当指定的表已存在时请求被拒绝,不做任何操作。
sync(model: Model).createTable(options);删除已有的数据表重新构建表结构。
// 同步 schema 为 public 下的 user 表
sync(model: Model).rebuildTable("public.user");
// 使用重构模式,删除并重建 user 表(未指定 schema 时,默认为 public)
sync(model: Model).rebuildTable("public.user");
// 使用批量增量模式,批量同步 public.admin 下所有的表
sync(model: Model).rebuildTable("public.admin");在已有表上新增字段,该模式只会添加新的列,不改变已有列和数据。
sync(model: Model).addColumn(options);
sync(model: Model).removeColumn(options);函数链提供了一种更加便捷、严谨和安全的 sql 编码方式,主要目的是尽可能避免 SQL 注入。
插入新数据
- condition object - and 过滤条件
查询多条记录
- condition object - and 过滤条件
查询单条记录
- id - 主键 id
查询主键 id
- options - 排序字段
- value - 限定查询结果的起始位置
- value - 限制返回结果数量
更新数据,用合并的方式更新 json、jsonb 类型
更新指定主键的数据
删除多条数据
删除指定主键的数据
逻辑函数链同时适用于 find、update、delete 操作。支持多个 options 参数,每个 options 内的子节点之间为 and 关系,options 与 options 之间为 or 关系
该设计方案的优点是结构简单、逻辑清晰、易解析。缺点是仅支持双层嵌套逻辑关系,但可满足大多数逻辑应用场景。
- condition - and 条件集合
model.where(condition, condition, ...).or(condition, condition, ...).and(condition, condition, ...);增删改查操作均支持 return 操作,用于定义返回列
返回指定字段
不返回指定字段
用于 options.where 属性中,作为数据筛选条件,包含逻辑运算符、比较运算符等。
添加原生 SQL 字符串,内置单引号转义。
等于
不等于
用于 json 类型数据的插入、合并、删除操作