npm package: @sol_o_o/next-starter
Description: easy next start pakage for newby
Author: hansol
Latest version: 1.6.7

![image](https://github.com/ouoHansol/img/blob/master/wellcome.png?raw=true)

![image](https://github.com/ouoHansol/img/blob/master/ThisProjectIsStillUnderDevelopment.png?raw=true)

# Next js Starter Integration Module For Newbie - Preparing isolated module

``` js

npm i @sol_o_o/next-starter
npm i @types/sharp --save-dev
npm i mysql2
npm i axios

```

<br/>

# 😸For those who are struggling to upload files

you can import<br/>
(Be careful. the sharp module has compatibility issues with the m1 chip of the mac! i feel the need to remove or change it.)

```js

import { solFs, solFsForm, sqlData } from '@sol_o_o/next-starter';
import formidable from 'formidable';

```

<br/>

## How to use

```js
const path : string = "yourPath";
const sizeName : string[] = ["_small", "_medium", "_large"];
const widthSize : number[] = [50, 100, 200];

const option = { multiples : true }

await solFsForm(req, formidable.IncomingForm(option), path, false, null, sizeName, widthSize);

```

## or

```js

const fileJson : fileJson = {};  // There's an explanation below
const fileList : fileType[] = [];
const path : string = "yourPath";
const sizeName : string[] = ["_small", "_medium", "_large"];
const widthSize : number[] = [50, 100, 200];

await solFs(fileJson, fileList, path, false, null, sizeName, widthSize);

```

<br/>

#### **✋ requirement**
For the form body value,<br/>
"file" and "fileJson" are required.<br/>
file is file, Below is the form of fileJson.<br/>

```js

export interface fileJson
{
  insert? : insertJson[],
  update? : updateJson[],
  delete? : deleteJson[]
}

interface insertJson
{
  /* Order of files, Use is your choice.However, this is a   required value, so if you do not want to use it, put a zero.*/
  order_no : number, 
  view_type : string // your option Feel free to add the value.
}

// Valid only when you use sqlData.
interface updateJson
{
  /* You can change the order_no in the file with seq_no eigenvalues. */
  seq_no : number,
  /* Order of files, Use is your choice.However, this is a required value, so if you do not want to use it, put a zero.*/
  order_no : number 
}

// Valid only when you use sqlData.
interface deleteJson
{
  /* You can delete a file using the seq_no eigenvalue. */
  seq_no : number
}

```

So that's what we need.

<br/>

**file is only one** 
> {"insert":[{"order_no":1, "view_type":"PROFILE"}], "update":[], "delete":[]}

<br/>

**file is multiples**
>{"insert":[{"order_no":1, "view_type":"PROFILE"}, {}, {}, ...], "update":[], "delete":[]}

<br/>

## Option
#1. sqlData
> You have a choice to sqlData
> > enter the information of the file auto uploaded to mysql

<br/>

##### 🎨 sqlData interface ▼
|user_id|tableName|targetColl|targetID|size_list
| --- | --- | --- | --- | --- |
| pk of upload user id | upload table name | pk column name | pk column value | image size name list

```js
const path : string = "yourPath";
const sizeName : string[] = ["_small", "_medium", "_large"];
const widthSize : number[] = [50, 100, 200];

const option = { multiples : true }

const sqlOption : boolean = true;
const sqlData : sqlData = {user_id : 1, tableName : "USER_FILE", targetColl : "user_id", targetID : 1, size_list : sizeName};

await solFs(req, formidable.IncomingForm(option), path, sqlOption, sqlData, sizeName, widthSize);
```

<br/>

#### **✋ requirement**
Must be Mysql or Maria DB to use sqlData Option!<br/>
also, <br/>
The structure of the table and column should always be as follows. <br/>
You can change the column name for all items, but the dataType must be followed.<br/>
(But "seq_no, edit_user_id, order_no, delete_state, update_dt" The item cannot be renamed for update... 😓)<br/>
For example, to match sqlData in the above code

<br/>

######  🎨 USER_FILE (TableName)
|user_id|seq_no|edit_user_id|file_path|file_name|file_type|file_size|content_type|order_no|view_type|delete_state|update_dt|create_dt
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| bigint | int | bigint | varchar | varchar | varchar | varchar | varchar | varchar | int | varchar | tinyint(1) | datetime | datetime |
| PK (targetColl) | PK(It is automatically increased) | id of the user who modified the content | path | file name | file extension | file length| mime type | file order | your option | delete = true | your update date | your create date |

<br/>

#2. sizeName, widthSize
> You have a choice to different sizes for image
> > Images created with these options are compressed

```js

const sizeName : string[] = ["_large"];
const widthSize : number[] = [500];

```
The string entered in widthSize is included between the original filename and the extension.<br/>
In other words, if the original file name was Hello.jpeg,<br/>
"Hello.jpeg"(original) and "Hello_large.jpeg" are created on the server.


<br/><br/>

# 😸 For those who are struggling to Database Connection

you can import

```js

import mysql from 'mysql2/promise';
import { solSelect, solInsert, getConn } from '@sol_o_o/next-starter';

```

<br/>

## How to use

make file to .env (There's no file name! It's just .env)<br/>
Location is the same space as package.json

``` js

DB_USER=your_db_username
DB_PASSWORD=your_db_userpassword
DB_HOST=your_db_host // maybe localhost? haha
DB_PORT=your_db_port // (The default is 3306.)
DB=your_db_name

```

</br>

you can source!

```js

// step 1 GetConn
const conn = await getConn(conn);

// stem 2 Connection
const sql = `SELECT hello_column FROM HELLO WHERE id = ? ;`;

const param = [1];
const cmd = await solSelect(conn, sql, param);

// if you need transaction
awiat conn.beginTransaction();

const insert_sql = `INSERT INTO HELLO (hello_column) VALUES ( ? );`;

const insert_param = [1];

try
{
    // solInsert for insert / update / delete
    const insert_cmd = await solInsert(conn, sql, param);

    conn.commit();
}
catch
{
    conn.rollback();
}
finally
{
    conn.release();
}

```


##### 🎨 cmd interface ▼
|conn|row|fields|
| --- | --- | --- | 
| connection data |  resultData | fileds | 

<br/>

#### 🐣 What does return "row" look like?
 * solInsert 
```js
 {
  fieldCount: 0,
  affectedRows: 1,
  insertId: 0, // This is the last automatic increment value. Maybe you'll use the most!
  info: '',
  serverStatus: 2,
  warningStatus: 0
}
```
*  solSelect
```js
 [
  { 
   your_column_name: 'your_coulmn_value' 
  }
]
```

<br/>

#### **✋ requirement**
1. Let's always release conn. <br/>
(It would be nice to finally do it in the syntax)

2. If you used transaction, recycle the conn that is open with getConn() ! <br/>
(If you always create a new one, your mysql process will increase a lot, If you are curious, check the show process list.)

<br/><br/>


# 😸 For those who are struggling to Random String And Number

you'll sometimes need a random number value, or a letter value, right?<br/>
It's a very simple code, but sometimes it's annoying to make it yourself.<br/>
that's why I just made it. 🤭<br/>
<br/>
It's a simple source code, so it's easy to use. <br/>
maybe you don't even need a guide.

<br/>
you can import

```js
import { solRan } from '@sol_o_o/next-starter';
```

<br/>

## How to use

```js
// you need only number (return example : "1524816042")
const you_need_number_length = 10;
const rand_num = Number(await solRan("num", you_need_number_length));


// you need only string (return example : "VSfDkz")
const you_need_char_length = 6;
const rand_num = await solRan("char", you_need_char_length);


// you need mix string and number (return example : "1Fd5Y")
const you_need_length = 5;
const rand_num = await solRan("mix", you_need_length);

```


<br/><br/>

# 😸 Simple Convert And Widely Used

oh...
This is too simple, too.<br/>
It's a source code you can make, but...there are times when it's annoying, right?<br/>
use it when that happens.<br/>
It'll be added whenever I'm bored or need anything.

<br/>

you can import

```js

import { unixToDateTime, convertDateStr, convertDateTimeStr, unixtoDateTimeStr } from '@sol_o_o/next-starter';

```

<br/>

## How to use

```js

// return example : Date Type value!!
const unixDate : Number = 1673942231;
const unixDateTime = unixToDateTime(value.lat)

// return example : "2022-01-01 12:11:11"
const unixDate : Number = 1673942231;
const unixDateTime = unixtoDateTimeStr(value.lat)

// return example : "2022-01-01 12:11:11"
const dateTimehaha : Date = new Date();
const datetimeStr = convertDateTimeStr(dateTimehaha);

// return example : "2022-01-01"
const dateTime : Date = new Date();
const datetimeStr = convertDateStr(dateTimehaha);


```

<br/><br/>

# 😸 For smooth transmission of results
We need a result delivery form.<br/>
let's go with the unified one! (objects and types that always change... 🤔)

<br/>
you can import.....(but there are so many import options. explain with option.)<br/>
basic for eventResult!

```js

import { eventResult } from '@sol_o_o/next-starter';

```

<br/>

##### 🎨 eventResult interface ▼
|  | requestMethod | resultCode | resultMsg | resultRow | resultData | resultException | cmd |
| --- | --- | --- | --- |  --- |  --- |  --- |  --- | 
| type | string | number |  string | RowDataPacket[] OR ResultSetHeader | any | any | insertResult OR selectResult
| description | auto (is mine) | for your resultcode |  for your msg | have you seen the database communication above? It's the row value returned from there. | whatever you want to contain | write an error (i usually use exception error here.) | have you seen the database communication above? It's the cmd value returned from there

<br/>

## How to use

```js

// hmmmm... just... so... Anyway! I use it like this.

let eventResult : eventResult = await yourMethod();

if(eventResult.resultCode == 0)
{
    // Sucess
}
else
{
    // Fail
}

// OR

const conn = await getConn(conn);

const sql = `your sql`;
const param = ["hi"];
const cmd = await solInsert(sql, param);

try
{
    conn.commit();
    
    // this is the option I mentioned earlier. of course, it's an eventResult type.
    return E0(cmd);
}
catch(err)
{
    conn.rollback();
    return E1001(err);
}
finally
{
    conn?.release;
}

```

<br/>

## Option
#1. Basically defined, for write comfortably.
> E0(cmd? : insertResult | selectResult)
> >  return -> resultCode: 0, resultMsg : "Complete", resultRow : cmd.row

<br/>

> E1001(exception? : any, cmd? : insertResult | selectResult)
> > return -> resultCode: 1001, resultMsg : "Server Error : Exception", resultException: exception, cmd : cmd

<br/>

....There's a lot more. 1002, 1005...2001,2002...3001... but it's in Korean! I'll fix it later. haha <br/>
I think you should just make it and use it. <br/>
Just make an error theorem ts, just turn it back. <br/>
I do that! <br/>
For example <br/>

<br/>

```js

export const E1009 = () => 
{
    return Result(1009, "Exist Data");
}

```

I define the code that I use often.
oh what's that Result()?


```js

Result(resultCode? : number, resultMsg? : string, resultRow? : RowDataPacket[] | ResultSetHeader, resultData? : {}, resultException? : any, cmd? : insertResult | selectResult)

```
<br/>

It's like this. made so that you can customize the eventResult as you please.<br/>
hm....  Or use ECustom. (If you use it for one time!)

<br/>

```js

ECustom(yourCode : number, yourMsg? : string, resultData? : any, resultRow? : RowDataPacket[] | ResultSetHeader)

```

<br/>

```js

const result = ECustom(1009, "Exist Data");

```


<br/>


#### 🐣 What does return eventResult look like?

If you complete everything, you can have pretty results. maybe. <br/>
(i think it's pretty no matter what anyone says)

```js

{
  "code": 0,
  "data": {
    "requestMethod": "at getYourLike ()",
    "resultCode": 0,
    "resultMsg": "Complete",
    "resultRow": [
      {
        "yourNumber": 1,
        "yourColumn": "Hello"
      },
      {
        "yourNumber": 2,
        "yourColumn": "World!"
      }
    ]
  },
  "message": "success"
}

```

what do you think? It's pretty, right?

<br/><br/>

## 😁 Here are my comments.

if you need anything, I'll make another one next time. I think this project still has a lot to fix.<br/>
i am most concerned about the compatibility of the m1 chip of the Sharp module.<br/>
Let me know if there are any complementary points.