Skip to content

Commit

Permalink
feat: docs site (#7)
Browse files Browse the repository at this point in the history
fixes #10
  • Loading branch information
aryan02420 authored Dec 18, 2023
1 parent 79ea289 commit d05bfe7
Show file tree
Hide file tree
Showing 37 changed files with 10,296 additions and 152 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/build-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Build Docs

on:
push:
branches:
- main
paths:
- 'docs/**'
- 'website/**'
workflow_dispatch:

permissions:
contents: write

jobs:
build-docs:
name: Build Docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
cache: yarn
cache-dependency-path: ./website/yarn.lock

- name: Install dependencies
run: yarn install --frozen-lockfile
working-directory: ./website

- name: Build website
run: yarn build
working-directory: ./website

# Docs: https://github.com/peaceiris/actions-gh-pages#%EF%B8%8F-docusaurus
- name: Deploy to GitHub Pages
uses: peaceiris/actions-gh-pages@v3
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_branch: gh-pages
publish_dir: ./website/build
user_name: lyearnDev
user_email: [email protected]
30 changes: 30 additions & 0 deletions .github/workflows/test-docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Test Docs

on:
pull_request:
branches:
- main
paths:
- 'docs/**'
- 'website/**'
workflow_dispatch:

jobs:
test-docs:
name: Test Docs
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
cache: yarn
cache-dependency-path: ./website/yarn.lock

- name: Install dependencies
run: yarn install --frozen-lockfile
working-directory: ./website

- name: Test build website
run: yarn build
working-directory: ./website
20 changes: 10 additions & 10 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ This directory contains user-facing documentation. For those who wish to underst
## Table of Contents

### Introduction
* [About](about.md)
* [About](./about.md)

### Beginners Guides
* [Installation](installation.md)
* [Basic usage](basic_usage.md)
### Beginners Guide
* [Installation](./installation.md)
* [Basic usage](./basic_usage.md)

### Features
* [Schema options](schema_options.md)
* [Field options](field_options.md)
* [Field transformers](field_transformers.md)
* [Meta fields](meta_fields.md)
* [Schema options](./schema_options.md)
* [Field options](./field_options.md)
* [Field transformers](./field_transformers.md)
* [Meta fields](./meta_fields.md)

### Advanced Guides
* [Unions](union_types.md)
### Advanced Guide
* [Unions](./union_types.md)
8 changes: 5 additions & 3 deletions docs/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
title: About
---

## What is `mgod`?
## What is mgod?

`mgod` is a MongoDB ODM specifically designed for Go. It provides a structured way to map Go models to MongoDB collections, simplifying database interactions in Go applications.

## Why use `mgod`?
## Why use mgod?

Creating `mgod` was driven by the need to **simplify MongoDB interactions** while keeping **one schema for all** in Go. Traditionally, working with MongoDB in Go involved either using separate structs for database and service logic or manually converting service structs to MongoDB documents, a process that was both time-consuming and error-prone. This lack of integration often led to redundant coding, especially when dealing with union types or adding meta fields for each MongoDB operation.

Inspired by the easy interface of MongoDB handling using [Mongoose](https://github.com/Automattic/mongoose) and [Typegoose](https://github.com/typegoose/typegoose) libraries available in Node, `mgod` aims to streamline these processes. It offers a more integrated approach, reducing the need to duplicate code and enhancing type safety, making MongoDB operations more intuitive and efficient in Go.
Inspired by the easy interface of MongoDB handling using [Mongoose](https://github.com/Automattic/mongoose) and [Typegoose](https://github.com/typegoose/typegoose) libraries available in Node, `mgod` aims to streamline these processes. It offers a more integrated approach, reducing the need to duplicate code and enhancing type safety, making MongoDB operations more intuitive and efficient in Go.
38 changes: 24 additions & 14 deletions docs/basic_usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ func init() {
err := mgod.ConfigureDefaultConnection(cfg, dbName, opts)
}
```
> [!NOTE]
> The above `err` variable will be a connection error (if occurs) returned by the Go Mongo Driver. So, handle the error accordingly.

:::note
The above `err` variable will be a connection error (if occurs) returned by the Go Mongo Driver. So, handle the error accordingly.
:::

Add tags _(wherever applicable)_ in existing struct _(or define a new model)_.

```go
type User struct {
Name string
Expand All @@ -46,6 +49,7 @@ type User struct {
```

Use `mgod` to get the entity ODM.

```go
import (
"github.com/Lyearn/mgod"
Expand All @@ -63,7 +67,8 @@ userModel, _ := mgod.NewEntityMongoModel(model, schemaOpts)

Use the entity ODM to perform CRUD operations with ease.

Insert a new document.
## Inserting a new document

```go
joinedOn, _ := dateformatter.New(time.Now()).GetISOString()
userDoc := User{
Expand All @@ -75,7 +80,8 @@ user, _ := userModel.InsertOne(context.TODO(), userDoc)
```

**Output:**
```json

```js
{
"_id": ObjectId("65697705d4cbed00e8aba717"),
"name": "Gopher",
Expand All @@ -86,41 +92,44 @@ user, _ := userModel.InsertOne(context.TODO(), userDoc)
"__v": 0
}
```

Notice how `_id`, `createdAt`, `updatedAt` and `__v` fields are added automatically.

---
## Finding documents using model properties

Find documents using model properties.
```go
users, _ := userModel.Find(context.TODO(), bson.M{"name": userDoc.Name})
```

**Output:**

```go
[]User{
User{
Name: "Gopher",
EmailID: "[email protected]",
JoinedOn: "2023-12-01T11:32:19.290Z",
}
}
}
```
---

Update document properties.
## Updating document properties

```go
result, _ := userModel.UpdateMany(context.TODO(), bson.M{"joinedOn": bson.M{"$gte": "2023-12-01T00:00:00.000Z"}}, bson.M{"$inc": {"__v": 1}})
```

**Output:**

```go
mongo.UpdateResult{
MatchedCount: 1,
ModifiedCount: 1,
UpsertedCount: 0,
}
```
```json

```js
// User Doc
{
"_id": ObjectId("65697705d4cbed00e8aba717"),
Expand All @@ -132,18 +141,19 @@ mongo.UpdateResult{
"__v": 1
}
```

Notice the updation of the `updatedAt` field.

---
## Removing documents matching certain or all model properties

Remove documents matching certain or all model properties.
```go
result, _ := userModel.DeleteMany(context.TODO(), bson.M{"name": userDoc.Name})
```

**Output:**

```go
mongo.DeleteResult{
DeletedCount: 1
}
```
}
```
55 changes: 36 additions & 19 deletions docs/field_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ Field Options are custom schema options available at field level (for fields of

`mgod` supports the following field options -

## __id
* BSON Tag: `mgoID`
* Accepts Type: `bool`
* Default Value: `true` for custom type fields
## \_\_id

- BSON Tag: `mgoID`
- Accepts Type: `bool`
- Default Value: `true` for custom type fields

It defines if `_id` field needs to be added in a object.
> [!NOTE]
> This option is only applicable for fields holding structs.

**Example**
:::note
This option is only applicable for fields holding structs.
:::

### Example

```go
type UserProject struct {
Name string
Expand Down Expand Up @@ -46,7 +50,8 @@ user, _ := userModel.InsertOne(context.TODO(), userDoc)
```

**Output:**
```json

```js
{
"_id": ObjectId("65697705d4cbed00e8aba717"),
"name": "Gopher",
Expand All @@ -64,29 +69,34 @@ user, _ := userModel.InsertOne(context.TODO(), userDoc)
}
}
```

See how `_id` field is added for `meta` object because `mgoID` is true by default for struct type fields. Also, note how `_id` field is skipped for `projects` object as it was set to false explicitly in type declaration.

## required
* Accepts Type: `bool`
* Default Value: `true`

- Accepts Type: `bool`
- Default Value: `true`

It defines if a field is required or not. The option can be invalidated using `omitempty` property of `bson` tag.

**Example**
### Example

```go
type User struct {
Name string
Age int32
Height *float `bson:",omitempty"`
}
```

In the above type, height field is set to not required.

```go
userDoc := User{
Name: "Gopher",
}
```

The above doc will throw error because `Age` field is required.

```go
Expand All @@ -95,18 +105,23 @@ userDoc := User{
Age: 18,
}
```

This doc will work fine.

## default
* BSON Tag: `mgoDefault`
* Accepts Type: `string`
* Default Value: `nil`

- BSON Tag: `mgoDefault`
- Accepts Type: `string`
- Default Value: `nil`

It provides the default value for a field. The value of this option is used when the field is not present in the input document.
> [!NOTE]
> This option is applicable only for fields that are not of custom type (custom structs).

**Example**
:::note
This option is applicable only for fields that are not of custom type (custom structs).
:::

### Example

```go
type UserProject struct {
Name string
Expand Down Expand Up @@ -135,7 +150,8 @@ user, _ := userModel.InsertOne(context.TODO(), userDoc)
```

**Output:**
```json

```js
{
"_id": ObjectId("65697705d4cbed00e8aba717"),
"name": "Gopher",
Expand All @@ -147,4 +163,5 @@ user, _ := userModel.InsertOne(context.TODO(), userDoc)
}
}
```
See how the value of `age` field was used because it was provided in the input doc and how the default value of `projects` field is used because it was missing from the input doc.

See how the value of `age` field was used because it was provided in the input doc and how the default value of `projects` field is used because it was missing from the input doc.
Loading

0 comments on commit d05bfe7

Please sign in to comment.