-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
161 lines (152 loc) · 3.68 KB
/
main.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const faunadb = require('faunadb')
const q = faunadb.query;
module.exports = class FaunaService {
constructor(faunaSecret, domain) {
let opts = {
secret: faunaSecret
}
if(domain) {
opts.domain = domain
}
this.serverClient = new faunadb.Client(opts)
}
async createRecord(collectionName, data) {
let record = await this.serverClient.query(
q.Create(
q.Collection(collectionName),
{ data }
)
)
let recordData = record.data
recordData.id = record.ref.id
return recordData
}
async listRecords(collectionName) {
try {
let records = await this.serverClient.query(
q.Map(
q.Paginate(
q.Documents(q.Collection(collectionName))),
q.Lambda("X", q.Get(q.Var("X")))
)
)
let recordsData = []
if(records && records.data && records.data.length > 0) {
recordsData = records.data.map(el => {
return {
id: el.ref.id,
...el.data
}
})
}
return recordsData
} catch (err) {
console.error('FaunaService.listRecords:', err.toString())
throw err
}
}
async getRecordById(collectionName, recordId) {
try {
const record = await this.serverClient.query(
q.Get(
q.Ref(
q.Collection(collectionName),
recordId
)
)
)
let recordData = record.data
recordData.id = record.ref.id
return recordData
} catch (err) {
console.error('FaunaService.getRecordById:', err.toString())
throw err
}
}
async deleteRecord(collectionName, recordId) {
try {
await this.serverClient.query(
q.Delete(
q.Ref(
q.Collection(collectionName),
recordId
)
)
)
} catch (err) {
console.error('FaunaService.deleteRecord:', err.toString())
throw err
}
}
async updateRecord(collectionName, recordId, updates) {
try {
let updated = await this.serverClient.query(
q.Update(
q.Ref(
q.Collection(collectionName),
recordId,
),
{
data: updates
}
)
)
let recordData = updated.data
recordData.id = updated.ref.id
return recordData
} catch (err) {
console.error('FaunaService.updateRecord:', err.toString())
throw err
}
}
async getRecordByIndex(indexName, value) {
try {
let record = await this.serverClient.query(
q.Get(
q.Match(
q.Index(indexName),
value
)
)
)
let recordData = record.data
recordData.id = record.ref.id
return recordData
} catch (err) {
console.error('FaunaService.getRecordByIndex:', err.toString())
throw err
}
}
async fetchRecordsInIndex(indexName, value) {
try {
let records
if(value) {
records = await this.serverClient.query(
q.Map(
q.Paginate(q.Match(q.Index(indexName), value)),
q.Lambda("X", q.Get(q.Var("X")))
)
)
} else {
records = await this.serverClient.query(
q.Map(
q.Paginate(q.Match(q.Index(indexName))),
q.Lambda("X", q.Get(q.Var("X")))
)
)
}
if(records && records.data && records.data.length > 0) {
recordsData = records.data.map(el => {
return {
id: el.ref.id,
...el.data
}
})
}
return recordsData
} catch (err) {
console.error('FaunaService.fetchRecordsInIndex:', err.toString())
throw err
}
}
}