Skip to content

Commit

Permalink
✨ Create firebase.findOne
Browse files Browse the repository at this point in the history
  • Loading branch information
jpventura committed Jan 18, 2017
1 parent c62ad64 commit 61e1516
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
51 changes: 51 additions & 0 deletions firebase/findOne.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* MIT License
*
* Copyright (c) 2017 Joao Paulo Fernandes Ventura
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/


var _ = require('lodash');
var find = require('./find');

/**
* Add a new row to the table
*
* @param {String} connection The datastore name to query on.
* @param {String} collection The table name to create a record into
* @param {Object} query The new record to be created
* @param {Promise} Unresolved promise if the created record,
* otherwise an error throw during the operation
*/
var FindOne = function FindOne(connection, collection, query) {
var head = function head(documents) {
return (documents.length > 0) ? _.head(documents) : null;
};

// FIXME: Optimize Firebase query
// This approach is querying ALL documents at Firebase and then filtering
// then through Waterline criteria.
// This solution is GOOD ENOUGH in order to pass on Waterline Adapter tests,
// but should not be used on production environment.
return find(connection, collection, query).then(head);
};

module.exports = FindOne;
2 changes: 2 additions & 0 deletions firebase/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var Define = require('./define');
var Destroy = require('./destroy');
var Drop = require('./drop');
var Find = require('./find');
var FindOne = require('./findOne');
var RegisterApplication = require('./registerApplication');
var TearDown = require('./tearDown');

Expand All @@ -39,6 +40,7 @@ module.exports = {
'destroy': Destroy,
'drop': Drop,
'find': Find,
'findOne': FindOne,
'registerApplication': RegisterApplication,
'tearDown': TearDown
};

0 comments on commit 61e1516

Please sign in to comment.