crecto travis-ci

Database wrapper for Crystal, inspired by Ecto

Crecto

Database wrapper for Crystal. Inspired by Ecto for Elixir language.


Build Status Join the chat at https://gitter.im/crecto/Lobby

Installation

Add this to your application’s shard.yml:

dependencies:
  crecto:
    github: fridgerator/crecto

Include a database adapter (currently only postgres has been tested)

Postgres

Include crystal-pg in your project

Make sure you have ENV['PG_URL'] set

TODO

Roadmap (in no particular order)

  • [ ] select in query
  • [ ] MySQL adapter
  • [ ] SQLite adapter
  • [ ] Choose adapter in config
  • [x] Associations
  • [x] Preload
  • [ ] Joins

Usage

require "crecto"

#
# Define table name, fields and validations in your class
#
class User < Crecto::Model

  schema "users" do
    field :age, Int32
    field :name, String
    field :is_admin, Bool
    field :temporary_info, Float64, virtual: true
    has_many :posts
  end

  validate_required [:name, :age]
  validate_format :name, /[*a-zA-Z]/
end

class Post < Crecto::Model
  
  schema "posts" do
    field :user_id, PkValue
    belongs_to :user
  end
end

user = User.new
user.name = "123"
user.age = 123

#
# Check the changeset to see changes and errors
#
changeset = User.changeset(user)
puts changeset.valid? # false
puts changeset.errors # {:field => "name", :message => "is invalid"}
puts changeset.changes # {:name => "123", :age => 123}

user.name = "test"
changeset = User.changeset(user)
changeset.valid? # true

#
# Use Repo to insert into database
#
changeset = Crecto::Repo.insert(user)
puts changeset.errors # []

#
# User Repo to update database
#
user.name = "new name"
changeset = Crecto::Repo.update(user)
puts changeset.instance.name # "new name"

#
# Query syntax
#
query = Crecto::Repo::Query
  .where(name: "new name")
  .where("users.age < ?", [124])
  .order_by("users.name ASC")
  .order_by("users.age DESC")
  .limit(1)

#
# All
#
users = Crecto::Repo.all(User, query)
users.as(Array) unless users.nil?

#
# Get by primary key
#
user = Crecto::Repo.get(User, 1)
user.as(User) unless user.nil?

#
# Get by fields
#
Crecto::Repo.get_by(User, name: "new name", id: 1121)
user.as(User) unless user.nil?

#
# Delete
#
changeset = Crecto::Repo.delete(user)

#
# Associations
#

user = Crecto::Repo.get(User, id).as(User)
posts = Crecto::Repo.all(user, :posts)

#
# Preload associations
#
users = Crecto::Repo.all(User, Crecto::Query.new, preload: [:posts])
users[0].posts # has_many relation is preloaded

posts = Crecto::Repo.all(Post, Crecto::Query.new, preload: [:user])
posts[0].user # belongs_to relation preloaded

Performance

crystal:

  • crystal 0.20.0

elapsed: 2.6528820 seconds

require "crecto"

class User < Crecto::Model

  schema "users" do
    field :name, String
    field :things, Int32
    field :stuff, Int32, virtual: true
    field :nope, Float64
    field :yep, Bool
    field :some_date, Time
  en

  validates :name,
    presence: true,
    inclusion: { in: ["fridge", "mcridge"] },
    format: { pattern: /[*a-zA-Z]/ },
    length: { min: 2, max: 10 }
end

start_time = Time.now
10000.times do
  user = User.new
  user.name = "fridge"
  changeset = User.changeset(user)
  changeset = Crecto::Repo.insert(changeset)
end
end_time = Time.now
puts "elapsed: #{end_time - start_time}"

Ruby / Rails

  • ruby 2.3.1
  • rails 5.0.0

elapsed: 14.624411 seconds

class User < ApplicationRecord
  validates :name,
    presence: true,
    inclusion: { in: ["fridge", "mcridge"] },
    format: { with: /[*a-zA-Z]/ },
    length: { minimum: 2, maximum: 10 }
end

start_time = Time.now
10000.times do
  u = User.new
  u.name = "fridge"
  u.save
end
end_time = Time.now
puts "elapsed: #{end_time-start_time}"

Contributing

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

Development Notes

When developing against crecto, the database must exist in Postgres prior to testing. The environment variable PG_URL must be set to the database that will be used for testing. A couple commands have been set up to ease development:

  • make migrate - This will remigrate the testing schema to the database.
  • make spec - Runs the crystal specs for crecto
  • make all - Runs the migration and subsequently runs specs

Thanks / Inspiration

Related Repositories

crecto

crecto

Database wrapper for Crystal, inspired by Ecto ...


Top Contributors

fridgerator neovintage cjgajard gitter-badger

Releases

-   v0.3.0 zip tar
-   v0.2.0 zip tar
-   0.1.0 zip tar