Skip to content

Commit

Permalink
docs(cli): Add documentation for .env (#2979)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl authored Jan 11, 2023
1 parent 54c883c commit 4b720f0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
54 changes: 18 additions & 36 deletions docs/api/schema/resolvers.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ import { Type } from '@feathersjs/typebox'
import type { Static } from '@feathersjs/typebox'
import type { HookContext } from '../declarations'


const messageSchema = Type.Object(
{
id: Type.Number(),
text: Type.String(),
createdAt: Type.Number(),
updatedAt: Type.Number(),
userId: Type.Number()
},
{ $id: 'Message', additionalProperties: false }
Expand All @@ -186,21 +186,28 @@ type MessageData = Static<typeof messageDataSchema>

// Resolver that automatically set `userId` and `createdAt`
const messageDataResolver = resolve<Message, HookContext>({
properties: {
userId: async (value, message, context) => {
// Associate the currently authenticated user
return context.params?.user.id
},
createdAt: async () => {
// Return the current date
return Date.now()
}
userId: async (value, message, context) => {
// Associate the currently authenticated user
return context.params?.user.id
},
createdAt: async () => {
// Return the current date
return Date.now()
}
})

// Resolver that automatically sets `updatedAt`
const messagePatchResolver = resolve<Message, HookContext>({
updatedAt: async () => {
// Return the current date
return Date.now()
}
})

app.service('users').hooks({
before: {
all: [schemaHooks.resolveData(messageDataResolver)]
create: [schemaHooks.resolveData(messageDataResolver)],
patch: [schemaHooks.resolveData(messagePatchResolver)]
}
})
```
Expand Down Expand Up @@ -357,28 +364,3 @@ app.service('users').hooks({
}
})
```

### resolveAll

The `resolveAll` hook combines the individual resolver hooks into a single easier to use format and must be used as an `around` hook. `create` takes separate resolver options for the `create`, `update` and `patch` method:

```ts
import { schemaHooks } from '@feathersjs/schema'

app.service('users').hooks({
around: {
all: [
schemaHooks.resolveAll({
dispatch: userDispatchResolver,
result: userResultResolver,
query: userQueryResolver,
data: {
create: userDataResolver,
update: userDataResolver,
patch: userPatchResolver
}
})
]
}
})
```
24 changes: 24 additions & 0 deletions docs/guides/cli/custom-environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,27 @@ This sets `app.get('port')` using the `PORT` environment variable (if it is avai
See the [node-config custom envrionment variable](https://github.com/node-config/node-config/wiki/Environment-Variables#custom-environment-variables) documentation for more information.

</BlockQuote>

## Dotenv

To add support for [dotenv](https://www.dotenv.org/) `.env` files run

```
npm install dotenv --save
```

And update `src/app.ts` as follows:

```ts
// dotenv replaces all environmental variables from ~/.env in ~/config/custom-environment-variables.json
import * as dotenv from 'dotenv'
dotenv.config()

import configuration from '@feathersjs/configuration'
```

<BlockQuote type="warning" label="important">

`dotenv.config()` needs to run _before_ `import configuration from '@feathersjs/configuration'`

</BlockQuote>

0 comments on commit 4b720f0

Please sign in to comment.