FlameWolf/formzilla

How to use another storage option?

Closed this issue · 3 comments

In the documentation, you say that there are 4 different types of storage options that you can choose when registering the plugin. But I can't import any of these classes at the moment.
It seems that your index.js uses StreamStorage() by default with no possibility to change it in our code :

formzilla/index.js

Lines 14 to 15 in 75ffb48

const formDataParser = async (instance, options) => {
const { limits, storage = new StreamStorage() } = options;

Did I miss something ?

Import the necessary classes first:

import formzilla from "formzilla";
import { BufferStorage } from "formzilla/BufferStorage";
import { CallbackStorage } from "formzilla/CallbackStorage";
import { DiscStorage } from "formzilla/DiscStorage";
import { StreamStorage } from "formzilla/StreamStorage";

Afterwards, while registering the plugin, pass an additional parameter:

server.register(formDataParser, {
	storage: new DiscStorage(file => {
		return {
			directory: path.join(__dirname, "public"),
			fileName: file.originalName.toUpperCase()
		};
	})
});

Oh yeah, I didn't realize it was a default variable in case the storage property was missing.
I was also confused by the import syntax. Is there a reason why you don't bundle the export of your Storages classes in your index.js?

It would be nice so we don't have to look at the directory structure of your plugin. That way, we could just import like this:

import formzilla, { DiscStorage } from "formzilla";

That's just how I organised my code, I think I copied the style from some other packages I used. Will consider refactoring, thank you for bringing this up.