all repos — litestore @ e465690e6e082935801e5c6a10a37663def8bb05

A minimalist nosql document store.

admin/js/models.js

 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
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
(function(){
  window.Page = {};
  window.Info = {};
  window.Doc = {};
  var u = window.LS.utils;
  
  Page.get = function(id) {
    var content = m.prop("");
    return m.request({
        method: "GET", 
        url: "md/"+id+".md",
        deserialize: function(value) {return value;}
      }).then(function(content){ return u.markdown(content);});
  };
  
  Info.get = function(){
    var content = m.prop("");
    return m.request({
        method: "GET", 
        url: "/v1/info"
      }).then(content);
  };
  
  Doc.getByTag = function(tag) {
    var docs = m.prop("");
    return m.request({
        method: "GET", 
        url: "/v1/docs?contents=false&tags="+tag
      }).then(docs);
  };
  
  Doc.search = function(search, offset, limit){
    offset = offset || 0;
    limit = limit || 10;
    var docs = m.prop("");
    return m.request({
        method: "GET", 
        url: "/v1/docs?contents=false&search="+search+"&limit="+limit+"&offset="+offset,
      }).then(docs);
  };

  Doc.get = function(id) {
    var doc = m.prop("");
    return m.request({
        method: "GET", 
        url: "/v1/docs/"+id+"?raw=true"
      }).then(doc);
  };
  
  Doc.delete = function(id){
    return m.request({
        method: "DELETE", 
        url: "/v1/docs/"+id
      });
  };
  
  Doc.put = function(doc, contentType){
    xhrcfg = u.setContentType(doc, contentType);
    console.log("Doc.put - Saving Document:", doc);
    return m.request({
        method: "PUT", 
        url: "/v1/docs/"+doc.id,
        data: doc.data,
        serialize: function(data){return data;},
        config: xhrcfg
      });
  };
  
  Doc.upload = function(doc) {
    console.log("Doc.put - Uploading Document:", doc);
    return m.request({
      method: "PUT",
      url: "/v1/docs/"+doc.id,
      data: doc.data,
      serialize: function(data) {return data}
    });
  };
  
  Doc.patch = function(id, updatedTags){
    return Doc.get(id).then(function(doc){
      var tags = doc.tags;
      var count = 0;
      var ops = [];
      tags.forEach(function(tag){
        if (updatedTags[count]){
          if (updatedTags[count] != tag){
            // update tag
            ops.push({"op": "replace", "path": "/tags/"+count, "value": updatedTags[count]});
          }
        } else {
          // delete tag
          ops.push({"op": "remove", "path": "/tags/"+count});
        }
        count++;
      });
      if (updatedTags.length > tags.length) {
        for (i = tags.length; i< updatedTags.length; i++){
          // add tag
          ops.push({"op": "add", "path": "/tags/"+i, "value": updatedTags[i]});
        }
      }
      console.log("Doc.patch - Saving Tags:", ops);
      return m.request({
        method: "PATCH",
        url: "/v1/docs/"+id,
        data: ops
      });
    });
  };
}());