Changing collection name according to query parameters in Nestjs - node.js

Can we get data from different collections of same or multiple mongo databases according to the query parameters in nest js ?
For example if parameter says get data from collection A, then collection A data should be displayed if it says get data from collection B, then collection B data should be displayed.
Can we do it in same controller or we need to make multiple controllers ?

I got it, I just have to make two models from different collections and use the desired model according to query parameter by using simple if then else.

Related

NodeJS MongoDB populate() bringing string instead of JSON

I have a product and I have a property that brings an array with a provider id and its price. The idea is that every product has an array with multiples providers, each one with its own price.
However, when I use populate() looking for ProviderID, it brings me populated as string instead of a JSON. Below is GraphQL query return from a query that bring products.
ProviderPrice class, inside ProductModel
Optional parameter in ProductModel
This is how I am executing populate
Has anyone gone through this?
Found the problem.
I was using the class ProviderPrice instead of the model ProviderPriceModel.

Choose data from mongoDB in browser and display results

I am new to NodeJS, MongoDB and express.
I am able to insert, delete and update data but I dont really know how to work with it.
I want to choose some data(e.g. from a dropdown form) and then pass that data to some functions and calculate results. How is it possible to show the results to the client in the webpage(e.g. in a table)?
For example: I have different JSON objects stored in my database and now I want to select one by the name. Then do some calculations with it and show the results to the client.
Also: Is it possible to tell the Database to only accept valid JSON data?
Yes you can get specified data from the database and it will work a lot better with mongoose. You can define schema in mongoose and you can focus more on the data instead of how your data look like. And you can do validation to accept the valid data.

bookshelf.js – how to fetch all records with empty query or no query at all?

Is it possible to fetch all records of a table in bookshelf without a query?
I'v got a Model named Person, and I'd like to retrieve all of its records.
What I am doing currently is this:
Person.where('id', '>', 0) ).fetchAll()
.then((result) => {
// doing stuff
});
which just doesn't feel right.
I was hoping for something like
// using empty querybuilder
Person.query().fetchAll()
or
Person.all //
Am I missing something?
I suppose model.fetchAll is the thing you are looking for.
Simple helper function for retrieving all instances of the given model.
See the API Reference of Bookshelf.js
In this case, you can use Model.fetchAll(), which fetches a collection of models from the database, using any query parameters currently set on the model to form a select query.

Can we post an array of objects with knex?

I'm using knex with node and I was thinking of making a field in the migration that would take an array of objects. I know simple stuff like
.string("bla")
.notNullable()
and so on, looking in the documentation can't seem to find it
wanting something like
.array_of_objects //field name
knex.schema.createTable('users', function (table) {
table.increments();
table.string('name');
table.timestamps();
//example
table.array
})
With table.specificType you can create which ever type of database column you like. You need to check from your database, which kind of columns it supports.
It would make easier to answer the question if you could tell what kind of SQL queries you are after. To be able to use knex, you need to know SQL. Knex will never abstract SQL away.
If you are looking for a database library for node.js that abstracts SQL away you might like to checkout sequelize.
Thanks for the post with the specificType. From what I saw PG didn't have an array of objects field, but did have an array of strings field. I ended up taking my array of objects and separating it into two string arrays one named keys and the other values. I stored both of those arrays in the database. Then when I did a get request I took the two arrays joined them together into an array of objects with some javascript tacking. Seems to do the trick.

What is the use of the FROM in the Azure DocumentDB SQL-like query language?

I am using Azure DocumentDB. I only have one collection with heterogenous document types. I am using a type parameter to distinguish between different document types. I am making use of their SQL-like query language to get documents as follows:
SELECT * FROM Collection c WHERE c.ID = 123
I am getting my connection information, including the Endpoint URI, AuthKey, Database name and Collection name, from a configuration file. It seems like I can use any value for "Collection c" and it essentially just becomes an alias for the whole collection. So what is the point of the FROM section of my query?
I think you already got it :)
FROM allows you to set an alias to refer to the collection in other clauses. This may make more sense to you when you include multiple references (e.g. using a JOIN to form a cross-product with nested array elements).
For example:
SELECT food.description, tag.name
FROM food
JOIN tag IN food.tags
WHERE food.id = "09052"
In the query above, we are using referencing both the collection as well as nested array elements within a projection.
You can try this query out on the query demo website.

Resources