Giter VIP home page Giter VIP logo

nodeorm's Introduction

NodeORM cover

NodeORM - A laravel-like database management library for nodejs

NodeORM is a lightweight and flexible Object-Relational Mapping (ORM) library for Node.js applications inspired by Laravel's one. It provides a seamless interface between JavaScript objects and relational databases, making it easier for developers to interact with their databases using familiar JavaScript syntax.

It simplifies and streamlines the process of working with relational databases, making database interactions more intuitive and developer-friendly. It empowers developers to focus on building robust applications without worrying about the complexities of database management. Whether you are working with MySQL, SQLite, or custom database solutions, NodeORM offers a seamless and consistent interface for your database needs.

Installation

It's heavily recommended to install from github to get latest changes.

npm install obaydmerz/nodeorm

However, for most stable builds you could use npm.

npm install @obayd/nodeorm

Keyfeatures

  • Database Agnostic: NodeORM supports multiple database drivers, including MySQL, SQLite, and custom raw function drivers, allowing users to work with their preferred databases.

  • Model Definition: Users can define their database models with properties such as table name, columns, and primary keys. This enables easy mapping of database tables to JavaScript classes.

  • Fluent Query Building: With a fluent method chaining syntax, NodeORM allows users to construct complex SQL queries in a natural and intuitive way, making it easy to create dynamic queries for database operations.

  • ModelItem Class: The ModelItem class represents individual items (rows) in the database table with CRUD methods. Users can interact with specific database records using this class.

  • Collection Class: The Collection class represents a group of ModelItems with grouping and filtering capabilities. It enables users to work with multiple records as a cohesive unit.

  • Error Handling: NodeORM defines custom error classes for specific error scenarios, such as EmptyDataError and UnmatchingStateError. This helps users handle errors more effectively and gracefully.

  • Dynamic Model Creation: Users can dynamically create models during runtime, eliminating the need to predefine them. This flexibility allows for more dynamic and adaptive database interactions.

  • Lazy Loading: NodeORM employs lazy loading optimizations during iteration, improving performance when dealing with large datasets.

  • Dependency-less: A plug and play library.

  • Fully compatible: The library automatically plugs with mysql, mysql2 and-or sqlite3.

  • Multi connections: You can connect to multiple databases at the same time seamlessly.

  • Unblocking structure: This library is based on async-await syntax.

  • And much other stuff...

Driver Usage

NodeORM will choose the best library to connect to MySQL server. Consider this little example.

MySQL Usage

Condition Library Used
Both mysql2 and mysql are present mysql2
Only mysql2 is present mysql2
Only mysql is present mysql
MySQLDBDriver.library = "mysql2" mysql2
MySQLDBDriver.library = "mysql" mysql
MySQLDBDriver.library = "xxx" xxx
Using env and DB_LIBRARY is unset MySQLDBDriver.library mysql2
Using env and DB_LIBRARY is set to mysql2 mysql2
Using env and DB_LIBRARY is set to mysql mysql
Using env and DB_LIBRARY is set to xxx xxx

SQLite Usage

Condition Library Used
sqlite3 is present sqlite3
SQLiteDBDriver.library = "sqlite3" sqlite3
SQLiteDBDriver.library = "xxx" xxx
Using env and DB_LIBRARY is unset SQLiteDBDriver.library sqlite3
Using env and DB_LIBRARY is set to sqlite3 sqlite3
Using env and DB_LIBRARY is set to xxx xxx

Examples

Let's assume that we have the following MySQL structure.

image

// MySQL example
// Inserting a new row/item into a table named `test`.

import { Model, initialize } from "@obayd/nodeorm";

class Test extends Model {}

(async () => {
  await initialize("mysql://root@localhost:3306/mydb", Test);

  const myNewItem = Test.create();

  myNewItem.string_value = "We have a better tomorrow!";
  myNewItem.integer_value = 2024;

  await myNewItem.save();

  console.log(myNewItem.id); // Output: 1
})();

Changes in database:

image

// Reading the row that we just inserted.
// We used here an attribute *getter*

import { Model, initialize } from "../index.js";

class Test extends Model {
  static _ourPhrase() {
    // We used here an attribute *getter*
    // Note we used the following syntax _attributeName
    return this.string_value + " in the year of " + this.integer_value;
  }
}

(async () => {
  await initialize("mysql://root@localhost:3306/mydb", Test);

  const myLastItem = await Test.last();

  // We accessed an additional attribute.
  console.log(myLastItem.ourPhrase); // Output: We have a better tomorrow! in the year of 2024
})();
// Editing the row that we just inserted.

import { Model, initialize } from "@obayd/nodeorm";

class Test extends Model {}

(async () => {
  await initialize("mysql://root@localhost:3306/mydb", Test);

  const myLastItem = await Test.last();

  myLastItem.integer_value = 2025;

  await myLastItem.save();
})();

Changes in database:

image

// Deleting the row that we just inserted.
// Demonstrates how to use find() to *find* items

import { Model, initialize } from "@obayd/nodeorm";

class Test extends Model {}

(async () => {
  await initialize("mysql://root@localhost:3306/mydb", Test);

  //const myLastItem = await Test.last();

  // We used find() syntax instead of last() just to demonstrate how it works.
  //const myLastItem = await Test.where("id", 1);
  //const myLastItem = await Test.where("id", "=", 1);
  const myLastItem = await Test.find(1);

  await myLastItem.delete();
})();

Changes in database:

image

HeidiSQL is used to show database changes, consider supporting it.

Multi-support

NodeORM features accessing and managing other types of databases like sqlite.

// SQLite example
import { Model, initialize } from "@obayd/nodeorm";

class Test extends Model {}

(async () => {
  await initialize("C:/Users/Hello/Documents/mysqlite.db", Test);

  const my = await Test.last();
  console.log(my.string_value);
})();

Easy, isn't it?

Read more

For more information and advanced usage, check out the NodeORM Wiki

You can join our discord server.

nodeorm's People

Contributors

obaydmerz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

nodeorm's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.