topaz

A simple and useful db wrapper activerecord db database model topaz db-wrapper orm orm-library
0.0.4 released
topaz-crystal/topaz
59 4 2
Topaz

Topaz

Topaz is a simple and useful db wrapper for crystal lang. Topaz is inspired by ActiveRecord design pattern, but not fully implemented. See sample code for detail.
Depends on crystal-lang/crystal-mysql and crystal-lang/crystal-sqlite3

Installation

Add this to your application's shard.yml:

dependencies:
  topaz:
    github: tbrand/topaz

Usage

1. Setup DB

Topaz::Db.setup("mysql://root@localhost/topaz") # For MySQL
Topaz::Db.setup("sqlite3://./db/data.db") # For SQLite3

2. Define models

class SampleModel < Topaz::Model
  columns(
    {name: name, type: String}
  )
end

# You can drop or create a table
SampleModel.create_table
SampleModel.drop_table

3. Create, find, update and delete models

s = SampleModel.create("Sample Name")

SampleModel.find(1).name
# => "Sample Name"
SampleModel.where("name = 'Sample Name'").size
# => 1

See sample code for detail.

4. Define associations between models

require "topaz"

class SampleParent < Topaz::Model
  columns # Empty columns
  has_many( {model: SampleChild, as: childs, key: parent_id} )
end

class SampleChild < Topaz::Model
  columns( # Define foreign key
    {name: parent_id, type: Int32}
  )
  belongs_to( {model: SampleParent, as: parent, key: parent_id} )
end

p = SampleParent.create

child1 = SampleChild.create(p.id)
child2 = SampleChild.create(p.id)
child3 = SampleChild.create(p.id)

p.childs.size
# => 3

child1.parent.id
# => 1

See sample code for detail.

Supported data types.

[MySQL]
String, Int32, Float64, Float32
[SQLite3]
String, Int64, Float64

TODO:

  • Support DATE
  • Support migration.

Contributing

  1. Fork it ( https://github.com/tbrand/topaz )
  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

Contributors

  • tbrand Taichiro Suzuki - creator, maintainer
topaz:
  github: topaz-crystal/topaz
  version: ~> 0.0.4
License MIT
Crystal none

Authors

Dependencies 3

  • mysql
    {'github' => 'crystal-lang/crystal-mysql'}
  • singleton
    {'github' => 'tbrand/crystal-singleton'}
  • sqlite3
    {'github' => 'crystal-lang/crystal-sqlite3'}

Development Dependencies 0

Dependents 0

Last synced .
search fire star recently