{"id":13527246,"url":"https://github.com/cyjake/leoric","last_synced_at":"2025-05-15T14:08:22.723Z","repository":{"id":37663963,"uuid":"113414268","full_name":"cyjake/leoric","owner":"cyjake","description":"👑 JavaScript ORM for MySQL, PostgreSQL, and SQLite.","archived":false,"fork":false,"pushed_at":"2025-02-10T10:28:29.000Z","size":3742,"stargazers_count":223,"open_issues_count":16,"forks_count":25,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-05-12T07:57:38.631Z","etag":null,"topics":["mysql","nodejs","nodejs-modules","orm","postgresql","querybuilder"],"latest_commit_sha":null,"homepage":"https://leoric.js.org","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cyjake.png","metadata":{"files":{"readme":"Readme.md","changelog":"History.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-12-07T06:53:44.000Z","updated_at":"2025-02-10T10:28:31.000Z","dependencies_parsed_at":"2023-11-06T09:28:39.446Z","dependency_job_id":"b9a452ae-e983-4701-adbf-75c11870899b","html_url":"https://github.com/cyjake/leoric","commit_stats":{"total_commits":515,"total_committers":22,"mean_commits":23.40909090909091,"dds":"0.38834951456310685","last_synced_commit":"2e31e11cab53db6a843330d4f6516acb3e40da5d"},"previous_names":["dotnil/jorma"],"tags_count":122,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyjake%2Fleoric","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyjake%2Fleoric/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyjake%2Fleoric/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cyjake%2Fleoric/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cyjake","download_url":"https://codeload.github.com/cyjake/leoric/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254355335,"owners_count":22057354,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mysql","nodejs","nodejs-modules","orm","postgresql","querybuilder"],"created_at":"2024-08-01T06:01:44.010Z","updated_at":"2025-05-15T14:08:17.703Z","avatar_url":"https://github.com/cyjake.png","language":"JavaScript","readme":"# Leoric\n\n[![Package Quality](https://packagequality.com/shield/leoric.svg)](https://packagequality.com/#?package=leoric)\n[![NPM Downloads](https://img.shields.io/npm/dm/leoric.svg?style=flat)](https://www.npmjs.com/package/leoric)\n[![NPM Version](http://img.shields.io/npm/v/leoric.svg?style=flat)](https://www.npmjs.com/package/leoric)\n[![Build Status](https://github.com/cyjake/leoric/actions/workflows/nodejs.yml/badge.svg)](https://github.com/cyjake/leoric/actions/workflows/nodejs.yml)\n[![codecov](https://codecov.io/gh/cyjake/leoric/branch/master/graph/badge.svg?token=OZZWTZTDS1)](https://codecov.io/gh/cyjake/leoric)\n\nLeoric is an object-relational mapping library for Node.js, which is heavily influenced by Active Record of Ruby on Rails. See the [documentation](https://leoric.js.org) for detail.\n\n## Usage\n\nAssume 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.\n\n```js\nconst { Bone, connect } = require('leoric')\n\n// define model\nclass Post extends Bone {\n  static initialize() {\n    this.belongsTo('author', { Model: 'User' })\n    this.hasMany('comments')\n  }\n}\n\nasync function main() {\n  // connect models to database\n  await connect({ host: 'example.com', models: [ Post ], /* among other options */ })\n\n  // CRUD\n  await Post.create({ title: 'New Post' })\n  const post = await Post.findOne({ title: 'New Post' })\n  post.title = 'Untitled'\n  await post.save()\n\n  // or UPDATE directly\n  await Post.update({ title: 'Untitled' }, { title: 'New Post' })\n\n  // find with associations\n  const post = await Post.findOne({ title: 'New Post' }).with('comments')\n  console.log(post.comments) // =\u003e [ Comment { id, content }, ... ]\n}\n```\n\nIf 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.\n\n```js\nconst { BIGINT, STRING } = Bone.DataTypes;\nclass Post extends Bone {\n  static attributes = {\n    id: { type: BIGINT, primaryKey: true },\n    email: { type: STRING, allowNull: false },\n    nickname: { type: STRING, allowNull: false },\n  }\n}\n\nconst realm = new Realm({ models: [ Post ] });\nawait realm.sync();\n```\n\n## Syntax Table\n\n| JavaScript                              | SQL                                                |\n|-----------------------------------------|----------------------------------------------------|\n| `Post.create({ title: 'New Post' })`    | `INSERT INTO posts (title) VALUES ('New Post')`    |\n| `Post.all`                              | `SELECT * FROM posts`                              |\n| `Post.find({ title: 'New Post' })`      | `SELECT * FROM posts WHERE title = 'New Post'`     |\n| `Post.find(42)`                         | `SELECT * FROM posts WHERE id = 42`                |\n| `Post.order('title')`                   | `SELECT * FROM posts ORDER BY title`               |\n| `Post.order('title', 'desc')`           | `SELECT * FROM posts ORDER BY title DESC`          |\n| `Post.limit(20)`                        | `SELECT * FROM posts LIMIT 0, 20`                  |\n| `Post.update({ id: 42 }, { title: 'Skeleton King' })` | `UPDATE posts SET title = 'Skeleton King' WHERE id = 42` |\n| `Post.remove({ id: 42 })`               | `DELETE FROM posts WHERE id = 42`                  |\n\nA more detailed syntax table may be found at the [documentation](https://leoric.js.org/#syntax-table) site.\n\n## TypeScript charged\n\n```ts\nimport { Bone, BelongsTo, Column, DataTypes: { TEXT } } from 'leoric';\nimport User from './user';\n\nexport default class Post extends Bone {\n  @Column({ autoIncrement: true })\n  id: bigint;\n\n  @Column(TEXT)\n  content: string;\n\n  @Column()\n  description: string;\n\n  @Column()\n  userId: bigint;\n\n  @BelongsTo()\n  user: User;\n}\n```\n\nMore about TypeScript integration examples can be found at [the TypeScript support documentation](https://leoric.js.org/types)\n\n## Contributing\n\nThere are many ways in which you can participate in the project, for example:\n\n- [Submit bugs and feature requests](https://github.com/cyjake/leoric/issues), and help us verify as they are checked in\n- [Review source code changes](https://github.com/cyjake/leoric/pulls)\n- Review the [documentation](https://leoric.js.org) and make pull requests for anything from typo to new content\n\nIf you are interested in fixing issues and contributing directly to the code base, please see the document [How to Contribute](https://leoric.js.org/contributing/guides), which covers the following:\n\n- The development workflow, including debugging and running tests\n- Coding guidelines\n- Submitting pull requests\n- Contributing to translations\n\n## egg-orm\n\nIf developing web applications with [egg framework](https://eggjs.org/), it's highly recommended using the [egg-orm](https://github.com/eggjs/egg-orm) plugin. More detailed examples about setting up egg-orm with egg framework in either JavaScript or TypeScript can be found at \u003chttps://github.com/eggjs/egg-orm/tree/master/examples\u003e\n\n## mysql nuances\n\nmacOS 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:\n\n```js\nError: connect ECONNREFUSED ::1:3306\n  at __node_internal_captureLargerStackTrace (node:internal/errors:490:5)\n  at __node_internal_exceptionWithHostPort (node:internal/errors:668:12)\n  at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)\n```\n\nPlease change the configuration as below:\n\n```diff\ndiff --git a/usr/local/etc/my.cnf b/usr/local/etc/my.cnf\nindex 7218354..d31859c 100644\n--- a/usr/local/etc/my.cnf\n+++ b/usr/local/etc/my.cnf\n@@ -1,5 +1,5 @@\n # Default Homebrew MySQL server config\n [mysqld]\n # Only allow connections from localhost\n-bind-address = 127.0.0.1\n+bind-address = 127.0.0.1,::1\n mysqlx-bind-address = 127.0.0.1\n ```\n\n and restart the mysql service:\n\n ```bash\n brew services mysql restart\n ```\n","funding_links":[],"categories":["Repository"],"sub_categories":["Database"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcyjake%2Fleoric","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcyjake%2Fleoric","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcyjake%2Fleoric/lists"}