Create upload service
In this guide, we will create a new collection with file field that will be saved in the public folder in the server, after you finish this guide you can change the storage in google-cloud or aws
1 - Generate a new service
Create a new mongoose service that going to record all the files. in this example we going to call the service 'organizations-files'
feathers generate service
? What kind of service is it? Mongoose
? What is the name of the service? organizations-files
? Which path should the service be registered on? /organizations-files
? Does the service require authentication? No2 - Create new validators file
Create organizations-file.validators.js in the validators folder path: src > validators > organizations-files.validators.js
const {Joi, enums} = require('feathers-mongoose-casl');
const getJoiObject = function(withRequired) {
const required = withRequired ? 'required' : 'optional';
return Joi.object({
// Data from user
displayName: Joi.string()[required](),
// File will be the file url in the storage - the uploadMiddleware will handle this value
file: Joi.string().meta({ dashboard: { doc: {inputType: 'file'}, list: {type: 'link'} } }),
// Data from the file
originalName: Joi.string(),
// The user that uplaod the file
user: Joi.objectId()
.meta({ type: 'ObjectId', ref: 'users', displayKey: 'email' })
.meta({ dashboard: { doc: {readOnly: true} } }),
// Data from the upload service
fileId: Joi.string().meta({ dashboard: { hide: 1 }}),
storage: Joi.string().valid(
enums.STORAGE_TYPES['local-public'], // When file saved on local-storage
enums.STORAGE_TYPES['others'], // When user pass link to file
).meta({ dashboard: { hide: 1 }})
});
};
module.exports = getJoiObject;3. Add service middleware
Inside the organizations-files.service.js , add the uploadMiddleware path : src > services > organizations-files > organizations-files.service.js
We have set in the example sys_admin ability - change it to your needs
4 - Update service model
path: src > models > organizations-files.model.js
5 - Update service hooks
path: src > services > organizations-files > organizations-files.hooks.js
6 - import upload service
path: src > services > index.js
7 - Validate config file
path: config\default.json
Check that your not missing this in your configuration file
8 - Done!
test the service
test from dashboard : 1) open feathersjs-mongoose-casl-admin and try to upload a file
Last updated
Was this helpful?