sequiturs / bookshelf-advanced-serialization Goto Github PK
View Code? Open in Web Editor NEWPlugin for Bookshelf.js supporting advanced serialization behavior
License: MIT License
Plugin for Bookshelf.js supporting advanced serialization behavior
License: MIT License
If contextDesignator
does not return a designation, we could default to:
- for ensureRelationsLoaded
, an empty array
- for contextSpecificVisibleProperties
, the visible properties determined by the role
Currently, if the caller uses context designations in their specification of ensureRelationsLoaded
or contextSpecificVisibleProperties
, those context designations must be complete in the sense of covering all possible models with the given table name (otherwise an error is thrown, cf.
bookshelf-advanced-serialization/lib/index.js
Line 429 in 94afe94
bookshelf-advanced-serialization/lib/index.js
Line 501 in 94afe94
Venue
.forge({}, {
accessor: req.user
})
.fetchAll({
withRelated: ['bars']
})
.then(venues => {
return venues.toJSON();
})
.then(res.json.bind(res));
The accessor has sufficient privileges to see the related 'bars' model, however, the bar does not appear in the serialization. However, the following code does return the related bars.
Venue
.forge({}, {
accessor: req.user
})
.fetchAll({
withRelated: ['bars']
})
.then(venues => Promise.all(venues.map(v => v.related('bars')
.toJSON())))
.then(res.json.bind(res))
Any insights on why the related model wouldn't make it into the serialization?
Thanks
(I've really enjoyed using this package, btw; it's refreshingly well-documented.)
This would be more efficient than the way we currently do it, where we load whatever relations the caller specified in ensureRelationsLoaded
and then remove them if they're not in the list of contextSpecificVisibleFields
.
Or we could potentially throw a sanity error to tell the caller that they instructed to load a relation which they also instructed not to be visible.
The url is returning {"error":"Server error."}
Currently ensureRelationsLoaded
is evaluated only as part of model.toJSON()
. That means each model in a collection will have its own JOIN
query executed to load the specified relation. This means n JOIN
s, where n is the length of the collection. This is inefficient. ensureRelationsLoaded
should make use of collection.load()
, so that only 1 JOIN
is performed.
This means making contextSpecificVisibleFields
support indexing merely by tableName
, with another level of indexing by designation optional.
Nice work on this, thank you!
Do you have plans to handle writes as well, or just keep the scope of the project to reads?
It seems like you use this in production already -- how does Sequiturs currently handle access control for writes?
Currently we only support passing the accessor
option to the Model constructor.
When both options are passed, shallow: true
should take priority since it is a standard Bookshelf option. Furthermore it should cause all ensureRelationsLoaded
behavior to be avoided, since there's no reason to do unnecessary work loading relations that aren't going to be serialized--unless plugin option ensureRelationsVisibleAndInvisible
is passed.
Use "properties" instead of "fields". "properties" refers to the properties of the serialized object, which in Bookshelf's terminology are known as attributes and/or relations. There's no way to distinguish attributes from relations on a serialized object, so the more generic "properties" term is more appropriate.
Consider whether there are better names for accessor
, roleDeterminer
, rolesToVisibleFields
, contextSpecificVisibleFields
, ensureRelationsLoaded
, _accessedAsRelationChain
, evaluator
.
evaluator
should probably be renamed to contextDesignator
.
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.