Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow conigure local custom storage drivers #530

Open
mcremer-able opened this issue Sep 23, 2022 · 7 comments
Open

Allow conigure local custom storage drivers #530

mcremer-able opened this issue Sep 23, 2022 · 7 comments
Labels

Comments

@mcremer-able
Copy link
Contributor

mcremer-able commented Sep 23, 2022

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

@Minecraftschurli
Copy link

I'm having the same problem but bigger because I wrote my driver in typescript and can't even use the workaround because it only gets bundled into the server and is not importable

@Minecraftschurli
Copy link

I had a look in the code and it is definitely more complicated than just adding the pure driver factory to the type definition because they use the string to generate the variable name and use dynamic imports

@maoosi
Copy link

maoosi commented Feb 26, 2023

I am facing the same problem trying to use a Custom Driver. Any updates on that issue?

@Hebilicious Hebilicious added unstorage bug Something isn't working labels Jul 1, 2023 — with Volta.net
@yuusheng
Copy link

I have the same problem. Does nitro support Custom Driver?

@Hebilicious
Copy link
Member

You should use an absolute path for your custom driver location :

import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";

export default defineNitroConfig({
  storage: {
    "custom-db": {
      driver: resolve(
        dirname(fileURLToPath(import.meta.url)),
        "unstorage/custom-driver.ts"
      ),
    },
  },
});

@yuusheng
Copy link

You should use an absolute path for your custom driver location :

import { resolve, dirname } from "node:path";
import { fileURLToPath } from "node:url";

export default defineNitroConfig({
  storage: {
    "custom-db": {
      driver: resolve(
        dirname(fileURLToPath(import.meta.url)),
        "unstorage/custom-driver.ts"
      ),
    },
  },
});

It worked for me. But I think we should support using Driver/Storage imported from other ts/js files directly. Since this didn't support TypeScript files and is not very elegant.

I would like to help if we have a plan to support this.

@Hebilicious
Copy link
Member

It supports typescript files. You can replace dirname(fileURLToPath(import.meta.url)) with __dirname if you want something less verbose.

@Hebilicious Hebilicious added discussion and removed bug Something isn't working labels Jul 17, 2023
@pi0 pi0 added enhancement New feature or request p2-nice-to-have and removed discussion unstorage labels May 16, 2024
@pi0 pi0 changed the title Config Missmatch to unjs/unstorage with Custom Drivers Allow conigure local custom storage drivers May 16, 2024
@pi0 pi0 added the storage label May 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants