Coder Social home page Coder Social logo

dynamormv1's Introduction

DynamORM Client

npm i dynamorm

DynamORM is a Javascript/Typescript ORM Client for AWS DynamoDB. It provides clean and powerful OOP APIs to productively work with DynamoDB form Node.js

Example

The following example shows a DynamORM class which stores .txt and .jpeg files objects on DynamoDB and provides utility methods to work with them.

import {readFile, writeFile, mkdir} from 'node:fs/promises'
import {DynamORMClient, Table} from 'dynamorm'

const {Connect, HashKey, RangeKey, Attribute, TimeToLive} = new DynamORMClient({/* DynamoDB Config */})

@Connect({TableName: 'Files'})
class FileItem extends Table {
    @HashKey.S()
    name: string

    @RangeKey.S()
    extension: '.txt' | '.jpeg'

    @Attribute.B()
    data?: Uint8Array

    @Attribute.S()
    encoding?: BufferEncoding

    get filename() {
        return this.name + this.extension
    }

    constructor(name: string, extension: '.txt' | '.jpeg') {
        super()
        this.name = name
        this.extension = extension
    }

    async writeFileToDisk(dir: string) {
        if (this.data) {
            await mkdir(dir, {recursive: true})
            return writeFile(dir + this.filename, this.data)
        }
    }

    async getDataFromDisk(path: string) {
        this.data = await readFile(path)
    }
    
    toString() {
        if (this.data) 
            return Buffer.from(this.data).toString(this.encoding)
    }
}

To use it, we just do

const txt = new FileItem('example', '.txt')

txt.data = Buffer.from('This is an example text file')
txt.encoding = 'utf-8'

await txt.save()
const jpg = new FileItem('example', '.jpg')

jpg.encoding = 'base64'
await jpg.getDataFromDisk('./path/to/some_image.jpg')

await jpg.save()

At this point, we have our items on DynamoDB. They can be retrieved from elsewhere like this

const {Data} = await FileItem.select({'example': ['.txt', '.jpg']}).get()

Now we can write the files to disk

if (Data) for (const file of Data) {
    await file.writeFileToDisk('./path/to/some_directory')
}

Or We can get a string representation of the file with the appropriate encoding

const encodedTxt = Data?.[0].toString() // <- 'This is an example text file'

Or Update the content and save it back

await Data?.[1]?.getDataFromDisk('./path/to/some_other_image.jpg')
await Data?.[1]?.save()

This was just a small, non exhaustive example of what you can do with DynamORM. To get started, please see the User Guide.

dynamormv1's People

Contributors

asnaeb avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.