I use mongoose
, typescript
and graphql
to build my app.
I am a full-stack developer.
The problem is I define the fields and types of model FIVE times.
server side:
models/book.ts
:
// first time
interface IBook extends mongoose.Document {
title: string;
author: string;
}
// second time
const bookSchema = new mongoose.Schema({
title: String,
author: String
})
const Book: mongoose.Model<IBook> = mongoose.model<IBook>('Book', bookSchema)
graphql/typeDefs.ts
const typeDefs = `
// third time
type Book {
title: String!
author: String!
}
// fourth time
input BookInput {
title: String!
author: String!
}
`
client side:
interfaces/book.ts
// fifth time
interface IBook {
title: string;
author: string;
}
As you can see. the title
and author
fields and types are defined FIVE times.
There are three main disadvantages:
- duplicated
- lack of maintainability
- inefficient
Is there a way to solve this? I think this is almost a DRY
problem.
Here are my thinkings:
- universal app - extract some common modules used in client and server side.
make a tool handle this.
make a project generator or command line tool like
ng-cli
for generating model and types statically which means before the run-timemake model definition
decorator
orsyntax sugar
generate model and types dynamically at run-time