掃二維碼與項目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
RDB.js 是適用于 Node.js 和 Typescript 的終極對象關(guān)系映射器,可與 Postgres、MS SQL、MySQL、Sybase SAP 和 SQLite 等流行數(shù)據(jù)庫無縫集成。無論您是使用 TypeScript 還是 JavaScript(包括 CommonJS 和 ECMAScript)構(gòu)建應用程序,RDB 都能滿足您的需求。

成都創(chuàng)新互聯(lián)專注于網(wǎng)站建設|成都網(wǎng)站維護|優(yōu)化|托管以及網(wǎng)絡推廣,積累了大量的網(wǎng)站設計與制作經(jīng)驗,為許多企業(yè)提供了網(wǎng)站定制設計服務,案例作品覆蓋成都除甲醛等行業(yè)。能根據(jù)企業(yè)所處的行業(yè)與銷售的產(chǎn)品,結(jié)合品牌形象的塑造,量身定制品質(zhì)網(wǎng)站。
RDB.js:https://rdbjs.org/
$ npm install rdb
這里我們選擇 SQLite。
npm install sqlite3map.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 過濾器.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 });
} 
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流