# Middleware

## Setup

{% code lineNumbers="true" %}

```javascript
const dbMiddleware = require('dubnium/middleware')
const dubnium = require('dubnium')
const db = new dubnium(...)
const middleware = dbMiddleware(db)
```

{% endcode %}

## Parameters

{% hint style="info" %}
If `tag.key` or `content.key` is omitted, the `from` value is used.
{% endhint %}

<table><thead><tr><th>Parameter</th><th>About</th><th>Type</th><th data-type="checkbox">Required</th></tr></thead><tbody><tr><td>tag</td><td>First param to set tag info.</td><td>Object</td><td>true</td></tr><tr><td>tag.from</td><td>Part of the <code>req</code> object to get the tag from (e.g. <code>headers</code> or <code>body</code>)</td><td>String</td><td>true</td></tr><tr><td>tag.key</td><td>Key from the <code>from</code> object to get tag from.</td><td>String</td><td>false</td></tr><tr><td>content</td><td>Second param to set content info</td><td>Object</td><td>true</td></tr><tr><td>content.from</td><td>Part of the <code>req</code> object to get the content from (e.g. <code>headers</code> or <code>body</code>)</td><td>String</td><td>true</td></tr><tr><td>content.key</td><td>Key from the <code>from</code> object to get tag from.</td><td>String</td><td>false</td></tr></tbody></table>

## Methods

### Create

{% code lineNumbers="true" %}

```javascript
app.get('/new', middleware.create({ from:"query", key:"tag" }, { from:"query", key:"content" }), (req, res) => {
// Creates a record with the tag and content from the query
})
```

{% endcode %}

### Get

{% code lineNumbers="true" %}

```javascript
app.get('/:tag', middleware.get({ from:"params", key:"tag" }), (req, res) => {
// Access the record directly from req.record, if it exists
})
```

{% endcode %}

### DB

{% code lineNumbers="true" %}

```javascript
app.get('/db', middleware.db(), (req, res) => {
// Access the database directly from req.db
})
```

{% endcode %}

### Delete

{% code lineNumbers="true" %}

```javascript
app.get('/', middleware.delete({ from:"headers", key:"id" }), (req, res) => {
    res.send('Account deleted.')
})
```

{% endcode %}

### Edit

{% code lineNumbers="true" %}

```javascript
app.get('/', middleware.edit({ from:"body" }, { from:"content" }), (req, res) => {
    res.send('Account edited!')
})
```

{% endcode %}

### Other

{% code lineNumbers="true" %}

```javascript
app.get('/', middleware.other('function', ...args), (req, res) => {
    res.send(`Ran ${req.db.function} with args ${req.db.args}`)
    // req.db['function'] is the requested function
    // req.db.function is the name of the requested function
    // req.db.args is the requested args
    // req.db.result is the result of the function
})
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://db.coolstone.dev/4/advanced/middleware.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
