onyx-sql

DB-agnostic SQL ORM with beautiful DSL and type-safe Query builder orm sql sql-orm query-builder crystal-db onyxframework
0.8.0-rc.1 released
onyxframework/sql
91 6 24
Onyx Framework

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:

dependencies:
  onyx:
    github: onyxframework/onyx
    version: ~> 0.4.0
  onyx-sql:
    github: onyxframework/sql
    version: ~> 0.8.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.0 β†’ 0.2.0 is breaking and 0.1.0 β†’ 0.1.1 is not.

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

dependencies:
  onyx:
    github: onyxframework/onyx
    version: ~> 0.3.0
  onyx-sql:
    github: onyxframework/sql
    version: ~> 0.7.0
+ pg:
+   github: will/crystal-pg
+   version: ~> 0.15.0

Usage πŸ’»

For this PostgreSQL table:

CREATE TABLE users (
  id          SERIAL      PRIMARY KEY,
  name        TEXT        NOT NULL
  created_at  TIMESTAMPTZ NOT NULL  DEFAULT now()
);

Define the user schema:

require "onyx/sql"

class User
  include Onyx::SQL::Model

  schema users do
    pkey id : Int32
    type name : String, not_null: true
    type created_at : Time, not_null: true, default: true
  end
end

Insert a new user instance:

user = User.new(name: "John")
user = Onyx::SQL.query(user.insert.returning("*")).first

pp user # => #<User @id=1, @name="John", @created_at=#<Time ...>>

Query the user:

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

With another PostgreSQL table:

CREATE TABLE posts (
  id          SERIAL      PRIMARY KEY,
  author_id   INT         NOT NULL  REFERENCES  users(id),
  content     TEXT        NOT NULL
  created_at  TIMESTAMPTZ NOT NULL  DEFAULT now()
);

Define the post schema:

class Post
  include Onyx::SQL::Model

  schema posts do
    pkey id : Int32
    type author : User, not_null: true, key: "author_id"
    type content : String, not_null: true
    type created_at : Time, not_null: true, default: true
  end
end

Add the posts reference to the user schema:

class User
  include Onyx::SQL::Model

  schema users do
    pkey id : Int32
    type name : String, not_null: true
    type created_at : Time, not_null: true, default: true
+   type authored_posts : Array(Post), foreign_key: "author_id"
  end
end

Create a new post:

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

Query all the posts by a user with name "John":

posts = Onyx::SQL.query(Post
  .join(author: true) do |x|
    x.select(:id, :name)
    x.where(name: "John")
  end
)

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

onyx-sql:
  github: onyxframework/sql
  version: ~> 0.8.0-rc.1
License MIT
Crystal 0.28.0

Authors

Dependencies 2

  • db ~> 0.5.0
    {'github' => 'crystal-lang/crystal-db', 'version' => '~> 0.5.0'}
  • time_format ~> 0.1.0
    {'github' => 'vladfaust/time_format.cr', 'version' => '~> 0.1.0'}

Development Dependencies 2

  • pg ~> 0.16.1
    {'github' => 'will/crystal-pg', 'version' => '~> 0.16.1'}
  • sqlite3 ~> 0.11.0
    {'github' => 'crystal-lang/crystal-sqlite3', 'version' => '~> 0.11.0'}

Dependents 3

Last synced .
search fire star recently