i有一个Angular 4应用程序,其中包括一些feathersjs Services具有RethinkDB数据库适配器。
我是SQL持久性的新手。在SQL中,我将使用SELECT DISTICT ...从...中获取来自汽车舰队的"不同"汽车制造商,以使用户可以过滤此列表或组。
我如何在羽毛中得到这个?
我用rethinkdb数据库适配器的自定义挂钩解决了我的问题:
module.exports = function (options = {}) {
return function distinct(hook) {
let query = hook.params.query;
// Must be a before hook
if (hook.type !== 'before') {
throw new Error('El hook 'distinct' sólo puede usarse como hook 'before' (hooks.distinct).');
}
// Throw error when no field is provided - eg. just users?$distinct
if (query.$distinct === '') {
throw new Error('$distinct no encontrado: ¿Qué campo debería ser distinto? (hooks.distinct)');
}
let distinctValue = query.$distinct || null;
console.log('distinctValue', distinctValue);
if (distinctValue == null) return hook;
// Remove $distinct param from query (preventing errors)
delete query.$distinct;
// Add only the field we are searching for
query = Object.assign({ $select: distinctValue },query)
const rethinkdbQuery = this.createQuery(query);
const r = this.options.r;
// In our case, disable pagination
hook.params.paginate = false;
// Update the query with an additional `distinct` condition
hook.params.rethinkdb = rethinkdbQuery.distinct();
return hook;
}
};