# Middleware

## 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', db.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

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

### DB

{% code lineNumbers="true" %}

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

{% endcode %}

### Delete

{% code lineNumbers="true" %}

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

{% endcode %}

### Edit

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

### Other

```javascript
app.get('/', db.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
})
```

## Full Example

{% code title="middleware.js" lineNumbers="true" %}

```javascript
const { Dubnium } = require('./v3')
const express = require('express')
const app = express()

const db = new Dubnium('./test_db', 'txt')

const c_m = db.middleware.create({ from:"query", key:"tag" }, { from:"query", key:"content" })
const g_m = db.middleware.get({ from:"params", key:"tag" })
const db_m = db.middleware.db()

app.get('/new', c_m, (req, res) => {
// Creates a record with the tag and content from the query
})

app.get('/:tag', g_m, (req, res) => {
// Access the record directly from req.record, if it exists
})

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

app.listen(3000, () => {
    console.log('Example app listening on port 3000!')
})
```

{% endcode %}
