Created
December 13, 2019 12:32
-
-
Save gotar/e7b2ae405c22773831058c31821a7200 to your computer and use it in GitHub Desktop.
ROM(PG, Sequel) + Dry-v example
This file contains hidden or 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 'pg' | |
gem 'rom', '~> 5.1' | |
gem 'rom-sql', '~> 3.0' | |
gem "dry-validation", "~> 1.4" | |
gem "pry" | |
end | |
require 'rom' | |
require 'rom-sql' | |
require 'dry-validation' | |
require 'logger' | |
config = ROM::Configuration.new(:sql, 'postgres://localhost/gotar') | |
conn = config.gateways[:default].use_logger(Logger.new($stdout)) | |
conn = config.gateways[:default].connection | |
conn.create_table(:foo) do | |
primary_key :id | |
column :name, String | |
end | |
config.relation(:foo) 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[:foo].where(name: value).where{ id.not(id) }.exist? | |
end | |
end | |
validation = Schema.new(id: 1).call({name: "foo"}) | |
validation.success? | |
conn.drop_table(:foo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment