# modelar

> An expressive ORM with query builder and supports multiple databases.

Latest version **3.2.4** (published 2019-05-08) · MIT license · 0 weekly downloads

## Install

```sh
npm install modelar
pnpm add modelar
yarn add modelar
bun add modelar
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.2.4 |
| Published | 2019-05-08 |
| First published | 2017-09-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=4.0 |
| Dependencies | 5 |
| Unpacked size | 289.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 55 |
| Author | Ayon Lee |
| Maintainers | hyurl |
| Keywords | model, orm, database, mysql, postgresql, oracledb, mssql, db2, sqlite |

## Links

- npm: https://www.npmjs.com/package/modelar
- Repository: https://github.com/hyurl/modelar
- Homepage: https://github.com/hyurl/modelar#readme
- Issues: https://github.com/hyurl/modelar/issues
- npm.io page: https://npm.io/package/modelar

## Dependencies (5)

- [tslib](https://npm.io/package/tslib.md) ^1.9.3
- [lodash](https://npm.io/package/lodash.md) ^4.17.11
- [bcrypt-nodejs](https://npm.io/package/bcrypt-nodejs.md) ^0.0.3
- [modelar-mysql-adapter](https://npm.io/package/modelar-mysql-adapter.md) ^3.0.7
- [hide-protected-properties](https://npm.io/package/hide-protected-properties.md) ^0.1.1

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 3.2.4 (latest) — 2019-05-08
- 2.2.0 (modelar2) — 2018-03-23
- 3.2.3 — 2019-02-22
- 3.2.2 — 2018-12-07
- 3.2.1 — 2018-11-18
- 3.2.0 — 2018-09-13
- 3.1.6 — 2018-09-12
- 3.1.5 — 2018-09-10
- 3.1.2 — 2018-07-25
- 3.1.1 — 2018-06-27
- 3.1.0 — 2018-06-06
- 3.0.6 — 2018-06-04
- 3.0.5 — 2018-06-04
- 3.0.4 — 2018-06-02
- 3.0.3 — 2018-04-07
- … 18 more at https://npm.io/package/modelar/versions

## README

# Modelar

**An expressive ORM with query builder and supports multiple databases.**

See [API documentation](https://modelarjs.com) on official website. Or on 
[GitHub Pages](https://hyurl.github.io/modelar).

## Prerequisites

- `NodeJS` version higher than 4.0.0.

## Install

To install Modelar in you project, just type the following command in your 
shell or CMD:

```sh
npm install modelar --save
```

## Supported Databases

This module currently supports these databases:

- `MySQL/MariaDB` see [modelar-mysql-adapter](https://github.com/hyurl/modelar-mysql-adapter).
- `PostgreSQL` see [modelar-postgres-adapter](https://github.com/hyurl/modelar-postgres-adapter).
- `SQLite` see [modelar-sqlite-adapter](https://github.com/hyurl/modelar-sqlite-adapter).
- `MicroSoft SQL Server` see [modelar-mssql-adapter](https://github.com/hyurl/modelar-mssql-adapter).
- `OracleDB` see [modelar-oracle-adapter](https://github.com/hyurl/modelar-oracle-adapter).
- `DB2` see [modelar-ibmdb-adapter](https://github.com/hyurl/modelar-ibmdb-adapter).

Not all adapters are installed automatically, only `MySQL/MariaDB` (since 
3.0.4) are internally included, you must manually install other adapters if 
you want to use them.

## What can I do with this module?

* **Write less code.**
    * You can just define a class that extends the Model, and most of the 
        work would be done for you.
    * Promise guarantees that all the procedures can be controlled within one 
        logic.
* **Write expressive and good looking code.**
    * Attributes of a model is actually properties of the instance.
    * All setter and getter supports.
* **Write one piece of code, run everywhere.**
    * Modelar exposes a common API that provides consistency across databases.
    * You can just write a piece of code, and run it with all the databases 
        supported, and don't have to worry the behavior of different 
        databases.
* **Use Query Builder to handle data.**
    * This module provides most of the SQL supports to the Model.
    * Query builder provides an Object-Oriented way to generate SQL statements.

Modelar is still growing, more features might be introduced in future 
versions.

## Example

```javascript
const { DB, Model } = require("modelar");

DB.init({
    type: "mysql", // Could be 'mysql', 'maria' or 'postgres' by default.
    database: "modelar",
    host: "127.0.0.1",
    port: 3306,
    user: "root",
    password: "161301"
});

// Add a global event handler to every queries.
DB.on("query", model=>{
    console.log(model.toString())
});

// Define a new class that extends the Model.
class Article extends Model {
    constructor(data = {}) {
        super(data, {
            table: "articles",
            primary: "id",
            fields: [ "id", "title", "content" ],
            searchable: [ "title", "content" ]
        });
    }
}

(async () => {
    var db = null;
    try {
        db = new DB();

        // Create a new table `articles`:

        var table = new Table("articles");
        table.addColumn("id").primary().autoIncrement();
        table.addColumn("title", "varchar", 255).notNull();
        table.addColumn("content", "varchar", 1024).notNull();

        table = await table.use(db).save();
        console.log(table);
        console.log("");

        // Insert an article into the database with 'Article' model:
        
        var article = new Article;
        article.title = "A new article in Modelar circumstance.";
        article.content = "This is the content of the article.";
        
        article = await article.use(db).save();
        console.log(article);
    } catch (e) {
        console.log(e);
    }

    if (db)
        db.close();
})();
```

Above gives a very simple example that shows the convenience and expressive 
functionality that this package carries, you can go into the real depth of it 
by checking the [API documentation](https://modelarjs.com).

This package is written in TypeScript and compiled to ES5 standard (since 
3.0.4) with some ES2015 features, so it can run in any version of NodeJS that 
higher than 4.0.0. But this feature relies on the support of the adapter 
you're using, so check the specification of the adapter carefully.

---
_Source: https://npm.io/package/modelar · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
