异步而不是连接——我在作弊吗?
Async instead of joins - am I cheating?
我一直在尝试在 postgres 中使用 json_agg 复制这组异步并行查询。
在我心里,虽然异步方法得到了我想要的结果,但我觉得这是作弊,将来会让我痛苦。
下面包含了我当前实施的结果。我怎样才能用 json_agg 达到同样的效果?
快递路线
router.get('/', function(req, res) {
async.parallel({
records: function(parCb) {
var query = knex('records').select('title','id').orderBy(knex.raw('RANDOM()')).limit(5)
query.then(function(results) {
parCb(null, results);
});
},
collections: function(parCb) {
var query = knex('collections').select('name','id').orderBy('name')
query.then(function(results){
console.log(results)
parCb(null, results);
});
},
},
function(err, results) {
res.render('index.html', {
title: 'Welcome',
data: results
});
});
});
输出
{ collection:
{ id: 31,
slug: 'BAR',
name: 'Barker',
long_name: 'Barker',
copyright: '',
description: null,
notes: '',
createdAt: Tue Jan 05 2016 16:47:35 GMT+0000 (UTC),
updatedAt: Tue Jan 05 2016 15:32:55 GMT+0000 (UTC) },
records:
[ { title: 'Whiddon Down: general view, Tawton, South',
id: 12595 },
{ title: 'STOKE IN TEIGNHEAD', id: 13937 },
{ title: 'Teign Estuary', id: 104573 },
{ title: 'Lydford Village', id: 106650 },
{ title: 'Metheral hut circle before submersion by Fernworthy Reservoir',
id: 1467 } ] }
首先,不要混用异步库和 promises。这样可以避免不必要的痛苦。
如果您使用的库之一是基于 promises(如 knex),我建议您放弃异步,使用合适的 promise 库(如 Bluebird)并使用它。
var Promise = require('bluebird');
var knex = require('knex');
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
Promise.all([
knex('records').select('title','id').orderBy(knex.raw('RANDOM()')).limit(5),
knex('collections').select('name','id').orderBy('name')
]).then(function (results) {
res.render('index.html', {
title: 'Welcome',
data: {
records: results[0],
collections: results[1]
}
});
});
});
不过,关于 json_agg
恐怕我不能说太多。
我一直在尝试在 postgres 中使用 json_agg 复制这组异步并行查询。
在我心里,虽然异步方法得到了我想要的结果,但我觉得这是作弊,将来会让我痛苦。
下面包含了我当前实施的结果。我怎样才能用 json_agg 达到同样的效果?
快递路线
router.get('/', function(req, res) {
async.parallel({
records: function(parCb) {
var query = knex('records').select('title','id').orderBy(knex.raw('RANDOM()')).limit(5)
query.then(function(results) {
parCb(null, results);
});
},
collections: function(parCb) {
var query = knex('collections').select('name','id').orderBy('name')
query.then(function(results){
console.log(results)
parCb(null, results);
});
},
},
function(err, results) {
res.render('index.html', {
title: 'Welcome',
data: results
});
});
});
输出
{ collection:
{ id: 31,
slug: 'BAR',
name: 'Barker',
long_name: 'Barker',
copyright: '',
description: null,
notes: '',
createdAt: Tue Jan 05 2016 16:47:35 GMT+0000 (UTC),
updatedAt: Tue Jan 05 2016 15:32:55 GMT+0000 (UTC) },
records:
[ { title: 'Whiddon Down: general view, Tawton, South',
id: 12595 },
{ title: 'STOKE IN TEIGNHEAD', id: 13937 },
{ title: 'Teign Estuary', id: 104573 },
{ title: 'Lydford Village', id: 106650 },
{ title: 'Metheral hut circle before submersion by Fernworthy Reservoir',
id: 1467 } ] }
首先,不要混用异步库和 promises。这样可以避免不必要的痛苦。
如果您使用的库之一是基于 promises(如 knex),我建议您放弃异步,使用合适的 promise 库(如 Bluebird)并使用它。
var Promise = require('bluebird');
var knex = require('knex');
var express = require('express');
var router = express.Router();
router.get('/', function(req, res) {
Promise.all([
knex('records').select('title','id').orderBy(knex.raw('RANDOM()')).limit(5),
knex('collections').select('name','id').orderBy('name')
]).then(function (results) {
res.render('index.html', {
title: 'Welcome',
data: {
records: results[0],
collections: results[1]
}
});
});
});
不过,关于 json_agg
恐怕我不能说太多。