RDB.js 是適用于 Node.js 和 Typescript 的終極對象關系映射器,可與 Postgres、MS SQL、MySQL、Sybase SAP 和 SQLite 等流行數據庫無縫集成。無論您是使用 TypeScript 還是 JavaScript(包括 CommonJS 和 ECMAScript)構建應用程序,RDB 都能滿足您的需求。
RDB.js:https://rdbjs.org/
$ 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 過濾器.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 });}
本文鏈接:http://www.tebozhan.com/showinfo-26-15185-0.htmlRDB.js:適用于 Node.js 和 Typescript 的終極對象關系映射器
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com
上一篇: 掌握Go編程中的錯誤處理和日志記錄
下一篇: Golang中的錯誤處理:全面指南及示例