How to use global variables in nestjs - nestjs

I want to use specific data as a global variable.
I thought about inserted it in the DataBase, but I thought it would be used too often, so I decided to leave it as a global variable.
However, I can't find it well even when I search for how to register.
The value is set at the start of the nestjs and does not change.
There will be 3 global variables.
How can I register?
Thank you!

There are a couple of ways to achieve it:
You can create a normal class and add the values of the constant as properties.
If the data is more likely to change (configuration, etc..) I would recommend
creating a config module or using the NestJS out-of-the-box config module to
make it simpler.
Both of the options are good in my opinion, if we are talking about a small number of variables I would not recommend storing constants in a database.

Related

Django Wagtail dynamically create form without new model

How would I allow my primary user to dynamically create forms they can issue to their end clients. Each of my primary users has their own unique information they would like to collect that I do not know before hand. I would like to avoid creating new models in code for their dynamic needs and then having to migrate the models.
I came across this which had an interesting response but it starts with disclaimer
The flexibility of Python and Django allow developers to dynamically create models to store and access data using Django’s ORM. But you need to be careful if you go down this road, especially if your models are set to change at runtime. This documentation will cover a number of things to consider when making use of runtime dynamic models.
Which leads me to believe a lot can go wrong.
However because I'm using wagtail I believe there is probably a way to use StructBlocks & StreamFields to accomplish it.
Any guidance would be helpful.
Wagtail provides a form builder module for this purpose.
I have two possible solutions for you, although it should be said that there is probably some library with Django that I don't know about that does this, but that being said.
Prompt your user for which fields they want and the field type.
Pass this as a dictionary to some function that would generate the HTML code for the form.
When this form is used, instead of worrying about storing the fields seperately, store a dictionary in the Models. There are two ways to do that here
Another way that you could do this, albeit more convoluted but more suited to your needs, is to use MongoDB for the database for Django instead. Because it is unstructured, it might be better suited for your use case. Instructions on using MongoDB for Django are here

Changing variable names after importing functions

I have a master .py file that has a collection of all functions that I need to use for various applications. However, each application needs the variable names in the functions to be changed after import.
May I know what’s the best way to modify functions after import?
Thanks!

is it acceptable to Require Node modules based on env var's or logic?

It might seem like an odd question but I am building a module that abstracts out certain logic for different data storage options. The Idea is that anyone using the module could use it with MongoDb or Redis or SQL or ( insert whatever option you want here )
I have a basic interface I am following in each of my implementations by exporting the same function names and signature just with different implementations for each of the various data storage options.
Right now I have a something like helper = require(process.env.data_storage_helper)
Then the helper can be used the same way.
Is this bad practise and if so why? Is there a better or suggested way to accomplish this kind of abstraction?
This isn't technically bad practice, but I would actually add a level of indirection. Instead, have those options stored in configuration files that get picked based on NODE_ENV or another environment variable. Then use the same key in the configuration object no matter what. A good example of a framework employing this is kraken.js, which auto-loads a configuration file based on NODE_ENV.
You can then grab a handle on the configuration object after Kraken has started up (or whatever you end up using - it uses confit under the hood - you can always just use this library directly), and you can grab the "data_storage_helper" key to see what your store is backed by within a storage module that does the decision making.
The big pro of this approach is that, now if you'd like to change the data storage or any other behavior of another module, you can just update a JSON file. :-)

dynamic ORM in node.js+mongodb

Is it possible to create a model where the relationships are dynamically generated by the application?
I saw the KeystoneJS project that does a nice job of defining the model (see: http://keystonejs.com/docs/database/#relationship-definitions)
But these need to be defined by node, I'm interested in creating these within the application. Are there any ORMs or framework projects that already do that? I I've seen frameworks like the MODxCMS that allow users to create additional fields, by putting everything from the custom (templatevar) values into one table. think mongodb would be great for setting this up without this single table approach.
Any idea how to go about setting this kind of system up? I'm not sure where to start.
I guess mongoose might help you here. And you may want to have a look at mongo-relation too.

Creating a file that contains the set of inputs in pyqt

I have created a widget with a text bix, a combo box, a checkbox and some pushbottons. I wish to make a record of the set of input given everytime in a file. how do I do that ? Pls suggest.
The easiest and not that bad way imho is by reading the values and serializing them using json. You have to set/get the values individually for each input (with different calls). It's a breeze in fact. Personally, I have made my own 'serializing' function so I keep references to all objects in a list and that function loops the list serializing everything.
Depending on your needs and the complexity of your project you may need something different. Why don't you share some more details?
Best Regards.

Resources