RDB.js:適用于 Node.js 和 Typescript 的終極對(duì)象關(guān)系映射器
作者:zhangzhang
RDB.js 是適用于 Node.js 和 Typescript 的終極對(duì)象關(guān)系映射器,可與 Postgres、MS SQL、MySQL、Sybase SAP 和 SQLite 等流行數(shù)據(jù)庫(kù)無(wú)縫集成。
RDB.js 是適用于 Node.js 和 Typescript 的終極對(duì)象關(guān)系映射器,可與 Postgres、MS SQL、MySQL、Sybase SAP 和 SQLite 等流行數(shù)據(jù)庫(kù)無(wú)縫集成。無(wú)論您是使用 TypeScript 還是 JavaScript(包括 CommonJS 和 ECMAScript)構(gòu)建應(yīng)用程序,RDB 都能滿足您的需求。
RDB.js:https://rdbjs.org/
關(guān)鍵特性
- 豐富的查詢模式:RDB 提供了強(qiáng)大而直觀的查詢模型,可輕松檢索、過(guò)濾和操作數(shù)據(jù)庫(kù)中的數(shù)據(jù)。
- 簡(jiǎn)明 API:RDB 擁有簡(jiǎn)明且便于開發(fā)人員使用的 API,可讓您使用簡(jiǎn)單而富有表現(xiàn)力的語(yǔ)法與數(shù)據(jù)庫(kù)進(jìn)行交互。
- 無(wú)需代碼生成:享受完整的智能感知,即使在表映射中,也不需要繁瑣的代碼生成。
- 支持 TypeScript 和 JavaScript:RDB 完全支持 TypeScript 和 JavaScript,讓您可以充分利用靜態(tài)類型和現(xiàn)代 ECMAScript 功能的優(yōu)勢(shì)。
- 可在瀏覽器中使用:通過(guò)使用 Express.js 插件,您可以在瀏覽器中安全地使用 RDB,該插件用于保護(hù)敏感的數(shù)據(jù)庫(kù)憑據(jù),避免在客戶端級(jí)別暴露。這個(gè)方法反映了傳統(tǒng)的 REST API,并使用了高級(jí) TypeScript 工具來(lái)增強(qiáng)功能。
安裝與使用
$ npm install rdb
示例
這里我們選擇 SQLite。
npm install sqlite3
map.js 地圖.js
import rdb from "rdb";
const map = rdb
.map((x) => ({
customer: x.table("customer").map(({ column }) => ({
id: column("id")
.numeric()
.primary()
.notNullExceptInsert(),
name: column("name").string(),
balance: column("balance").numeric(),
isActive: column("isActive").boolean(),
})),
order: x.table("_order").map(({ column }) => ({
id: column("id")
.numeric()
.primary()
.notNullExceptInsert(),
orderDate: column("orderDate").date().notNull(),
customerId: column("customerId")
.numeric()
.notNullExceptInsert(),
})),
orderLine: x.table("orderLine").map(({ column }) => ({
id: column("id").numeric().primary(),
orderId: column("orderId").numeric(),
product: column("product").string(),
})),
deliveryAddress: x
.table("deliveryAddress")
.map(({ column }) => ({
id: column("id").numeric().primary(),
orderId: column("orderId").numeric(),
name: column("name").string(),
street: column("street").string(),
postalCode: column("postalCode").string(),
postalPlace: column("postalPlace").string(),
countryCode: column("countryCode").string(),
})),
}))
.map((x) => ({
order: x.order.map((v) => ({
customer: v.references(x.customer).by("customerId"),
lines: v.hasMany(x.orderLine).by("orderId"),
deliveryAddress: hasOne(x.deliveryAddress).by(
"orderId"
),
})),
}));
export default map;
update.js 更新.js
import map from "./map";
const db = map.sqlite("demo.db");
updateRow();
async function updateRow() {
const order = await db.order.getById(2, {
lines: true,
});
order.lines.push({
product: "broomstick",
});
await order.saveChanges();
}
filter.js 過(guò)濾器.js
import map from "./map";
const db = map.sqlite("demo.db");
getRows();
async function getRows() {
const filter = db.order.lines
.any((line) => line.product.contains("broomstick"))
.and(db.order.customer.name.startsWith("Harry"));
const orders = await db.order.getMany(filter, {
lines: true,
deliveryAddress: true,
customer: true,
});
console.dir(orders, { depth: Infinity });
}
責(zé)任編輯:華軒
來(lái)源:
獨(dú)立開發(fā)者張張