0.7.1 • Published 5 years ago

abstorage v0.7.1

Weekly downloads
7
License
ISC
Repository
github
Last release
5 years ago

abstorage

CircleCI Coverage Status npm version

Abstract storage (S3, Local)

Inspired by carrierwave and fog.

Usage

const { S3Storage, LocalStorage } = from 'abstorage'

let storage: Storage = null
if (process.env.NODE_ENV === 'production') {
  storage = new S3Storage({bucket: 'some-bucket'})
} else {
  storage = new LocalStorage({dst: 'tmp'}))
}

const key = 'awesome/object.jpg'
const data = fs.readFileSync(path.resolve('awesome', 'object.jpg'))
storage.put(key, data, {ContentType: 'image/jpeg'})
storage.get(key)

Sequelize bind

import { DataTypes, Model } from 'sequelize'
import { sequelize } from './connection'
import { storage } from './storage'

import { bindStorage } from 'abstorage'
import { Stream, PassThrough } from 'stream'
import sharp from 'sharp'

export class User extends Model {
  public id: number
  public name: string
  public iconKey: string
  public createdAt: Date
  public updatedAt: Date
  public icon: any
}

const { getter, setter, onSaveHook, onDestroyHook } = bindStorage<User>({
  column: 'iconKey',
  storage,
  contentType: 'image/jpeg',
  resolveKey: user => `users/icon/${user.id}.jpg`,
  preprocess: async data => {
    const pipeline = sharp()
      .resize(100, 100)
      .jpeg({ quality: 80 })
    if (data instanceof Stream) {
      return data.pipe(pipeline)
    } else if (data instanceof Buffer) {
      const ret = new PassThrough()
      sharp(data)
        .pipe(pipeline)
        .pipe(ret)
      return ret
    } else {
      throw new Error('Bad data type')
    }
  }
})

User.init(
  {
    name: DataTypes.STRING,
    iconKey: DataTypes.STRING
  },
  {
    getterMethods: {
      icon: getter
    },
    setterMethods: {
      icon: setter
    },
    sequelize
  }
)

User.afterUpdate(onSaveHook)
User.afterCreate(onSaveHook)
User.afterDestroy(onDestroyHook)
0.7.1

5 years ago

0.7.0

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.9

5 years ago

0.3.8

5 years ago

0.3.7

5 years ago

0.3.6

5 years ago

0.3.5

5 years ago

0.3.4

5 years ago

0.3.3

5 years ago

0.3.2

5 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.2

6 years ago

0.0.1

6 years ago