RDB
The ultimate ORM for Node and Typescript
README
The ultimate Object Relational Mapper for Node.js and Typescript, offering seamless integration with a variety of popular databases. Whether you're building applications in TypeScript or JavaScript (including both CommonJS and ECMAScript), Orange ORM has got you covered.
Key Features
Rich Querying Model: Orange provides a powerful and intuitive querying model, making it easy to retrieve, filter, and manipulate data from your databases.
Active Record: With a concise and expressive syntax, Orange enables you to interact with your database using the Active Record Pattern .
No Code Generation Required: Enjoy full IntelliSense, even in table mappings, without the need for cumbersome code generation.
TypeScript and JavaScript Support: Orange fully supports both TypeScript and JavaScript, allowing you to leverage the benefits of static typing and modern ECMAScript features.
Works in the Browser: You can securely use Orange in the browser by utilizing the Express.js plugin, which serves to safeguard sensitive database credentials from exposure at the client level. This method mirrors a traditional REST API, augmented with advanced TypeScript tooling for enhanced functionality.
Supported Databases
✅ Postgres✅ MS SQL✅ MySQL✅ Oracle✅ SAP ASE✅ SQLite
This is the Modern Typescript Documentation. Are you looking for the Classic Documentation ?
Sponsorship ♡
If you value the hard work behind Orange and wish to see it evolve further, consider sponsoring . Your support fuels the journey of refining and expanding this tool for our developer community.
Installation
- ``` shell
- $ npm install orange-orm
- ```
Example
Watch the tutorial video on YouTube
Here we choose SQLite.
- ``` shell
- $ npm install sqlite3
- ```
📄 map.ts
- ``` js
- import orange from 'orange-orm';
- const map = orange.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(),
- amount: column('amount').numeric(),
- })),
- package: x.table('package').map(({ column }) => ({
- id: column('packageId').numeric().primary().notNullExceptInsert(),
- lineId: column('lineId').numeric().notNullExceptInsert(),
- sscc: column('sscc').string() //the barcode
- })),
- 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 => ({
- orderLine: x.orderLine.map(({ hasMany }) => ({
- packages: hasMany(x.package).by('lineId')
- }))
- })).map(x => ({
- order: x.order.map(v => ({
- customer: v.references(x.customer).by('customerId'),
- lines: v.hasMany(x.orderLine).by('orderId'),
- deliveryAddress: v.hasOne(x.deliveryAddress).by('orderId'),
- }))
- }));
- export default map;
- ```
📄 update.ts
- ``` 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',
- amount: 300
- });
- await order.saveChanges();
- }
- ```
📄 filter.ts
- ``` js
- import map from './map';
- const db = map.sqlite('demo.db');
- getRows();
- async function getRows() {
- const orders = await db.order.getAll({
- where: x => x.lines.any(line => line.product.contains('broomstick'))
- .and(db.order.customer.name.startsWith('Harry')),
- lines: {
- packages: true
- },
- deliveryAddress: true,
- customer: true
- });
- }
- ```