question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Manually specify table name

See original GitHub issue

Summary:

Would it be a terrible idea to add a separate config for the table name inside of the schema definition? My specific use case is that the name of my Model does not always match the table name, especially since some of the tables that I use are for timeseries data, where I have a new table each month.

Ideally, the key would also accept a function that gets called at runtime for dynamic table names.

Code sample:

Schema

new dynamoose.Schema(
  {
    // attributes
  },
  {
    tableName: `prod_readings_NOV2019`,
    tableName: () => `${stage}_readings_${date}`
  }
)

Model

dynamoose.model('Reading', schema, options)

Current output and behavior:

The table name is directly tied to the model name, and cannot change once compiled.

Expected output and behavior:

Table name is a separate value to model name, though it still falls back to the model name as default behavior.

Type (select 1):

  • Bug report
  • Feature suggestion
  • Question
  • Other suggestion
  • Something not listed here

Other:

  • I have read through the Dynamoose documentation before posting this issue
  • I have searched through the GitHub issues (including closed issues) and pull requests to ensure this issue has not already been raised before
  • I have searched the internet and Stack Overflow to ensure this issue hasn’t been raised or answered before
  • I have tested the code provided and am confident it doesn’t work as intended
  • I have ensured that all of my plugins that I’m using with Dynamoose are listed above
  • I have filled out all fields above
  • I am running the latest version of Dynamoose

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
dolsemcommented, Nov 20, 2019

@DeltaByte unless I missed the point @fishcharlie’s approach works just fine to solve your problem. That being said, what’s the motivation behind separating chunks of time series data into separate tables? I understand why you would want that with a traditional SQL database, but when it comes to DynamoDB I believe it is standard practice to keep it in a single table due to DynamoDB’s properties. Instead separating data into multiple tables you might wanna separate into different shards instead.

1reaction
fishcharliecommented, Nov 19, 2019

@DeltaByte This will likely never be supported in Dynamoose. I don’t think Mongoose supports this, and it seems unnecessary and not really taking into account the purpose of models and schemas. If you are able to show that this is supported in Mongoose let me know and I’d be happy to reconsider it.

Here would be my suggestion for how I would implement this in the current version of Dynamoose.

Basically you can have the model be created dynamically as needed with a custom name. So something like the following:

ReadingsModel.js

const dynamoose = require("dynamoose");
const schema = new dynamoose.Schema(
  {
    // attributes
  }
);

return (stage, date) => dynamoose.model(`${stage}_readings_${date}`, schema, options);

Then you can access it by doing the following:

main.js

const Model = require("./ReadingsModel.js");
const model = Model("prod", "NOV2019");

model.get("item1", (item) => {
    console.log(item);
})

So basically you can create the models dynamically as needed by passing in the stage and date that you want.

I forget how Dynamoose handles duplicate model creation (ex. if you call Model with the exact same parameters multiple times), you might wanna test that and add a layer of caching to store models created into an object or something and check to see if they exist in that object and if so return that instead of calling dynamoose.model.

The biggest pitfalls in this design are the fact that since models are being created on the fly, you need to be careful around table creation and waitForActive settings on Dynamoose. Really think about how you want to verify the table is created and active here for tables that might not exist.

Hopefully that answers your question! If not let me know what further requirements you have and we can continue to explore possible options.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to specify a different name for the db table that a ... - GitHub
I have an existing database with table names that are different than what I would like my model names to be. The documentation...
Read more >
8. How to specify the table name for a model? - Books by Agiliq
A model's database table name is constructed by joining the model's “app label” – the name you used in manage.py startapp – to...
Read more >
How to change Laravel model's table name - Stack Overflow
I am using Laravel 5 and have changed the name of a database table from "domain_related_settings" to "DomainRelatedSettings" ...
Read more >
How to Rename a Table in MySQL in Different Ways
1. In MySQL Workbench Navigator, search the table name you want to change and then click it. · 2. Click the wrench icon...
Read more >
Specifying Table Names in SQL Statements
When defining locations, you specify a name for the location along with a directory, or path and file name. ... Specifying Table Names...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found