awslabs/dynamodb-data-mapper-js

Is there anyway to access the table schema of an annotated domain object

ziggy6792 opened this issue · 2 comments

Firstly awesome tool I really enjoy using it thanks a lot!

My question

Lets say I have an annotated domain object e.g

import { attribute, table, hashKey } from '@aws/dynamodb-data-mapper-annotations';

import { v4 as uuidv4 } from 'uuid';

abstract class Identifiable {
    @hashKey({ defaultProvider: () => uuidv4() })
    id: string;
}

@table('User')
class User extends Identifiable {
    @rangeKey()
    email: string;

    @attribute()
    firstName: string;

    @attribute()
    lastName: string;
}

export default User;

This means that from another .ts file I can do something like


import User from 'path/to/user'

mapper.ensureTableExists(User, {readCapacityUnits: 5, writeCapacityUnits: 5})
    .then(() => {
        // the table has been provisioned and is ready for use!
    })

But is there anyway that I can get the table information (i.e; tableName with prefix, hashKey, rangeKey).

I would want something like

import User from 'path/to/user'
const tableData = myMagicFunction(User)

console.log(tableData) //logs {tableName: 'some-prefix-User', hashKey : 'id', rangeKey: 'email', attributes: ['id','email','firstName', 'lastName'] }

Why I want to do this

I am using dynamodb-data-mapper-js in a lambda function and building my stack with CDK. I don't want to have to define my schema (table names, hash keys etc..) twice (once in the lambda for using that tables and once in CDK for building them). I would rather be able to build the tables by importing the schema defined with dynamodb-data-mapper-js

Hi, did anyone find any solution of this? I am also looking for the same!

You can use the getSchema and getTableName functions exported by the data mapper package.

To extract the table keys, you can also use the keysFromSchema function (invoke it with the value returned by getSchema).