-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproduct-service.js
47 lines (41 loc) · 1013 Bytes
/
product-service.js
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
import faunadb from 'faunadb'
const q = faunadb.query
export class ProductService {
constructor(data) {
this.client = data.client
}
async getProducts() {
return new Promise((resolve, reject) => {
this.client
.query(q.Paginate(q.Match(q.Ref('indexes/all_products'))))
.then((response) => {
const productRefs = response.data
const getAllProductDataQuery = q.Map(productRefs, q.Lambda(['ref'], q.Get(q.Var('ref'))))
this.client.query(getAllProductDataQuery).then((ret) => {
resolve(ret)
})
})
.catch((error) => {
console.log('error', error)
reject(error)
})
})
}
async getProductById(id) {
return new Promise((resolve, reject) => {
if (!id) {
reject('No ID provided')
}
this.client
.query(q.Get(q.Ref(q.Collection('products'), id)))
.then((response) => {
console.log('response', response)
resolve(response)
})
.catch((error) => {
console.log('error', error)
reject(error)
})
})
}
}