UNPKG

4.24 kBJavaScriptView Raw
1var Collection, Definition, HARDENED_OFFSET, HDKey, bs, defer, deferObject, exports;
2
3bs = require('bs58check');
4
5defer = require('when');
6
7deferObject = require('when/keys');
8
9HDKey = require('hdkey');
10
11Definition = require('./definition');
12
13HARDENED_OFFSET = 0x80000000;
14
15Collection = (function() {
16 function Collection(custodian, model, key, hardened) {
17 this.custodian = custodian;
18 this.model = model;
19 this.hardened = hardened != null ? hardened : false;
20 if (!(this instanceof Collection)) {
21 return new Collection(this.custodian, this.model, key, this.hardened);
22 }
23 if (typeof key === 'string') {
24 key = bs.decode(key);
25 }
26 this.hdkey = HDKey.fromExtendedKey(bs.encode(key));
27 this.privateKey = this.hdkey.privateKey;
28 this.publicKey = this.hdkey.publicKey;
29 this.readonly = this.privateKey == null;
30 if (this.hardened) {
31 this.offset = HARDENED_OFFSET;
32 } else {
33 this.offset = 0;
34 }
35 this._definitions = {};
36 this._documents = [];
37 }
38
39 Collection.prototype.definition = function(key, definition) {
40 if (definition != null) {
41 return Definition(this.custodian, definition).save().then((function(_this) {
42 return function(hash) {
43 return _this._definitions[key] = bs.encode(hash);
44 };
45 })(this));
46 } else if (key != null) {
47 if (typeof key === 'string' && key.length <= 32) {
48 key = this._definitions[key];
49 }
50 return Definition(this.custodian, key);
51 } else {
52 return deferObject.map(this._definitions, (function(_this) {
53 return function(definition) {
54 return Definition(_this.custodian, definition);
55 };
56 })(this));
57 }
58 };
59
60 Collection.prototype.insert = function(definition, data, autosave) {
61 if (autosave == null) {
62 autosave = true;
63 }
64 if (this.readonly) {
65 return false;
66 }
67 return this.sync().then((function(_this) {
68 return function() {
69 var document;
70 document = _this.at();
71 return document.definition(_this.definition(definition)).then(function() {
72 return document.data(data);
73 }).then(function() {
74 if (autosave) {
75 return document.save();
76 }
77 }).then(function() {
78 return _this._documents[document.hdkey.index - _this.offset] = document;
79 });
80 };
81 })(this));
82 };
83
84 Collection.prototype.at = function(index) {
85 var document, hdkey, key, _ref;
86 if (index == null) {
87 index = this._documents.length + this.offset;
88 }
89 document = this._documents[index - this.offset];
90 if (document != null) {
91 return document;
92 }
93 hdkey = this.hdkey.deriveChild(index);
94 key = (_ref = hdkey.privateExtendedKey) != null ? _ref : hdkey.publicExtendedKey;
95 return new this.model(this.custodian, key);
96 };
97
98 Collection.prototype.all = function(refresh, pluck) {
99 if (typeof refresh === 'string') {
100 pluck = refresh;
101 refresh = false;
102 }
103 return this.sync(refresh).then((function(_this) {
104 return function() {
105 console.log('all', _this._documents.length);
106 if (pluck) {
107 return defer.map(_this._documents, function(d) {
108 return typeof d[pluck] === "function" ? d[pluck]() : void 0;
109 });
110 } else {
111 return _this._documents;
112 }
113 };
114 })(this));
115 };
116
117 Collection.prototype.sync = function(refresh) {
118 var start;
119 if (refresh == null) {
120 refresh = false;
121 }
122 start = this.offset;
123 if (!refresh) {
124 start = start + this._documents.length;
125 }
126 return defer.iterate((function(_this) {
127 return function(document) {
128 return _this.at(document.hdkey.index + 1);
129 };
130 })(this), (function(_this) {
131 return function(document) {
132 return document.fetch()["yield"](false)["else"](true);
133 };
134 })(this), (function(_this) {
135 return function(document) {
136 return _this._documents[document.hdkey.index - _this.offset] = document;
137 };
138 })(this), this.at(start)).then((function(_this) {
139 return function() {
140 return _this;
141 };
142 })(this));
143 };
144
145 return Collection;
146
147})();
148
149exports = module.exports = Collection;