Crecto

This section is based on Crecto's README

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

Installation

Add this to your application's shard.yml:

dependencies:
  crecto:
    github: fridgerator/crecto

Include a database adapter:

Postgres

Include crystal-pg in your project before crecto

in your application:

require "pg"
require "crecto"

Mysql

Include crystal-mysql in your project before crecto

in your application:

Sqlite

Include crystal-sqlite3 in your project before crecto

in your application:

Migrations

Micrate is recommended. It is used and supported by core crystal members.

Usage

First create a Repo. The Repo maps to the datastore and the database adapter and is used to run queries. You can even create multiple repos if you need to access multiple databases.

For those coming from Active Record: Repo provides a level of abstraction between database logic (Repo) and business logic (Model).

Let's create a repo for Postgres:

And another for SQLite:

Shortcut variables

Optionally you can use constants shorcuts using:

Definitions

Define table name, fields, validations, and constraints in your model. By default, Crecto assumes your table has the following columns defined id, created_at, updated_at. These are in addition to whatever columns you decide to add.

Defining a new class using Crecto::Model:

Defining another one:

Creating a new User:

If your schema doesn't require the default fields (id, created_at, updated_at), you can omit them.

For a schema with an ID that is custom (UUID, Random String, etc...)

You can check out the spec/spec_helper.cr for more examples.

Check the changeset to see changes and errors

Use Repo to insert record into table.

Repo returns a new changeset.

Use Repo to update record in table.

Use Repo to delete record from table.

Use Repo to delete all records from table.

Set Associations

Query syntax

If you need to query through a join table, Query also has a join method.

One thing to note about the query syntax is that the table you query on isn't decided until you pass the query to Repo.

All

Or you can just get all the records

Get by primary key

Get by fields

Associations

Preload associations

Nil-check associations

If an association is not loaded, the normal accessor will raise an error.

For has_many preloads, the result will always be an array.

For belongs_to and has_one preloads, the result may still be nil if no record exists. If the association is nullable, always use association?.

Aggregate functions

Can use the following aggregate functions: :avg, :count, :max, :min:, :sum

Multi / Transactions

Create the multi instance

Build the transactions

Insert the multi using a transaction

Check for errors

If there are any errors in any of the transactions, the database will rollback as if none of the transactions happened

Non-nillable attributes

If you wish to access attributes of a model without having to check for nil, in the case that you are using a NOT NULLdatabase constraint you can use the non-nillable attribute accessors. CAUTION: Mis-use of this could lead to Nil reference runtime errors.

JSON type

Array type

Database Logging

By default nothing is logged. To enable pass any type of IO to the logger. For STDOUT use:

Write to a file:

Benchmarks

Last updated