Intro

Leoric is an object-relational mapping library for Node.js, with which you can manipulate database like this:

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

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

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

  // 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
  await Post.include('comments').where('posts.title = ?', 'New Post')
  // => Post { id: 1, title: 'New Post', ...,
  //           comments: [ Comment { id, content }, ... ] }
}

本站点提供中文版本,猛戳左边了解更多。

Table of Contents

  1. Use in Web Frameworks
  2. Syntax Table
  3. Guides
  4. Contributing
  5. Plugin & Component

Use in Web Frameworks

Leoric can be used in many web frameworkds from Node.js community. If you are developing with Egg framework, it is highly recommended using the egg-orm plugin:

/* config/plugin.js */
exports.orm = {
  enable: true,
  package: 'egg-orm',
};

/* config/config.default.js */
exports.orm = {
  client: 'mysql',
  database: 'temp',
  host: 'localhost',
  models: 'app/model',
};

The models defined in app/model will be accessible from ctx.model, such as ctx.model.User:

// app/controller/home.js
const { Controller } = require('egg');
module.exports = class HomeController extends Controller {
  async index() {
    const users = await ctx.model.User.find({
      corpId: ctx.model.Corp.findOne({ name: 'alipay' }),
    });
    ctx.body = users;
  }
};

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: 'Untitled' })
UPDATE posts SET title = 'Untitled' WHERE id = 42;
Post.remove({ id: 42 })
DELETE FROM posts WHERE id = 42;
Post.find({ id: [1, 2, 3] })
SELECT * FROM posts WHERE id IN (1, 2, 3);
Post.select('id, title').where('title like ?', '%Post%')
SELECT id, title FROM posts WHERE title LIKE '%Post%';
Post.where('title like ? || authorId = ?', '%Post%', 42)
SELECT *
  FROM posts
 WHERE title LIKE '%Post%' OR author_id = 42;
Post
  .select('count(id) as count')
  .group('authorId')
  .having('count > 0')
  .order('count', 'desc')
  SELECT count(id) AS count, author_id
    FROM posts
GROUP BY author_id
  HAVING count > 0
ORDER BY count DESC;
Book.average('price').group('genre').having('average > 50')
  SELECT AVG('price') AS average, genre
    FROM books
GROUP BY genre
  HAVING average > 50;
Post.find({
  id: TagMap.select('targetId').where({ tagId: 1 }),
})
SELECT *
  FROM posts
 WHERE id
    IN (SELECT target_id FROM tag_maps WHERE tag_id = 1);
Post.include('author', 'comments')
   SELECT *
     FROM posts AS posts
LEFT JOIN users ON users.id = posts.author_id
LEFT JOIN comments ON comments.post_id = posts.id;
Post.join(Attachment, 'attachments.postId = posts.id')
   SELECT *
     FROM posts AS posts
LEFT JOIN attachments ON attachments.post_id = posts.id;
Post
  .join(TagMap,
    'tagMaps.targetId = posts.id and tagMaps.targetType = 0')
  .join(Tag, 'targetMaps.tagId = tags.id')
   SELECT *
     FROM posts AS posts
LEFT JOIN tag_maps AS tagMaps
       ON tagMaps.target_id = posts.id AND tagMaps.target_type = 0
LEFT JOIN tags
       ON tagMaps.tag_id = tags.id;

Guides

For detailed informations, please check out following guides accordingly:

  1. Basics
  2. Migrations
  3. Validations
  4. Associations
  5. Query Interfaces
  6. Hooks
  7. TypeScript Support
  8. Sequelize Adapter

Contributing

There are many ways in which you can participate in the project, for example:

If you are interested in fixing issues and contributing directly to the code base, please see the document How to Contribute, which covers the following:

Plugin & Component

If developing web applications with Egg framework, it’s highly recommended using the egg-orm plugin. More detailed examples about setting up egg-orm with egg framework in either JavaScript or TypeScript can be found at https://github.com/eggjs/egg-orm/tree/master/examples

If developing web applications (or serverless functions) with Midway, the corresponding component called @midwayjs/leoric is what you need. Please refer to the linked documentation for starter, or the repo directory for more examples.