I have a few models in my DB. I wanted to know if there is a way to auto-generate the code to apply CRUD operations on them - especially for those that have foreign keys.
Not sure, if I get your question right. With prisma generate you can generate javascript API to run CRUD operations against your database.
If you want to generate a RESTful API with CRUD operations, take a look at e.g. https://github.com/kepelrs/nestjs-prisma-crud (requires Nestjs).
Related
NestJS encourages the use of TypeORM and entities to interact with database. I have experience with Express, but NestJS, TypeORM and their entities concepts are new to me.
Previously, when I worked with Express back end, I have been using raw SQL statements (and with command line in some cases) to interact with the database (from CRUD to database deployment). NestJS can also connect to an existing database (says, PostgreSQL in this case) without using entities (completely delete entities directory).
With that said, why do we need entities? Is it necessary for a NestJS backend when I'm familiar with raw SQL? And what's a good practice for working with database in NestJS?
Do we need entities?
Nope. Not at all. You can write raw SQL with pg or any other package all you want. It's actually what I do here
Why do we need entities?
It really depends on how you're managing your database schema, in my opinion. If you're letting TypeO/Mikro/prisma manage the schema for you, then entities are useful. If you're using something like Hasura, then the migrations are written in SQL and Hasura generates the types for you (I think prisma might be similar here actually).
Personally, what I prefer doing is making my own mini-orm that deserializes and validates the data from the database before passing it back on to the client. I can write raw SQL this way, and map it to my defined types, rather than relying than on a full ORM.
I am reading a book about GraphQL, and they claim that one strong point of GraphQL is selecting a field from the database. See sample below:
On this image, they selected just the title from the movies on the database. I can do the same using mongoose, using "select".
Could I say that mongoose is already GraphQL?
One is a query language that can be resolved by reaching into databases and gathering data, the other is a friendly wrapper that helps you interact with MongoDB, a NoSQL database.
You cannot say that it is already GQL because it does not deal in GraphQL language, it deals in JS queries. They are different languages with different syntaxes.
You might write a resolver for a GraphQL query using Mongoose, but they are still 2 separate things.
After I have asked the question, I gave some thoughts on the matter, indeed, as said zero298, they are two different things. Here, they build a app using both technologies, and justify the reasons it is better.
An excerpt from the aforementioned article:
"GraphQL is a technology that helps developers across the board to build more robust software more quickly. The ability to request all of the information you need in a single request is a game-changer. It has simplified my backend development of APIs for consumption by mobile and web applications that would normally rely on RESTful APIs. A normal RESTful API may have several endpoints for various entities (e.g. users, submissions, etc.); with GraphQL, you can get all of this information in a single go using GraphQL’s query language, also known as GQL."
I adding this extra answer as so if somebody falls into the same trap, they can easily get out!
Related question:
Do I need mongoose with graphql?
GraphQL with mongoose
I have a one file which has details of the database named xyz.js and another file which created the database using that file in mongodb and also has various functions like adding to database etc.The name of this file is create.js. Now I want to convert create.js code using mongodb to waterline.
me_aj, there is no possible short and simple answer for what you want. Nor there is any tool that will do what you need. Here are some pointers for what you'll need to do.
1. Learn about using waterline
It may sound obvious but you should start by getting acquainted with waterline and the best places are the waterline project and the Waterline Docs.
By then you'll know waterline is an adapter based ORM and that you'll have to decide which adapter to use. If you are using mongodb, you are probably looking at sails-mongo.
2. Config waterline to connect to the desired data store
Waterline, contrary to some other ORMs, has to be initialised before you can start using it. If you are using it outside sails you should look at these examples on how to set up a simple app. You'll also need to configure it by following the specific adapter settings. If you are connecting to mongodb check the instructions on the sails-mongo project.
3. CRUD operations
By now you've setup waterline to initialise and connect to your chosen data store, now it's time to do things with it. Similar to many other ORMs. waterline uses methods such as find, update, create and destroy to perform CRUD operations, to learn more about using these check the query methods documentation page.
This should be enough to point you in the right direction.
I'm currently building a rest api and I'm struggling to find the best way to unit test each route.
A route handler performs various things and one of them is to execute a query to mongodb. I can unit test the route handler by using stubs, but if I'm testing the query I cannot stub the query itself, I need to have an in-memory mongodb that I could reset and insert new data for each test.
How do you test queries? I'm thinking that the only real way to ensure that the query does what I need is to use a real mongodb database installed in the testing machine (typically in the same machine used for developing).
yes, just as for relation databases, you need to have real base. if mongo offers in-memory auto-created version then it's easy. if not, then each developer has to have running mongo before he runs integration tests. for CI you can have one single dedicated mongo but than you have to prevent concurrent access (schema creation, multiple transactions etc). you should also implement automatic creation of schema if needed and empty database before each test. in relational db rollback is usually enough. when it's not enough then trimming all tables helps. although we had to implement it manually as we couldn't find any existing tools
What is the optimal way to perform CRUD operations when using Node.js with MongoDB. Can I reuse the same queries that work on mongo shell? What advantages does a ODM like mongoose provide? Any other ODMs which fit into the mean.io stack?
vmr.
Well, I guess it depends of what you want to do.
Lets take what Mongoose says in their website:
Mongoose provides a straight-forward, schema-based solution to modeling your application data and includes built-in type casting, validation, query building, business logic hooks and more, out of the box.
Resuming what I understood of that it helps you to model your database and helps you to mantain your logic organized using model like in an MVC. It's a very mature ODM and very recomended for using with MVC.
In my personal experience I started using Monk, which did the trick for a while, but the I started to need to use aggregate and other stuff that apparently Monk can't handle. And I don't wanted to tie my system to an model because is a very mutable project, so I started using Mongoskin which is, at least for now, perfect for me because I can use pratically the same query that I use at Robomongo (Which is like a Navicat, PgAdmin, PhpMyAdmin but for MongoDB) in my ExpressJs code.
Mongoose is saving your time by mapping the JavaScript Data objects to the MongoDB database.
MongoDB and NodeJS Integration