nitrojs / nitrojs/nitro

Allow conigure local custom storage drivers

Open
#530 8 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement p2-nice-to-have storage v2
Dominant language
TypeScript
Stars
11.2k
Forks
899
Avg merge
2d 24m
Merged PRs (30d)
40

Description

Environment

nitropack@0.4.24
$ node --version
v16.17.0

Reproduction

Follow the example of unstorage https://github.com/unjs/unstorage#making-custom-drivers
I used the original memory driver as "custom driver"

import { defineDriver } from './utils'
import type { StorageValue } from '../types'

export default defineDriver(() => {
  const data = new Map<string, StorageValue>()

  return {
    hasItem (key) {
      return data.has(key)
    },
    getItem (key) {
      return data.get(key) || null
    },
    setItem(key, value) {
      data.set(key, value)
    },
    removeItem (key) {
      data.delete(key)
    },
    getKeys() {
      return Array.from(data.keys())
    },
    clear() {
      data.clear()
    },
    dispose() {
      data.clear()
    }
  }
})

Describe the bug

The current interface for storage options is:

interface StorageMounts {
    [path: string]: {
        driver: BuiltinDriverName | CustomDriverName;
        [option: string]: any;
    };
}

but unstorage allows the use of custom drivers and we are not using them.

Additional context

I found https://github.com/unjs/nitro/blob/main/src/rollup/plugins/storage.ts
on line 47

${driverImports.map(i => genImport(i, genSafeVariableName(i))).join('\n')}
I am not quite sure if that means we should do the following

storage:{
  db: {
    driver: "../../../../CosmoDriver.js",
    options: {
     // my driver options 
    },
  },
}

This kind of complex and error prone
We should allow the user to pass in the driver directly and just
driver:new Driver({})

Logs

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with src/rollup/plugins/storage.ts, especially the driverImports generation around line 47, and trace how StorageMounts and custom drivers are currently resolved. Compare that flow with the unstorage custom-driver example in the issue; the work is done when users can configure a custom driver directly without relying on complex generated path imports.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.