Created
December 13, 2019 12:32
-
-
Save gotar/181e2027e763fc6e6cc197d080dbd9ba to your computer and use it in GitHub Desktop.
ROM (SQLite) + DRY-V example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'bundler/inline' | |
DB_URL = "postgres://localhost/gotar" | |
gemfile(true) do | |
source 'https://rubygems.org' | |
gem 'rom', '~> 5.1' | |
gem 'rom-sql', '~> 3.0' | |
gem 'sqlite3' | |
gem "dry-validation", "~> 1.4" | |
gem "pry" | |
end | |
require 'rom' | |
require 'rom-sql' | |
require 'dry-validation' | |
require 'logger' | |
config = ROM::Configuration.new(:sql, 'sqlite::memory') | |
config.gateways[:default].use_logger(Logger.new($stdout)) | |
conn = config.gateways[:default].connection | |
conn.create_table(:users) do | |
primary_key :id | |
column :name, String | |
end | |
config.relation(:users) do | |
schema(infer: true) | |
end | |
$rom = ROM.container(config) | |
class Schema < Dry::Validation::Contract | |
option :id | |
params do | |
required(:name).filled | |
end | |
rule(:name) do | |
key.failure("must be unique") if $rom.relations[:users].where(name: value).where{ id.not(id) }.exist? | |
end | |
end | |
validation = Schema.new(id: 1).call({name: "foo"}) | |
validation.success? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment