npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@phpixel/node-eloquent

v0.1.13

Published

- more drivers - migrations support - more cli tools

Downloads

32

Readme

Node Eloquent

Todo

  • more drivers
  • migrations support
  • more cli tools

Node Eloquent is a fork of another project by Zach Silveira Done under Construction Jobs . Relation is the original package which takes inspiration from knex and sequelize, but the end goal is to completely mimic Laravel's Eloquent package. In order to achieve the best syntax possible, we are using ES6 Proxies, which is now supported in the latest version of node. Currently, only mysql is supported, but adding a new driver is trivial.

Why this over xyz?

Read this wiki page for why this is better than knex and sequelize. The gist is this: syntax, and lazy loading relationships.

npm install @phpixel/node-eloquent --save

//if using mysql driver this is peer dependency anyway
npm install mysql --save

Setup

You must set the following environment variables in your app. We recommend creating a .env file and using dotenv (this is a peer dependency)

DB_DRIVER=mysql
DB_HOST=localhost
DB_USERNAME=test
DB_PASSWORD=secret
DB_NAME=blah

Create a Model

chat.js

import { Model } from 'node-eloquent'

export default class Chat extends Model {

  /*
  overwrite table name, this function is optional

  static tableName() {
    return 'dashboard_chats'
  }
  */
}

Using the Model

As long as the plural version of the model is available in the database (you can overwrite this), you can query the database.

import Chat from './chat'

async function getChats {
  let chats = await Chat.all()
  console.log(chats)
}

Supported methods

  • .all() returns everything in the table
  • .where({ fieldName: 'value' }) returns any matching results
  • .create({ field: 'value'}) create a new row
  • .select('column', 'column2') constraint rows to select
  • .first() returns first results
  • .limit(5) limits the query
  • find(<primary key>) finds and returns a relation currently only id as primary key is supported but dynamic primary key is coming soon

Query Building


Chat.select('messages', 'id').where({ messages: 'blah' }).get()

Chat.where({ messages: 'blah' }).get()

Chat.select('messages').first()

Chat.where({ messages: 'blah' }).limit(2).get()

Relationships

This is a huge WIP, feel free to contribute :)

Supported:

  • One To One
  • One To Many

Todo:

  • Many To Many
  • Has Many Through
  • Polymorphic Relations
  • Many To Many Polymorphic Relations

One to One Example

import { Model } from 'node-eloquent'


export default class User extends Model {

}

export default class Chat extends Model {
  user() {
    return this.hasOne(User)
  }
}

let chat = await Chat.first()

//any relationship will return a promise with the result
let user = await chat.user

expect(user.name).to.be.equal('Bob')

One to Many Example

import { Model } from 'node-eloquent'


export default class User extends Model {
  chats() {
    return this.hasMany(Chat)
  }
}

export default class Chat extends Model {

}

let user = await User.first()

//has many results return a query builder instance
let chats = await user.chats.first()

Migrations

Will go over this very soon...

CLI

If you install node-eloquent globally (npm install @phpixel/node-eloquent -g) you can access the CLI methods to help create migrations, models, etc.

Migrations

eloquent make:migration User -m -m will create a model as well

This will create a migration file that will allow you to build out tables.

Models

eloquent make:model User

Creates a file in your current directory /models/user.js with a default model