4.0.3 • Published 4 months ago

jeloquent v4.0.3

Weekly downloads
178
License
MIT
Repository
github
Last release
4 months ago

jeloquent

npm jeloquent npm jeloquent npm jeloquent npm jeloquent Node.js CI Scrutinizer Code Quality

Is an orm memory store for javascript.

Main goals are:

  • to quickly select models and/or his relations using primaryKeys and foreignKeys
  • automated indexing of relations
  • use the relation names of laravel eloquent
  • a simple setup of the relations
  • return array collections of relations
  • add easy accessors for relations on model
  • keep the package lightweight and for modern browsers

Speed test with more than 600 000 entities

5000 times a selection of an entity and 3 of his relations of all relation types, average speed in this test was 0.07ms :smile: speed test

Quick Setup Guide

Quick Setup Guide

Documentation

Documentation

Install

npm jeloquent

npm install jeloquent

Playground

Edit shy-rgb-681hz

Usage

Class example

import {Model, Field, HasMany, BelongsTo} from 'jeloquent';
import Team from "./Team.js";
import Comment from "./Comment";

export default class User extends Model {
    constructor() {
        const fields = [
            new Field('id', true),
            new Field('name'),
            new Field('team_id'),
            new BelongsTo(Team),
            new HasMany(Comment),
        ];
        super(fields);
    }
}

import {Model, Field, HasMany, HasManyThrough} from 'jeloquent';
import User from "./User";
import Comment from "./Comment";

export default class Team extends Model {

    constructor() {
        const fields = [
            new Field('id', true),
            new Field('name'),
            new HasMany(User),
            new HasManyThrough(Comment, User),
        ];

        super(fields);
    }
}

import {Model, Field, BelongsTo} from 'jeloquent';
import User from "./User";

export default class Comment extends Model {

    constructor() {
        const fields = [
            new Field('id', true),
            new Field('title'),
            new Field('text'),
            new Field('user_id'),
            new BelongsTo(User),
        ];

        super(fields);
    }
}

Store example

import {Database, Store} from 'jeloquent';
import {User, Team, Comment} from './Models';

const models = [
    User,
    Team,
    Comment,
];

const store = new Store();
store.add(new Database('chess', models));
store.use('chess');

Add data to table

User.insert([
    {id: 1, name: 'name', 'team_id': 1},
    {id: 2, name: 'name 2', 'team_id': 1}
]);

Add one model or temporary model to table

const newUser = new User();
newUser.name = 'New User';
newUser.team_id = 1;
newUser.save();

It generates a temporary key that can be used to find the model

User.find('_1');

When you update the primary key, then the primary key index will be replaced.

const newUser = User.find('_1');
newUser.id = 1234;
newUser.save();

//now use 
User.find(1234);

Selecting data from tables

User.find(1);
User.find([1, 5, 9]);
User.all();
User.ids();

Updating table data

User.update(model);
User.delete(1);

Fetching relation

// return Team object or null
User.find(1).team;

//returns array of comments or empty array
User.find(1).comments;

// Has many trough
// same as Team.select(1).users.reduce((array, user) => {array.push(...user.comments)}, []);
Team.find(1).comments;

Relations getters

//BelongsTo

//return true or false
User.find(1).hasTeam 

//HasMany

User.find(1).hasComments

// returns number of comments of user 1
User.find(1).commentsCount

Filter on relation properties

User.all().filter(user => user.commentsCount > 5);
User.all().filter(user => !user.hasComment);

Getting model and direct relations as json object

const jsonDataOfUser = User.find(1).toJson();

Results in

{ 
    "comments": [
        {"id": 1, "title": "Hello", "text": "Bla Bla", "user_id": 1}, 
        {"id": 2, "title": "Hello 2", "text": "Bla Bla Bla", "user_id": 1}
    ],
    "id": 1,
    "name": "test 1",
    "team_id": 1,
    "team": {"id": 1, "name": "name"}
}
4.0.3

4 months ago

4.0.1

4 months ago

4.0.0

4 months ago

4.0.2

4 months ago

3.2.1

2 years ago

3.0.3

2 years ago

3.2.0

2 years ago

3.0.2

2 years ago

3.0.1

2 years ago

3.0.8

2 years ago

3.0.7

2 years ago

3.0.0

2 years ago

3.0.9

2 years ago

3.1.3

2 years ago

3.1.1

2 years ago

3.1.0

2 years ago

2.14.3

3 years ago

2.14.1

3 years ago

2.14.2

3 years ago

2.14.0

3 years ago

2.13.0

3 years ago

2.12.0

3 years ago

2.11.0

3 years ago

2.10.1

3 years ago

2.10.0

3 years ago

2.9.0

3 years ago

2.8.2

3 years ago

2.8.1

3 years ago

2.8.0

3 years ago

2.7.0

3 years ago

2.6.2

3 years ago

2.6.1

3 years ago

2.6.0

3 years ago

2.5.0

3 years ago

2.4.6

3 years ago

2.4.5

3 years ago

2.4.4

3 years ago

2.4.3

4 years ago

2.4.2

4 years ago

2.4.1

4 years ago

2.4.0

4 years ago

2.3.8

4 years ago

2.3.7

4 years ago

2.3.6

4 years ago

2.3.5

4 years ago

2.3.4

4 years ago

2.3.3

4 years ago

2.3.2

4 years ago

2.3.1

4 years ago

2.3.0

4 years ago

2.2.0

4 years ago

2.1.0

4 years ago

2.0.1

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

2.0.0

4 years ago

1.6.0

4 years ago

1.5.0

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago