Releases: onyxframework/sql
Releases · onyxframework/sql
v0.5.0
Meet new Core rewritten from scratch! Featuring new schema declaration syntax, type-safe querying, spec splitting and overall code reduction!
Removed functionality:
- Validations have been removed. Use external shard, e.g. validations.cr. Closes #46
- Repository
#insert
,#update
,#delete
and all query other than#query
methods are removed in favour of type safety, thus closing #47 and closing #61 and also closing #33. UseSchema#insert
,Schema#update
andSchema#delete
instead - Converters concept has been liquidated, types now rely on
#to_db
and#from_rs
methods via monkey patching, which closes #60
New schema declaration syntax (#58, #52)
Given SQL:
CREATE TABLE users(
uuid UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name VARCHAR(100) NOT NULL,
age INT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE posts(
id SERIAL PRIMARY KEY,
author_uuid INT NOT NULL REFERENCES users (uuid),
content TEXT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ
);
Crystal code:
require "pg"
require "core"
class User
include Core::Schema
schema users do
pkey uuid : UUID # UUIDs are supported out of the box
type name : String # Has NOT NULL in the column definition
type age : Union(Int32 | Nil) # Doesn't have NOT NULL in the column definition
type created_at : Time = DB::Default # Has DEFAULT in the column definition
type posts : Array(Post), foreign_key: "author_uuid" # That is an implicit reference
end
end
class Post
include Core::Schema
schema posts do
pkey id : Int32
type author : User, key: "author_id" # That is an explicit reference
type content : String
type created_at : Time = DB::Default
type updated_at : Union(Time | Nil)
end
end
Type-safe Query (#48)
Query
's #insert
, #join
, #order_by
, #returning
, #select
, #set
and #where
methods are now type-safe! It means that they would raise if passed invalid arguments in compilation time, e.g:
User.where(id: 42)
# Class 'User' doesn't have an attribute or reference with name 'id' defined in its schema eligible for 'Core::Query(User)#where' call
User.where(age: "18")
# Invalid type 'String' of argument 'age' for 'Core::Query(User)#where' call. Expected: 'Union(Int32 | Nil)'
user = User.new # Would raise because `name` is not null
user.insert # `Core::Query(User)` instance
# equals to
User.insert(name: "John")
user = repo.query(user.insert)
# or
repo.exec(user.insert)
v0.4.2
v0.4.1
v0.4.0
Basically, in v0.4.0 Core became simpler and more abstract. 65 commits and 6033 line changes 👍
Read the changelog here: v0.3.4...v0.4.0
Thanks to AngularJS commit style conventions, reading the changelog by commits is easy-peasy 🍋