From 4b720f058855282adf44a05c1ae31132ab7a62e2 Mon Sep 17 00:00:00 2001 From: David Luecke Date: Tue, 10 Jan 2023 21:08:51 -0800 Subject: [PATCH] docs(cli): Add documentation for .env (#2979) --- docs/api/schema/resolvers.md | 54 +++++++------------ .../cli/custom-environment-variables.md | 24 +++++++++ 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/docs/api/schema/resolvers.md b/docs/api/schema/resolvers.md index 7922d95c53..13c8066bce 100644 --- a/docs/api/schema/resolvers.md +++ b/docs/api/schema/resolvers.md @@ -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 } @@ -186,21 +186,28 @@ type MessageData = Static // Resolver that automatically set `userId` and `createdAt` const messageDataResolver = resolve({ - 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({ + 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)] } }) ``` @@ -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 - } - }) - ] - } -}) -``` diff --git a/docs/guides/cli/custom-environment-variables.md b/docs/guides/cli/custom-environment-variables.md index fcc15b17d1..97a77188c0 100644 --- a/docs/guides/cli/custom-environment-variables.md +++ b/docs/guides/cli/custom-environment-variables.md @@ -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. + +## 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' +``` + +
+ +`dotenv.config()` needs to run _before_ `import configuration from '@feathersjs/configuration'` + +