Skip to content

Commit

Permalink
updated the plugin section of the DB docs
Browse files Browse the repository at this point in the history
Signed-off-by: Fortune Ikechi <ikechifortune@gmail.com>
  • Loading branch information
iamfortune committed May 14, 2024
1 parent 513fadb commit 7e6a36f
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions docs/db/plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,19 @@ The paths are relative to the config file path.

Once the config file is set up, you can write your plugin to extend Platformatic DB API or write your custom business logic.

You should export an async `function` which receives a parameters
- `app` (`FastifyInstance`) that is the main fastify [instance](https://www.fastify.io/docs/latest/Reference/Server/#instance) running Platformatic DB
- `opts` all the options specified in the config file after `path`
You should export an async `function` which receives a parameters:

- `app` (`FastifyInstance`) the main Fastify [instance](https://www.fastify.io/docs/latest/Reference/Server/#instance) running Platformatic DB.
- `opts` all the options specified in the config file after `path`.
-
You can always access Platformatic [data mapper](/reference/sql-mapper/introduction.md) through `app.platformatic` property.
You can always access Platformatic [data mapper](../packages/sql-mapper/overview.md) through `app.platformatic` property.


## Context Integration in Plugin Operations

:::info
To make sure that a user has the appropriate set of permissions to perform any action on an entity the `context` should be passed to the `entity mapper` operation like this:
To ensure robust authorization and data management, it's important to pass the `context` object to the `entity mapper`. This `context` includes user-specific data, permissions, and other parameters that influence how data operations are executed.

Here's how you can integrate context into your plugin:

```js
app.post('/', async (req, reply) => {
Expand All @@ -37,13 +42,12 @@ app.post('/', async (req, reply) => {
})
})
```
:::

Check some [examples](/guides/add-custom-functionality/introduction.md).

## Hot Reload

Plugin file is being watched by [`fs.watch`](https://nodejs.org/api/fs.html#fspromiseswatchfilename-options) function.
Plugin files are monitored by the [`fs.watch`](https://nodejs.org/api/fs.html#fspromiseswatchfilename-options) function.

You don't need to reload Platformatic DB server while working on your plugin. Every time you save, the watcher will trigger a reload event and the server will auto-restart and load your updated code.

Expand Down Expand Up @@ -72,7 +76,7 @@ Consider the following directory structure:
```

By default the folder will be added as a prefix to all the routes defined within them.
See the autoload documentation for all the options to customize this behavior.
See the [autoload documentation](../runtime/configuration.md#autoload) for all the options to customize this behavior.

## Multiple plugins

Expand All @@ -90,7 +94,6 @@ Multiple plugins can be loaded in parallel by specifying an array:
}
}
```
fastify.swagger()

## TypeScript and autocompletion

Expand All @@ -115,6 +118,6 @@ export default async function (fastify: FastifyInstance, opts: FastifyPluginOpti
}
```

Note that you need to add the `"plugins": { "typescript": true }` configuration to your `platformatic.service.json`.
Note that you need to add the `"plugins": { "typescript": true }` configuration to your `platformatic.json`.

<Issues />

0 comments on commit 7e6a36f

Please sign in to comment.