在 REST 中查询时挂钩过于繁琐。有任何想法吗?
Too tidious hooks when querying in REST. Any ideas?
我刚刚开始使用 feathers 构建 REST 服务器。我需要您的帮助来查询技巧。文档说
When used via REST URLs all query values are strings. Depending on the service the values in params.query might have to be converted to the right type in a before hook. (https://docs.feathersjs.com/api/databases/querying.html)
,这让我很困惑。 find({query: {value: 1} })
是指 value === "1"
而不是 value === 1
吗?这是令我困惑的示例客户端代码:
const feathers = require('@feathersjs/feathers')
const fetch = require('node-fetch')
const restCli = require('@feathersjs/rest-client')
const rest = restCli('http://localhost:8888')
const app = feathers().configure(rest.fetch(fetch))
async function main () {
const Items = app.service('myitems')
await Items.create( {name:'one', value:1} )
//works fine. returns [ { name: 'one', value: 1, id: 0 } ]
console.log(await Items.find({query:{ name:"one" }}))
//wow! no data returned. []
console.log(await Items.find({query:{ value:1 }})) // []
}
main()
服务器端代码在这里:
const express = require('@feathersjs/express')
const feathers = require('@feathersjs/feathers')
const memory = require('feathers-memory')
const app = express(feathers())
.configure(express.rest())
.use(express.json())
.use(express.errorHandler())
.use('myitems', memory())
app.listen(8888)
.on('listening',()=>console.log('listen on 8888'))
我制作了挂钩,效果很好,但是太整洁了,我想我漏掉了什么。有什么想法吗?
挂钩代码:
app.service('myitems').hooks({
before: { find: async (context) => {
const value = context.params.query.value
if (value) context.params.query.value = parseInt(value)
return context
}
}
})
此行为取决于您使用的数据库和 ORM。一些具有模式(如 feathers-mongoose
、feathers-sequelize
和 feathers-knex
)的值将自动转换为这样的值。
Feathers 本身并不知道您的数据格式,并且大多数适配器(例如您在此处使用的 feathers-memory
)会进行严格比较,因此必须对其进行转换。处理这个问题的通常方法是创建一些可重用的钩子(而不是每个字段一个),如下所示:
const queryToNumber = (...fields) => {
return context => {
const { params: { query = {} } } = context;
fields.forEach(field => {
const value = query[field];
if(value) {
query[field] = parseInt(value, 10)
}
});
}
}
app.service('myitems').hooks({
before: {
find: [
queryToNumber('age', 'value')
]
}
});
或使用 JSON schema e.g. through the validateSchema common hook.
我刚刚开始使用 feathers 构建 REST 服务器。我需要您的帮助来查询技巧。文档说
When used via REST URLs all query values are strings. Depending on the service the values in params.query might have to be converted to the right type in a before hook. (https://docs.feathersjs.com/api/databases/querying.html)
,这让我很困惑。 find({query: {value: 1} })
是指 value === "1"
而不是 value === 1
吗?这是令我困惑的示例客户端代码:
const feathers = require('@feathersjs/feathers')
const fetch = require('node-fetch')
const restCli = require('@feathersjs/rest-client')
const rest = restCli('http://localhost:8888')
const app = feathers().configure(rest.fetch(fetch))
async function main () {
const Items = app.service('myitems')
await Items.create( {name:'one', value:1} )
//works fine. returns [ { name: 'one', value: 1, id: 0 } ]
console.log(await Items.find({query:{ name:"one" }}))
//wow! no data returned. []
console.log(await Items.find({query:{ value:1 }})) // []
}
main()
服务器端代码在这里:
const express = require('@feathersjs/express')
const feathers = require('@feathersjs/feathers')
const memory = require('feathers-memory')
const app = express(feathers())
.configure(express.rest())
.use(express.json())
.use(express.errorHandler())
.use('myitems', memory())
app.listen(8888)
.on('listening',()=>console.log('listen on 8888'))
我制作了挂钩,效果很好,但是太整洁了,我想我漏掉了什么。有什么想法吗?
挂钩代码:
app.service('myitems').hooks({
before: { find: async (context) => {
const value = context.params.query.value
if (value) context.params.query.value = parseInt(value)
return context
}
}
})
此行为取决于您使用的数据库和 ORM。一些具有模式(如 feathers-mongoose
、feathers-sequelize
和 feathers-knex
)的值将自动转换为这样的值。
Feathers 本身并不知道您的数据格式,并且大多数适配器(例如您在此处使用的 feathers-memory
)会进行严格比较,因此必须对其进行转换。处理这个问题的通常方法是创建一些可重用的钩子(而不是每个字段一个),如下所示:
const queryToNumber = (...fields) => {
return context => {
const { params: { query = {} } } = context;
fields.forEach(field => {
const value = query[field];
if(value) {
query[field] = parseInt(value, 10)
}
});
}
}
app.service('myitems').hooks({
before: {
find: [
queryToNumber('age', 'value')
]
}
});
或使用 JSON schema e.g. through the validateSchema common hook.