# mssql-cg-lib

> MicrosoftSQL Server Connector Library

Latest version **1.1.3** (published 2024-06-06) · ISC license · 0 weekly downloads

## Install

```sh
npm install mssql-cg-lib
pnpm add mssql-cg-lib
yarn add mssql-cg-lib
bun add mssql-cg-lib
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.3 |
| Published | 2024-06-06 |
| First published | 2024-02-26 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 13.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ishita Chakkalakkal |
| Maintainers | cloudgenuser |
| Keywords | mssql, lib, server, connector, cloudGen, n3xgen |

## Links

- npm: https://www.npmjs.com/package/mssql-cg-lib
- npm.io page: https://npm.io/package/mssql-cg-lib

## Dependencies (3)

- [mssql](https://npm.io/package/mssql.md) ^10.0.2
- [express](https://npm.io/package/express.md) ^4.18.2
- [utils-nxg-cg](https://npm.io/package/utils-nxg-cg.md) ^1.6.3

## Alternatives

- [@cantoo/pdf-lib](https://npm.io/package/@cantoo/pdf-lib.md) — 297.9K weekly downloads
- [datatables.net-buttons](https://npm.io/package/datatables.net-buttons.md) — 200.1K weekly downloads
- [@ckeditor/ckeditor5-export-pdf](https://npm.io/package/@ckeditor/ckeditor5-export-pdf.md) — 167.0K weekly downloads
- [scanbot-web-sdk](https://npm.io/package/scanbot-web-sdk.md) — 15.0K weekly downloads
- [@syncfusion/ej2-angular-pdfviewer](https://npm.io/package/@syncfusion/ej2-angular-pdfviewer.md) — 8.8K weekly downloads

## Recent versions

- 1.1.3 (latest) — 2024-06-06
- 1.1.2-SNAPSHOT — 2024-05-29
- 1.1.1-SNAPSHOT — 2024-05-29
- 1.1.0-SNAPSHOT — 2024-05-29
- 1.0.9-SNAPSHOT — 2024-05-29
- 1.0.8-SNAPSHOT — 2024-05-29
- 1.0.7-SNAPSHOT — 2024-05-22
- 1.0.6-SNAPSHOT — 2024-05-17
- 1.0.5-SNAPSHOT — 2024-04-24
- 1.0.4-SNAPSHOT — 2024-04-24
- 1.0.3-SNAPSHOT — 2024-04-24
- 1.0.2-SNAPSHOT — 2024-04-12
- 1.0.1-SNAPSHOT — 2024-04-12
- 1.0.6 — 2024-03-22
- 1.0.5 — 2024-03-22
- … 5 more at https://npm.io/package/mssql-cg-lib/versions

## README

[![N|Solid](https://cloudgensys.com/cg-demo/wp-content/uploads/2019/05/CG-Logo-01.png)](https://www.cloudgensys.com/)

# mssql-nxg-cg-lib (MicrosoftSQL Library)

## 1. Introduction

The purpose of this library is to execute queries using the mssql database. The query can be a single operation or batch operations using parameters. The library used is  ***[mssql](https://www.npmjs.com/package/mssql)***, and it is  based in NodeJS.

## 2.	Library Objects and Methods

The library can be installed from the npm page with the following commands:

**`npm i mssql-nxg-cg-lib`**, **`npm install mssql-nxg-cg-lib`** or **`yarn install mssql-nxg-cg-lib`**

* ### 2.1. Methods

  The following method is available in the library:

   ***processQuery***: This method executes single or parametric queries.
  
   >Method is asynchronous, then it uses `async/await`
   
   >Before the transformation is executed, it is validated that the `JSON` to be processed is a valid `JSON`, only if it use content property, otherwise an exception will be `thrown` and the flow or process will be `stopped`.
   
   >Before the method returns the information, it will be validated that the response is a valid `JSON` or valid `JSON Array`. If this is correct, the response will be `returned`, otherwise an exception will be `thrown` and the flow or process will `stop`.


* ### 2.2. Objects and properties

  The next are the objects and properties available in the library.

  * ### 2.2.1 Objects
      * ***objectLib***: this object contains the required properties for the method `processQuery`
       * ***addProps***: this object contains the optional properties for the method `processQuery`

  * ### 2.2.2 Properties

    ```js
    {
        user: null,
        password: null,
        server: null,
        database: null,
        port: null
    }
    ```
     `objectLib` contains these properties:
    
    * The `user` property indicates the user that has grants to connect to the database.
    * The `password` property indicates the password of the user that has grants to connect to the database.
    * The `server` property indicates the name of the mssql server.
    * The `database` property indicates the name of the database.
    * The `port` property indicates the name of the port.
   
    ```js
    {
        content: null,
        query: null,
        values: null
    }
    ```
    `addProps` contains these properties.

    * The `content` property indicates the data that will be processed in batch operations, this data is going to replace que `?` or variable names accordig with the possition and the query will be executed __*n*__ times where n is the number of objects in the property times.This property can be arrays of array's, array of an object array, a single array (only values inside), an JSON object and an array of JSON objects.
    * The `query` property indicates the query given by the user to be executed.
    * The `values` property indicates the array of values given by the user to be added into the query. 

## 3.	Examples

In an implementatión of this library in an Open Integration Hub (OIH) based component the properties and data can be obtained from the `msg` and `cfg` attributes.

- **Arguments:**
1. The first argument is the message (msg) that will come from the OIH component, the property that contains the information in the `data` object.
2. The second parameter is the configuration (cfg) that will also come from the OIH component.
3. The third argument is only used to define if the library is used in test mode, by default it is false. It will be true if to test the method(s) from a OIH component without running this in a flow.

   ### 3.1. processQuery

- **Description:** This method will execute a query in batch or single form.
- **Object and Properties**: To use this method, it is madatory to use the object called `objectLib` and, if required, the `addProps` object to send and replace the default values.
    
- **Examples:**
  
    In this first example, the **singleQuery** method is used to execute a single query to insert one set of values:
     
    ```js
    let properties = {
        user:'myuser',
        password:'mypassword';
        server:'myserver',
        database:'mydatabase',
        port: 1433
    };
    
    let query = `SELECT * from dbo.Users`;

    const _data = await processQuery({
        data: properties
    }, {}, true);
    console.log(_data)
     ```

    In this second example, the **parametricQuery** method is used to execute multiple queries at once using parameters:
    
     ```js
    let properties = {
        user:'myuser',
        password:'mypassword';
        server:'myserver',
        database:'mydatabase',
        port: 1433
    };
    
    let query = `INSERT INTO dbo.Users (name, age, email) VALUES ($1, $2, $3)`;
    
    let values = [['Shankar', 78, 'shankar@gmail.com'], ['Deepa', 25, 'deepa@gmail.com'], ['Sunny', 17, 'sunny@gmail.com']];

    const _data = await processQuery({
        data: properties
    }, {}, true);
    console.log(_data)
    ```
    
   ---
   
    **Example Results**:
    For the first example, the result would be the following:
    ```json
    {
    "result": "0 rows affected."
    }
    ```
    For the second example, the result would be the following:
    ```json
    {
    "result": "3 rows affected."
    }
    ```

---
_Source: https://npm.io/package/mssql-cg-lib · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
