/
fweetstats.js
49 lines (41 loc) · 1.78 KB
/
fweetstats.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
const faunadb = require('faunadb')
const q = faunadb.query
const { Paginate, Lambda, Documents, Var, CreateCollection, CreateIndex, Collection, Exists, If, Index, Delete } = q
/* Collection */
const CreateFweetStatsCollection = CreateCollection({ name: 'fweetstats' })
/* Indexes */
const CreateIndexFweetsStatsByUserAndFweet = CreateIndex({
name: 'fweetstats_by_user_and_fweet',
source: Collection('fweetstats'),
// We keep a collection to store which fweets that have been liked (or in a later phase refweeted) by users.
// Wait.. Couldn't we just store this as an array i fweets?
// { data:
// {
// likedby: [ <userid>, <userid> ]
// }
// }
// Although it's possible and you coudl index on data.likedby it's not a good idea in terms of performance.
// This list might grow to become very big which would make it hard inefficient to remove an element from the list.
terms: [
{
field: ['data', 'user']
},
{
field: ['data', 'fweet']
}
],
serialized: true
})
async function createFweetStatsCollection(client) {
await client.query(If(Exists(Collection('fweetstats')), true, CreateFweetStatsCollection))
await client.query(If(Exists(Index('fweetstats_by_user_and_fweet')), true, CreateIndexFweetsStatsByUserAndFweet))
}
async function deleteFweetStatsCollection(client) {
await client.query(If(Exists(Collection('fweetstats')), Delete(Collection('fweetstats')), true))
await client.query(If(Exists(Index('fweetstats_by_user_and_fweet')), Delete(Index('fweetstats_by_user_and_fweet')), true))
}
const DeleteAllFweetStats = If(
Exists(Collection('fweetstats')),
q.Map(Paginate(Documents(Collection('fweetstats'))), Lambda('ref', Delete(Var('ref')))), true
)
export { DeleteAllFweetStats, createFweetStatsCollection, deleteFweetStatsCollection }