-
-
Save magicsketchweb/9cefe00c7cba02d95537e44f03c25842 to your computer and use it in GitHub Desktop.
hook-airtable_find.js
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
// GET https://hook.io/jamztang/airtable_find?apiKey=<key>&baseId=<base>&tableName=<table>&key=<key>&value=<value> | |
var Airtable = require('airtable'); | |
var request = require('request'); | |
var async = require('async'); | |
var _ = require('lodash'); | |
module['exports'] = function echoHttp (hook) { | |
var p = hook.params; | |
var apiKey = p["apiKey"]; | |
var baseId = p["baseId"]; | |
var tableName = p["tableName"]; | |
var view = p["view"]; | |
var key = p["key"]; // search key | |
var value = p["value"]; // search value | |
var select = {}; | |
if (typeof view === 'string') { | |
select["view"] = view; | |
} | |
base = new Airtable({ apiKey: apiKey }).base(baseId); | |
var allRecords = []; | |
base(tableName).select(select).eachPage(function page(records, fetchNextPage) { | |
if (typeof key === 'string' && typeof value === 'string') { | |
allRecords = allRecords.concat(records.filter(function(item) { | |
return item.get(key) === value | |
})); | |
} else { | |
allRecords = allRecords.concat(records); | |
} | |
// To fetch the next page of records, call `fetchNextPage`. | |
// If there are more records, `page` will get called again. | |
// If there are no more records, `done` will get called. | |
fetchNextPage(); | |
}, function done(error) { | |
if (error) { | |
console.log(error); | |
hook.res.end(JSON.stringify({'success': false, 'message': error})); | |
return; | |
} | |
hook.res.end(JSON.stringify({'success': true, 'records': allRecords})); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment