I am using a single Table approach for my dynamodb design.
Take a look here (Single Table Structure Example). No need to read all the page. The idea is that several different kind of entities will be stored in the same table. (for example, orders, products, clients, etc).
I use dynamoose and I have the following schemas:
const schema_1 = {
PK: { type: String, hashKey: true },
SK: { type: String, rangeKey: true },
someAttribute: { type: String, required: true }
};
const schema_2 = {
PK: { type: String, hashKey: true },
SK: { type: String, rangeKey: true },
someOtherAttribute: { type: String, required: true }
};
I use the following to create tables:
const ExampleModel = dynamoose.model('TestTable', schema_1);
const AnotherModel = dynamoose.model('TestTable', schema_2, {create: false});
The problem is that when I run this code, I get a Cannot do operations on a non-existent table error.
Note: The table is created, but the error also raised.
But if I add a sleep time between both calls, the problem goes away.
const ExampleModel = dynamoose.model('TestTable', schema_1);
await new Promise(resolve => setTimeout(resolve, 1000));
const AnotherModel = dynamoose.model('TestTable', schema_2, {create: false});
(Note: I run this in async code - below is the full code)
I think that this wait should not exists and in the environment I am running the code, make the models creations async produces another error since the code that needs the schemas is executed before initialized.
Hope you can help me.
The full code is here:
let testFunction = async () => {
dynamoose.aws.sdk.config.update({
region: 'local.region',
});
dynamoose.aws.ddb.local();
const schema_1 = {
PK: { type: String, hashKey: true },
SK: { type: String, rangeKey: true },
someAttribute: { type: String, required: true }
};
const schema_2 = {
PK: { type: String, hashKey: true },
SK: { type: String, rangeKey: true },
someOtherAttribute: { type: String, required: true }
};
const ExampleModel = dynamoose.model('TestTable', schema_1);
//await new Promise(resolve => setTimeout(resolve, 1000));
const AnotherModel = dynamoose.model('TestTable', schema_2, {create: false});
}
testFunction();
The dependencies needed are:
- dynamoose:
npm install dynamoose
- Local dynamodb: (See this page)
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb -inMemory