Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

leoric

Package Overview
Dependencies
Maintainers
1
Versions
141
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

leoric

JavaScript Object-relational mapping alchemy

  • 0.4.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
568
increased by47.92%
Maintainers
1
Weekly downloads
 
Created
Source

Leoric

NPM Downloads NPM Version Build Status Coverage Status

Leoric is an object-relational mapping for Node.js, which is heavily influenced by Active Record of Ruby on Rails. See the documentation for detail.

Usage

Assume the tables of posts, users, and comments were setup already. We may declare the models as classes by extending from the base class Bone of Leoric. After the models are connected to the database, the columns of the tables are mapped as attributes, the associations are setup, feel free to start querying.

const { Bone, connect } = require('leoric')

// define model
class Post extends Bone {
  static describe() {
    this.belongsTo('author', { Model: 'User' })
    this.hasMany('comments')
  }
}

async function() {
  // connect models to database
  await connect({ host: 'example.com', models: [Post], /* among other options */ })

  // CRUD
  await Post.create({ title: 'New Post' })
  const post = await Post.findOne({ title: 'New Post' })
  post.title = 'Untitled'
  await post.save()

  // or UPDATE directly
  await Post.update({ title: 'Untitled' }, { title: 'New Post' })

  // find with associations
  const post = await Post.findOne({ title: 'New Post' }).with('comments')
  console.log(post.comments) // => [ Comment { id, content }, ... ]
}

Syntax Table

JavaScriptSQL
Post.create({ title: 'New Post' })INSERT INTO posts (title) VALUES ('New Post')
Post.allSELECT * FROM posts
Post.find({ title: 'New Post' })SELECT * FROM posts WHERE title = 'New Post'
Post.find(42)SELECT * FROM posts WHERE id = 42
Post.order('title')SELECT * FROM posts ORDER BY title
Post.order('title', 'desc')SELECT * FROM posts ORDER BY title DESC
Post.limit(20)SELECT * FROM posts LIMIT 0, 20
Post.update({ id: 42 }, { title: 'Skeleton King' })UPDATE posts SET title = 'Skeleton King' WHERE id = 42
Post.remove({ id: 42 })DELETE FROM posts WHERE id = 42

A more detailed syntax table may be found at the documentation site.

Migrations

Currently, Leoric doesn't provide a way to do database migrations. There are two popular approaches:

  • A separated migration DSL and database metadata, like Active Record.
  • A detailed enumeration of attributes and types in the models, like Django.

There is a third way, which is the very reason Leoric has yet to implement migrations, which is that the database can be designed through a third-party service. It can be an ER designer, a GUI software for MySQL, or a MySQL-compliant database in the cloud.

But I'm sure we'll get to that.

Keywords

FAQs

Package last updated on 28 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc