Are Entities between two sub-apps in NestJs sharable? - nestjs

I am very new to NestJS and I am trying to create a project with multiple subapps. The query that I have is, if one entity is present in one sub-app, can we use that same entity in another sub-app of the same project?
This is my project strucutre:
apps
subApp1
entities
- entityone.ts
controller, modules and services
subApp2
entities
- entityTWo.ts
controller, modules and services
I want to use the entity one in subApp2 service. Is it possible? If so, how we can acheive this? Is there any better way to approach this situation where I need to use Entity of one sub app into another.
I tried exporting the Entity from one sub-app and import the Module in another sub app. But I am getting this error:
Nest cannot export a provider/module that is not a part of the currently processed module (StoreModule). Please verify whether the exported Store is available in this particular context.
I am not sure, if it was the correct way for this situation, but this solution came into my mind and i tried it.

you don't need to specify entities for every module.
keep them all together apart, so you can use them wherever you want.

Related

GraphQL Mesh sharing types across handler or different sources

I am working on a project with GraphQL Mesh.
With one of the use cases I have one openapi endpoint. In there, I need to handcraft a resolver that is using types from that openapi schema definations. I tried using different handlers, but types are not sharing across. I also tried to add multiple sources inside one handler, but the customised one (in 2nd place) is not showing in the schema.
If anyone know how to deal with this case, lease help me out. Thank you.
I am expecting to use types from the openapi spec in the custome handler.

Correct way to implement a REST API using MVC

Summary
I am seeing a lot of contradictory architectural examples of a REST API in my work and keep getting different opinions on the subject.
I am familiar with the principles of REST and seeing as each endpoint points to a resource followed by a verb such as /project/create project/123/read.
Following the MVC pattern assuming I have a controller that is responsible for updating a project resource:
router.put("/project/:id/update", ProjectController.put)
First question:
Should this route be responsible for all updates to this resource, in example, assuming different features on my client like marking a project as finished or changing it's title are separated and might not have anything in common for the user. Ending up with the route described above, or should there be something like this:
router.put("/project/:id/mark-as-done", ProjectController.markAsDone)
router.put("/project/:id/update-info", ProjectController.updateInfo)
Second question:
Assuming I want to create a notification resource if a project is created/updated/deleted. Since the notification is a resource on it's own I am not sure how to go about this, but what I assumed and was taught is to use another callback:
router.put("/project/:id/update", ProjectController.put, NotificationController.create)
Third question:
Could I use the same controller to read all resources or just one, for example:
router.get("/project/read", ProjectController.get)
router.get("/project/:id/read", ProjectController.get)
Making the logic in the controller method determinate if it will return all projects or just one. Or should it be separated into different methods?
I would define APIs like this:-
CRUD for Project entity
create- router.post(/projects)
update:- router.put(/projects/:id)
delete:- router.delete(/projects/:id)
read:- router.get(/projects) and/or router.get(/projects/:id)
You can define all above routes in ProjectController.
Regarding Notification entity you can define as follows
read:- router.get(/projects/:id/notifications)
The same can be applied to PUT, DELETE, POST
Here is a good article defining rest guidelines https://stackoverflow.blog/2020/03/02/best-practices-for-rest-api-design/

Google Cloud Datastore query by all kinds in node.js

In Google Cloud Console using GQL I can do this.
SELECT __key__
And this will return all keys from all kinds of the current namespace. One of the use case, is to delete tenant. Tenant will not exist as soon as no records existing inside.
I can't do this from the node.js via google cloud client library, because, it seems like function doesn't support that.
db.createQuery("5630110493310976", undefined).select("__key__");
One interesting thing. This will work and will return all entities from all tenants.
db.createQuery().select("__key__");
What am I missing?
I know, that I can bypass it by using __kind__ query, grab all kinds and go through them, but, I'm looking to more elegant way first.
Found the issue. I launched this query under another project in which such namespace is not exist. Therefore I though that the result is wrong and datastore or client library not support it.
So, the correct way to fetch all entities of all kinds from single namespace would be.
db.createQuery("5630110493310976", undefined).select("__key__");
And to fetch all entities from all namespaces
db.createQuery().select("__key__");

Fetching Initial Data from CloudKit

Here is a common scenario: app is installed the first time and needs some initial data. You could bundle it in the app and have it load from a plist or something, or a CSV file. Or you could go get it from a remote store.
I want to get it from CloudKit. Yes, I know that CloudKit is not to be treated as a remote database but rather a hub. I am fine with that. Frankly I think this use case is one of the only holes in that strategy.
Imagine I have an object graph I need to get that has one class at the base and then 3 or 4 related classes. I want the new user to install the app and then get the latest version of this class. If I use CloudKit, I have to load each entity with a separate fetch and assemble the whole. It's ugly and not generic. Once I do that, I will go into change tracking mode. Listening for updates and syncing my local copy.
In some ways this is similar to the challenge that you have using Services on Android: suppose I have a service for the weather forecast. When I subscribe to it, I will not get the weather until tomorrow when it creates its next new forecast. To handle the deficiency of this, the Android Services SDK allows me to make 'sticky' services where I can get the last message that service produced upon subscribing.
I am thinking of doing something similar in a generic way: making it possible to hold a snapshot of some object graph, probably in JSON, with a version token, and then for initial loads, just being able to fetch those and turn them into CoreData object graphs locally.
Question is does this strategy make sense or should I hold my nose and write pyramid of doom code with nested queries? (Don't suggest using CoreData syncing as that has been deprecated.)
Your question is a bit old, so you probably already moved on from this, but I figured I'd suggest an option.
You could create a record type called Data in the Public database in your CloudKit container. Within Data, you could have a field named structure that is a String (or a CKAsset if you wanted to attach a JSON file).
Then on every app load, you query the public database and pull down the structure string that has your classes definitions and use it how you like. Since it's in the public database, all your users would have access to it. Good luck!

Creating a node module for the SailsJS App

I have a sails project which has many functionalities .
ex: Address creation, event management, notification, payment and many
others.
Now I want to make different services for each one of these. As far as my knowledge, I have two ways to do so:
Create a micro service for every service. (Which is not possible to maintain for now)
or
Create respective modules.
So, I'm able to create the module for Notifications and Payment.
But able to understand how can I create a module for address which can have:
Module (want to bind with the sails module)
Controllers
Routes
I just need a plug and play module for the functionalities which can access the database same way as sails module does.
Few more questions to add on further :
Is there any other better method by which this can be achieved ?
Can I bind or control the sails controller and the module before sails lift by the node_module?
Maybe you should consider making a hook instead
http://sailsjs.org/documentation/concepts/extending-sails/hooks

Resources