|
| 1 | +var util = require('util'), |
| 2 | + _ = require('lodash'), |
| 3 | + async = require('async'), |
| 4 | + ConcurrencyError = require('../concurrencyError'), |
| 5 | + gcFirestore = require('@google-cloud/firestore'), |
| 6 | + Repository = require('../base'), |
| 7 | + uuid = require('uuid').v4, |
| 8 | + ViewModel = Repository.ViewModel; |
| 9 | + |
| 10 | +var collections = []; |
| 11 | + |
| 12 | +function Firestore(options) { |
| 13 | + Repository.call(this); |
| 14 | + this.options = _.merge({ timestampsInSnapshots: true }, options); |
| 15 | +} |
| 16 | + |
| 17 | +util.inherits(Firestore, Repository); |
| 18 | + |
| 19 | +function implementError (callback) { |
| 20 | + var err = new Error('Storage method add is not implemented'); |
| 21 | + if (callback) callback(err); |
| 22 | + throw err; |
| 23 | +} |
| 24 | + |
| 25 | +function parseFirestoreQuery(query) { |
| 26 | + if (_.isArray(query)) { |
| 27 | + return query; |
| 28 | + } else if (_.isPlainObject(query)) { |
| 29 | + return _.map(query, function(value, key) { |
| 30 | + return [key, '==', value]; |
| 31 | + }); |
| 32 | + } |
| 33 | + throw new Error('Unknown query type'); |
| 34 | +}; |
| 35 | + |
| 36 | +function firestoreQueryParser(collectionRef, queryParams) { |
| 37 | + var params = parseFirestoreQuery(queryParams); |
| 38 | + return _.reduce(params, function(acc, q) { |
| 39 | + return acc.where.apply(acc, q); |
| 40 | + }, collectionRef); |
| 41 | +}; |
| 42 | + |
| 43 | +function emptyCollection(db, collection, callback) { |
| 44 | + var collectionRef = db.collection(collection); |
| 45 | + var query = collectionRef.get().then(function (querySnapshot) { |
| 46 | + var writeBatch = db.batch(); |
| 47 | + querySnapshot.forEach(function (documentSnapshot) { |
| 48 | + var documentPath = collection + '/' + documentSnapshot.id; |
| 49 | + var documentRef = db.doc(documentPath); |
| 50 | + writeBatch.delete(documentRef); |
| 51 | + }); |
| 52 | + writeBatch.commit().then(function () { |
| 53 | + if (callback) callback(null); |
| 54 | + }); |
| 55 | + }); |
| 56 | +}; |
| 57 | + |
| 58 | +function getPrecondition(vm) { |
| 59 | + var precondition = {}; |
| 60 | + if (!_.isUndefined(vm.get('_updateTime'))) { |
| 61 | + const time = vm.get('_updateTime'); |
| 62 | + if (_.isDate(time)) { |
| 63 | + precondition['lastUpdateTime'] = time.toISOString(); |
| 64 | + } else if (_.isString(time)) { |
| 65 | + precondition['lastUpdateTime'] = time; |
| 66 | + } |
| 67 | + } |
| 68 | + return precondition; |
| 69 | +} |
| 70 | + |
| 71 | +function enrichVMWithTimestamps(vm, documentSnapshot) { |
| 72 | + _.isUndefined(documentSnapshot.readTime) ? false : vm.set('_readTime', documentSnapshot.readTime); |
| 73 | + _.isUndefined(documentSnapshot.createTime) ? false : vm.set('_createTime', documentSnapshot.createTime); |
| 74 | + _.isUndefined(documentSnapshot.updateTime) ? false : vm.set('_updateTime', documentSnapshot.updateTime); |
| 75 | + return vm; |
| 76 | +}; |
| 77 | + |
| 78 | +function applyQueryOptions(query, options) { |
| 79 | + if (!_.isUndefined(options)) { |
| 80 | + // Apply supported queryOptions |
| 81 | + if (_.has(options, 'limit')) { |
| 82 | + query = query.limit(options.limit); |
| 83 | + } |
| 84 | + if (_.has(options, 'skip')) { |
| 85 | + query = query.offset(options.skip); |
| 86 | + } |
| 87 | + if (_.has(options, 'sort')) { |
| 88 | + var sortKey = options.sort.keys[0]; |
| 89 | + var direction = options.sort.keys[sortKey] == 1 ? 'asc' : 'desc'; |
| 90 | + query = query.orderBy(sortKey, direction); |
| 91 | + } |
| 92 | + } |
| 93 | + return query; |
| 94 | +} |
| 95 | + |
| 96 | +_.extend(Firestore.prototype, { |
| 97 | + |
| 98 | + connect: function (callback) { |
| 99 | + var self = this; |
| 100 | + var options = this.options; |
| 101 | + self.db = new gcFirestore(options); |
| 102 | + self.emit('connect'); |
| 103 | + if (callback) callback(null, self); |
| 104 | + }, |
| 105 | + |
| 106 | + disconnect: function (callback) { |
| 107 | + var self = this; |
| 108 | + delete self.db; |
| 109 | + self.emit('disconnect'); |
| 110 | + if (callback) callback(null, self); |
| 111 | + }, |
| 112 | + |
| 113 | + getNewId: function (callback) { |
| 114 | + this.checkConnection(); |
| 115 | + |
| 116 | + var id = uuid().toString(); |
| 117 | + if (callback) callback(null, id); |
| 118 | + }, |
| 119 | + |
| 120 | + get: function (id, callback) { |
| 121 | + this.checkConnection(); |
| 122 | + |
| 123 | + if(_.isFunction(id)) { |
| 124 | + callback = id; |
| 125 | + id = null; |
| 126 | + } |
| 127 | + |
| 128 | + if (!id) { |
| 129 | + id = uuid().toString(); |
| 130 | + } |
| 131 | + |
| 132 | + var self = this; |
| 133 | + |
| 134 | + var documentPath = this.collection + '/' + id; |
| 135 | + var documentRef = this.db.doc(documentPath); |
| 136 | + |
| 137 | + documentRef.get().then(function (documentSnapshot) { |
| 138 | + var vm = new ViewModel(documentSnapshot.data() || { id }, self); |
| 139 | + vm = enrichVMWithTimestamps(vm, documentSnapshot); |
| 140 | + if (documentSnapshot.exists) { |
| 141 | + vm.actionOnCommit = 'update'; |
| 142 | + } else { |
| 143 | + vm.actionOnCommit = 'create'; |
| 144 | + } |
| 145 | + callback(null, vm); |
| 146 | + }); |
| 147 | + }, |
| 148 | + |
| 149 | + find: function (queryParams, queryOptions, callback) { |
| 150 | + this.checkConnection(); |
| 151 | + |
| 152 | + var self = this; |
| 153 | + var collectionRef = this.db.collection(this.collection); |
| 154 | + |
| 155 | + var query = firestoreQueryParser(collectionRef, queryParams); |
| 156 | + query = applyQueryOptions(query, queryOptions); |
| 157 | + |
| 158 | + query.get().then(function (querySnapshot) { |
| 159 | + var vms = _.map(querySnapshot.docs, function(documentSnapshot) { |
| 160 | + var vm = new ViewModel(documentSnapshot.data(), self); |
| 161 | + vm = enrichVMWithTimestamps(vm, documentSnapshot); |
| 162 | + vm.actionOnCommit = 'update'; |
| 163 | + return vm; |
| 164 | + }); |
| 165 | + callback(null, vms); |
| 166 | + }); |
| 167 | + }, |
| 168 | + |
| 169 | + findOne: function (queryParams, queryOptions, callback) { |
| 170 | + // NOTE: queryOptions is ignored |
| 171 | + this.checkConnection(); |
| 172 | + |
| 173 | + var self = this; |
| 174 | + var collectionRef = this.db.collection(this.collection); |
| 175 | + |
| 176 | + var query = firestoreQueryParser(collectionRef, queryParams); |
| 177 | + _.unset(queryOptions, 'limit'); |
| 178 | + query = applyQueryOptions(query, queryOptions); |
| 179 | + query.limit(1).get().then(function (querySnapshot) { |
| 180 | + if (querySnapshot.size == 0) { |
| 181 | + callback(null, null); |
| 182 | + } |
| 183 | + querySnapshot.forEach(function (documentSnapshot) { |
| 184 | + var vm = new ViewModel(documentSnapshot.data(), self); |
| 185 | + vm = enrichVMWithTimestamps(vm, documentSnapshot); |
| 186 | + vm.actionOnCommit = 'update'; |
| 187 | + callback(null, vm); |
| 188 | + }); |
| 189 | + }); |
| 190 | + }, |
| 191 | + |
| 192 | + commit: function (vm, callback) { |
| 193 | + this.checkConnection(); |
| 194 | + |
| 195 | + if (!vm.actionOnCommit) return callback(new Error('actionOnCommit is not defined!')); |
| 196 | + |
| 197 | + var self = this; |
| 198 | + |
| 199 | + switch(vm.actionOnCommit) { |
| 200 | + case 'delete': |
| 201 | + var documentPath = this.collection + '/' + vm.id; |
| 202 | + var documentRef = this.db.doc(documentPath); |
| 203 | + var precondition = getPrecondition(vm); |
| 204 | + documentRef.delete(precondition).then(function () { |
| 205 | + callback(null); |
| 206 | + }).catch(function (err) { |
| 207 | + return callback(new ConcurrencyError()); |
| 208 | + }); |
| 209 | + break; |
| 210 | + case 'create': |
| 211 | + var documentPath = this.collection + '/' + vm.id; |
| 212 | + var documentRef = this.db.doc(documentPath); |
| 213 | + documentRef.get().then(function (documentSnapshot) { |
| 214 | + if (documentSnapshot.exists) { |
| 215 | + return callback(new ConcurrencyError()); |
| 216 | + } |
| 217 | + documentRef.set(vm.attributes).then(function () { |
| 218 | + vm.actionOnCommit = 'update'; |
| 219 | + callback(null, vm); |
| 220 | + }); |
| 221 | + }); |
| 222 | + break; |
| 223 | + case 'update': |
| 224 | + var documentPath = this.collection + '/' + vm.id; |
| 225 | + var documentRef = this.db.doc(documentPath); |
| 226 | + documentRef.get().then(function (documentSnapshot) { |
| 227 | + if (!documentSnapshot.exists) { |
| 228 | + documentRef.set(vm.attributes).then(function () { |
| 229 | + vm.actionOnCommit = 'update'; |
| 230 | + callback(null, vm); |
| 231 | + }); |
| 232 | + } else { |
| 233 | + if (!_.isUndefined(documentSnapshot.updateTime) && |
| 234 | + _.isUndefined(vm.get('_updateTime'))) { |
| 235 | + return callback(new ConcurrencyError()); |
| 236 | + } |
| 237 | + |
| 238 | + var precondition = getPrecondition(vm); |
| 239 | + documentRef.update(vm.attributes, precondition).then(function () { |
| 240 | + self.get(vm.id, callback); |
| 241 | + }, function (err) { |
| 242 | + return callback(new ConcurrencyError()); |
| 243 | + }); |
| 244 | + } |
| 245 | + }); |
| 246 | + break; |
| 247 | + default: |
| 248 | + return callback(new Error('Unknown actionOnCommit: ' + vm.actionOnCommit)); |
| 249 | + }; |
| 250 | + }, |
| 251 | + |
| 252 | + checkConnection: function (callback) { |
| 253 | + if (this.collection) { |
| 254 | + return; |
| 255 | + } |
| 256 | + |
| 257 | + if (collections.indexOf(this.collectionName) < 0) { |
| 258 | + collections.push(this.collectionName) |
| 259 | + } |
| 260 | + |
| 261 | + this.collection = this.collectionName; |
| 262 | + if (callback) callback(null); |
| 263 | + }, |
| 264 | + |
| 265 | + clear: function (callback) { |
| 266 | + this.checkConnection(); |
| 267 | + |
| 268 | + var self = this; |
| 269 | + if (!this.collection) { |
| 270 | + if (callback) callback(null); |
| 271 | + return; |
| 272 | + } |
| 273 | + |
| 274 | + emptyCollection(this.db, this.collection, callback); |
| 275 | + }, |
| 276 | + |
| 277 | + clearAll: function (callback) { |
| 278 | + var self = this; |
| 279 | + async.each(collections, function (col, callback) { |
| 280 | + emptyCollection(self.db, col, callback); |
| 281 | + }, callback); |
| 282 | + }, |
| 283 | + |
| 284 | +}); |
| 285 | + |
| 286 | +module.exports = Firestore; |
0 commit comments