Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/domain.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

// Domain logic: message

let storage = null;

const init = (storageProvider) => {
storage = storageProvider;
};

const create = async ({ author, title, content, feed }) => {
const authorExists = await storage.has(author);
if (!authorExists) throw new Error('Author not found');

const feedExists = await storage.has(feed);
if (!feedExists) throw new Error('Feed not found');

const created = new Date().getTime();
const status = 'draft';
const post = { title, content, author, created, status };
const postId = await storage.insert(post);

return { postId };
};

const publish = async ({ postId }) => {
const postRecord = await storage.get(postId);
if (!postRecord) throw new Error('Post not found');
postRecord.status = 'published';
await postRecord.save();
};

module.exports = { init, create, publish };
23 changes: 23 additions & 0 deletions examples/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const globalStorage = require('../gs.js');
const domain = require('./domain.js');

const main = async () => {
// App start and init
const storage = await globalStorage.open();
domain.init(storage);

// Create post
const title = 'Example';
const content = 'Text';
const post = { author: '123', title, content, feed: '123' };
const postId = await domain.create(post);
console.log('Post created:', postId);

// Publish post
await domain.publish({ postId });
console.log('Post published');
};

main();