Giter Site home page Giter Site logo

Allow nested serialization about eve HOT 9 CLOSED

pyeve avatar pyeve commented on July 23, 2024
Allow nested serialization

from eve.

Comments (9)

nicolaiarocci avatar nicolaiarocci commented on July 23, 2024

Hello and thanks for your suggestion.

MongoDB doesn't provide a native referential integrity/FK mechanism so something like this would need to be abstracted at the application level. Fortunately Eve already supports a data_relation keyword, which allows to (basically) define a RI rule. You would have something like:

DOMAIN = {
     'letter': {
         'schema': {
             author: {
                 'type': 'objectid', 
                 'data_relation': {
                     'collection': 'user', 
                     'field': '_id', 
                     'embed': True
                  },
             },
             subject: {'type': 'string'},
             body: {'type': 'string'}, 
         }
     },

This being said, since there are no JOINS in the MONGO world, such a mechanism would cause a double lookup on the database, for each GET on the resource endpoint. The natural Mongo way to solve this kind of scenario is to just embed the documents within the main document. As a matter of fact, Implementing the embed feature would make Eve something closer to an ORM, but I see the usefulness of such a feature. Thoughts?

from eve.

oriolrius avatar oriolrius commented on July 23, 2024

Hello, IMHO this is very useful because in some schemas it's necessary to do
'application JOINs' embed documents it's not always a good idea for some
operations. There are several scenarios where this is a MUST.

Best.

Oriol Rius

from eve.

bcattle avatar bcattle commented on July 23, 2024

That's a fair point, regarding ORM. I wonder if this also has negative
RESTful-ness, since there are two ways to access a document: through a
canonical URL (/posts//) and additionally through a collection
(/tags/football/). If a link is broken via the collection URL this doesn't
mean that the entity is gone, merely that it is no longer in that
collection. Since applications could (incorrectly) rely on the "collection
URL" maybe this is best left out.
On Jul 23, 2013 11:41 PM, "Nicola Iarocci" [email protected] wrote:

Hello and thanks for your suggestion.

MongoDB doesn't provide a native referential integrity/FK mechanism so
something like this would need to be abstracted at the application level.
Fortunately Eve already supports a data_relation keyword, which allows to
(basically) define a RI rule. You would have something like:

DOMAIN = {
'letter': {
'schema': {
author: {
'type': 'objectid',
'data_relation': {
'collection': 'user',
'field': '_id',
'embed': True
},
},
subject: {'type': 'string'},
body: {'type': 'string'},
}
},

This being said, since there are no JOINS in the MONGO world, such a
mechanism would cause a double lookup on the database, for each GET on the
resource endpoint. The natural Mongo way to solve this kind of scenarios,
is to just embed the documents within the main document. As a matter of
fact, Implementing the embed feature would make Eve something closer to
an ORM, but I see the usefulness of such a feature. Any thoughts?


Reply to this email directly or view it on GitHubhttps://github.com/nicolaiarocci/eve/issues/68#issuecomment-21466612
.

from eve.

nicolaiarocci avatar nicolaiarocci commented on July 23, 2024

An interesting alternative would be to grant clients the power to activate document embedding on per-request basis, by means of a query parameter (or maybe a custom header). A GET like this /invoices/?embedded={"user":1} would return a fully embedded user record whereas the same request without the embedded argument would just return the user ObjectId. The API maintainer could still control what gets embedded:

DOMAIN = {
     'letter': {
         'schema': {
             author: {
                 'type': 'objectid', 
                 'data_relation': {
                     'collection': 'user', 
                     'field': '_id', 
                     'embeddable': True
                  },
             },
             subject: {'type': 'string'},
             body: {'type': 'string'}, 
         }
     }

By default, embeddable would be False (which basically means that client-controlled document embedding is disabled, preserving the current behaviour). This is of course just a raw draft inspired by @bcattle suggestion, what do you guys think about it?

from eve.

nicolaiarocci avatar nicolaiarocci commented on July 23, 2024

Of course, a mix of the two solutions is also workable (explicitly embed a linked document in the settings while setting embeddable proprierty on other linked fields).

from eve.

bcattle avatar bcattle commented on July 23, 2024

Thanks for the good suggestions Nicola. I like the query parameter idea, because it's consistent with the projection operator, i.e. /people/?projection={"lastname": 1, "born": 1}

This sidesteps the ORM issue by allowing the client to request a particular representation of the data. Just as it does when requesting XML or JSON, all fields or only some.

from eve.

bcattle avatar bcattle commented on July 23, 2024

Just an update, I have this working experimentally for the case of a one-to-one relationship. I need to write the unit tests. If there's interest I can extend it to one-to-many relationships as well.

from eve.

wbashir avatar wbashir commented on July 23, 2024

@bcattle +1

from eve.

nicolaiarocci avatar nicolaiarocci commented on July 23, 2024

@bcattle +1 go ahead man, cant' wait!

from eve.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.