📜  express orm - Javascript (1)

📅  最后修改于: 2023-12-03 15:14:57.241000             🧑  作者: Mango

Express ORM - Javascript

Express ORM Logo

Introduction

Express ORM is a simple and intuitive Node.js ORM for SQL databases. It's built on top of Knex.js and provides an easy-to-use interface to interact with your database. Express ORM is designed to be lightweight, yet powerful, and flexible, allowing you to quickly and easily build applications that require database integration.

Features

Some of the key features of Express ORM include:

  • Easy to Use: Express ORM provides a simple and intuitive API for interacting with your database, making it easy to get started with minimal configuration.
  • Flexible Querying: Express ORM allows you to query your database using a simple and flexible syntax, enabling you to build complex queries with ease.
  • Model-View-Controller (MVC) Architecture: Express ORM follows the MVC architecture, allowing you to separate your application logic into modular components and maintain a cleaner codebase.
  • Validation and Sanitization: Express ORM provides built-in support for data validation and sanitization, helping you to keep your database free of invalid or malicious data.
  • Hooks and Events: Express ORM provides a flexible hook and event system, allowing you to easily add custom behavior to your application.
Installation

To install Express ORM, you can use npm:

npm install express-orm

Then include it in your application:

const { ExpressORM } = require('express-orm');
Usage

The first thing you need to do is configure Express ORM to use your database. In the following example, we'll configure Express ORM to use MySQL:

const { ExpressORM } = require('express-orm');

const orm = new ExpressORM({
  client: 'mysql',
  connection: {
    host: 'localhost',
    user: 'your_database_user',
    password: 'your_database_password',
    database: 'your_database_name',
  },
});

Next, we'll define a simple model for our users table:

const { Model } = require('express-orm');

class User extends Model {
  static get tableName() {
    return 'users';
  }
}

// Register the user model with the ORM
orm.registerModel(User);

Now we can use the model to interact with our database:

// Find all users in the database
const users = await User.query();

// Find a specific user by ID
const user = await User.query().findById(1);

// Create a new user in the database
const newUser = await User.query().insert({ name: 'John Doe', email: 'john.doe@example.com' });

// Update an existing user
await User.query().findById(1).patch({ name: 'Jane Doe' });

// Delete a user from the database
await User.query().findById(1).delete();

For more advanced querying, Express ORM supports complex conditions, joins, and eager loading:

// Find all users with an email ending in 'example.com'
const users = await User.query().where('email', 'like', '%example.com');

// Find all users with an email ending in 'example.com' and a name starting with 'J'
const users = await User.query()
  .where('email', 'like', '%example.com')
  .andWhere('name', 'like', 'J%');

// Join the `posts` table and select all users with a post count greater than 5
const users = await User.query()
  .join('posts', 'posts.user_id', 'users.id')
  .select('users.*', orm.raw('count(posts.id) as post_count'))
  .groupBy('users.id')
  .having('post_count', '>', 5);

// Eager load all posts for each user
const users = await User.query().eager('posts');
Conclusion

Express ORM is a powerful and flexible Node.js ORM for SQL databases. It provides an intuitive API for interacting with your database and supports a wide range of querying options. Express ORM is also easy to use and can help streamline your application's architecture with its support for the MVC pattern. If you're looking for a reliable and easy-to-use ORM for Node.js, give Express ORM a try!