Last active
June 8, 2016 04:31
-
-
Save ritch/8036077 to your computer and use it in GitHub Desktop.
LoopBack w/ a co-routine based API
This file contains hidden or 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
var co = require('co'); | |
var thunk = require('thunkify'); | |
var loopback = require('loopback'); | |
var memory = loopback.memory(); | |
var product = memory.createModel('memory'); | |
// thunkify the loopback model | |
thunkify(); | |
// create some products in parallel | |
// then find them all | |
co(function *() { | |
var max = 99; | |
var thunks = []; | |
while(max--) { | |
thunks.push( | |
product.create({ | |
name: 'product-' + max, | |
price: Math.floor(Math.random() * 10000) / 100 | |
}) | |
) | |
} | |
yield thunks; | |
console.log('Products (%s):', yield product.count()); | |
console.log(yield product.find()); | |
})(); | |
function thunkify() { | |
var methods = [ | |
"validate", | |
"create", | |
"upsert", | |
"exists", | |
"findById", | |
"find", | |
"findOne", | |
"destroyAll", | |
"remove", | |
"destroyById", | |
"deleteById", | |
"removeById", | |
"count", | |
"include" | |
]; | |
methods.forEach(function(name){ | |
if (!product[name]) return; | |
product[name] = thunk(product[name]); | |
}); | |
} |
This file contains hidden or 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
Products (99): | |
[ { id: 1, name: 'product-98', price: 28.13 }, | |
{ id: 2, name: 'product-97', price: 40.92 }, | |
{ id: 3, name: 'product-96', price: 25.68 }, | |
{ id: 4, name: 'product-95', price: 46.95 }, | |
{ id: 5, name: 'product-94', price: 58.01 }, | |
{ id: 6, name: 'product-93', price: 25.91 }, | |
{ id: 7, name: 'product-92', price: 99.22 }, | |
{ id: 8, name: 'product-91', price: 19.17 }, | |
{ id: 9, name: 'product-90', price: 39.54 }, | |
{ id: 10, name: 'product-89', price: 31.44 }, | |
{ id: 11, name: 'product-88', price: 48.96 }, | |
{ id: 12, name: 'product-87', price: 48.27 }, | |
{ id: 13, name: 'product-86', price: 24.58 }, | |
{ id: 14, name: 'product-85', price: 26.57 }, | |
{ id: 15, name: 'product-84', price: 76.91 }, | |
{ id: 16, name: 'product-83', price: 34.65 }, | |
{ id: 17, name: 'product-82', price: 19.35 }, | |
{ id: 18, name: 'product-81', price: 4.69 }, | |
{ id: 19, name: 'product-80', price: 1.91 }, | |
{ id: 20, name: 'product-79', price: 68.23 }, | |
{ id: 21, name: 'product-78', price: 25.33 }, | |
{ id: 22, name: 'product-77', price: 27.68 }, | |
{ id: 23, name: 'product-76', price: 40.65 }, | |
{ id: 24, name: 'product-75', price: 48.55 }, | |
{ id: 25, name: 'product-74', price: 6.24 }, | |
{ id: 26, name: 'product-73', price: 1.28 }, | |
{ id: 27, name: 'product-72', price: 6.59 }, | |
{ id: 28, name: 'product-71', price: 52.26 }, | |
{ id: 29, name: 'product-70', price: 51.41 }, | |
{ id: 30, name: 'product-69', price: 61.54 }, | |
{ id: 31, name: 'product-68', price: 30.63 }, | |
{ id: 32, name: 'product-67', price: 42.57 }, | |
{ id: 33, name: 'product-66', price: 16.03 }, | |
{ id: 34, name: 'product-65', price: 65.12 }, | |
{ id: 35, name: 'product-64', price: 18.9 }, | |
{ id: 36, name: 'product-63', price: 8.79 }, | |
{ id: 37, name: 'product-62', price: 77.51 }, | |
{ id: 38, name: 'product-61', price: 94.44 }, | |
{ id: 39, name: 'product-60', price: 40.79 }, | |
{ id: 40, name: 'product-59', price: 32.34 }, | |
{ id: 41, name: 'product-58', price: 9.29 }, | |
{ id: 42, name: 'product-57', price: 19.71 }, | |
{ id: 43, name: 'product-56', price: 56.94 }, | |
{ id: 44, name: 'product-55', price: 95.82 }, | |
{ id: 45, name: 'product-54', price: 57.08 }, | |
{ id: 46, name: 'product-53', price: 56.75 }, | |
{ id: 47, name: 'product-52', price: 32.74 }, | |
{ id: 48, name: 'product-51', price: 58.6 }, | |
{ id: 49, name: 'product-50', price: 30.93 }, | |
{ id: 50, name: 'product-49', price: 11.52 }, | |
{ id: 51, name: 'product-48', price: 90.34 }, | |
{ id: 52, name: 'product-47', price: 69.98 }, | |
{ id: 53, name: 'product-46', price: 82.29 }, | |
{ id: 54, name: 'product-45', price: 71.84 }, | |
{ id: 55, name: 'product-44', price: 31.41 }, | |
{ id: 56, name: 'product-43', price: 63.31 }, | |
{ id: 57, name: 'product-42', price: 89.54 }, | |
{ id: 58, name: 'product-41', price: 67.08 }, | |
{ id: 59, name: 'product-40', price: 84.62 }, | |
{ id: 60, name: 'product-39', price: 10.17 }, | |
{ id: 61, name: 'product-38', price: 66.66 }, | |
{ id: 62, name: 'product-37', price: 78.48 }, | |
{ id: 63, name: 'product-36', price: 46.19 }, | |
{ id: 64, name: 'product-35', price: 10.62 }, | |
{ id: 65, name: 'product-34', price: 64.9 }, | |
{ id: 66, name: 'product-33', price: 32.63 }, | |
{ id: 67, name: 'product-32', price: 43.96 }, | |
{ id: 68, name: 'product-31', price: 3.5 }, | |
{ id: 69, name: 'product-30', price: 53.54 }, | |
{ id: 70, name: 'product-29', price: 46.29 }, | |
{ id: 71, name: 'product-28', price: 60.26 }, | |
{ id: 72, name: 'product-27', price: 94.06 }, | |
{ id: 73, name: 'product-26', price: 58.41 }, | |
{ id: 74, name: 'product-25', price: 6.96 }, | |
{ id: 75, name: 'product-24', price: 57.55 }, | |
{ id: 76, name: 'product-23', price: 91.86 }, | |
{ id: 77, name: 'product-22', price: 0.05 }, | |
{ id: 78, name: 'product-21', price: 31.79 }, | |
{ id: 79, name: 'product-20', price: 11.48 }, | |
{ id: 80, name: 'product-19', price: 2.9 }, | |
{ id: 81, name: 'product-18', price: 21.55 }, | |
{ id: 82, name: 'product-17', price: 9.2 }, | |
{ id: 83, name: 'product-16', price: 34.38 }, | |
{ id: 84, name: 'product-15', price: 37.41 }, | |
{ id: 85, name: 'product-14', price: 18.85 }, | |
{ id: 86, name: 'product-13', price: 34.57 }, | |
{ id: 87, name: 'product-12', price: 99.93 }, | |
{ id: 88, name: 'product-11', price: 24.16 }, | |
{ id: 89, name: 'product-10', price: 80.29 }, | |
{ id: 90, name: 'product-9', price: 91.32 }, | |
{ id: 91, name: 'product-8', price: 24.5 }, | |
{ id: 92, name: 'product-7', price: 14.42 }, | |
{ id: 93, name: 'product-6', price: 37.19 }, | |
{ id: 94, name: 'product-5', price: 26.1 }, | |
{ id: 95, name: 'product-4', price: 55.39 }, | |
{ id: 96, name: 'product-3', price: 64.54 }, | |
{ id: 97, name: 'product-2', price: 78.81 }, | |
{ id: 98, name: 'product-1', price: 54.25 }, | |
{ id: 99, name: 'product-0', price: 2.3 } ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment