Selenite is a persistency-model based library
Add this to your application's shard.yml
:
dependencies:
selenite-db:
github: xdougx/selenite-db
require "selenite-db"
# setup out connection
Selenite::DB::Configuration.configure do |conf|
conf.client_encoding = "utf8"
conf.port = "5432"
conf.database = "crystal"
conf.user = "root"
conf.password = ""
conf.host = "localhost"
conf.env = "development"
end
When you build your model you'll need to define your properties, initializers, attributes types, and the table name.
class Users < Selenite::DB::Persistence
end
def self.table_name
"users"
end
@name : String?
@email : String?
@password : String?
@password_digest : String?
property(id, created_at, updated_at) # is needed to be separeted
set_property(name, email, password, password_digest) # base attributes
set_initializer(true, name, email, password, password_digest) # base initializer, **true** is for timestamp initialization
class Users < Selenite::DB::Persistence
def self.table_name
"users"
end
@name : String?
@email : String?
@password : String?
@password_digest : String?
property(id, created_at, updated_at) # is needed to be separeted
set_property(name, email, password, password_digest) # base attributes
set_initializer(true, id, name, email, password, password_digest)
end
user = Users.new({"name" => "Vitor Hugo", "email" => "[email protected]"})
user.save
puts "User name is: #{user.name}"
puts "User email is: #{user.email}"
def self.exec(query); end # executes a query and return a result from PG
def exec(query); end # alias for an instance
def save; end # persist your model
def update_columns(params); end # update the values direct
def self.exists?(column, value); end # verify if an id exists
def self.exists?(params); end # verify if exists using many params
def self.find(id : Int32 | Int64 | String | Nil); end # find with id
def self.get_result_by(params); end # find using many params
def self.find_by(params); end # find using many params
- has_one
- has_many
- belongs_to
- joins
- where
- validations
- order
- limit
- offset
- scheema
Would like to help us to improve our library?
- Fork it ( https://github.com/xdougx/selenite-db/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
- xdougx Douglas Rossignolli - creator, maintainer
- Crystal-Lang Irc/Gitter
- @mverzilli