eddyystop / feathers-test-populate-etc Goto Github PK
View Code? Open in Web Editor NEWWork in progress for a populate++ hook for Feathers
License: MIT License
Work in progress for a populate++ hook for Feathers
License: MIT License
include: { // Which child items to join to the parent item
post: { // This name is only used for some defaults
service: 'posts', // The service to populate from
parentField: 'postId', // The matching field in the parent. Supports dot notation a.b.c
childField: 'id', // The matching field in the child. Supports dot notation a.b.c
include: {
author: {
service: 'users',
parentField: 'author',
childField: 'id' // Would convert a.b.c to find({'a.b.c':value}). Use .query or .select for something else.
},
comment: {
service: 'comments',
parentField: 'id',
childField: 'postId',
select: (hook, parent) => ({ something: { $exists: false }}), // add to query using runtime data
nameAs: 'comments', // Parent prop name where to place the populated items
asArray: true, // store as an array if result has just 1 element
query: { // Normal feathers query syntax. Get selected fields from the last 5 comments
$limit: 5,
$select: ['title', 'content', 'postId'],
$sort: { createdAt: -1 }
},
},
readers: {
service: 'users',
parentField: 'readers', // This is an array, so id: { $in: { readers } } will be used.
childField: 'id'
}
}
}
}
But just one question: when nameAs
is absent, where to place the populated items in the parent? Say:
include: {
author: {
service: 'users',
parentField: 'author',
childField: 'id' // Would convert a.b.c to find({'a.b.c':value}). Use .query or .select for something else.
},
Here we get from service users
an item whose id
is the parent's author
, and in which field do we put this item in the parent? The doc didn't seem to make this clear.
select
and query
in the following part?comment: {
service: 'comments',
parentField: 'id',
childField: 'postId',
select: (hook, parent) => ({ something: { $exists: false }}), // add to query using runtime data
nameAs: 'comments', // Parent prop name where to place the populated items
asArray: true, // store as an array if result has just 1 element
query: { // Normal feathers query syntax. Get selected fields from the last 5 comments
$limit: 5,
$select: ['title', 'content', 'postId'],
$sort: { createdAt: -1 }
},
},
I understand the select
function returns an object that would be added into the query
object, so basically the two are one thing, only that select
is a function that accepting hook
and parent
as parameters so it is dynamic, but query
is static. Is this right?
If so, I don't think it's necessary to make it into two different settings. We could only have one setting, say, query
, and allow query
to be either a static object or a function that dynamically returns an object using runtime data.
BTW, what does parent
mean in select: (hook, parent) => ({ something: { $exists: false }})
? the parent service in the poplulation schema?
I am not familiar with feathers-permissions
so I didn't get how the permissions work. Any more examples?
I am not very sure what serialize
means here, either, since this term is never officially introduced in feathers docs. I found serializers
are excluding things from the result but populaters
are adding things into the result. Is that right?
It's wonderful that the client side could determine which popluation schema should be used in a find
opeartion. But the folloing syntax seems not very pretty:
favorites.find({
query: {
_clientParams: { // how client passes params to server
populate: 'standard', // Client wants favorites.standard populate. Supports dot notation a.b.c
serialize: 'standard', // Client wants favorites.standard serialize. Supports dot notation a.b.c
}
}
})
The _clientParams
doesn't look nice with an underscore ahead. Maybe better to use something like $params
. I understand currently only query
is sent back to server, so it is a pity that we are not able to make it look nice like this:
favorites.find({
query: { ...},
populate: 'standard', // Client wants favorites.standard populate. Supports dot notation a.b.c
serialize: 'standard', // Client wants favorites.standard serialize. Supports dot notation a.b.c
})
But even embedded in query
we still be able to make it look nicer like this:
favorites.find({
query: {
$populate: 'standard', // Client wants favorites.standard populate. Supports dot notation a.b.c
$serialize: 'standard', // Client wants favorites.standard serialize. Supports dot notation a.b.c
}
})
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.