-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.js
More file actions
55 lines (47 loc) · 1.23 KB
/
Copy pathstore.js
File metadata and controls
55 lines (47 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const knex = require('knex')(require('./knexfile'))
const createProduct = ({name, description, price}) => {
console.log(`
Adding product:
- Name: ${name}
- Description: ${description}
- Price: ${price}
`);
return knex('product').insert({
name,
description,
price
}).then(insertedIds => {
if (insertedIds.length > 0) {
return knex('product').where('id', insertedIds[0]);
}
});
};
const updateProduct = ({id, name, description, price}) => {
return knex('product').update({
name,
description,
price
}).where({id}).then(rowsCount => {
return knex('product').where({id});
});
}
const deleteProduct = (id) => {
return knex('product').delete().where({id});
}
const countProducts = () => {
return knex('product').count('id AS total_count').first();
}
const getProducts = ({limit, offset, orderBy}) => {
return knex('product').limit(limit).offset(offset).orderBy(orderBy[0], orderBy[1]);
}
const getProduct = (id) => {
return knex('product').where({id: id}).first();
}
module.exports = {
countProducts,
createProduct,
updateProduct,
deleteProduct,
getProducts,
getProduct
}