-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for firestore #62
Open
rickerbh
wants to merge
2
commits into
thenativeweb:master
Choose a base branch
from
rickerbh:firestore
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,286 @@ | ||
var util = require('util'), | ||
_ = require('lodash'), | ||
async = require('async'), | ||
ConcurrencyError = require('../concurrencyError'), | ||
gcFirestore = require('@google-cloud/firestore'), | ||
Repository = require('../base'), | ||
uuid = require('uuid').v4, | ||
ViewModel = Repository.ViewModel; | ||
|
||
var collections = []; | ||
|
||
function Firestore(options) { | ||
Repository.call(this); | ||
this.options = _.merge({ timestampsInSnapshots: true }, options); | ||
} | ||
|
||
util.inherits(Firestore, Repository); | ||
|
||
function implementError (callback) { | ||
var err = new Error('Storage method add is not implemented'); | ||
if (callback) callback(err); | ||
throw err; | ||
} | ||
|
||
function parseFirestoreQuery(query) { | ||
if (_.isArray(query)) { | ||
return query; | ||
} else if (_.isPlainObject(query)) { | ||
return _.map(query, function(value, key) { | ||
return [key, '==', value]; | ||
}); | ||
} | ||
throw new Error('Unknown query type'); | ||
}; | ||
|
||
function firestoreQueryParser(collectionRef, queryParams) { | ||
var params = parseFirestoreQuery(queryParams); | ||
return _.reduce(params, function(acc, q) { | ||
return acc.where.apply(acc, q); | ||
}, collectionRef); | ||
}; | ||
|
||
function emptyCollection(db, collection, callback) { | ||
var collectionRef = db.collection(collection); | ||
var query = collectionRef.get().then(function (querySnapshot) { | ||
var writeBatch = db.batch(); | ||
querySnapshot.forEach(function (documentSnapshot) { | ||
var documentPath = collection + '/' + documentSnapshot.id; | ||
var documentRef = db.doc(documentPath); | ||
writeBatch.delete(documentRef); | ||
}); | ||
writeBatch.commit().then(function () { | ||
if (callback) callback(null); | ||
}); | ||
}); | ||
}; | ||
|
||
function getPrecondition(vm) { | ||
var precondition = {}; | ||
if (!_.isUndefined(vm.get('_updateTime'))) { | ||
const time = vm.get('_updateTime'); | ||
if (_.isDate(time)) { | ||
precondition['lastUpdateTime'] = time.toISOString(); | ||
} else if (_.isString(time)) { | ||
precondition['lastUpdateTime'] = time; | ||
} | ||
} | ||
return precondition; | ||
} | ||
|
||
function enrichVMWithTimestamps(vm, documentSnapshot) { | ||
_.isUndefined(documentSnapshot.readTime) ? false : vm.set('_readTime', documentSnapshot.readTime); | ||
_.isUndefined(documentSnapshot.createTime) ? false : vm.set('_createTime', documentSnapshot.createTime); | ||
_.isUndefined(documentSnapshot.updateTime) ? false : vm.set('_updateTime', documentSnapshot.updateTime); | ||
return vm; | ||
}; | ||
|
||
function applyQueryOptions(query, options) { | ||
if (!_.isUndefined(options)) { | ||
// Apply supported queryOptions | ||
if (_.has(options, 'limit')) { | ||
query = query.limit(options.limit); | ||
} | ||
if (_.has(options, 'skip')) { | ||
query = query.offset(options.skip); | ||
} | ||
if (_.has(options, 'sort')) { | ||
var sortKey = options.sort.keys[0]; | ||
var direction = options.sort.keys[sortKey] == 1 ? 'asc' : 'desc'; | ||
query = query.orderBy(sortKey, direction); | ||
} | ||
} | ||
return query; | ||
} | ||
|
||
_.extend(Firestore.prototype, { | ||
|
||
connect: function (callback) { | ||
var self = this; | ||
var options = this.options; | ||
self.db = new gcFirestore(options); | ||
self.emit('connect'); | ||
if (callback) callback(null, self); | ||
}, | ||
|
||
disconnect: function (callback) { | ||
var self = this; | ||
delete self.db; | ||
self.emit('disconnect'); | ||
if (callback) callback(null, self); | ||
}, | ||
|
||
getNewId: function (callback) { | ||
this.checkConnection(); | ||
|
||
var id = uuid().toString(); | ||
if (callback) callback(null, id); | ||
}, | ||
|
||
get: function (id, callback) { | ||
this.checkConnection(); | ||
|
||
if(_.isFunction(id)) { | ||
callback = id; | ||
id = null; | ||
} | ||
|
||
if (!id) { | ||
id = uuid().toString(); | ||
} | ||
|
||
var self = this; | ||
|
||
var documentPath = this.collection + '/' + id; | ||
var documentRef = this.db.doc(documentPath); | ||
|
||
documentRef.get().then(function (documentSnapshot) { | ||
var vm = new ViewModel(documentSnapshot.data() || { id }, self); | ||
vm = enrichVMWithTimestamps(vm, documentSnapshot); | ||
if (documentSnapshot.exists) { | ||
vm.actionOnCommit = 'update'; | ||
} else { | ||
vm.actionOnCommit = 'create'; | ||
} | ||
callback(null, vm); | ||
}); | ||
}, | ||
|
||
find: function (queryParams, queryOptions, callback) { | ||
this.checkConnection(); | ||
|
||
var self = this; | ||
var collectionRef = this.db.collection(this.collection); | ||
|
||
var query = firestoreQueryParser(collectionRef, queryParams); | ||
query = applyQueryOptions(query, queryOptions); | ||
|
||
query.get().then(function (querySnapshot) { | ||
var vms = _.map(querySnapshot.docs, function(documentSnapshot) { | ||
var vm = new ViewModel(documentSnapshot.data(), self); | ||
vm = enrichVMWithTimestamps(vm, documentSnapshot); | ||
vm.actionOnCommit = 'update'; | ||
return vm; | ||
}); | ||
callback(null, vms); | ||
}); | ||
}, | ||
|
||
findOne: function (queryParams, queryOptions, callback) { | ||
// NOTE: queryOptions is ignored | ||
this.checkConnection(); | ||
|
||
var self = this; | ||
var collectionRef = this.db.collection(this.collection); | ||
|
||
var query = firestoreQueryParser(collectionRef, queryParams); | ||
_.unset(queryOptions, 'limit'); | ||
query = applyQueryOptions(query, queryOptions); | ||
query.limit(1).get().then(function (querySnapshot) { | ||
if (querySnapshot.size == 0) { | ||
callback(null, null); | ||
} | ||
querySnapshot.forEach(function (documentSnapshot) { | ||
var vm = new ViewModel(documentSnapshot.data(), self); | ||
vm = enrichVMWithTimestamps(vm, documentSnapshot); | ||
vm.actionOnCommit = 'update'; | ||
callback(null, vm); | ||
}); | ||
}); | ||
}, | ||
|
||
commit: function (vm, callback) { | ||
this.checkConnection(); | ||
|
||
if (!vm.actionOnCommit) return callback(new Error('actionOnCommit is not defined!')); | ||
|
||
var self = this; | ||
|
||
switch(vm.actionOnCommit) { | ||
case 'delete': | ||
var documentPath = this.collection + '/' + vm.id; | ||
var documentRef = this.db.doc(documentPath); | ||
var precondition = getPrecondition(vm); | ||
documentRef.delete(precondition).then(function () { | ||
callback(null); | ||
}).catch(function (err) { | ||
return callback(new ConcurrencyError()); | ||
}); | ||
break; | ||
case 'create': | ||
var documentPath = this.collection + '/' + vm.id; | ||
var documentRef = this.db.doc(documentPath); | ||
documentRef.get().then(function (documentSnapshot) { | ||
if (documentSnapshot.exists) { | ||
return callback(new ConcurrencyError()); | ||
} | ||
documentRef.set(vm.attributes).then(function () { | ||
vm.actionOnCommit = 'update'; | ||
callback(null, vm); | ||
}); | ||
}); | ||
break; | ||
case 'update': | ||
var documentPath = this.collection + '/' + vm.id; | ||
var documentRef = this.db.doc(documentPath); | ||
documentRef.get().then(function (documentSnapshot) { | ||
if (!documentSnapshot.exists) { | ||
documentRef.set(vm.attributes).then(function () { | ||
vm.actionOnCommit = 'update'; | ||
callback(null, vm); | ||
}); | ||
} else { | ||
if (!_.isUndefined(documentSnapshot.updateTime) && | ||
_.isUndefined(vm.get('_updateTime'))) { | ||
return callback(new ConcurrencyError()); | ||
} | ||
|
||
var precondition = getPrecondition(vm); | ||
documentRef.update(vm.attributes, precondition).then(function () { | ||
self.get(vm.id, callback); | ||
}, function (err) { | ||
return callback(new ConcurrencyError()); | ||
}); | ||
} | ||
}); | ||
break; | ||
default: | ||
return callback(new Error('Unknown actionOnCommit: ' + vm.actionOnCommit)); | ||
}; | ||
}, | ||
|
||
checkConnection: function (callback) { | ||
if (this.collection) { | ||
return; | ||
} | ||
|
||
if (collections.indexOf(this.collectionName) < 0) { | ||
collections.push(this.collectionName) | ||
} | ||
|
||
this.collection = this.collectionName; | ||
if (callback) callback(null); | ||
}, | ||
|
||
clear: function (callback) { | ||
this.checkConnection(); | ||
|
||
var self = this; | ||
if (!this.collection) { | ||
if (callback) callback(null); | ||
return; | ||
} | ||
|
||
emptyCollection(this.db, this.collection, callback); | ||
}, | ||
|
||
clearAll: function (callback) { | ||
var self = this; | ||
async.each(collections, function (col, callback) { | ||
emptyCollection(self.db, col, callback); | ||
}, callback); | ||
}, | ||
|
||
}); | ||
|
||
module.exports = Firestore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use
Repository.use('@google-cloud/firestore')
instead ofrequire('@google-cloud/firestore')
?