-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
102 lines (86 loc) · 1.88 KB
/
index.ts
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
import express, { Request, Response } from 'express';
import faunadb from 'faunadb';
import dotenv from 'dotenv';
dotenv.config();
const app = express();
const client = new faunadb.Client({
secret: <string>process.env.FAUNA_KEY
});
const {
Call,
Collection,
Create,
Get,
Index,
Join,
Lambda,
Match,
Paginate,
Ref,
Select,
Var,
Function: Fn
} = faunadb.query;
app.get('/posts/:username', async (req: Request, res: Response) => {
const document = await client.query(
Paginate(
Match(
Index('posts_by_user'),
Call(Fn('getUser'), req.params.username)
)
)
).catch(e => res.send(e));
res.send(document);
});
app.get('/post/:id', async (req: Request, res: Response) => {
const document = await client.query(
Get(
Ref(
Collection('posts'), req.params.id
)
)
).catch(e => res.send(e));
res.send(document);
});
app.post('/post', async (req: Request, res: Response) => {
const data = {
user: Call(Fn('getUser'), 'egocentryk'),
text: 'This is an example post'
}
const document = await client.query(
Create(
Collection('posts'), { data }
)
).catch(e => res.send(e));
res.send(document);
});
app.post('/relationship', async (req: Request, res: Response) => {
const data = {
follower: Call(Fn('getUser'), 'jane'),
followee: Call(Fn('getUser'), 'egocentryk')
}
const document = await client.query(
Create(
Collection('relationships'), { data }
)
);
res.send(document);
});
app.get('/feed', async (req: Request, res: Response) => {
const document = await client.query(
Paginate(
Join(
Match(
Index('followers_by_followee'),
Call(Fn('getUser'), 'jane')
),
Index('posts_by_user')
)
)
);
res.send(document);
});
app.listen(
5000,
() => console.log('API on https://localhost:5000')
)