Do i really need to define models in sequelizejs? - mysql

I'm playing around with expressjs for a while and now the time has come to connect to a mysql database. Now i'm searching for a way to write and retrieve data from it. Normally i'm creating the database tables with MySQL Workbench. I'm using MySQL because of the ability of using ForeignKeys. Couldn't figure out yet how to do it in other database types...
Currently i'm more used to medoo for PHP. It only connected to a database and gave me the ability to access it in an object oriented way.
Is there a similar module for nodejs out there?

Try knex for a sql-builder library, it's pretty solid and looks sort of similar to medoo (but it uses Promises and is asynchronous).

Related

Building dynamic data table with nodeJS backend for mysql database

I would like to build a data table that provides features like searching, filtering, free text search and so on (if anyone has more idea please share to create modern data table). My backend has to be in NodeJS and Front end could be simple html, css. If I need to create any middleware to make the data load faster, how will that be? Any suggestion regarding this will be very much apppreciated.
P.S: I have a mysql database.
Building a dynamic data table with a Node.js backend for a MySQL database can be a challenge, but there are a few libraries that can help you get the job done.
One library that can be used is the mysqljs/mysql library. This library provides a Node.js interface for MySQL databases. It can be used to create a connection to a MySQL database, run queries, and close the connection when finished.
Another library that can be used is the node-mysql2/promise library. This library provides a Node.js interface for MySQL databases with promise support. It can be used to create a connection to a MySQL database, run queries, and close the connection when finished.
Finally, the sequelize library can be used. This library provides an easy-to-use interface for interacting with MySQL databases. It can be used to create a connection to a MySQL database, define models, and run queries.
All of these libraries can be used to build a dynamic data table with a Node.js backend for a MySQL database.

What is the correct way of structuring mySQL code in a nodejs projects?

I have been working on a project lately and using node as my back-end but instead of Mongodb I am using MySQL as my DBMS as I will need it in my project but I am having trouble organizing MySQL queries and can't find a good solution that helps me structure my code and files right. And another issue is that I don't know when to close my connection or if leaving it is a good practice or not.
NOTE: I already use express generator for generating my file architecture my only problem is where to place the MySQL related code and what are the best practices for a clean code.
You can create a model folder and add all your 'tables'.js there.
You can also create a DB.js file, so you can use Mysql from Nodejs and also manipulate it from the prompt:
Model Exemple
I.e of DB connection:
DB Connection
You can create tables on the way below. ID's, creating date and updated date are created automatically by Nodejs, so you don't need to worry about.
how to create a table
You also will need to download mySQL (npm install MySQL --save) and to interact with MySQL (selects, deletes,...)
Another way is create a repository.js and add SQL queries there, but is not really useful since you are using nodejs and it provides you these queries.

How can I connect my react native app to MySQL server?

I'm creating a mobile app for an existing website and trying to connect to a local instance I have running on a MySQL workbench. I've seen others recommend against the use of MySQL but I'm stuck with it, since that's the current database. I'm using expo to run my React Native code. Do I need to use a server, like "MAMP?"
Let me know if there is any more info that is needed.
You cannot connect your app directly to your database.
You will need a server/API that acts as an intermediate between the app and the DB. You can code it in most programming languages and if you know PHP, having MAMP on your system will allow you to build your API with PHP.
I've seen others recommend against the use of mySQL
You should definitely question their reasoning. I've been using MySQL for many years now in small and big projects and it has never been an issue. If they're comparing it to non-relational DBs like Mongo, I can understand, it's easier to setup and maintain a NoSQL database than a relational one.
I assume you're not that experienced but I still purposefully used some terms that may be new to a beginner. Since I don't know your skills, I will refrain from pointing you to specific tutorials/articles.
I recommend you to Google anything you don't understand from this answer.

Connecting to other database types in the 2sxc module

Is there currently anything in the 2sxc module that allows you to connect to other database types? Specifically I would like to connect to a MySQL database. I know you can connect to other tables within the database.
This depends a bit on your question. Let's split
Can you use Razor-Views in 2sxc to visualize data from any kind of database?
Yes, just use c# code to get the data. Just create the sql-objects or whatever in .net and use that. For example, I wrote about using PetaPoco http://2sxc.org/en/docs/docs/feature/feature/2583
Can you use non-sql-data in your Visual Query Designer
Yes, but you'll have to work a bit. The easiest way is to map your my-sql tables in the DNN SQL server. This is a bit like a translation layer, which then let's use use them as if they were in the SQL Server.
A harder way is to create your own data source. Best to inherit the SqlDataSource (which provides a lot of security around parameter injection etc.) https://github.com/2sic/eav-server/blob/master/ToSic.Eav.DataSources/SqlDataSource.cs - and then modify it to use your mysql

Mongoose / Mongodb migration to MySQL

I have a NodeJS project running with mongodb database (using mongoose).
For a technological constraint reason I need to migrate the app from using mongodb to mysql - is there a way to migrate to mysql without having to rewrite the whole mongoose model files?
PS. although I'm using mongodb all the query is mainly still not on the nested document (I'm querying only by ID or by some first-level attribute) so actually putting nested document into a field in mysql table should still be fine
I would suggest letting your application run with Mongo for now. Meanwhile write a wrapper for MySQL that would translate your Mongo queries to mysql. Switch to that wrapper once done. Then write another wrapper for Mongo, just in case you need to switch back.
Try and keep all your Database specific function calls in the wrapper. So, that you won't need to do this again and again. Just write a new wrapper for whatever Database you will use and just switch.
And you'll probably need to run some sort of job to migrate your data from Mongo to MySQL.