Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: mongodb driver #155

Merged
merged 12 commits into from
Feb 27, 2023
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.*
__*
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,50 @@ 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`

⚠️ Due to it's dependency on the `mongodb` package. this driver is currently not compatible with edge workers like Cloudflare Workers or Vercel Edge Functions.
itpropro marked this conversation as resolved.
Show resolved Hide resolved

Store data in a MongoDB [mongodb](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",
}),
});
itpropro marked this conversation as resolved.
Show resolved Hide resolved
```

**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.20230214.0",
Expand All @@ -71,6 +72,8 @@
"jiti": "^1.17.0",
"jsdom": "^21.1.0",
"monaco-editor": "^0.35.0",
"mongodb": "^5.0.1",
"mongodb-memory-server": "^8.11.4",
"msw": "^1.0.1",
"prettier": "^2.8.4",
"types-cloudflare-worker": "^1.2.0",
Expand Down
Loading