Nestjs storage
This package allow the management of your projects uploaded files using differents strategies
Installation
yarn add @neoxia-js/nestjs-storage
Example of usage
Create an entity which extends the package storage.entity.ts
import { Entity, OneToMany } from 'typeorm';
import {
Storage as StorageBase,
StorageInterface,
} from '@neoxia-js/nestjs-storage';
import { User } from '../user/user.entity';
@Entity()
export class Storage extends StorageBase implements StorageInterface {
@OneToMany(() => User, (user) => user.thumbnail)
users: User[];
}
this entity is the one where all your files informations will be stored, you have to put a relation between the original entity that needs the file and the storage entity
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne } from 'typeorm';
import { Storage } from '../storage/storage.entity';
@Entity()
export class User {
@ManyToOne(() => Storage, (stoarge) => storage.users, {
eager: true,
})
thumbnail: Storage;
}
import the storage module in app.module.ts
, you can use the exported multer config from StorageService to initialize the MulterModule
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { MulterModule } from '@nestjs/platform-express';
import {
StorageModule,
StorageModuleConfig,
StorageService,
} from '@neoxia-js/nestjs-storage';
import { MulterOptions } from '@nestjs/platform-express/multer/interfaces/multer-options.interface';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [databaseConfig, storageConfig],
envFilePath: !ENV ? '.env' : `.env.${ENV}`,
}),
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) =>
configService.get('database'),
inject: [ConfigService],
}),
StorageModule.registerAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService): StorageModuleConfig =>
configService.get('storage'),
inject: [ConfigService],
}),
MulterModule.registerAsync({
imports: [StorageModule],
useFactory: (storageService: StorageService): MulterOptions =>
storageService.getMulterConfig(),
inject: [StorageService],
}),
do not forget to import the ServeStaticModule
if the used strategy is local
(and if you need it)
@Module({
imports: [
ServeStaticModule.forRoot({
rootPath: join(process.cwd(), '.', 'public'),
}),
})
here is un example of StorageModuleConfig
import { registerAs } from '@nestjs/config';
import { StorageModuleConfig, STORAGE_KINDS } from '@neoxia-js/nestjs-storage';
export default registerAs('storage', (): StorageModuleConfig => ({
fileSystem: process.env.FILE_SYSTEM || STORAGE_KINDS.LOCAL,
maxFileSize: process.env.MAX_FILE_SIZE,
local: {
baseUrl: process.env.BASE_URL || 'http://localhost',
publicDir: process.env.PUBLIC_DIR || 'public',
destinationDir: process.env.STORAGE_DIR || 'storage',
},
azure: {
connectionString: process.env.AZURE_STORAGE_CONNECTION_STRING,
container: process.env.AZURE_STORAGE_CONTAINER,
sasTime: process.env.AZURE_SAS_TIME,
},
}));
now the multer configuration used by the FileInterceptor will be the one retrived by the storageService.getMulterConfig()
and every eagered storage entity will have at least the properties
fieldname
originalname
mimetype
path
url
where url is the autogenerated (not in database) url of the media
this url is also generated using the shaed access signature
(only for azure strategy for now), allowing the url to be public for a short period of time
Config description
{
fileSystem: "local",
maxFileSize: "8000000",
local: {
baseUrl: 'http://localhost',
publicDir: 'public',
destinationDir: 'storage',
},
azure: {
connectionString: "",
container: "",
sasTime: "",
},
}
TO DO
- tests
- improve documentation
- optional sas for image url
- implement sas for local strategy
- custom compute filename functiion for all strategies
- ...