forked from swlkr/roda-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.rb
52 lines (42 loc) · 1.16 KB
/
models.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
require 'sequel'
require 'logger'
# DATABASE
DB = Sequel.connect ENV.fetch('DATABASE_URL')
DB.loggers << Logger.new($stdout)
# set pragma stuff
DB.run 'PRAGMA busy_timeout=5000;'
DB.run 'PRAGMA synchronous=NORMAL;'
DB.run 'PRAGMA foreign_keys=1;'
DB.run 'PRAGMA journal_mode=WAL;'
# MIGRATE
Sequel.extension :migration
Sequel::Migrator.run(DB, 'migrations')
# MODELS
Model = Class.new(Sequel::Model(DB))
# set_fields doesn't set existing models' columns to nil
Model.default_set_fields_options[:missing] = :skip
Model.def_Model(self)
Model.plugin :dataset_associations
Model.plugin :auto_validations, not_null: :presence
Model.plugin :timestamps
Model.plugin :dataset_associations
Model.plugin :string_stripper
Model.plugin :association_proxies
Model.plugin :hash_id, salt: ENV.fetch('HASH_ID_SALT').freeze, length: 7
Model.plugin :validation_helpers
Model.plugin :forme
class Model
class << self
def schema
meta_columns = %i[id updated_at created_at]
db_schema.reject { |k| meta_columns.include?(k) }
end
def fields
schema.keys.map(&:to_s)
end
end
end
Dir[File.join(__dir__, 'models', '*.rb')].each do |file|
require file
end
DB.freeze