leoric
JavaScript Object-relational mapping alchemy
Last updated 21 days ago by dotnil .
BSD-3-Clause · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install leoric 
SYNC missed versions from official npm registry.

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

JavaScript SQL
Post.create({ title: 'New Post' }) INSERT INTO posts (title) VALUES ('New Post')
Post.all SELECT * 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.

Current Tags

  • 0.4.7                                ...           latest (21 days ago)

42 Versions

  • 0.4.7                                ...           21 days ago
  • 0.4.6                                ...           4 months ago
  • 1.0.3                                ...           5 months ago
  • 1.0.2                                ...           5 months ago
  • 1.0.1                                ...           5 months ago
  • 1.0.0                                ...           5 months ago
  • 0.5.3                                ...           5 months ago
  • 0.5.2                                ...           5 months ago
  • 0.5.1                                ...           5 months ago
  • 0.5.0                                ...           6 months ago
  • 0.5.0-alpha.17                                ...           6 months ago
  • 0.5.0-alpha.16                                ...           6 months ago
  • 0.5.0-alpha.15                                ...           6 months ago
  • 0.5.0-alpha.14                                ...           6 months ago
  • 0.5.0-alpha.13                                ...           6 months ago
  • 0.5.0-alpha.12                                ...           6 months ago
  • 0.5.0-alpha.11                                ...           6 months ago
  • 0.5.0-alpha.10                                ...           6 months ago
  • 0.5.0-alpha.9                                ...           6 months ago
  • 0.5.0-alpha.8                                ...           6 months ago
  • 0.5.0-alpha.7                                ...           6 months ago
  • 0.5.0-alpha.6                                ...           6 months ago
  • 0.5.0-alpha.5                                ...           6 months ago
  • 0.5.0-alpha.4                                ...           6 months ago
  • 0.5.0-alpha.3                                ...           6 months ago
  • 0.5.0-alpha.2                                ...           7 months ago
  • 0.5.0-alpha.1                                ...           7 months ago
  • 0.4.5                                ...           8 months ago
  • 0.4.4                                ...           a year ago
  • 0.4.3                                ...           a year ago
  • 0.4.2                                ...           a year ago
  • 0.4.1                                ...           a year ago
  • 0.4.0                                ...           a year ago
  • 0.1.8                                ...           3 years ago
  • 0.1.7                                ...           3 years ago
  • 0.1.6                                ...           3 years ago
  • 0.1.5                                ...           3 years ago
  • 0.1.4                                ...           3 years ago
  • 0.1.3                                ...           3 years ago
  • 0.1.2                                ...           3 years ago
  • 0.1.1                                ...           3 years ago
  • 0.1.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 2
Last Day 1
Last Week 2
Last Month 19
Dependencies (4)
Dev Dependencies (11)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |