Skip to content

Commit 5e177cd

Browse files
Merge pull request #96 from monarch-orm/docs-improvements
Documentation improvements
2 parents 4e265e2 + d2e3d7e commit 5e177cd

13 files changed

Lines changed: 1807 additions & 1634 deletions

.github/workflows/main.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ name: CI
22

33
on:
44
pull_request:
5+
paths-ignore:
6+
- "docs/**"
57
push:
68
branches:
79
- main
10+
paths-ignore:
11+
- "docs/**"
812

913
jobs:
1014
build:

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ node_modules
22
dist
33
.DS_Store
44
coverage
5-
docs/.vitepress
5+
docs/.vitepress/cache
6+
docs/.vitepress/dist

docs/.vitepress/config.mts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { defineConfig } from "vitepress";
22

33
export default defineConfig({
44
title: "Monarch ORM",
5-
description: "Type safe Object Document Mapper (ODM) for MongoDB",
5+
description: "Type safe ODM for MongoDB",
66
cleanUrls: true,
77
themeConfig: {
88
nav: [
@@ -17,6 +17,7 @@ export default defineConfig({
1717
items: [
1818
{ text: "Getting Started", link: "/guide/getting-started" },
1919
{ text: "Schemas & Types", link: "/guide/schemas-and-types" },
20+
{ text: "Queries & Operators", link: "/guide/queries-and-operators" },
2021
{ text: "Advanced Schemas", link: "/guide/advanced-schemas" },
2122
{ text: "Aggregations & Relations", link: "/guide/aggregation-and-relations" },
2223
],
@@ -27,8 +28,6 @@ export default defineConfig({
2728
},
2829
],
2930

30-
socialLinks: [
31-
{ icon: "github", link: "https://github.com/monarch-orm/monarch" },
32-
],
31+
socialLinks: [{ icon: "github", link: "https://github.com/monarch-orm/monarch" }],
3332
},
3433
});

docs/guide/advanced-schemas.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ In expansive, microservice-like or component-based setups, placing all schemas i
9191
import { defineSchemas, mergeSchemas } from "monarch-orm";
9292

9393
// 1. Module defined for Users
94-
const userGroup = defineSchemas({ UserSchema }).withRelations((s) => ({
95-
users: { tutor: s.users.$one.users({ from: "tutorId", to: "_id" }) },
94+
const userGroup = defineSchemas({ UserSchema }).withRelations((r) => ({
95+
users: { tutor: r.one.users({ from: r.users.tutorId, to: r.users._id }) },
9696
}));
9797

9898
// 2. Module defined for Content
@@ -102,12 +102,12 @@ const contentGroup = defineSchemas({ PostSchema, CategorySchema });
102102
const mergedGroups = mergeSchemas(userGroup, contentGroup);
103103

104104
// Optionally attach relationships ACROSS the groups post-merge
105-
const finalSchema = mergedGroups.withRelations((s) => ({
105+
const finalSchema = mergedGroups.withRelations((r) => ({
106106
users: {
107-
posts: s.users.$many.posts({ from: "_id", to: "authorId" }),
107+
posts: r.many.posts({ from: r.users._id, to: r.posts.authorId }),
108108
},
109109
posts: {
110-
author: s.posts.$one.users({ from: "authorId", to: "_id" }),
110+
author: r.one.users({ from: r.posts.authorId, to: r.users._id }),
111111
},
112112
}));
113113

docs/guide/aggregation-and-relations.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Monarch ORM provides powerful ways to connect collections through relations and
44

55
## Relations
66

7-
You can establish relations between collections after defining your schemas. Use the `defineSchemas` higher-order function to bundle schemas and the `.withRelations` method to establish `$one` and `$many` connections between them.
7+
You can establish relations between collections after defining your schemas. Use the `defineSchemas` higher-order function to bundle schemas and the `.withRelations` method to establish `one` and `many` connections between them.
88

99
### Defining Relations
1010

@@ -29,13 +29,13 @@ const schemas = defineSchemas({
2929
});
3030

3131
// Configure the relationships
32-
const relations = schemas.withRelations((s) => ({
32+
const relations = schemas.withRelations((r) => ({
3333
users: {
34-
tutor: s.users.$one.users({ from: "tutorId", to: "_id" }),
35-
posts: s.users.$many.posts({ from: "_id", to: "authorId" }),
34+
tutor: r.one.users({ from: r.users.tutorId, to: r.users._id }),
35+
posts: r.many.posts({ from: r.users._id, to: r.posts.authorId }),
3636
},
3737
posts: {
38-
author: s.posts.$one.users({ from: "authorId", to: "_id" }),
38+
author: r.one.users({ from: r.posts.authorId, to: r.users._id }),
3939
},
4040
}));
4141

docs/guide/getting-started.md

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ pnpm add monarch-orm
2222
## Basic Usage
2323

2424
```typescript
25-
import { boolean, createClient, createDatabase, createSchema, number, string } from "monarch-orm";
25+
import { createClient, createDatabase, createSchema, defineSchemas } from "monarch-orm";
26+
import { boolean, number, string } from "monarch-orm/types";
2627

2728
const UserSchema = createSchema("users", {
2829
name: string().nullable(),
@@ -125,28 +126,20 @@ Update documents in your collection using the `updateOne` or `updateMany` method
125126
Example: Updating a single user's email
126127

127128
```typescript
128-
const updatedUser = await collections.users
129-
.updateOne()
130-
.set({
131-
email: "alice.updated@example.com",
132-
})
133-
.where({
134-
name: "Alice",
135-
});
129+
const updatedUser = await collections.users.updateOne(
130+
{ name: "Alice" },
131+
{ $set: { email: "alice.updated@example.com" } }
132+
);
136133
console.log(updatedUser);
137134
```
138135

139136
Example: Updating multiple users' `isVerified` field
140137

141138
```typescript
142-
const updatedUsers = await collections.users
143-
.updateMany()
144-
.set({
145-
isVerified: true,
146-
})
147-
.where({
148-
isVerified: false,
149-
});
139+
const updatedUsers = await collections.users.updateMany(
140+
{ isVerified: false },
141+
{ $set: { isVerified: true } }
142+
);
150143
console.log(updatedUsers);
151144
```
152145

@@ -157,14 +150,15 @@ Note: The update method returns the number of documents updated.
157150
You can also decentralize the models:
158151

159152
```typescript
160-
const { db } = createDatabase(client.db());
153+
import { createDatabase, defineSchemas } from "monarch-orm";
161154

162155
const UserSchema = createSchema("users", {
163156
name: string(),
164157
isVerified: boolean(),
165158
});
166159

167-
const UserModel = db(UserSchema);
160+
const monarchDb = createDatabase(client.db(), defineSchemas({ users: UserSchema }));
161+
const UserModel = monarchDb.use(UserSchema);
168162
export default UserModel;
169163
```
170164

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Queries & Operators
2+
3+
Monarch ORM provides a powerful, type-safe API for querying your MongoDB collections. By using the query builders and exported operators, you can construct complex filters without losing type safety.
4+
5+
## Query Modifiers
6+
7+
When you use `.find()` or `.findOne()`, Monarch returns a lazy query builder. The query is not executed until you `await` it. This allows you to chain modifiers to shape your results.
8+
9+
### Selecting & Omitting Fields
10+
11+
You can control exactly which fields are returned from the database using `.select()` and `.omit()`.
12+
13+
```typescript
14+
// Only return the name and email fields
15+
const users = await db.collections.users
16+
.find()
17+
.select({ name: true, email: true });
18+
19+
// Return everything EXCEPT the age and password fields
20+
const publicUsers = await db.collections.users
21+
.find()
22+
.omit({ age: true, password: true });
23+
```
24+
25+
### Sorting, Limiting, and Skipping
26+
27+
Use these modifiers to paginate and order your results.
28+
29+
```typescript
30+
const latestUsers = await db.collections.users
31+
.find({ isVerified: true })
32+
.sort({ createdAt: -1 }) // Sort descending by createdAt
33+
.limit(10) // Return max 10 documents
34+
.skip(20); // Skip the first 20 documents
35+
```
36+
37+
### Cursors
38+
39+
For large datasets where you don't want to load everything into memory at once, you can use `.cursor()` to iterate over documents one by one.
40+
41+
```typescript
42+
const cursor = await db.collections.users.find({ isVerified: true }).cursor();
43+
44+
for await (const user of cursor) {
45+
console.log(user.name);
46+
}
47+
```
48+
49+
## Operators
50+
51+
Monarch provides typed wrapper functions for standard MongoDB query operators. These functions enforce type safety and ensure your queries align with your schema.
52+
53+
You can import them from `monarch-orm/operators`.
54+
55+
```typescript
56+
import { eq, gt, or, inArray } from "monarch-orm/operators";
57+
```
58+
59+
### Comparison Operators
60+
61+
- **`eq(value)`**: Matches values equal to the specified value.
62+
- **`neq(value)`**: Matches values not equal to the specified value.
63+
- **`gt(value)`**: Matches values greater than the specified value.
64+
- **`lt(value)`**: Matches values less than the specified value.
65+
- **`gte(value)`**: Matches values greater than or equal to the specified value.
66+
- **`lte(value)`**: Matches values less than or equal to the specified value.
67+
68+
```typescript
69+
const adults = await db.collections.users.find({
70+
age: gte(18)
71+
});
72+
```
73+
74+
### Logical Operators
75+
76+
- **`and(...expressions)`**: Matches documents that satisfy all expressions.
77+
- **`or(...expressions)`**: Matches documents that satisfy at least one expression.
78+
- **`nor(...expressions)`**: Matches documents that fail all expressions.
79+
- **`not(expression)`**: Inverts the effect of a filter expression.
80+
81+
```typescript
82+
const specificUsers = await db.collections.users.find(
83+
or(
84+
{ age: lt(18) },
85+
{ isVerified: false }
86+
)
87+
);
88+
```
89+
90+
### Array Operators
91+
92+
- **`inArray(values)`**: Matches values that exist in the specified array.
93+
- **`notInArray(values)`**: Matches values that do not exist in the specified array.
94+
- **`size(value)`**: Matches arrays with the specified number of elements.
95+
96+
```typescript
97+
const targetedUsers = await db.collections.users.find({
98+
role: inArray(["admin", "moderator"])
99+
});
100+
```
101+
102+
### Element Operators
103+
104+
- **`exists()`**: Matches documents where the field exists.
105+
- **`notExists()`**: Matches documents where the field does not exist.
106+
107+
```typescript
108+
const usersWithPhone = await db.collections.users.find({
109+
phoneNumber: exists()
110+
});
111+
```
112+
113+
## Native MongoDB Syntax
114+
115+
Because Monarch wraps the underlying MongoDB Node.js driver, you are still free to use native MongoDB query syntax if you prefer. The fields and operators are fully typed based on your schema.
116+
117+
```typescript
118+
const activeUsers = await db.collections.users.find({
119+
age: { $gte: 18 },
120+
role: { $in: ["admin", "moderator"] }
121+
});
122+
```

docs/guide/schemas-and-types.md

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,6 @@ Monarch provides a rich set of schema builders to strictly enforce your MongoDB
44

55
## Primitives
66

7-
### Shape `createShape()`
8-
9-
Use `createShape()` when you want to define a reusable shape for schemas or objects.
10-
11-
```typescript
12-
const addressShape = createShape({
13-
street: string(),
14-
city: string(),
15-
});
16-
17-
const userSchema = createSchema("users", {
18-
name: string(),
19-
address: object(addressShape),
20-
});
21-
```
22-
237
### String `string()`
248

259
Defines a field that accepts string values.
@@ -70,16 +54,6 @@ const UserSchema = createSchema("users", {
7054
});
7155
```
7256

73-
### Date String `dateString()`
74-
75-
Defines a field that accepts date strings in ISO format.
76-
77-
```typescript
78-
const UserSchema = createSchema("users", {
79-
registrationDate: dateString(),
80-
});
81-
```
82-
8357
### UUID `uuid()`
8458

8559
Defines a field that accepts MongoDB `UUID` values or valid UUID strings.

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ layout: home
33

44
hero:
55
name: "Monarch ORM"
6-
text: "Type safe Object Document Mapper (ODM) for MongoDB"
6+
text: "Type-safe ODM for MongoDB"
77
tagline: "Designed to provide a seamless and efficient way to interact with your MongoDB database in a type-safe manner."
88
actions:
99
- theme: brand

docs/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "monarch-docs",
3+
"private": true,
4+
"scripts": {
5+
"dev": "vitepress dev .",
6+
"build": "vitepress build .",
7+
"preview": "vitepress preview ."
8+
},
9+
"devDependencies": {
10+
"vitepress": "^1.6.4"
11+
}
12+
}

0 commit comments

Comments
 (0)