Giter Club home page Giter Club logo

leoric's Introduction

Leoric

Package Quality NPM Downloads NPM Version Build Status codecov

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 initialize() {
    this.belongsTo('author', { Model: 'User' })
    this.hasMany('comments')
  }
}

async function main() {
  // 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 }, ... ]
}

If table structures were intended to be maintained in the models, Leoric can be used as a table migration tool as well. We can just define attributes in the models, and call realm.sync() whenever we are ready.

const { BIGINT, STRING } = Bone.DataTypes;
class Post extends Bone {
  static attributes = {
    id: { type: BIGINT, primaryKey: true },
    email: { type: STRING, allowNull: false },
    nickname: { type: STRING, allowNull: false },
  }
}

const realm = new Realm({ models: [ Post ] });
await realm.sync();

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.

TypeScript charged

import { Bone, BelongsTo, Column, DataTypes: { TEXT } } from 'leoric';
import User from './user';

export default class Post extends Bone {
  @Column({ autoIncrement: true })
  id: bigint;

  @Column(TEXT)
  content: string;

  @Column()
  description: string;

  @Column()
  userId: bigint;

  @BelongsTo()
  user: User;
}

More about TypeScript integration examples can be found at the TypeScript support documentation

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:

  • The development workflow, including debugging and running tests
  • Coding guidelines
  • Submitting pull requests
  • Contributing to translations

egg-orm

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

leoric's People

Contributors

chenhui5416 avatar corey600 avatar cyjake avatar danielkehoe avatar fengmk2 avatar freshgum-bubbles avatar jimmydaddy avatar lb4027221 avatar luckydrq avatar nightink avatar okbug avatar smartorange avatar snapre avatar xudafeng avatar zimond avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.