Middleware

Dubnium has built-in Express middleware functions to manage Records. Data is never sent to the client without your permission.

Parameters

If tag.key or content.key is omitted, the from value is used.

Parameter
About
Type
Required

tag

First param to set tag info.

Object

tag.from

Part of the req object to get the tag from (e.g. headers or body)

String

tag.key

Key from the from object to get tag from.

String

content

Second param to set content info

Object

content.from

Part of the req object to get the content from (e.g. headers or body)

String

content.key

Key from the from object to get tag from.

String

Methods

Create

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
})

Get

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

DB

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

Delete

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

Edit

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

Other

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

middleware.js
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!')
})

Last updated