Skip to content

hyper63/hyper-adapter-mongodb

Repository files navigation

hyper-adapter-mongodb

A hyper data port adapter that uses mongodb in the hyper service framework

Nest Badge Test Current Version


Background

MongoDB is a NoSQL database that is very popular in the developer ecosystem. With this adapter, you will be able to use MongoDB as your data store for your hyper applications.

For more information on MongoDB: https://www.mongodb.com/

Getting Started

create hyper.config.js

import { default as mongo } from 'https://raw.githubusercontent.com/hyper63/hyper-adapter-mongodb/{TAG}/mod.ts'

const connectionString = Deno.env.get('MONGODB_URL')

export default {
  app: express,
  adapter: [
    {
      port: 'data',
      plugins: [mongo({ url: connectionString })],
    },
  ],
}

create mod.js

import core from 'https://raw.githubusercontent.com/hyper63/hyper/hyper%40v4.3.2/packages/core/mod.ts'
import config from './hyper.config.js'

core(config)

MongoDB In-Memory

This adapter supports dynamically starting a local MongoDB server, running in memory. This is a great option for running a hyper Server locally, without needing to also spin up MongoDB locally (this what hyper-nano is doing underneath the hood).

To start a local MongoDB server, simply pass the dir option to the adapter. You can also specify dirVersion to specify the version of MongoDB to spin up locally.

import { default as mongo } from 'https://raw.githubusercontent.com/hyper63/hyper-adapter-mongodb/{TAG}/mod.ts'

export default {
  app: express,
  adapter: [
    {
      port: 'data',
      plugins: [mongo({ dir: '__hyper__', dirVersion: '7.0.4' })],
    },
  ],
}

The adapter will dynamically download the corresponding MongoDB binary, start it, then generate a connection string that the adapter will use to connect to it.

The MongoDB binary is downloaded and cached in dir, if it does not already exist. This may take some time the first startup, but then is fast after caching. MongoDB data is stored in {dir}/data. The binary plus the internal MongoDB data can typically require a non-trivial amount of disk, around 500MB.

Installation

This is a Deno module available to import from Github via Git Tags

deps.js

export { default as mongodb } from "https://raw.githubusercontent.com/hyper63/hyper-adapter-mongodb/{TAG}/mod.ts"

Features

  • Create a MongoDb database
  • Remove a MongoDb database
  • Create a document
  • Retrieve a document
  • Update a document
  • Remove a document
  • List documents
  • Query documents
  • Index documents
  • Bulk manage documents

Methods

This adapter fully implements the Data port and can be used as the hyper Data Service adapter

See the full port here

Contributing

Contributions are welcome! See the hyper contribution guide

Testing

Run the unit tests, lint, and check formatting run:

deno task test

Integration Tests

If you're developing in Gitpod, a MongoDB instance is automatically started for you

To run the integration tests, you will need an instance of MongoDB running, along with setting MONGO_URL to your connection string. For convenience you may use the Dockerfile in .mongodb directory:

docker build -t hyper-mongodb .mongodb
docker run -it -p 27017:27017 hyper-mongodb

You can also use a MongoDB Atlas Instance, as long your MONGO_URL is set to the correct connection string.

To run the tests on the adapter methods run:

deno task test:integration-native

TODO

License

Apache-2.0