2.13.1 • Published 2 months ago

leoric v2.13.1

Weekly downloads
18
License
BSD-3-Clause
Repository
github
Last release
2 months ago

Leoric

Package Quality NPM Downloads NPM Version Build Status codecov

Leoric is an object-relational mapping library 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

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.

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

mysql nuances

macOS binds localhost to ipv6 ::1, yet both mysql and mysql2 connect database with localhost by default, which means both will try connecting to mysql with ::1. However, the mysql distribution installed with HomeBrew sets bind_address = 127.0.0.1, hence causes following error:

Error: connect ECONNREFUSED ::1:3306
  at __node_internal_captureLargerStackTrace (node:internal/errors:490:5)
  at __node_internal_exceptionWithHostPort (node:internal/errors:668:12)
  at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)

Please change the configuration as below:

diff --git a/usr/local/etc/my.cnf b/usr/local/etc/my.cnf
index 7218354..d31859c 100644
--- a/usr/local/etc/my.cnf
+++ b/usr/local/etc/my.cnf
@@ -1,5 +1,5 @@
 # Default Homebrew MySQL server config
 [mysqld]
 # Only allow connections from localhost
-bind-address = 127.0.0.1
+bind-address = 127.0.0.1,::1
 mysqlx-bind-address = 127.0.0.1

and restart the mysql service:

brew services mysql restart
2.13.1

2 months ago

2.13.0

3 months ago

2.12.3

7 months ago

2.12.2

12 months ago

2.12.1

12 months ago

2.12.0

1 year ago

2.11.4

1 year ago

2.11.5

1 year ago

2.11.3

1 year ago

2.11.5-beta.0

1 year ago

2.11.2

1 year ago

2.11.0

2 years ago

2.11.1

1 year ago

2.10.2

2 years ago

2.10.3

2 years ago

2.11.0-beta.0

2 years ago

2.10.1

2 years ago

2.10.0

2 years ago

2.10.0-beta.0

2 years ago

2.10.0-beta.1

2 years ago

2.9.2

2 years ago

2.9.1

2 years ago

2.8.7

2 years ago

2.8.6

2 years ago

2.8.8

2 years ago

2.9.0

2 years ago

2.8.1

2 years ago

2.8.0

2 years ago

2.7.0

2 years ago

2.7.2

2 years ago

2.7.1

2 years ago

2.8.3

2 years ago

2.8.2

2 years ago

2.8.5

2 years ago

2.8.4

2 years ago

2.7.3

2 years ago

2.6.1

2 years ago

2.6.0

2 years ago

2.6.3

2 years ago

2.6.2

2 years ago

2.4.1

3 years ago

2.4.0

3 years ago

2.5.0

3 years ago

2.3.0

3 years ago

2.3.2

3 years ago

2.3.1

3 years ago

2.2.3

3 years ago

2.2.2

3 years ago

2.2.1

3 years ago

2.0.3

3 years ago

2.2.0

3 years ago

2.0.2

3 years ago

2.0.4

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.15.1

3 years ago

2.1.1

3 years ago

2.1.0

3 years ago

1.14.4

3 years ago

1.14.3

3 years ago

1.15.0

3 years ago

1.14.1

3 years ago

1.14.0

3 years ago

1.14.2

3 years ago

1.13.5

3 years ago

1.13.4

3 years ago

1.13.2

3 years ago

1.13.1

3 years ago

1.13.0

3 years ago

1.13.3

3 years ago

1.12.0

3 years ago

1.11.1

3 years ago

1.11.0

3 years ago

1.10.0

3 years ago

1.9.0

3 years ago

1.8.0

3 years ago

1.7.1

3 years ago

1.7.0

3 years ago

1.6.7

3 years ago

1.6.6

3 years ago

1.6.4

3 years ago

1.6.5

3 years ago

1.6.3

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.0

3 years ago

1.5.2

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.4.0-alpha.3

3 years ago

1.4.0-alpha.4

3 years ago

1.4.0-alpha.2

3 years ago

1.4.0-alpha.1

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

0.4.7

4 years ago

0.4.6

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.5.0-alpha.17

5 years ago

0.5.0-alpha.16

5 years ago

0.5.0-alpha.15

5 years ago

0.5.0-alpha.14

5 years ago

0.5.0-alpha.13

5 years ago

0.5.0-alpha.11

5 years ago

0.5.0-alpha.12

5 years ago

0.5.0-alpha.10

5 years ago

0.5.0-alpha.9

5 years ago

0.5.0-alpha.8

5 years ago

0.5.0-alpha.7

5 years ago

0.5.0-alpha.6

5 years ago

0.5.0-alpha.3

5 years ago

0.5.0-alpha.5

5 years ago

0.5.0-alpha.4

5 years ago

0.5.0-alpha.2

5 years ago

0.5.0-alpha.1

5 years ago

0.4.5

5 years ago

0.4.4

5 years ago

0.4.3

6 years ago

0.4.2

6 years ago

0.4.1

6 years ago

0.4.0

6 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago