-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFaunaConnection.js
322 lines (300 loc) · 9.14 KB
/
FaunaConnection.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*------------------------------------------------------------------------
Copyright 2020 William F Brinkert
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-------------------------------------------------------------------------*/
const faunadb = require('faunadb')
/**
* A wrapper class for using the faunuDB javascript API
*/
class FaunaConnection {
constructor(options) {
const { secret } = options
this.q = faunadb.query
this.client = new faunadb.Client({ secret })
this.size = 64;
}
/**
* If the client was instantiated using an admin key
* this will create a database with the given name.
*
* @param {string} name the name to use for the database
* @return {Object} Response object with database name and ref
*/
createDB(name) {
return this.client.query(
this.q.CreateDatabase({ name: name })
)
}
/**
* If the client was instantiated with at least a
* database key, this will return a server role for
* the given named database.
*
* @param {string} dbName the name of the database to create a server for
* @return {Object} Response object with database name and ref
*/
createServer(dbName) {
return this.client.query(
this.q.CreateKey({
database: this.q.Database(dbName),
role: 'server'
})
)
}
/**
* Creates a collection of the given name
*
* @param {string} name A descriptive name for the collection
* @return {Object} Reference to collection object
*/
createCollection(name) {
return this.client.query(
this.q.CreateCollection({ name: name})
)
}
/**
* Creates an index for use on a collection
*
* @param {string} name descriptive name for the index
* @param {string} src the name of the collection to apply this to
* @param {Array} terms the terms to be used for the index
* @param {Array} values the values to be used for the index
* @return {Object} the index object
*/
createIndex(name, src, terms=null, values=null) {
return this.client.query(
this.q.CreateIndex({
name: name,
source: this.q.Collection(src),
terms: terms,
values: values
})
)
}
/**
* Creates a document within the named collection given either single doc
* or an array of documents.
* @param {string} collection name of the collection to put the doc into
* @param {Object} doc the document to put in the database collection
* @return {Object} the document that was inserted
*/
create(collection, docs) {
const documents = Array.isArray(docs) ? docs : [docs]
return this.client.query(
this.q.Map(
documents,
this.q.Lambda(
'doc',
this.q.Create(
this.q.Collection(collection),
{ data: this.q.Var('doc') }
)
)
)
)
}
/**
* Given an array of touples [custom id,document] will iterate through
* and place each into the given collection in the database with the
* custom id is the 'ref' field for the document.
*
* @param {string} collection name of the collection to put the docs into
* @param {Array} docs the documents to put in the database collection
* @return {Array} an array of the documents put in the database
*/
createWithCustomID(collection, touples) {
return this.client.query(
this.q.Map(
touples,
this.q.Lambda(
['id', 'data'],
this.q.Create(
this.q.Ref(this.q.Collection(collection), this.q.Var('id')),
{ data: this.q.Var('data') }
)
)
)
)
}
/**
* Retrieve a document given its ref and collection
*
* @param {string} collection the name of the collection
* @param {string} ref the ID or ref of the document
* @return {Object} the document identified by the query
*/
get(collection, ref) {
return this.client.query(
this.q.Get(
this.q.Ref(
this.q.Collection(collection),
ref
)
)
)
}
/**
* Retrieve the first document that matches the given value
* of a term used to create the index. For example, if we
* have an index called "posts_by_title" whose term was set
* to "title", we can search that index using a value of
* "My Blog Post" to see if any posts' titles match our value.
*
* @param {string} index name of index to search on
* @param {multiple} value the value to match on the index term
* @return {Object} the first matching document
*/
getMatch(index, value) {
return this.client.query(
this.q.Get(
this.q.Match(
this.q.Index(index),
value
)
)
)
}
/**
* Given an index 'all_<items>' with no set terms or values,
* this will return the refs of all documents in that index.
*
* @param {string} index name of the index to use
* @param {integer} size number of docs to be returned per page
* @return {Array} an array of the refs to the docs within
* the index
*/
getAllRefsByIndex(index, size) {
return this.client.query(
this.q.Paginate(
this.q.Match(
this.q.Index(index)
),
{ size: size || this.size }
)
)
}
_buildRef(options) {
return this.q.Ref(this.q.Collection(options.collection), options.ref)
}
/**
* Provided with an index, returns all matching documents
*
* @param {string} index name of the index
* @param {Object} options options that may be passed in include:
* before - ref to document for traversing pagination backward
* after - ref to document for traversing pagination forward
* term - a such term to use with a searchable index
* size - the number of docs to return per page
* scope - the database ref within which to perform the match
* @return {Object} Returns a collection of docs matching
* the query conditions
*/
getAllDocsByIndex(index, options = {}) {
if (options.before && options.before.collection) {
options.before = this._buildRef(options.before)
}
else if (options.after && options.after.collection) {
options.after = this._buildRef(options.after)
}
return this.client.query(
this.q.Map(
this.q.Paginate(
this.q.Match(
this.q.Index(index, options.scope),
options.term
),
{
size: options.size || this.size,
before: options.before,
after: options.after
}
),
ref => this.q.Get(ref)
)
)
}
/**
* Given the ref and collection this will update the
* data of that document.
*
* @param {string} collection the name of the collection
* @param {string} ref the ID or ref of the doc
* @param {Object} data the data to be updated
* @return {Object} the updated document
*/
update(collection, ref, data) {
return this.client.query(
this.q.Update(
this.q.Ref(
this.q.Collection(collection),
ref
),
{ data : data }
)
)
}
/**
* Given the ref and collection this will replace the
* data of that document whose fields match the given data.
* Any old fields in the document that are not mentioned in
* the data param are removed.
*
* @param {string} collection the name of the collection
* @param {string} ref the ID or ref of the doc
* @param {Object} data the data to be replace
* @return {Object} the updated document
*/
replace(collection, ref, data) {
return this.client.query(
this.q.Replace(
this.q.Ref(
this.q.Collection(collection),
ref
),
{ data: data }
)
)
}
/**
* Destroys a document
*
* @param {string} collection the name of the collection
* @param {string} ref the ID or ref of the doc
* @return {Object} the deleted document
*/
delete(collection, ref) {
return this.client.query(
this.q.Delete(
this.q.Ref(
this.q.Collection(collection),
ref
)
)
)
}
paginateDB(dbName, size) {
return this.client.query(
this.q.Paginate(
this.q.Database(dbName),
{ size: size || this.size }
)
)
}
getDatabase(dbName) {
return this.client.query(
this.q.Get(
this.q.Database(dbName)
)
)
}
}
module.exports = FaunaConnection