DB Class

by edskeizer

HTML

<link rel="stylesheet" href="http://code.ionicframework.com/1.0.0-beta.11/css/ionic.min.css">
<script src="http://cdn.jsdelivr.net/pouchdb/3.0.2/pouchdb.min.js"></script>
<div class="padding">
    <a href="#" class="add button button-balanced">Check In</a> 
    <a href="#" class="info button button-positive">DB info</a> 
    <a href="#" class="destroy button button-assertive">Delete DB</a>
</div>
<div class="padding">
    <ul class="list">
    </ul>
</div>

CoffeeScript

class DB
  constructor: (@dbname,@dbuser_name,@dbuser_pass,@onChange)->
    @remoteUrl = "http://alpha.api.aware.care/#{@dbname}"
    PouchDB @dbname, adapter: "websql", (error, db) => #setup local db
      unless error
        @db = db
        @db.changes(
          continuous : true
          include_docs : true
          onChange : @onChange
        )
        #setup replicate
        @db.replicate.to @remoteUrl, {live: true, batch_size: 500}
        @db.replicate.from @remoteUrl, {live: true, batch_size: 500}
      else #Crap!
        alert "error"
        
  add: (doc, cb = ()-> true) ->
    unless "_id" of doc
      @db.post doc, cb
    else if "_id" of doc and doc._id.slice(0,8) != "_design/"
      @db.put doc, cb

  info: ->
    @db.info (err, info) ->
      alert JSON.stringify(info)

  destroy: ->
    @db.destroy (err, info) ->
      alert "bey bey DB"

  get: (id, cb = ()-> true) ->
    @db.get id, cb
    
  remove: (id, cb = ()-> true) ->
    @get id, (error, doc) =>
      @db.remove doc, cb unless error
        
        
window.db = new DB "aware_user_yfgeumej763nczkj9rei28bwwk3lgfy9sh6xkx0c", "eds%40aware.care", "Testtest1", (change)->
  if change.id[0] isnt "_"
    unless change.doc._deleted
      $(".list").append('<li class="item item-button-right" id="">'+JSON.stringify(change.doc)+'<button class="del button button-assertive"><i class="icon ion-close"></i></button></li>');


    
$(".add").click (event) ->
  event.preventDefault()
  now = new Date()
  dd = now.getDate()
  mm = now.getMonth() + 1
  yyyy = now.getFullYear()
  hour = now.getHours()
  min = now.getMinutes()
  sec = now.getSeconds()
  dd = "0" + dd  if dd < 10
  mm = "0" + mm  if mm < 10
  hour = "0" + hour  if hour < 10
  min = "0" + min  if min < 10
  sec = "0" + sec  if sec < 10
  CheckInData = 
    type: "CheckIn"
    title: hour + ":" + min + ":" +sec+ " " + dd + "/" + mm + "/" + yyyy
  db.add CheckInData, (err, resp) ->
    console.log "ADD"

$(".info").click (event) ->
 ...