Skip to content

Commit

Permalink
feat: mongodb driver (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
itpropro authored Feb 27, 2023
1 parent 90980e0 commit 47c5e66
Show file tree
Hide file tree
Showing 7 changed files with 1,258 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ tmp
/drivers
/server*
/test.*
__*
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,49 @@ const storage = createStorage({
- `storage`: The name of the table to read from. It defaults to `storage`.
- `boostCache`: Whether to enable cached queries: see [docs](https://planetscale.com/docs/concepts/query-caching-with-planetscale-boost#using-cached-queries-in-your-application).

### `mongodb`

Store data in a MongoDB [mongodb](https://www.npmjs.com/package/mongodb) using [Node.js mongodb package](https://www.npmjs.com/package/mongodb)

This driver stores KV information in a MongoDB collection with a separate document for each key value pair.

To use it, you will need to install `mongodb` in your project:

```json
{
"dependencies": {
"mongodb": "^5.0.1"
}
}
```

Usage:

```js
import { createStorage } from "unstorage";
import mongodbDriver from "unstorage/drivers/mongodb";

const storage = createStorage({
driver: mongodbDriver({
connectionString: "CONNECTION_STRING",
databaseName: "test",
collectionName: "test",
}),
});
```

**Authentication:**

The driver supports the following authentication methods:

- **`connectionString`**: The MongoDB connection string. This is the only way to authenticate.

**Options:**

- **`connectionString`** (required): The connection string to use to connect to the MongoDB database. It should be in the format `mongodb://<username>:<password>@<host>:<port>/<database>`.
- `databaseName`: The name of the database to use. Defaults to `unstorage`.
- `collectionName`: The name of the collection to use. Defaults to `unstorage`.

## Making custom drivers

It is possible to extend unstorage by creating custom drives.
Expand Down
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"ufo": "^1.1.0"
},
"optionalDependencies": {
"@planetscale/database": "^1.5.0"
"@planetscale/database": "^1.5.0",
"mongodb": "^5.0.1"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230221.0",
Expand All @@ -71,6 +72,8 @@
"ioredis-mock": "^8.2.6",
"jiti": "^1.17.1",
"jsdom": "^21.1.0",
"mongodb": "^5.0.1",
"mongodb-memory-server": "^8.11.4",
"monaco-editor": "^0.36.0",
"msw": "^1.1.0",
"prettier": "^2.8.4",
Expand Down
Loading

0 comments on commit 47c5e66

Please sign in to comment.