Skip to content

Commit 2af3cc0

Browse files
committed
Add domain code example
1 parent 9f676fa commit 2af3cc0

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

examples/domain.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
// Domain logic: message
4+
5+
let storage = null;
6+
7+
const init = (storageProvider) => {
8+
storage = storageProvider;
9+
};
10+
11+
const create = async ({ author, title, content, feed }) => {
12+
const authorExists = await storage.has(author);
13+
if (!authorExists) throw new Error('Author not found');
14+
15+
const feedExists = await storage.has(feed);
16+
if (!feedExists) throw new Error('Feed not found');
17+
18+
const created = new Date().getTime();
19+
const status = 'draft';
20+
const post = { title, content, author, created, status };
21+
const postId = await storage.insert(post);
22+
23+
return { postId };
24+
};
25+
26+
const publish = async ({ postId }) => {
27+
const postRecord = await storage.get(postId);
28+
if (!postRecord) throw new Error('Post not found');
29+
postRecord.status = 'published';
30+
await postRecord.save();
31+
};
32+
33+
module.exports = { init, create, publish };

examples/main.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const globalStorage = require('../gs.js');
4+
const domain = require('./domain.js');
5+
6+
const main = async () => {
7+
// App start and init
8+
const storage = await globalStorage.open();
9+
domain.init(storage);
10+
11+
// Create post
12+
const title = 'Example';
13+
const content = 'Text';
14+
const post = { author: '123', title, content, feed: '123' };
15+
const postId = await domain.create(post);
16+
console.log('Post created:', postId);
17+
18+
// Publish post
19+
await domain.publish({ postId });
20+
console.log('Post published');
21+
};
22+
23+
main();

0 commit comments

Comments
 (0)