Skip to content

Commit

Permalink
add db models to backend docs
Browse files Browse the repository at this point in the history
  • Loading branch information
maxmwang committed Feb 23, 2025
1 parent 1058cd6 commit b08cee9
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions docs/src/core/backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,42 @@ The above diagram shows a simplified request-to-response pipeline within a modul
[^2]: The Mongoose abstraction is very similar to the built-in MongoDB query language.

[^3]: Fields not requested are automatically removed.

### Database Models

In addition to the API server, the backend service is responsible for managing MongoDB usage—specifically, how our data is organized and defined through collections, models, and indexes.

```
.
├── apps
│ └── backend # Backend codebase
├── packages # Shared packages across apps
│ └── common
│ └── src
│ └── models # All database models
│ └── [model].ts # Example model file
```

A model file will contain TypeScript types mirroring the database model, a Mongoose model definition, and database index declarations.

```ts
// packages/common/src/models/termNew.ts

// defines TypeScript type for nested object
export interface ISessionItem { /* ... */ }

// defines TypeScript type for term object
export interface ITermItem { /* ... */ }

// defines Mongoose schema using TypeScript type
const termSchema = new Schema<ITermItem>({ /* ... */ });

// defines database indexes
termSchema.index( /* ... */ );

// creates Mongo model instance
export const TermModel: Model<ITermItem> = model<ITermItem>(
"term",
termSchema
);
```

0 comments on commit b08cee9

Please sign in to comment.