Use this file to discover all available pages before exploring further.
Modules are the fundamental building blocks of a Modelence application. They help you organize your backend functionality into cohesive, self-contained units that encapsulate queries, mutations, stores, and configuration.
A Module in Modelence is similar to a feature module in other frameworks. It groups related functionality together, making your codebase more maintainable and easier to reason about.
import { Module } from 'modelence/server';export default new Module('todo', { // Module configuration goes here});
Stores define your MongoDB collections with schemas, indexes, and custom methods. Including stores in your module ensures they’re automatically provisioned when the server starts.
import { Store, schema } from 'modelence/server';export const dbTodos = new Store('todos', { schema: { title: schema.string(), isCompleted: schema.boolean(), userId: schema.userId(), createdAt: schema.date() }, indexes: [ { key: { userId: 1 } } ]});export default new Module('todo', { // Register the store with the module stores: [dbTodos], queries: { async getAll({}, { user }) { return await dbTodos.fetch({ userId: user.id }); } }});
Queries are read operations that fetch data without modifying state.For full query patterns, including client usage with callMethod,
modelenceQuery, and typed client modules, see
Queries.
Mutations are write operations that create, update, or delete data.For full mutation patterns, including client usage with callMethod,
modelenceMutation, and typed client modules, see
Mutations.
Each module should represent a single domain or feature:
// Good: Focused todo moduleexport default new Module('todo', { // Only todo-related functionality});// Good: Separate user moduleexport default new Module('users', { // Only user-related functionality});
Always handle potential errors and provide meaningful messages:
mutations: { async delete({ id }, { user }) { const todo = await dbTodos.findById(id); if (!todo) { throw new Error('Todo not found'); } if (todo.userId !== user.id) { throw new Error('Not authorized to delete this todo'); } return await dbTodos.deleteOne({ id }); }}