项目作者: onyxframework

项目描述 :
A delightful SQL ORM ☺️
高级语言: Crystal
项目地址: git://github.com/onyxframework/sql.git
创建时间: 2017-09-26T00:40:20Z
项目社区:https://github.com/onyxframework/sql

开源协议:MIT License

下载


Onyx::SQL

Built with Crystal
Travis CI build
Docs
API docs
Latest release

A deligtful SQL ORM.

About 👋

Onyx::SQL is a deligthful database-agnostic SQL ORM for Crystal language. It features a convenient schema definition DSL, type-safe SQL query builder, clean architecture with Repository and more!

Installation 📥

Add these lines to your application’s shard.yml:

  1. dependencies:
  2. onyx:
  3. github: onyxframework/onyx
  4. version: ~> 0.6.0
  5. onyx-sql:
  6. github: onyxframework/sql
  7. version: ~> 0.9.0

This shard follows Semantic Versioning v2.0.0, so check releases and change the version accordingly.

Note that until Crystal is officially released, this shard would be in beta state (0.*.*), with every minor release considered breaking. For example, 0.1.00.2.0 is breaking and 0.1.00.1.1 is not.

You’d also need to add a database dependency conforming to the crystal-db interface. For example, pg:

  1. dependencies:
  2. onyx:
  3. github: onyxframework/onyx
  4. version: ~> 0.6.0
  5. onyx-sql:
  6. github: onyxframework/sql
  7. version: ~> 0.9.0
  8. + pg:
  9. + github: will/crystal-pg
  10. + version: ~> 0.18.0

Usage 💻

For this PostgreSQL table:

  1. CREATE TABLE users (
  2. id SERIAL PRIMARY KEY,
  3. name TEXT NOT NULL
  4. created_at TIMESTAMPTZ NOT NULL DEFAULT now()
  5. );

Define the user schema:

  1. require "onyx/sql"
  2. class User
  3. include Onyx::SQL::Model
  4. schema users do
  5. pkey id : Int32
  6. type name : String, not_null: true
  7. type created_at : Time, not_null: true, default: true
  8. end
  9. end

Insert a new user instance:

  1. user = User.new(name: "John")
  2. user = Onyx::SQL.query(user.insert.returning("*")).first
  3. pp user # => #<User @id=1, @name="John", @created_at=#<Time ...>>

Query the user:

  1. user = Onyx::SQL.query(User.where(id: 1)).first?

With another PostgreSQL table:

  1. CREATE TABLE posts (
  2. id SERIAL PRIMARY KEY,
  3. author_id INT NOT NULL REFERENCES users(id),
  4. content TEXT NOT NULL
  5. created_at TIMESTAMPTZ NOT NULL DEFAULT now()
  6. );

Define the post schema:

  1. class Post
  2. include Onyx::SQL::Model
  3. schema posts do
  4. pkey id : Int32
  5. type author : User, not_null: true, key: "author_id"
  6. type content : String, not_null: true
  7. type created_at : Time, not_null: true, default: true
  8. end
  9. end

Add the posts reference to the user schema:

  1. class User
  2. include Onyx::SQL::Model
  3. schema users do
  4. pkey id : Int32
  5. type name : String, not_null: true
  6. type created_at : Time, not_null: true, default: true
  7. + type authored_posts : Array(Post), foreign_key: "author_id"
  8. end
  9. end

Create a new post:

  1. user = User.new(id: 1)
  2. post = Post.new(author: user, content: "Hello, world!")
  3. Onyx::SQL.exec(post.insert)

Query all the posts by a user with name “John”:

  1. posts = Onyx::SQL.query(Post
  2. .join(author: true) do |x|
  3. x.select(:id, :name)
  4. x.where(name: "John")
  5. end
  6. )
  7. posts.first # => #<Post @id=1, @author=#<User @id=1 @name="John">, @content="Hello, world!">

Documentation 📚

The documentation is available online at docs.onyxframework.org/sql.

Community 🍪

There are multiple places to talk about Onyx:

Support 🕊

This shard is maintained by me, Vlad Faust, a passionate developer with years of programming and product experience. I love creating Open-Source and I want to be able to work full-time on Open-Source projects.

I will do my best to answer your questions in the free communication channels above, but if you want prioritized support, then please consider becoming my patron. Your issues will be labeled with your patronage status, and if you have a sponsor tier, then you and your team be able to communicate with me privately in Twist. There are other perks to consider, so please, don’t hesistate to check my Patreon page:

You could also help me a lot if you leave a star to this GitHub repository and spread the word about Crystal and Onyx! 📣

Contributing

  1. Fork it ( https://github.com/onyxframework/sql/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am ‘feat: some feature’) using Angular style commits
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

Licensing

This software is licensed under MIT License.

Open Source Initiative