Reputation: 51
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:
npm install dynamoose
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb -inMemory
Upvotes: 2
Views: 6056
Reputation: 51
Put the answer here too (Thanks Charlie for adding the link in the comments)
This code should technically fail. You shouldn't be allowed to create two models with the same name.
I'd recommend testing out v2.3.0-beta.1, which adds better support for multi table design. Your code could be:
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, schema_2]);
Leave this other comment for future reading (with the current solution that worked for me till the final v2.3.0 is released).
Since I am using this code for test on a local environment (node + jest), I finally use "@shelf/jest-dynamodb" (take a look here) that creates the db when tests runs and I can avoid creating schemas on code. So both model definitions has {create: false}.
Upvotes: 2