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.

Avoid code duplication when using a strongly typed model

See original GitHub issue

Summary:

How can I avoid duplicating the schema definition if I want a strongly typed model? It can become areal nightmare if your schema has heaps of properties.

Schema

const userSchema = new dynamoose.Schema({
	"id": String,
	"name": String,
	"age": Number
})

class User extends Document {
	// <--- DRY? --->
	id: string;
	name: string;
	age: number
	// <------------>
}

Model

const UserModel = dynamoose.model<User>("User", userSchema);

Environment:

Node.js version: v12.19.0 NPM version: (npm -v): 6.14.8 Dynamoose version: 2.4.1

Other:

  • [ x ] I have read through the Dynamoose documentation before posting this issue
  • [ x ] I have searched through the GitHub issues (including closed issues) and pull requests to ensure this question has not already been raised before
  • [ x ] I have searched the internet and Stack Overflow to ensure this question hasn’t been raised or answered before
  • [ x ] I have filled out all fields above
  • [ x ] I am running the latest version of Dynamoose

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
fishcharliecommented, Jun 16, 2022

@RickvdP I did see that recently. Sadly, we just need more support to make it happen.

v3, still has a bit to go before release. Tho we are getting closer.

Yes. I think v4 support for this using TypeScript Decorators would be great along with bringing TypeScript out of beta. Seems like a great idea.

1reaction
hardyscccommented, Dec 10, 2020

@fishcharlie I agree too, but I think there is no easy way to do that yet, however I will definitely spend sometime on it later. here is some discussion about this topic https://stackoverflow.com/questions/45771307/typescript-dynamically-create-interface

Read more comments on GitHub >

github_iconTop Results From Across the Web

The key to eliminating duplicated code patterns - Medium
“Sloppiness”, “carelessness” and “sheer unprofessionalism” are strong words, but it goes to underline the importance of not duplicating code.
Read more >
How to avoid code duplication from handling "structually ...
You're trying to codify a relationship between the types, when in reality, you just need to extract an id , which can be...
Read more >
Avoiding duplicate code when performing operation on ...
"The real question is how to avoid code duplication when performing an identical operation on two different properties of an object.".
Read more >
How to avoid code duplication in MVVM? - MSDN - Microsoft
In MVVM people seem to suggest that the model should not be bloated out and that logic should be put in the view...
Read more >
The Impact of Duplicate Code - Level Up Coding
The most effective way to prevent duplicate code from slipping into the codebase is by dividing code and logic into smaller reusable units....
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