Skip to content

Releases: onyxframework/sql

v0.5.0

15 Sep 16:28
Compare
Choose a tag to compare
v0.5.0 Pre-release
Pre-release

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. Use Schema#insert, Schema#update and Schema#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

26 Aug 11:34
Compare
Choose a tag to compare

New Features

  • b1fde0b add Converters::EnumArray

v0.4.1

16 Aug 16:50
Compare
Choose a tag to compare

Bug Fixes

  • f1d3d11 Converters::Enum now works with Int16 & Int64 columns

v0.4.0

02 Jan 17:23
Compare
Choose a tag to compare

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 🍋

v0.3.4

30 Nov 18:12
Compare
Choose a tag to compare

New features:

  • a9108af Repository#insert returns inserted primary key
  • 0c0be47 Better Query.where and Query.having code

Bugfixes:

  • 4209656 Query.where and Query.having work by reference keys
  • ff5458e Track references changes

v0.3.3

08 Nov 22:51
Compare
Choose a tag to compare
  • Add reference keys as properties @ 05438c6
    For example reference :author, key: :author_id will add author_id property. Note that if the key type differs from Int32, you'll have to explicitly define it via key_type: Type .

v0.3.2

31 Oct 19:10
Compare
Choose a tag to compare
  • Wrap JOIN aliases with double quotes; fixes join(:user) @ 6b2bf33
  • Fix WHERE and HAVING by references @ 13beb13

v0.3.1

16 Oct 20:07
Compare
Choose a tag to compare
  • Remove version from README & version.cr @ 036a463
  • Introduce Converters::DB::PG::Numeric with non-precise Float64 conversion @ 3024d49

v0.3.0

05 Oct 06:57
Compare
Choose a tag to compare
  • Split DB and JSON converters @ 0ef3726
  • Introduce Converters::JSON::TimeEpoch @ ed91c31

v0.2.3

05 Oct 07:03
Compare
Choose a tag to compare
  • More options for Schema's field helpers @ d0789f8
  • Models can have no primary keys @ e26f3dd