simplifield-sql
Version:
A simplifield sql package that makes your work more easier, simpler and smarter!.
725 lines (676 loc) • 16.8 kB
Markdown
<div align="center">
<p>
<a href="https://www.npmjs.com/package/simplifield-sql" target="_blank"><img src="https://nodei.co/npm/simplifield-sql.png?downloads=true&downloadRank=true&stars=true"></a>
</p>
<p>
<a href="https://www.npmjs.com/package/simplifield-sql"><img src="https://img.shields.io/npm/v/simplifield-sql.svg?maxAge=3600" alt="npm version" /></a>
<a href="https://nodejs.org/" target="_blank"><img alt="node-current" src="https://img.shields.io/node/v/simplifield-sql?logo=node.js&logoColor=white"></a>
<a href="https://discord.gg/DmddDKmygG" target="_blank"><img alt="chat-online" src="https://img.shields.io/discord/907619601943244853"></a>
<a href="https://www.npmjs.com/package/simplifield-sql"><img src="https://img.shields.io/npm/dt/simplifield-sql.svg?maxAge=3600" alt="npm downloads" /></a>
</p>
</div>
<br>
<br>
About
simplifield-sql is a powerful [Node.js](https://nodejs.org) module that allows you to easily interact with the
[MySQL Database](https://dev.mysql.com/doc/).
<br>
<br>
Installation
**Node.js 16.9.0 or newer is required.**
```sh-session
npm install simplifield-sql
yarn add simplifield-sql
```
<br>
<br>
Example usage
Install simplifield-sql:
```sh-session
npm install simplifield-sql
yarn add simplifield-sql
```
Create new database connection:
```js
const Database = require("simplifield-sql");
const db = new Database({
user: "root",
host: "localhost",
database: "myowndatabase",
password: "mysecretshellpassword",
});
db.on("connect", (connection) => {
console.log(`Connected to ${connection.config.database} database 👌`);
});
```
And now you're able to **insert**, **update** and **delete** the database **rows**
<br>
<br>
Methods
## **Connection**
- **[Destroy Connection](#destroy)**
- **[Ping](#ping)**
## **Table**
- **[tables](#tables-2)**
- **[createTable](#createtabletable-rows)**
- **[deleteTable](#deletetabletable)**
## **Row**
- **[select](#selecttable-condetions)**
- **[selectAll](#selectalltable-condetions)**
- **[insert](#inserttable-values)**
- **[update](#updatetable-conditions-values)**
- **[delete](#deletetable-conditions)**
<br>
<br>
Documents
## Connection
### `.destroy()`
> Destroys the database connection.
> **Returns:** `void`
### `.ping()`
> Pings the database to check the connection status.
> **Returns:** `number`
## **Table**
### `.tables()`
> Fetches the list of tables in the database.
> **Returns:** `Table[]`
### `.createTable(table, rows)`
> Creates a new table in the database.
> | PARAMETER | TYPE | DESCRIPTION |
| :-------: | :-------------: | :----------------: |
| table | string | The new table name |
| rows | [Row](#row-2)[] | The new table rows |
> **Returns:** `{ table: string, rows: `[Row](#row-2)`[] }[]`
### `.deleteTable(table)`
> Drops a table from the database.
> | PARAMETER | TYPE | DESCRIPTION |
| :-------: | :----: | :------------------: |
| table | string | The MySQL table name |
> **Returns:** `boolean`
## **Row**
### `.select(table, condetions)`
> Retrieves data from a specific table with given conditions.
> | PARAMETER | TYPE | DESCRIPTION |
| :--------: | :----: | :-------------------------------: |
| table | string | The MySQL table |
| conditions | object | The conditions for data retrieval |
> **Returns:** `Data?`
### `.selectAll(table, condetions)`
> Retrieves all data from a specific table with given conditions.
> | PARAMETER | TYPE | DESCRIPTION |
| :--------: | :----: | :-------------------------------: |
| table | string | The MySQL table |
| conditions | object | The conditions for data retrieval |
> **Returns:** `Data[]`
### `.insert(table, values)`
> Inserts new data into a specific table.
> | PARAMETER | TYPE | DESCRIPTION |
| :-------: | :----: | :-----------------------: |
| table | string | The MySQL table |
| values | object | The values to be inserted |
> **Returns:** `Data`
### `.update(table, conditions, values)`
> Updates existing data in a specific table.
> | PARAMETER | TYPE | DESCRIPTION |
| :--------: | :----: | :----------------------------: |
| table | string | The MySQL table |
| conditions | object | The conditions for data update |
| values | object | The new values |
> **Returns:** `Data`
### `.delete(table, conditions)`
> Deletes data from a specific table based on given conditions.
> | PARAMETER | TYPE | DESCRIPTION |
| :--------: | :----: | :------------------------------: |
| table | string | The MySQL table |
| conditions | object | The conditions for data deletion |
> **Returns:** `boolean`
<br>
<br>
**Types**
## Row
> Represents a database row configuration.
> | Property | Type | Optional | Description |
| :------------ | :----------------- | :------- | :----------------------------- |
| name | string | ✖ | Column name |
| dataType | MySQLDataType | ✖ | Data type |
| dataLength | number | ✔ | Length of data (if applicable) |
| characterSet | MySQLCharacterSet | ✔ | Character set (if applicable) |
| collation | MySQLCollation | ✔ | Collation (if applicable) |
| allowNull | boolean | ✔ | Is NULL allowed |
| defaultValue | string | null | ✔ | Default value (if applicable) |
| primaryKey | boolean | ✔ | Is primary key |
| autoIncrement | boolean | ✔ | Is auto-incrementing |
| unique | boolean | ✔ | Is unique constraint |
## MySQL Data Types
> ### `MySQLDataType`
> Represents various MySQL data types.
> - `INT`
- `BIGINT`
- `FLOAT`
- `DOUBLE`
- `DECIMAL`
- `CHAR`
- `VARCHAR`
- `TEXT`
- `DATE`
- `TIME`
- `DATETIME`
- `TIMESTAMP`
- `YEAR`
- `BOOLEAN`
- `TINYINT`
- `SMALLINT`
- `MEDIUMINT`
- `ENUM`
- `SET`
- `BINARY`
- `VARBINARY`
- `BLOB`
- `JSON`
- `JSONB`
- `GEOMETRY`
## MySQL Character Sets
> ### `MySQLCharacterSet`
> Represents various MySQL character sets.
> - `big5`
- `dec8`
- `cp850`
- `hp8`
- `koi8r`
- `latin1`
- `latin2`
- `swe7`
- `ascii`
- `ujis`
- `sjis`
- `hebrew`
- `tis620`
- `euckr`
- `koi8u`
- `gb2312`
- `greek`
- `cp1250`
- `gbk`
- `latin5`
- `armscii8`
- `utf8`
- `ucs2`
- `cp866`
- `keybcs2`
- `macce`
- `macroman`
- `cp852`
- `latin7`
- `utf8mb4`
- `cp1251`
- `utf16`
- `utf16le`
- `cp1256`
- `cp1257`
- `utf32`
- `binary`
- `geostd8`
- `cp932`
- `eucjpms`
## MySQL Collations
> ### `MySQLCollation`
> Represents various MySQL collations.
> - `big5_chinese_ci`
- `big5_bin`
- `dec8_swedish_ci`
- `dec8_bin`
- `cp850_general_ci`
- `cp850_bin`
- `hp8_english_ci`
- `hp8_bin`
- `koi8r_general_ci`
- `koi8r_bin`
- `latin1_german1_ci`
- `latin1_swedish_ci`
- `latin1_danish_ci`
- `latin1_german2_ci`
- `latin1_bin`
- `latin1_general_ci`
- `latin1_general_cs`
- `latin1_spanish_ci`
- `latin2_czech_cs`
- `latin2_general_ci`
- `latin2_hungarian_ci`
- `latin2_croatian_ci`
- `latin2_bin`
- `swe7_swedish_ci`
- `swe7_bin`
- `ascii_general_ci`
- `ascii_bin`
- `ujis_japanese_ci`
- `ujis_bin`
- `sjis_japanese_ci`
- `sjis_bin`
- `hebrew_general_ci`
- `hebrew_bin`
- `tis620_thai_ci`
- `tis620_bin`
- `euckr_korean_ci`
- `euckr_bin`
- `koi8u_general_ci`
- `koi8u_bin`
- `gb2312_chinese_ci`
- `gb2312_bin`
- `greek_general_ci`
- `greek_bin`
- `cp1250_general_ci`
- `cp1250_czech_cs`
- `cp1250_croatian_ci`
- `cp1250_bin`
- `gbk_chinese_ci`
- `gbk_bin`
- `latin5_turkish_ci`
- `latin5_bin`
- `armscii8_general_ci`
- `armscii8_bin`
- `utf8_general_ci`
- `utf8_bin`
- `utf8_unicode_ci`
- `utf8_icelandic_ci`
- `utf8_latvian_ci`
- `utf8_romanian_ci`
- `utf8_slovenian_ci`
- `utf8_polish_ci`
- `utf8_estonian_ci`
- `utf8_spanish_ci`
- `utf8_swedish_ci`
- `utf8_turkish_ci`
- `utf8_czech_ci`
- `utf8_danish_ci`
- `utf8_lithuanian_ci`
- `utf8_slovak_ci`
- `utf8_spanish2_ci`
- `utf8_roman_ci`
- `utf8_persian_ci`
- `utf8_esperanto_ci`
- `utf8_hungarian_ci`
- `utf8_sinhala_ci`
- `utf8_german2_ci`
- `utf8_croatian_ci`
- `utf8_unicode_520_ci`
- `utf8_vietnamese_ci`
- `utf8_general_mysql500_ci`
- `ucs2_general_ci`
- `ucs2_bin`
- `ucs2_unicode_ci`
- `ucs2_icelandic_ci`
- `ucs2_latvian_ci`
- `ucs2_romanian_ci`
- `ucs2_slovenian_ci`
- `ucs2_polish_ci`
- `ucs2_estonian_ci`
- `ucs2_spanish_ci`
- `ucs2_swedish_ci`
- `ucs2_turkish_ci`
- `ucs2_czech_ci`
- `ucs2_danish_ci`
- `ucs2_lithuanian_ci`
- `ucs2_slovak_ci`
- `ucs2_spanish2_ci`
- `ucs2_roman_ci`
- `ucs2_persian_ci`
- `ucs2_esperanto_ci`
- `ucs2_hungarian_ci`
- `ucs2_sinhala_ci`
- `ucs2_german2_ci`
- `ucs2_croatian_ci`
- `ucs2_unicode_520_ci`
- `ucs2_vietnamese_ci`
- `ucs2_general_mysql500_ci`
- `cp866_general_ci`
- `cp866_bin`
- `keybcs2_general_ci`
- `keybcs2_bin`
- `macce_general_ci`
- `macce_bin`
- `macroman_general_ci`
- `macroman_bin`
- `cp852_general_ci`
- `cp852_bin`
- `latin7_estonian_cs`
- `latin7_general_ci`
- `latin7_general_cs`
- `latin7_bin`
- `utf8mb4_general_ci`
- `utf8mb4_bin`
- `utf8mb4_unicode_ci`
- `utf8mb4_icelandic_ci`
- `utf8mb4_latvian_ci`
- `utf8mb4_romanian_ci`
- `utf8mb4_slovenian_ci`
- `utf8mb4_polish_ci`
- `utf8mb4_estonian_ci`
- `utf8mb4_spanish_ci`
- `utf8mb4_swedish_ci`
- `utf8mb4_turkish_ci`
- `utf8mb4_czech_ci`
- `utf8mb4_danish_ci`
- `utf8mb4_lithuanian_ci`
- `utf8mb4_slovak_ci`
- `utf8mb4_spanish2_ci`
- `utf8mb4_roman_ci`
- `utf8mb4_persian_ci`
- `utf8mb4_esperanto_ci`
- `utf8mb4_hungarian_ci`
- `utf8mb4_sinhala_ci`
- `utf8mb4_german2_ci`
- `utf8mb4_croatian_ci`
- `utf8mb4_unicode_520_ci`
- `utf8mb4_vietnamese_ci`
- `ucs2mb4_general_ci`
- `ucs2mb4_bin`
- `ucs2mb4_unicode_ci`
- `ucs2mb4_icelandic_ci`
- `ucs2mb4_latvian_ci`
- `ucs2mb4_romanian_ci`
- `ucs2mb4_slovenian_ci`
- `ucs2mb4_polish_ci`
- `ucs2mb4_estonian_ci`
- `ucs2mb4_spanish_ci`
- `ucs2mb4_swedish_ci`
- `ucs2mb4_turkish_ci`
- `ucs2mb4_czech_ci`
- `ucs2mb4_danish_ci`
- `ucs2mb4_lithuanian_ci`
- `ucs2mb4_slovak_ci`
- `ucs2mb4_spanish2_ci`
- `ucs2mb4_roman_ci`
- `ucs2mb4_persian_ci`
- `ucs2mb4_esperanto_ci`
- `ucs2mb4_hungarian_ci`
- `ucs2mb4_sinhala_ci`
- `ucs2mb4_german2_ci`
- `ucs2mb4_croatian_ci`
- `ucs2mb4_unicode_520_ci`
- `ucs2mb4_vietnamese_ci`
- `cp1251_bulgarian_ci`
- `cp1251_ukrainian_ci`
- `cp1251_bin`
- `cp1251_general_ci`
- `cp1251_general_cs`
- `cp1251_general_mysql500_ci`
- `utf16_general_ci`
- `utf16_bin`
- `utf16_unicode_ci`
- `utf16_icelandic_ci`
- `utf16_latvian_ci`
- `utf16_romanian_ci`
- `utf16_slovenian_ci`
- `utf16_polish_ci`
- `utf16_estonian_ci`
- `utf16_spanish_ci`
- `utf16_swedish_ci`
- `utf16_turkish_ci`
- `utf16_czech_ci`
- `utf16_danish_ci`
- `utf16_lithuanian_ci`
- `utf16_slovak_ci`
- `utf16_spanish2_ci`
- `utf16_roman_ci`
- `utf16_persian_ci`
- `utf16_esperanto_ci`
- `utf16_hungarian_ci`
- `utf16_sinhala_ci`
- `utf16_german2_ci`
- `utf16_croatian_ci`
- `utf16_unicode_520_ci`
- `utf16_vietnamese_ci`
- `utf16le_general_ci`
- `utf16le_bin`
- `cp1256_general_ci`
- `cp1256_bin`
- `cp1257_lithuanian_ci`
- `cp1257_bin`
- `utf32_general_ci`
- `utf32_bin`
- `utf32_unicode_ci`
- `utf32_icelandic_ci`
- `utf32_latvian_ci`
- `utf32_romanian_ci`
- `utf32_slovenian_ci`
- `utf32_polish_ci`
- `utf32_estonian_ci`
- `utf32_spanish_ci`
- `utf32_swedish_ci`
- `utf32_turkish_ci`
- `utf32_czech_ci`
- `utf32_danish_ci`
- `utf32_lithuanian_ci`
- `utf32_slovak_ci`
- `utf32_spanish2_ci`
- `utf32_roman_ci`
- `utf32_persian_ci`
- `utf32_esperanto_ci`
- `utf32_hungarian_ci`
- `utf32_sinhala_ci`
- `utf32_german2_ci`
- `utf32_croatian_ci`
- `utf32_unicode_520_ci`
- `utf32_vietnamese_ci`
- `binary`
- `armscii8_general_ci`
- `armscii8_bin`
- `ascii_general_ci`
- `ascii_bin`
- `cp1250_general_ci`
- `cp1250_czech_cs`
- `cp1250_croatian_ci`
- `cp1250_bin`
- `cp1250_polish_ci`
- `cp1251_bulgarian_ci`
- `cp1251_ukrainian_ci`
- `cp1251_bin`
- `cp1251_general_ci`
- `cp1251_general_cs`
- `cp1251_general_mysql500_ci`
- `cp1251_croatian_ci`
- `cp1251_polish_ci`
- `cp1251_serbian_ci`
- `cp1251_macedonian_ci`
- `cp1251_general_cs`
- `cp1251_general_mysql500_ci`
- `cp1251_croatian_ci`
- `cp1251_polish_ci`
- `cp1251_serbian_ci`
- `cp1251_macedonian_ci`
- `cp1256_general_ci`
- `cp1256_bin`
- `cp1257_lithuanian_ci`
- `cp1257_bin`
- `cp850_general_ci`
- `cp850_bin`
- `cp852_general_ci`
- `cp852_bin`
- `cp866_general_ci`
- `cp866_bin`
- `cp932_japanese_ci`
- `cp932_bin`
- `dec8_swedish_ci`
- `dec8_bin`
- `euckr_korean_ci`
- `euckr_bin`
- `gb2312_chinese_ci`
- `gb2312_bin`
- `gbk_chinese_ci`
- `gbk_bin`
- `geostd8_general_ci`
- `geostd8_bin`
- `greek_general_ci`
- `greek_bin`
- `hebrew_general_ci`
- `hebrew_bin`
- `hp8_english_ci`
- `hp8_bin`
- `keybcs2_general_ci`
- `keybcs2_bin`
- `koi8r_general_ci`
- `koi8r_bin`
- `koi8u_general_ci`
- `koi8u_bin`
- `latin1_german1_ci`
- `latin1_swedish_ci`
- `latin1_danish_ci`
- `latin1_german2_ci`
- `latin1_bin`
- `latin1_general_ci`
- `latin1_general_cs`
- `latin1_spanish_ci`
- `latin2_czech_cs`
- `latin2_general_ci`
- `latin2_hungarian_ci`
- `latin2_croatian_ci`
- `latin2_bin`
- `latin5_turkish_ci`
- `latin5_bin`
- `latin7_estonian_cs`
- `latin7_general_ci`
- `latin7_general_cs`
- `latin7_bin`
- `macce_general_ci`
- `macce_bin`
- `macroman_general_ci`
- `macroman_bin`
- `sjis_japanese_ci`
- `sjis_bin`
- `swe7_swedish_ci`
- `swe7_bin`
- `tis620_thai_ci`
- `tis620_bin`
- `ucs2_general_ci`
- `ucs2_bin`
- `ucs2_unicode_ci`
- `ucs2_icelandic_ci`
- `ucs2_latvian_ci`
- `ucs2_romanian_ci`
- `ucs2_slovenian_ci`
- `ucs2_polish_ci`
- `ucs2_estonian_ci`
- `ucs2_spanish_ci`
- `ucs2_swedish_ci`
- `ucs2_turkish_ci`
- `ucs2_czech_ci`
- `ucs2_danish_ci`
- `ucs2_lithuanian_ci`
- `ucs2_slovak_ci`
- `ucs2_spanish2_ci`
- `ucs2_roman_ci`
- `ucs2_persian_ci`
- `ucs2_esperanto_ci`
- `ucs2_hungarian_ci`
- `ucs2_sinhala_ci`
- `ucs2_german2_ci`
- `ucs2_croatian_ci`
- `ucs2_unicode_520_ci`
- `ucs2_vietnamese_ci`
- `ujis_japanese_ci`
- `ujis_bin`
- `utf16_general_ci`
- `utf16_bin`
- `utf16_unicode_ci`
- `utf16_icelandic_ci`
- `utf16_latvian_ci`
- `utf16_romanian_ci`
- `utf16_slovenian_ci`
- `utf16_polish_ci`
- `utf16_estonian_ci`
- `utf16_spanish_ci`
- `utf16_swedish_ci`
- `utf16_turkish_ci`
- `utf16_czech_ci`
- `utf16_danish_ci`
- `utf16_lithuanian_ci`
- `utf16_slovak_ci`
- `utf16_spanish2_ci`
- `utf16_roman_ci`
- `utf16_persian_ci`
- `utf16_esperanto_ci`
- `utf16_hungarian_ci`
- `utf16_sinhala_ci`
- `utf16_german2_ci`
- `utf16_croatian_ci`
- `utf16_unicode_520_ci`
- `utf16_vietnamese_ci`
- `utf16le_general_ci`
- `utf16le_bin`
- `utf32_general_ci`
- `utf32_bin`
- `utf32_unicode_ci`
- `utf32_icelandic_ci`
- `utf32_latvian_ci`
- `utf32_romanian_ci`
- `utf32_slovenian_ci`
- `utf32_polish_ci`
- `utf32_estonian_ci`
- `utf32_spanish_ci`
- `utf32_swedish_ci`
- `utf32_turkish_ci`
- `utf32_czech_ci`
- `utf32_danish_ci`
- `utf32_lithuanian_ci`
- `utf32_slovak_ci`
- `utf32_spanish2_ci`
- `utf32_roman_ci`
- `utf32_persian_ci`
- `utf32_esperanto_ci`
- `utf32_hungarian_ci`
- `utf32_sinhala_ci`
- `utf32_german2_ci`
- `utf32_croatian_ci`
- `utf32_unicode_520_ci`
- `utf32_vietnamese_ci`
## MySQL Default Values
> ### `MySQLDefaultValue`
> Represents possible default values for MySQL columns.
> - `NONE`
- `NULL`
- `CURRENT_TIMESTAMP`
Solved Bugs 🐛
- ### **_✔_** The db wasn't accept the string characters `` (', ", `) ``
- ### **_✔_** When you delete a row you will got an error
- ### **_✔_** deleteAll doesn't work