Giter VIP home page Giter VIP logo

type-dynamodb's Introduction

type-dynamodb

type-dynamodb is simple but powerful dynamodb ORM that uses decorators to get the most benefits from typescript and OOP like inheritance, override methods and adding multiple layers of decorators.

semantic-release


Installation

  • first install the lib and its peerDependencies
npm i class-transformer aws-sdk type-dynamodb --save
  • install reflect-metadata :
npm install `reflect-metadata` --save
  • add reflect-metadata to the root file of your project
import "reflect-metadata";
  • make sure to set AWS credients in your env variables using command line or .env ...etc:
export AWS_ACCESS_KEY_ID=<AWS_ID> # macos or linux
export AWS_SECRET_ACCESS_KEY=<AWS_ACCESS_KEY>
export AWS_DEFAULT_REGION=<AWS_REGION>

or here for other os.

  • in typescript, you need to add the following config to your tsconfig.json:
"emitDecoratorMetadata": true,
"experimentalDecorators": true,

Usage example with table User:

1- first we add @DyTable decorator and extends BaseDyTable to get the db functionalities.

@DyTable('<TableName>') // default is User
class User extends BaseDyTable{
    id: string
    name: string
    userPassword: string
    created: string
}

2- then we determined which fields are part of our dynamodb table by adding @DyField:

@DyTable()
class User extends BaseDyTable{
    @DyField()
    id: string

    @DyField()
    name: string

    @DyField()
    userPassword: string

    @DyField()
    created: string
}

3- then we specify the partition key field in @DyField and sorting key fields if exist:

    ...

    @DyField({isPartitionKey: true})
    id: string

    @DyField({isSortingKey: true})
    created: string
    
    ...

4- now we can use the db functions (dyGet, dyUpdate, dyDelete, dyPut, dyScan, dyQuery) as shown here:

    /* using await */
    
    const user = await User.dyGet({id: "id1"})
    await user.dyUpdate({userPassword: "12345"});
    await user.dyDelete();
    // scan function
    const scannedUsers = await User.dyScan(
        {
            filter: {
                created: {range: {to:"<ISOSTRING>"}},
                name: {equal: "Amro"}
            },// note every key can have only one   filter parameter.
            limit: 50,
        }
    );
    // faster query method that uses PartitionKey(must be specified) and specify sortingKey automatically
    const users = await User.dyQuery(
        "PARTITION_KEY_VALUE",
        {
        filter: {
                created: {range: {from:"<ISOSTRING>"}},
                name: {contains: "Amro"}
            } 
        }
    );

    /* using then */
    User.dyGet({id: "user1"})
        .then((user)=> user.dyUpdate( {userPassword: "12345"}))
        .then((updatedUser)=> {
            // some other logic
            updatedUser.dyDelete();
        })

DyGlobalSecondaryIndex and DyLocalSecondaryIndex

  • for specifying local and global secondary index, use the following decorators:
@DyTable()
class User {
    ... 

    @DyGlobalSecondaryIndex("name_index", {isPartitionKey: true})
    @DyField()
    name: string

    @DyLocalSecondaryIndex("created_index")
    @DyField({isSortingKey: true})
    created: string
    
    ...
}
  • to use it specify the IndexName in the filter options:

type-dynamodb's People

Contributors

amranwar avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.