Skip to content

Commit

Permalink
chore(beaking-changes): refactor and simplify codebase (#8)
Browse files Browse the repository at this point in the history
* chore(beaking-changes): refactor and simplify codebase

* fix(ci): format lockfile

* feat(docs): enable switch for dark and light mode

* feat(docs): add script for development

* feat(db): backup with option fields

* fix(docs): improve example

* example: split backup controller
  • Loading branch information
shivarm authored Jan 31, 2025
1 parent 609ba51 commit 0d5a7b6
Show file tree
Hide file tree
Showing 20 changed files with 647 additions and 391 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ app.listen(PORT, () => {

## Core Features

- **Easy MongoDB Atlas Connection:** Establish secure and reliable connections with MongoDB Atlas using simple configuration.
- **Backup and Restore**: Effortlessly backup and restore your MongoDB collections with our built-in methods.
- **Easy MongoDB Atlas Connection:** Establish secure and reliable connections with MongoDB Atlas.
- **Backup and Restore**: Effortlessly backup and restore your MongoDB collections with built-in methods.
- **Schema Validation**: Ensure data integrity with JSON schema validation powered by Zod.
- **TypeScript-First:** Enjoy fully typed APIs for improved developer experience and better code quality.

Expand Down
2 changes: 1 addition & 1 deletion docs/coverpage.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# mongodb-atlas-sdk

> A modern, TypeScript-based SDK designed to simplify and supercharge your interaction with MongoDB Atlas.
> A modern, SDK designed to simplify and supercharge your interaction with MongoDB Atlas.
- TypeScript-First Enjoy fully typed APIs for improved developer experience and better code quality.

Expand Down
4 changes: 2 additions & 2 deletions docs/guide/auditLogging.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This feature will track changes to documents and log them for auditing purposes.

```typescript
import mongoose from 'mongoose';
import { auditLogging } from 'mongodb-atlas-sdk';
import { auditLog } from 'mongodb-atlas-sdk';

const userSchema = new mongoose.Schema({
name: { type: String, required: true },
Expand All @@ -14,7 +14,7 @@ const userSchema = new mongoose.Schema({
});

// Apply the audit logging to the user schema
userSchema.plugin(auditLogging);
userSchema.plugin(auditLog);

export const User = mongoose.model('User', userSchema);
```
Expand Down
32 changes: 32 additions & 0 deletions docs/guide/backup.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,38 @@ You can test the backup and restore endpoints using Postman or any other API tes
}
```

## Backup with optional fields

You can also pass optional fields to the `backupModel` method. Below is an example of how to backup data from the User model with optional fields.

```typescript
import { Request, Response } from 'express';
import { User } from '../model/userModel';
import { Backup } from 'mongodb-atlas-sdk';

const backup = new Backup();

export const backupUser = async (req: Request, res: Response) => {
try {
const filePath = req.body.filePath;
const fields = req.body.fields; // Get fields from request body
await backup.backupModel(User, filePath, { fields });
res.status(200).json({ message: 'Backup completed successfully' });
} catch (err) {
res.status(500).json({ message: 'Failed to backup data' });
}
};
```

You can test the backup and restore endpoints using Postman or any other API testing tool.

```json
{
"filePath": "./backup/user_backup.json",
"fields": ["name", "email"]
}
```

## Example backend application

Check our user CRUD application on [Github](https://github.com/shivarm/mongodb-atlas-sdk/tree/main/examples/typescript)
6 changes: 3 additions & 3 deletions docs/guide/queryCaching.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## How to use query caching ?

The `queryCachingPlugin` method is a Mongoose plugin that adds query caching functionality to your Mongoose models using Redis. This plugin helps improve the performance of your application by caching the results of frequently accessed queries, reducing the load on the database, and speeding up response times for your users. Let's see how to use this plugin in your Mongoose models.
The `queryCache` method that adds query caching functionality to your Mongoose models using Redis. This plugin helps improve the performance of your application by caching the results of frequently accessed queries, reducing the load on the database, and speeding up response times for your users. Let's see how to use this plugin in your Mongoose models.

```typescript
import mongoose from 'mongoose';
import { queryCachingPlugin } from 'mongodb-atlas-sdk';
import { queryCache } from 'mongodb-atlas-sdk';

const userSchema = new mongoose.Schema({
name: { type: String, required: true },
Expand All @@ -14,7 +14,7 @@ const userSchema = new mongoose.Schema({
});

// Apply the query caching plugin to the user schema
userSchema.plugin(queryCachingPlugin);
userSchema.plugin(queryCache);

export const User = mongoose.model('User', userSchema);
```
Expand Down
36 changes: 19 additions & 17 deletions docs/guide/typescript.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
## How to use with Typescript ?
## Example backend application

You can use the `MongoDbConnection` class from the package and you will be able to use the following methods:
This is a simple example of a backend application using the MongoDB Atlas SDK for Node.js. The application is written in TypeScript and uses the Express framework. Check our user CRUD application on [Github](https://github.com/shivarm/mongodb-atlas-sdk/tree/main/examples/typescript)

```typescript
import express from 'express';
import dotenv from 'dotenv';
import { MongoDbConnection } from 'mongodb-atlas-sdk';
dotenv.config();
## Running the Example

const mongoKit = new MongoDbConnection(process.env.DB_URI!);
const app = express();
const PORT = process.env.PORT || 5000;
You can run the example by following these steps:

app.use(express.json({ limit: '10mb' }));
Install Dependencies

app.listen(PORT, () => {
console.log('Server is running on http://localhost:' + PORT);
mongoKit.connect();
});
```bash
npm install
```

## Example backend application
Set Environment Variables
Create a `.env` file in the root directory of the project and add the following environment variables:

```env
DB_URI=
PORT=5000
```

Start the Server

Check our user CRUD application on [Github](https://github.com/shivarm/mongodb-atlas-sdk/tree/main/examples/typescript)
```bash
npm run dev
```
5 changes: 1 addition & 4 deletions docs/home.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ Unlike other MongoDB SDKs, MongoKit focuses on developer productivity, scalabili
- **Schema Validation**: Ensure data integrity with JSON schema validation powered by Zod.
- **TypeScript-First:** Enjoy fully typed APIs for improved developer experience and better code quality.

> [!IMPORTANT]
> You can check Node.js/Express.js example [Application](./examples)
## Development

See our [Contributing Guide](./CONTRIBUTING.md).
- See our [Contributing Guide](./CONTRIBUTING.md).

## Support

Expand Down
10 changes: 10 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
<meta name="description" content="A simple and easy to use MongoDB Atlas SDK for Node.js" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0" />
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/docsify@4/lib/themes/vue.css" />
<link
rel="stylesheet"
href="//cdn.jsdelivr.net/npm/docsify-darklight-theme@latest/dist/style.min.css"
title="docsify-darklight-theme"
type="text/css"
/>
</head>
<body>
<div id="app"></div>
Expand Down Expand Up @@ -50,5 +56,9 @@
<script src="//cdn.jsdelivr.net/npm/prismjs@1/components/prism-bash.min.js"></script>
<script src="//cdn.jsdelivr.net/npm/prismjs@1/components/prism-typescript.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/docsify-updated/src/time-updater.min.js"></script>
<script
src="//cdn.jsdelivr.net/npm/docsify-darklight-theme@latest/dist/index.min.js"
type="text/javascript"
></script>
</body>
</html>
7 changes: 1 addition & 6 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,8 @@ npm run dev

- **Body**: (JSON)

````json
{
"filePath": "./backup/user_backup.json"
}

```json
{
"filePath": "./backup/user_backup.json"
}
````
```
9 changes: 9 additions & 0 deletions examples/typescript/backup/user_backup.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"_id": "679a1343c406f835121d0f9d",
"name": "John Doe",
"email": "[email protected]",
"age": 31,
"__v": 0
}
]
8 changes: 4 additions & 4 deletions examples/typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dependencies": {
"dotenv": "^16.4.7",
"express": "^4.21.2",
"mongodb-atlas-sdk": "^1.7.12",
"mongodb-atlas-sdk": "^1.7.13",
"mongoose": "^8.9.5",
"zod": "^3.24.1"
},
Expand Down
15 changes: 15 additions & 0 deletions examples/typescript/src/controllers/backupController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Request, Response } from 'express';
import { Backup } from 'mongodb-atlas-sdk';
import { User } from '../model/userModel';

const backup = new Backup();

export const backupUser = async (req: Request, res: Response) => {
try {
const filePath = req.body.filePath;
await backup.backupModel(User, filePath);
res.status(200).json({ message: 'Backup completed successfully' });
} catch (err) {
res.status(500).json({ message: 'Failed to backup data' });
}
};
13 changes: 0 additions & 13 deletions examples/typescript/src/controllers/userController.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
import { Request, Response } from 'express';
import { User } from '../model/userModel';
import { Backup } from 'mongodb-atlas-sdk';

const backup = new Backup();

export const backupUser = async (req: Request, res: Response) => {
try {
const filePath = req.body.filePath;
await backup.backupModel(User, filePath);
res.status(200).json({ message: 'Backup completed successfully' });
} catch (err) {
res.status(500).json({ message: 'Failed to backup data' });
}
};

export const createUser = async (req: Request, res: Response) => {
try {
Expand Down
11 changes: 3 additions & 8 deletions examples/typescript/src/routes/userRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import express from 'express';
const router = express.Router();

import {
createUser,
getUsers,
getUserById,
updateUserById,
deleteUserById,
backupUser,
} from '../controllers/userController';
import { createUser, getUsers, getUserById, updateUserById, deleteUserById } from '../controllers/userController';

import { backupUser } from '../controllers/backupController';

router.post('/', createUser);
router.get('/', getUsers);
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.7.13",
"description": "mongodb-atlas-sdk is a modern, TypeScript-based SDK designed to simplify and supercharge your interaction with MongoDB Atlas.",
"scripts": {
"docs": "docsify serve docs",
"build": "tsup",
"dev": "tsup src/index.ts --watch",
"test": "vitest run",
Expand All @@ -20,6 +21,8 @@
"mongodb",
"atlas",
"sdk",
"backup",
"restore",
"typescript"
],
"main": "dist/index.js",
Expand Down
Loading

0 comments on commit 0d5a7b6

Please sign in to comment.