Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

0.15.0

Choose a tag to compare

@etsuo etsuo released this 06 May 22:50
· 67 commits to develop since this release
01a231f

See Milestone 0.15.0

New Features

#184 - It is now possible to apply projection when converting a model toJson

This is preferable in cases where you want to present projection as part of your API, but you need various fields to be present regardless of the API consumer's projection so you can't pass the API consumer's projection directly to the DB or you might end up missing fields you need. For example, the API consumer is probably not concerned with what fields are necessary to support security, so it'd be a bother to force them to include those relevant fields in their projections.

Projection is defined in an IProjection document that's passed to toJson via the IContext.projection field. It is completely optional.

An IProjection can selectively exclude certain fields. For example:

{
    id: 0,
    user: {
        lastName: 0
    }
}
// note you can use `0` or false or `1` or `true`

The above will exclude the id field from the parent document and the lastName field from the sub document user.

If you explicitly include a field, then you must select all the fields you want at that level. For example:

{
     purchases: 1,
     user:1
}

The above document would include the purchases field from the parent document, and would include the sub document user, but user will include all of its fields. Explicitivity is defined at each document level (i.e., parent, sub document, sub sub document, etc.).

Assuming the first and the second projections were from the same document source, the second would not include the id field, but it would include everything in the user document.

Note: unlike MongoDB projection, toJson does not automatically include id (_id).

#186 buildProjectionFromQuery added as a utility function to facilitate support for #184

with buildProjectionFromQuery you can build an IProjection from a query string that is passed in as a JSON parsable IProjection object or a projection array. Projection arrays follow these rules:

  • They are string arrays
  • You explicitly include a field by adding its name to the array. E.g., ['name']
  • You exclude a field by adding a - to its name in the array. E.g., ['-name']
  • You can control the projection of sub documents with dot notation. E.g., ['id', 'user.firstName', 'user.lastName'], would include the first and last name fields of the user sub document.

If a query passed in is not parsable, InvalidQueryError will be thrown (message: invalid_query).

Field names are @Json field names, not @Db field names.