-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5a6c52f
Showing
8 changed files
with
1,857 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
node_modules | ||
connection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Copyright © 2022 Yadi Apriyadi | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
<div id="header" align="center"> | ||
<img src="https://github.com/YadBro/tes/blob/master/LOGO.jpg?raw=true" width="200"/> | ||
</div> | ||
|
||
# What is this? | ||
|
||
This is a package module for querying the postgresql database in Framework [EXPRESSjs](http://expressjs.com/) | ||
|
||
There are 4 CRUD queries in this package. | ||
|
||
- [selectAll](#selectAll) = Select all rows from a table. | ||
- [selectOneById](#selectOneById) = Select single data in your table database. | ||
- [save](#save) = insert or update a row in a table. | ||
- [remove](#remove) = remove a row from a table. | ||
|
||
# Installation | ||
|
||
Use npm, make sure your npm is up to date. | ||
To install this package, run the following command: | ||
|
||
``` | ||
npm install expostsql-queries | ||
``` | ||
|
||
# Usage / Examples | ||
|
||
( = ) is detail information\ | ||
( : ) is default value | ||
|
||
```javascript | ||
import { SetUp } from "expostsql-queries"; | ||
import express from "express"; | ||
|
||
/* | ||
Set Up your database postgresql | ||
* @param {string} dbname = your database name | ||
* @param {number} port = your port postgresql : 5432 | ||
* @param {string} user = your username postgresql : postgres | ||
* @param {string} password = your password postgresql | ||
*/ | ||
const client = new SetUp("b34s_chapter2", 5432, "postgres", "mypassword"); | ||
|
||
const table_name = "tb_project"; | ||
``` | ||
|
||
### selectAll | ||
|
||
Select all rows in your table database | ||
|
||
- @param {string} table_name = your table name database | ||
- @param {()} callback | ||
- @param {boolean} stop = stopping the client requiring : false | ||
|
||
```javascript | ||
// READ | ||
client.selectAll( | ||
table, | ||
(data) => { | ||
console.log(data); | ||
}, | ||
true | ||
); | ||
``` | ||
|
||
### selectOneById | ||
|
||
Select single data in your table database | ||
|
||
- @param {string} table_name = your table name database | ||
- @param {number} id = your id database | ||
- @param {()} callback | ||
- @param {boolean} stop = stopping the client requiring : false | ||
|
||
```javascript | ||
// GET SINGLE DATA | ||
client.save( | ||
table_name, | ||
[ | ||
{ | ||
name: "NEW DATA", | ||
start_date: "2022-08-12", | ||
end_date: "2022-08-13", | ||
description: "ANJAY 99 baru", | ||
image: "tes_baru99.jpg", | ||
technologies: "{nodeJsTechnology, reactJsTechnology}", | ||
}, | ||
], | ||
true | ||
); | ||
``` | ||
|
||
### save | ||
|
||
Create or Update your field database | ||
|
||
- @param {string} table_name = your table name database | ||
- @param {()} callback | ||
- @param {boolean} = stop stopping the client requiring : false | ||
|
||
```javascript | ||
// CREATE OR UPDATE | ||
|
||
/// CREATE | ||
client.save(table_name, [ | ||
{ | ||
name: "NEW DATA", | ||
start_date: "2022-08-12", | ||
end_date: "2022-08-13", | ||
description: "ANJAY 99 baru", | ||
image: "tes_baru99.jpg", | ||
technologies: "{nodeJsTechnology, reactJsTechnology}", | ||
}, | ||
]); | ||
|
||
/// UPDATE | ||
client.save( | ||
table_name, | ||
[ | ||
{ | ||
id: 1, | ||
name: "NEW DATA", | ||
start_date: "2022-08-12", | ||
end_date: "2022-08-13", | ||
description: "ANJAY 99 baru", | ||
image: "tes_baru99.jpg", | ||
technologies: "{nodeJsTechnology, reactJsTechnology}", | ||
}, | ||
], | ||
true | ||
); | ||
``` | ||
|
||
### remove | ||
|
||
Remove your field database | ||
|
||
- @param {string} table_name = your table name database | ||
- @param {number} id = your id database | ||
- @param {boolean} stop = stopping the client requiring : false | ||
|
||
```javascript | ||
// REMOVING DATA | ||
client.remove(table_name, 9, true); | ||
``` | ||
|
||
## License | ||
|
||
Copyright © 2022 Yadi Apriyadi | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
## | ||
|
||
[POSTGRESQL](https://www.postgresql.org/) | ||
|
||
[EXPRESSJS](http://expressjs.com/) | ||
|
||
[pg](https://github.com/brianc/node-postgres) | ||
|
||
[node-postgresql](https://node-postgres.com/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import pkg from "pg"; | ||
|
||
const IdT = "Your id request, it doesn't exist!"; | ||
|
||
export class SetUp{ | ||
/** | ||
*Set Up your database postgresql | ||
* | ||
* @param {string} dbname | ||
* @param {number} port 5432 | ||
* @param {string} user postgres | ||
* @param {string} password ***** | ||
*/ | ||
constructor(dbname, port, user, password){ | ||
const {Pool} = pkg; | ||
this.dbname = dbname; | ||
this.port = 5432 || port; | ||
this.user = 'postgres' || user; | ||
this.password = password; | ||
this.client = new Pool({ | ||
database: dbname, | ||
port: 5432 || port, | ||
user: 'postgres' || user, | ||
password: password | ||
}); | ||
this.client.connect((err, client, done) => { | ||
if (err) throw err; | ||
done() | ||
}); | ||
} | ||
/** | ||
* Checking rowCount data | ||
* @param {string} table_name your table name database | ||
* @param {number} id your id database | ||
* @param {()} callback | ||
*/ | ||
checking(table_name, id, callback){ | ||
if(id){ | ||
let query = `SELECT * FROM ${table_name} WHERE id=${id}`; | ||
this.client.query(query, (err, result)=>{ | ||
if(result.rowCount === 0){ | ||
callback(true); | ||
} | ||
else{ | ||
callback(false); | ||
} | ||
}); | ||
}else{ | ||
callback(false) | ||
} | ||
} | ||
/** | ||
* Select all rows in your table database | ||
* @param {string} table_name your table name database | ||
* @param {()} callback | ||
* @param {boolean} stop stopping the client requiring | ||
*/ | ||
selectAll(table_name, callback, stop=false){ | ||
let query = `SELECT * FROM ${table_name}`; | ||
this.client.query(query, (err, result)=>{ | ||
if(err) throw err | ||
if(stop === true){ | ||
this.client.end(); | ||
} | ||
callback(result.rows); | ||
}); | ||
|
||
} | ||
|
||
/** | ||
* Select single data in your table database | ||
* @param {string} table_name your table name database | ||
* @param {number} id your id database | ||
* @param {()} callback | ||
* @param {boolean} stop stopping the client requiring | ||
*/ | ||
selectOneById(table_name, id, callback, stop=false){ | ||
let query = `SELECT * FROM ${table_name} WHERE id=${id}`; | ||
this.client.query(query, (err, result)=>{ | ||
if(result.rowCount === 0){ | ||
if(err) throw err | ||
this.client.end() | ||
return console.log(`SELECTING ERROR: ${IdT}`) | ||
}else{ | ||
if(stop === true){ | ||
this.client.end(); | ||
} | ||
callback(result.rows); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Create or Update your field database | ||
* @param {string} table_name your table name database | ||
* @param {()} callback | ||
* @param {boolean} stop stopping the client requiring | ||
*/ | ||
save(table_name, data, stop=false){ | ||
let query = ``; | ||
let a= ``; | ||
let d= ``; | ||
const dataID = data[0].id; | ||
this.checking(table_name, dataID, checked=> | ||
{ | ||
// CHECK APAKAH ID ADA ATAU TIDAK | ||
if(checked === true){ | ||
this.client.end(); | ||
return console.log(`SAVING ERROR: ${IdT}`); | ||
}else if(checked === false){ | ||
data.map(m => { | ||
const b = Object.keys(m); | ||
const c = Object.values(m); | ||
|
||
if(b[0] === 'id'){ | ||
for(let i = 1; i < b.length; i++){ | ||
a += `${b[i]} = '${c[i]}', `; | ||
}; | ||
const slicing = a.slice(0, -2); // menghapus koma di belakang | ||
query += `UPDATE ${table_name} SET ${slicing} WHERE id='${c[0]}'`; | ||
}else{ | ||
for(let i = 0; i < b.length; i++){ | ||
a += `${b[i]}, ` | ||
d += `'${c[i]}', ` | ||
} | ||
const slicing = a.slice(0, -2); // menghapus koma di belakang | ||
const slicingc = d.slice(0, -2); // menghapus koma di belakang | ||
query += `INSERT INTO ${table_name}(${slicing}) VALUES(${slicingc})`; | ||
} | ||
}); | ||
this.client.query(query, (err, result)=>{ | ||
if(err) throw err | ||
if(stop === true){ | ||
this.client.end(); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
/** | ||
* Remove your field database | ||
* @param {string} table_name your table name database | ||
* @param {number} id your id database | ||
* @param {boolean} stop stopping the client requiring | ||
*/ | ||
remove(table_name, id, stop=false){ | ||
this.checking(table_name, id, checked =>{ | ||
// CHECK APAKAH ID ADA ATAU TIDAK | ||
if(checked === true){ | ||
this.client.end(); | ||
return console.log(`DELETING ERROR: ${IdT}`); | ||
}else if(checked === false){ | ||
let query = `DELETE FROM ${table_name} WHERE id=${id}`; | ||
this.client.query(query, (err, result)=>{ | ||
if(err) throw err | ||
if(stop === true){ | ||
this.client.end(); | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.