import S3 from 'aws-sdk/clients/s3' ; import { documentPath } from '../lib/utils' ; import { Config , IStorageAdapter , JsonObject , PathItem } from '../types' ; import constants from '../lib/constants' ; export class S3Adapter implements IStorageAdapter { readonly config : Config ; readonly client : S3 ; constructor ( config ) { this . config = config ; this . client = new S3 ( this . config . storage ) ; } static init ( config ) { return new S3Adapter ( config ) ; } async set ( value , path ) { const fileKey = documentPath ( path ) ; const params = { Body : JSON . stringify ( value ) , Bucket : this . config . storage . bucket , Key : fileKey } ; await this . client . putObject ( params ) . promise ( ) ; return fileKey ; } async update ( value , path ) { const doc = ( await this . get ( path ) ) || { } ; for ( const prop in value ) { if ( value [ prop ] === constants . deleteValue ) { delete value [ prop ] ; delete doc [ prop ] ; } } const updatedValue = { ... doc , ... value } ; await this . set ( updatedValue , path ) ; } async get ( path ) { const fileKey = documentPath ( path ) ; const params = { Bucket : this . config . storage . bucket , Key : fileKey } ; const data = await this . client . getObject ( params ) . promise ( ) ; if ( ! data . Body ) return null ; return JSON . parse ( data . Body . toString ( 'utf8' ) ) ; } async remove ( path ) { const fileKey = documentPath ( path ) ; const params = { Bucket : this . config . storage . bucket , Key : fileKey } ; await this . client . deleteObject ( params ) . promise ( ) ; } }