How To: Define a data model and store history data to database

Tony Wieczorek edited this page Sep 9, 2015 · 2 revisions

Dashing is build based on Sinatra, if you are already familiar with Sinatra, then I am sure this won't be a problem.

This guide aims for the people who are not so familiar with Sinatra and want a quick hint.

1. Update Gemfile, add the following lines

gem 'dm-sqlite-adapter'
gem 'data_mapper'

2. Define your data model under lib directory, lib files will be loaded before jobs directory

require "data_mapper"

DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/database.db")

class YourDataModel
  include DataMapper::Resource

  property :id,    Serial    
  property :createdAt, DateTime
  property :serviceName, String
  property :available, Boolean, :default  => true

end

# Perform basic sanity checks and initialize all relationships
# Call this when you've defined all your models
DataMapper.finalize

# automatically create the post table
YourDataModel.auto_upgrade!

3. Use it in your job file

   YourDataModel.new({:createdAt => DateTime.now, :serviceName => 'service1', :available => succeed}).save()
   puts "YourDataModel.all().count #{YourDataModel.all().count}"