npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@neoxia-js/nestjs-storage

v1.0.0

Published

nestjs module allawing communication with different kind of local and cloud storage

Downloads

1

Readme

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 {}

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

// user.entity.ts
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne } from 'typeorm';
import { Storage } from '../storage/storage.entity';

@Entity()
export class User {
  // ...
  @OneToOne(() => Storage, { eager: true, cascade: true })
  @JoinColumn()
  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)

// app.moduke.ts

@Module({
  imports: [
    // ...
    ServeStaticModule.forRoot({
      rootPath: join(process.cwd(), '.', 'public'),
    }),
})

here is un example of StorageModuleConfig

// src/config/storage.config.ts

import { registerAs } from '@nestjs/config';
import { StorageModuleConfig, STORAGE_KINDS } from '@neoxia-js/nestjs-storage';

export default registerAs('storage', (): StorageModuleConfig => ({
  defaultFileSystem: process.env.FILE_SYSTEM || STORAGE_KINDS.LOCAL,
  maxFileSize: parseInt(process.env.MAX_FILE_SIZE),
  fileSystems: {
    someLocalFileSystem: {
      strategy: STORAGE_KINDS.LOCAL,
      baseUrl: process.env.BASE_URL || 'http://localhost',
      publicDir: process.env.PUBLIC_DIR || 'public',
      destinationDir: process.env.STORAGE_DIR || 'storage',
    },
    someAzurePrivateFileSystem: {
      strategy: STORAGE_KINDS.AZURE,
      connectionString: process.env.AZURE_STORAGE_CONNECTION_STRING,
      container: process.env.AZURE_STORAGE_CONTAINER,
      sasTime: parseInt(process.env.AZURE_SAS_TIME),
      isPublic: false,
    },
    azurePublicFileSystem: {
      strategy: STORAGE_KINDS.AZURE,
      connectionString: process.env.AZURE_STORAGE_CONNECTION_STRING,
      container: process.env.AZURE_STORAGE_PUBLIC_CONTAINER,
      isPublic: true,
    },
  },
}));

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

the function getMulterConfig accepts the fileSystem name as argument, default is the defaultFileSystem

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


{
  defaultFileSystem: "someLocalFileSystem", // fileSystem kind, it must be one of the "fileSystems" key property
  maxFileSize: 8000000, // max file size in bytes, default 8000000
  fileSystems: {
    someLocalFileSystem: {
      strategy: "local", // only "local" or "azure" for now
      baseUrl: 'http://localhost', // url of the server
      publicDir: 'public', // server public directory, default 'public'
      destinationDir: 'storage', // server image directory inside publiic directory, default 'storage'
    },
    someAzureFileSystem: {
      strategy: "azure", // only "local" or "azure" for now
      connectionString: "", // azure bucket connection string
      container: "", // azure bucket container name
      sasTime: 100, // azue public sas url time validity (in minutes) default 100
      isPublic: false, // the container (and so the image) is publicly accessible or not
      // even if the property is set to true, but the container already exists and it is private,
      // the image won't be publicly accessible
    },
  }
}

TO DO

  • tests
  • improve documentation
  • custom compute filename function for all strategies
  • ...