pon

Maiha's private ORM
0.4.1 released
maiha/pon.cr
3
maiha

pon.cr Build Status

Maiha's private ORM for Crystal.

  • crystal: 0.27.0

Usage

require "pon"
require "pon/adapter/mysql"  # "mysql", "pg", "sqlite"

enum Code
  OK  = 200
  ERR = 500
end

class Job < Pon::Model
  adapter mysql              # "mysql", "pg", "sqlite"
  field   name : String
  field   time : Time
  field   code : Code
end

Pon::Adapter::Mysql.setting.url = "mysql://root@127.0.0.1:3306/test"
Job.migrate! # drop and create the table

Job.count # => 0

job = Job.new(name: "foo", code: Code::OK)
job.name  # => "foo"
job.time? # => nil
job.save  # => true

Job.find(job.id).code.ok? # => true

API : Adapter

  def exec(sql) : Nil
  def lastval : Int64
  def scalar(*args)
  def reset! : Nil

  # CRUD
  def insert(fields, params)
  def all(fields : Array(String), types, rest = nil, **opts)
  def one?(id, fields : Array(String), as types : Tuple)
  def count : Int32
  def delete(key) : Bool
  def delete : Nil
  def truncate : Nil

  # ODBC
  def databases : Array(String)
  def tables : Array(String)

  # Experimental
  def transaction(&block) : Nil # only sqlite and pg

API : Model

class Pon::Model
  # Databases
  def self.adapter : Adapter(A)
  def self.migrator : Migrator
  def self.migrate! : Nil

  # Core
  def self.table_name : String
  def new_record? : Bool
  def to_h : Hash(String, ALL_TYPES)

  # CRUD
  def self.create! : M
  def self.create : M
  def self.count : Int32
  def self.all : Array(M)
  def self.where(condition : String) : Array(M)
  def self.first : M
  def self.first? : M?
  def save : Bool
  def save! : Bool
  def self.delete_all
  def delete

  # Field "foo"
  def foo : T
  def foo? : T?

  # Aggregations
  def self.count_by_xxx : Hash(Type, Int64)

API : Module

Pon.logger=(v : Logger)      # logger
Pon.query_logging=(v : Bool) # writes queries into the logger or not

Roadmap

  • Adapter Core
    • [x] connect lazily
    • [x] exec plain sql
    • [x] exec prepared statement
    • [x] all
    • [x] count
    • [x] scalar
    • [x] quote
    • [ ] escape
    • [x] migrator
  • Adapter Drivers
    • [x] reset connections
    • RDB
      • [x] mysql
      • [x] pg
      • [x] sqlite
    • KVS
      • [ ] redis
  • Core
    • [x] pluralize table names
    • [x] special types (Enum)
    • [ ] custom type
    • [x] multibytes
    • [x] record status
    • [x] inspect class and records
    • [ ] callbacks
    • [ ] validations
    • [x] natural keys
  • CRUD
    • [x] all, count, first
    • [x] create
    • [x] delete, delete_all
    • [x] find
    • [x] save
  • Aggregations
    • [x] count_by_xxx
  • Relations
    • [ ] belongs_to
    • [ ] has_many
    • [ ] has_many through
  • Misc
    • [ ] bulk insert
    • [ ] upsert

Installation

Add this to your application's shard.yml:

dependencies:
  pon:
    github: maiha/pon.cr
    version: 0.4.1

  # one of following adapter
  mysql:
    github: crystal-lang/crystal-mysql
    version: ~> 0.4.0
  sqlite3:
    github: crystal-lang/crystal-sqlite3
    version: ~> 0.9.0
  pg:
    github: will/crystal-pg
    version: ~> 0.14.1

Development

TODO: Write development instructions here

Contributing

  1. Fork it ( https://github.com/maiha/pon.cr/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

Contributors

  • maiha maiha - creator, maintainer

Thanks / Inspiration

pon:
  github: maiha/pon.cr
  version: ~> 0.4.1
License MIT
Crystal none

Authors

Dependencies 4

  • db ~> 0.5.1
    {'github' => 'crystal-lang/crystal-db', 'version' => '~> 0.5.1'}
  • toml-config >= 0.4.0
    {'github' => 'maiha/toml-config.cr', 'version' => '>= 0.4.0'}
  • var >= 1.2.0
    {'github' => 'maiha/var.cr', 'version' => '>= 1.2.0'}
  • wordsmith ~> 0.2
    {'github' => 'luckyframework/wordsmith', 'version' => '~> 0.2'}

Development Dependencies 3

  • mysql ~> 0.5.1
    {'github' => 'crystal-lang/crystal-mysql', 'version' => '~> 0.5.1'}
  • pg ~> 0.15.0
    {'github' => 'will/crystal-pg', 'version' => '~> 0.15.0'}
  • sqlite3 ~> 0.10.0
    {'github' => 'crystal-lang/crystal-sqlite3', 'version' => '~> 0.10.0'}

Dependents 1

Last synced .
search fire star recently