New Node.js ORM(Opensql)
Tags: node, sql, javascript, typescript
Published: 2023-01-07 21:08:46

It's 2023 and I want to introduce you to a new orm for node.js

## Introduction

Opensql is a new project that was started on Jun 23, 2022. Its initial version was released on Jun 27 and it only supported mysql driver. Currently, version 2.0.6 of three databases ([Mysql](https://en.wikipedia.org/wiki/MySQL), [Postgresql](https://en.wikipedia.org/wiki/PostgreSQL), [Microsoft Sql Server](https://en.wikipedia.org/wiki/Microsoft_SQL_Server)) supports in this post, we will implement the crud operation in opensql, let's get started :)

## Install and setup

To start the installation from the npm package manager, (
📒Note: make sure to install npm on your system), then write the following command and wait for opensql to be installed.


`npm i opensql`

📒Tip: Install the database driver you want to use in your project, For more information, refer to this link. In the [link](https://github.com/opensql-org/opensql/blob/main/docs/start.md#installing), there is a list of drivers along with the package name.


## How to use

### Connection

The way to connect to the desired database is very easy. The bottom part is the simple way to connect to the mysql database.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e2oe1zk91sj2smon06o6.png)

As you can see, the connection method is possible with only one text string. You can use its second parameter to apply option changes in the database.


### CRUD

#### SELECT

To write a select query, you can use the find method and enter the name of your table as shown below :



![Image description1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkorq6nxr59mk7oeongf.png)

You can also filter your data using the where keyword :


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4byxt95pk9b1envn7bpj.png)

#### INSERT

The insert operation is possible in two ways in opensql. The first method is to use the addOne method. This method adds only one row to the table:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h3odbnbr55p8cx34278c.png)

The second method is the addMany method. Using this method, you can add several consecutive queries to your table.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4445llk8vf7kllk2svew.png)


#### UPDATE

The update method allows you to update your data :


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3dhhw4dwrwv4nmfe4ebs.png)

#### DELETE

The remove method allows you to delete your data :


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/697rkanx965qng1gy87h.png)

Full source code on [GitHub](https://github.com/TREER00T/opensql-crud)

We have reached the end of the training, opensql has other methods, if you are interested in it, you can refer to this [link](https://github.com/opensql-org/opensql/blob/main/docs/interface/OpensqlMethods.md)


If you had a problem or encountered an error, or if you have an idea to improve this project, you can let us know on [GitHub](https://github.com/opensql-org/opensql/milestones). If you also want to contribute to the project, refer to this [link](https://github.com/opensql-org/opensql/blob/main/CONTRIBUTING.md).