Skip to content
Draft
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
9 changes: 9 additions & 0 deletions src/server/example/cron.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { time } from 'modelence';

export const dailyTestCron = {
description: 'Daily cron job example',
interval: time.days(1),
handler: async () => {
// This is just an example. Any code written here will run daily.
},
};
1 change: 1 addition & 0 deletions src/server/example/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const dbExampleItems = new Store('exampleItems', {
schema: {
title: schema.string(),
createdAt: schema.date(),
userId: schema.userId(),
},
indexes: []
});
72 changes: 70 additions & 2 deletions src/server/example/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,86 @@
import z from 'zod';
import { Module, ObjectId } from 'modelence/server';
import { AuthError } from 'modelence';
import { Module, ObjectId, UserInfo, getConfig } from 'modelence/server';
import { dbExampleItems } from './db';
import { dailyTestCron } from './cron';

export default new Module('example', {
configSchema: {
itemsPerPage: {
type: 'number',
default: 5,
isPublic: false,
},
},

stores: [dbExampleItems],

queries: {
getItem: async (args: unknown) => {
getItem: async (args: unknown, { user }: { user: UserInfo | null }) => {
if (!user) {
throw new AuthError('Not authenticated');
}

const { itemId } = z.object({ itemId: z.string() }).parse(args);
const exampleItem = await dbExampleItems.requireOne({ _id: new ObjectId(itemId) });

if (exampleItem.userId.toString() !== user.id) {
throw new AuthError('Not authorized');
}

return {
title: exampleItem.title,
createdAt: exampleItem.createdAt,
};
},

getItems: async (_args: unknown, { user }: { user: UserInfo | null }) => {
if (!user) {
throw new AuthError('Not authenticated');
}

const itemsPerPage = getConfig('example.itemsPerPage') as number;
const exampleItems = await dbExampleItems.fetch({}, { limit: itemsPerPage })
return exampleItems.map((item) => ({
_id: item._id.toString(),
title: item.title,
createdAt: item.createdAt,
}));
}
},

mutations: {
createItem: async (args: unknown, { user }: { user: UserInfo | null }) => {
if (!user) {
throw new AuthError('Not authenticated');
}

const { title } = z.object({ title: z.string() }).parse(args);

await dbExampleItems.insertOne({ title, createdAt: new Date(), userId: new ObjectId(user.id) });
},

updateItem: async (args: unknown, { user }: { user: UserInfo | null }) => {
if (!user) {
throw new AuthError('Not authenticated');
}

const { itemId, title } = z.object({ itemId: z.string(), title: z.string() }).parse(args);

const exampleItem = await dbExampleItems.requireOne({ _id: new ObjectId(itemId) });
if (exampleItem.userId.toString() !== user.id) {
throw new AuthError('Not authorized');
}

const { modifiedCount } = await dbExampleItems.updateOne({ _id: new ObjectId(itemId) }, { $set: { title } });

if (modifiedCount === 0) {
throw new Error('Item not found');
}
},
},

cronJobs: {
dailyTest: dailyTestCron
}
});