Create a sqlite database to use on tests - node.js

I have a server implemented on Adonis.js with tests that perform operations against the main database. I want to use #Adonis.js/Vow and create a sqlite database dedicated only to use on the tests. The server is running on docker, and i use a docker-compose file to build the complete solution (3 servers and 2 other databases on postgreSQL). How can i create a sqlite database on docker to run it and connect to it on my tests?

Like always, the first step is to install the package from npm by running the following command.
npm i #adonisjs/lucid#alpha
Once done, run the following command to set up the package.
node ace invoke #adonisjs/lucid
1.You can choose sqlite of the available databases.
Right after the setup command is completed, we need to copy the code for validating the environment variables to the env.ts file. Since environment variables are injected from the outside, AdonisJS recommends you validate them and ensure that your app is always running with the correct set of configuration values.
The config/database.ts file holds all the configuration related to the database.
Finally, the config file relies on the environment variables and you can update them inside the .env file.
PG_HOST=localhost
PG_PORT=5432
PG_USER=root
PG_PASSWORD=
PG_DB_NAME=tests
learn more
database introduction

Related

How to ensure that I can only access a database while testing?

I have built an API in Node.js which communicates with a MongoDB as database. I am also using Vitest as my testing library.
I have two databases: prod and test. I want to restrict the access to the test database to only during the testing calls made due to test files ran by Vitest. Is there any way to do the same?
Proposed Solution (Maybe?)
Configure Vitest to setup an ENV flag or a NODE_ENVIRONMENT flag to TESTING whenever npm run test is called, and revert that back to DEVELOPMENT after the command is complete.
Add check while connecting to the database that if the environment is TESTING, only then connect to the test database, otherwise connect to the usual database.
The problem I am facing in this approach is configuring step 1. Any guides on the same? (Or other possible solutions to the problem?)

typeorm synchronize in production

In Typeorm there is a feature called synchronize. You can synchronize entities with a database, so there is no need for migirations. But as you know synchronize is dangerous for production.
Here is the question, when should I use the synchronize feature? Imagine at first (in the development environment) I started using the synchronize feature. If I disable it in production while I have no migration, how should my production database will going to be created?
Also, I'm going to deliver the project on some milestones. Should I disable it at the first milestone or at the end? And for long time maintenance, should I use synchronize disabled and use migration after the first production release?
Any idea would be appreciated.
Migrations in TypeORM
Even Though synchronization is a good option to synchronize your entity with the database, it is unsafe for production databases. Therefore migrations can be an alternative solution for safer migrations in production databases.
When doing a migration, you should follow the below steps.
1. Update the Typeorm config file and package.json file
You should change the synchronize attribute to false in Typeorm config file as the first step to prevent schema synchronization.
Then add the following command to the scripts attribute under the package.json file.
“typeorm”: “ts-node ./node_modules/typeorm/cli -f ./ormconfig.json”
2. Generate the migration
npm run typeorm migration:generate -n
Here you can give a name to your migration. After you run the command you will find a migration file under migrations with the name .
In the migration file, there are two functions namely up and down where up function responsible for running the migration and down for reverting the migration.
3. Run the migration
npm run typeorm migration:run
This command will run the migration which you have already created in the above command. When you run this command, it will execute the up function in the migration file.
4. Revert the migration
npm run typeorm migration:revert
This command will revert the migration which you have already executed in the above command. When you run this command, it will revert all the migrations which you have already done. Basically, it will run the down command of the migration file.
Synchronize is a great option to get up an running, but in my opinion you should always default to creating migrations. This is because it will enforce you to run your development environment similar to production, which is always key. You want to make your Dev environment run like production.
migration:generate is a great middle ground to building your migration files from your entities.
This was my question too, so I searched for it. I found out that as the documentation says:
Once you get into production you'll need to synchronize model changes into the database. Typically, it is unsafe to use synchronize: true for schema synchronization on production once you get data in your database. Here is where migrations come to help.
We can realize that once you have valuable data in your production database, you should turn synchronization off forever and start using migrations on development and production
source
I also have to decide that issue now. And i will use the syncronize option only for my e2e test db. As Roger King already mentioned, you want to have your dev and prod environments using the same tools to change the database. In this way you can prevent different behaviors between them.

Node JS db-migrate for different database

I'm already using db-migrate package to migrate MySQL database script and it works well, I've even setup the DATABASE_URL variable in the server environment.
Now I've a requirement to store few details in the sqlite in the same service, I checked db-migrate package for this feature apparently nothing mentioned regard to the executing sql scripts in multiple different databases at one go. Is it possible to do in db-migrate? or do I've to write my own service for this?

how to access sqlite database from node-red node?

I am creating a node-red API service that access SQLite database using SQLite node.
How to configure the database with SQLite node?
I have hard-coded db file in SQLite node.
What is the right way to develop a node-red API that access the SQLite database ?
I think I want to make the database filename configurable, so that it is fetched dynamically.
I am new to node-red programming.
If the changes are only at deploy time not runtime then you can use Environment variables as described in the doc here
If you replace the path to the DB in the config node with ${DB_PATH} then Node-RED will replace this with the value of the DB_PATH environment variable at deploy time.
Have you looked at the documentation for creation configuration nodes?
https://nodered.org/docs/creating-nodes/config-nodes

NodeJS config.json vs process.env for configuartion management

I've come across people using both methods to do config management.
What are the pros and cons of each approach?
If I have a lot of variables that I store in my config object, will I have to set them all one by one in an upstart script before executing the node app?
You generally use envvar to keep an application stateless. The same codebase should work in dev, staging, test and production environment.
You will put var like MySQL config, API keys, if log is enabled or not, if debug is on or not ...
Config file are used for variables which are not dependent of the environment. For instance, name of the application, number of items per page ...
I guess you can use config.json file for storing big configs. ENV I've usually use for passing application port or something very important for normal application start. For example if you use some external lib, it's better to be able to pass custom path to lib executor in ENV.
P.S. You should never save config.json in SVN.

Resources