Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

node.js - How to create common database configuration code to consume in 2 different apis

I have an api which is consumed by a mobile client (the app) and a browser client (back office). Now as it has started to grow and I need to keep it as clean as possible I will create two different api's one for back office and one for the app. The problem I found is that I do not want to duplicate code, I hate it I know is a bad practice for known reasons. So, the database is exactly the same, as you can image. I am using mongo with mongoose. So what I would like to achieve, at a high level would be something like this

      DATABASE
Api-1         Api-2

Both apis hitting the same database code.

I thought creating a different repo for the database, but I am just imagining and guessing because I dont really know how to approach to this. And if I create a different repo..how can I "import" it in both apis?.

I really appreciate your help, and every proposal or idea is more than welcome

Thank you in advance.

question from:https://stackoverflow.com/questions/65943966/how-to-create-common-database-configuration-code-to-consume-in-2-different-apis

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can have the same repository for the Database access and it should be agnostic of clients(browser/app in your case) as much as possible. If you have some data tied to the clients you can have separate fields in the object denoting those params.

For API's your API can talk to a business/transformation layer, and convert the Database response to the API format you expect. This way you can expose the Database response to multiple APIs you want to support reusing a lot of your code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...