From 543892daa62e79569d8d50083b28f6097dab3340 Mon Sep 17 00:00:00 2001 From: jlenon7 Date: Thu, 18 Apr 2024 15:10:54 +0100 Subject: [PATCH] feat(orm): change isHidden docs --- docs/orm/annotations.mdx | 16 +++++++++++++++- docs/orm/query-builder.mdx | 22 +++++++--------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/docs/orm/annotations.mdx b/docs/orm/annotations.mdx index c39c10cb..f42f3396 100644 --- a/docs/orm/annotations.mdx +++ b/docs/orm/annotations.mdx @@ -118,13 +118,27 @@ public id: number > Default: `false` -Set if the column should be hidden when retrieving it from database: +Set if the field should be hidden when executing the +`toJSON()` method of the model: ```typescript @Column({ isHidden: true }) public password: string ``` +:::tip + +To force return hidden fields in `toJSON()` calls, +use the `withHidden` option: + +```typescript +const flight = await Flight.find() + +const flightJson = flight.toJSON({ withHidden: true }) +``` + +::: + ### `isUnique` > Default: `false` diff --git a/docs/orm/query-builder.mdx b/docs/orm/query-builder.mdx index 17fec944..b429e246 100644 --- a/docs/orm/query-builder.mdx +++ b/docs/orm/query-builder.mdx @@ -58,7 +58,7 @@ You may use any of these methods when writing your model queries. ::: -### Hiding fields +### Hidding fields Sometimes you might need to hide some sensitive field from your model queries, to do so, you can set the `isHidden` property to true in your @@ -75,29 +75,21 @@ export class User extends BaseModel { } ``` -Everytime you call the `query()` method of your models, Athenna will -automatically select all the columns from your model but never the -ones where the `isHidden` property is true. +Everytime you call the `toJSON()` method of your models, Athenna will +ignore all the hidden columns from your model and return the rest. #### Retrieve hidden fields Is possible to bypass the `isHidden` validation using the -`select()` method from the query builder: +`withHidden` option of the `toJSON()` method: ```typescript -const { password } = await User.query() +const user = await User.query() .select('id') - .select('password') 👈 + .select('password') .find() -``` - -If you wish to get all the hidden fields for a specify use case, you -can use the `withHidden()` method: -```typescript -const { password } = await User.query() - .withHidden() - .find() +const { password } = user.toJSON({ withHidden: true }) 👈 ``` ## Collections