-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (47 loc) · 1.39 KB
/
index.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
const fortune = require('fortune');
const jsonApiSerializer = require('fortune-json-api');
const nedbAdapter = require('fortune-nedb');
const http = require('http');
const models = {
user: {
name: { type: String },
// Following and followers are inversely related (many-to-many).
following: { link: 'user', inverse: 'followers', isArray: true },
followers: { link: 'user', inverse: 'following', isArray: true },
// Many-to-one relationship of user posts to post author.
posts: { link: 'post', inverse: 'author', isArray: true }
},
post: {
message: { type: String },
date: { type: Date },
// One-to-many relationship of post author to user posts.
author: { link: 'user', inverse: 'posts' }
}
};
const transforms = {
post: [
(context, record, update) => {
const method = context.request.method;
if (method === 'create') {
record.date = new Date();
return record;
}
return update || null;
},
(context, record) => {
record.date = record.date && record.date.toLocaleString();
return record;
}
]
};
const store = fortune(models, {
adapter: [ nedbAdapter, { dbPath: 'data/example' } ],
transforms
});
const listener = fortune.net.http(store, {
serializers: [
[ jsonApiSerializer ]
]
});
const server = http.createServer(listener);
store.connect().then(() => server.listen(1337));