To implement the ability to create multiple spaces and switch between them in codex.docs, you will need to make changes across the backend, database layer, and frontend interfaces. Here's a step-by-step guide:
1. Define the Concept of a "Space"
A "space" can be thought of as a container for pages, allowing users to group related pages into distinct collections. To implement this:
Each Page should belong to a specific Space.
Each user should be able to select a Space to work in.
Spaces should be switchable, and the currently selected space should determine which pages are displayed.
2. Add "Space" Model
Create a new Space model to represent individual spaces. Use the existing structure of Page or Alias models as a reference.
public static async getAllPages(space: EntityId): Promise<Page[]> {
return Page.getAll({ space });
}
public static async insert(data: PageData): Promise<Page> {
// Ensure the page belongs to a space
if (!data.space) {
throw new Error('Space is required');
}
return super.insert(data);
}
New File: ./repos/codex.docs/src/backend/controllers/spaces.ts
Create a controller for managing spaces:
ts
import Space, { SpaceData } from '../models/space.js';
class Spaces {
/**
* Get all spaces
*
* @returns {Promise<Space[]>}
*/
public static async getAll(): Promise<Space[]> {
return Space.getAll();
}
/**
* Create a new space
*
* @param {SpaceData} data
* @returns {Promise<Space>}
*/
public static async create(data: SpaceData): Promise<Space> {
const space = new Space(data);
return space.save();
}
}
export default Spaces;
5. Add API Routes
Expose endpoints for managing spaces and filtering pages by space.
export default {
pages: new Database<PageData>('pages'),
aliases: new Database<AliasData>('aliases'),
pagesOrder: new Database<PageOrderData>('pagesOrder'),
files: new Database<FileData>('files'),
spaces: new Database<SpaceData>('spaces'), // Add spaces
};
For MongoDB:
Add a spaces collection.
For the local database:
Add a file for spaces (e.g., spaces.db).
8. Test the Implementation
Test creating new spaces via the API.
Test assigning pages to spaces.
Test switching between spaces in the frontend.
9. (Optional) Permissions and Authentication
If required, you can add user-specific access to spaces by associating spaces with users in the database and restricting access in the API.
This implementation provides the foundation for managing multiple spaces and switching between them. You can expand it further by adding features such as space-specific settings, user roles, or analytics.