client-manager-fido 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/app.js +109 -0
- package/package.json +18 -0
- package/src/models/ClientsModel.js +40 -0
- package/src/modules/postgres.js +18 -0
package/app.js
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
const postgres = require("./src/modules/postgres");
|
2
|
+
let psql = postgres();
|
3
|
+
|
4
|
+
module.exports.add_client = async function(name, surname, birth_date, age){
|
5
|
+
try{
|
6
|
+
psql = await psql;
|
7
|
+
|
8
|
+
let client = await psql.clients.create({
|
9
|
+
name,
|
10
|
+
surname,
|
11
|
+
birth_date,
|
12
|
+
age,
|
13
|
+
},{
|
14
|
+
raw: true
|
15
|
+
})
|
16
|
+
|
17
|
+
return({
|
18
|
+
id: client.id,
|
19
|
+
msg: "client added",
|
20
|
+
client
|
21
|
+
})
|
22
|
+
|
23
|
+
}catch(e){
|
24
|
+
console.log(e + "")
|
25
|
+
}
|
26
|
+
}
|
27
|
+
|
28
|
+
module.exports.edit_client = async function(id, name, surname, birth_date, age){
|
29
|
+
try{
|
30
|
+
psql = await psql;
|
31
|
+
|
32
|
+
let client = await psql.clients.findOne({
|
33
|
+
where:{
|
34
|
+
id,
|
35
|
+
}
|
36
|
+
})
|
37
|
+
|
38
|
+
if(!client) throw new Error("this client not found");
|
39
|
+
|
40
|
+
client = await psql.clients.update({
|
41
|
+
name,
|
42
|
+
surname,
|
43
|
+
birth_date,
|
44
|
+
age,
|
45
|
+
},{
|
46
|
+
where:{
|
47
|
+
id
|
48
|
+
}
|
49
|
+
})
|
50
|
+
|
51
|
+
return({
|
52
|
+
msg: "edited",
|
53
|
+
client
|
54
|
+
})
|
55
|
+
|
56
|
+
}catch(e){
|
57
|
+
console.log(e + "");
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
module.exports.delete_client = async function(id){
|
62
|
+
try{
|
63
|
+
psql = await psql;
|
64
|
+
|
65
|
+
let client = await psql.clients.findOne({
|
66
|
+
where:{
|
67
|
+
id,
|
68
|
+
}
|
69
|
+
})
|
70
|
+
|
71
|
+
if(!client) throw new Error("this client not found");
|
72
|
+
|
73
|
+
await psql.clients.destroy({
|
74
|
+
where: {
|
75
|
+
id
|
76
|
+
}
|
77
|
+
})
|
78
|
+
|
79
|
+
return({
|
80
|
+
msg: "deleted"
|
81
|
+
})
|
82
|
+
}catch(e){
|
83
|
+
console.log(e + "")
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
module.exports.get_element_by_id = async function(id){
|
88
|
+
try{
|
89
|
+
psql = await psql;
|
90
|
+
|
91
|
+
let client = await psql.clients.findOne({
|
92
|
+
where:{
|
93
|
+
id,
|
94
|
+
}
|
95
|
+
})
|
96
|
+
|
97
|
+
if(!client) throw new Error("this client not found");
|
98
|
+
|
99
|
+
return({
|
100
|
+
msg: "found",
|
101
|
+
name: client.name,
|
102
|
+
surname: client.surname,
|
103
|
+
birth_date: client.birth_date,
|
104
|
+
age: client.age,
|
105
|
+
})
|
106
|
+
}catch(e){
|
107
|
+
console.log(e + "")
|
108
|
+
}
|
109
|
+
}
|
package/package.json
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
{
|
2
|
+
"name": "client-manager-fido",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "",
|
5
|
+
"main": "app.js",
|
6
|
+
"scripts": {
|
7
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
8
|
+
},
|
9
|
+
"keywords": [
|
10
|
+
"client-manager-fido"
|
11
|
+
],
|
12
|
+
"author": "Bobajanov Xumoyun",
|
13
|
+
"license": "ISC",
|
14
|
+
"dependencies": {
|
15
|
+
"pg": "^8.7.3",
|
16
|
+
"sequelize": "^6.20.1"
|
17
|
+
}
|
18
|
+
}
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module.exports = async function(Sequelize, sequelize){
|
2
|
+
return sequelize.define("clients",{
|
3
|
+
id:{
|
4
|
+
type: Sequelize.DataTypes.UUID,
|
5
|
+
defaultValue: Sequelize.DataTypes.UUIDV4(),
|
6
|
+
primaryKey: true
|
7
|
+
},
|
8
|
+
|
9
|
+
name:{
|
10
|
+
type: Sequelize.DataTypes.STRING,
|
11
|
+
allowNull: false
|
12
|
+
},
|
13
|
+
|
14
|
+
surname:{
|
15
|
+
type: Sequelize.DataTypes.STRING,
|
16
|
+
allowNull: false
|
17
|
+
},
|
18
|
+
|
19
|
+
birth_date: {
|
20
|
+
type: Sequelize.DataTypes.DATE,
|
21
|
+
allowNull: false,
|
22
|
+
},
|
23
|
+
|
24
|
+
age: {
|
25
|
+
type: Sequelize.DataTypes.STRING,
|
26
|
+
allowNull: false,
|
27
|
+
},
|
28
|
+
|
29
|
+
created_date:{
|
30
|
+
type: Sequelize.DataTypes.DATE,
|
31
|
+
defaultValue: new Date(),
|
32
|
+
},
|
33
|
+
|
34
|
+
modified_date:{
|
35
|
+
type: Sequelize.DataTypes.DATE,
|
36
|
+
allowNull: true,
|
37
|
+
},
|
38
|
+
|
39
|
+
})
|
40
|
+
}
|
@@ -0,0 +1,18 @@
|
|
1
|
+
const db_url = 'postgres://jsdvrzqv:S4RMy1XSOStL_vZcv__cdCgM3H_2rzx8@jelani.db.elephantsql.com/jsdvrzqv';
|
2
|
+
const { Sequelize } = require("sequelize");
|
3
|
+
const sequelize = new Sequelize(db_url);
|
4
|
+
const ClientsModel = require("../models/ClientsModel");
|
5
|
+
|
6
|
+
module.exports = async function(){
|
7
|
+
try{
|
8
|
+
const db = {};
|
9
|
+
|
10
|
+
db.clients = await ClientsModel(Sequelize, sequelize);
|
11
|
+
|
12
|
+
sequelize.sync({alter: false})
|
13
|
+
|
14
|
+
return db;
|
15
|
+
}catch(e){
|
16
|
+
console.log(e);
|
17
|
+
}
|
18
|
+
}
|