This repository contains a template which can be used for generating new projects using MongoDB and Vapor via MongoDBVapor with Vapor's command line tool, Vapor Toolbox:
vapor new MyProject --template https://github.com/mongodb/mongodb-vapor-template/
-
Install Vapor Toolbox. Installation instructions can be found here for macOS and here for Linux.
-
Initialize a new project from the command line, and move into the project directory:
vapor new MyProject --template https://github.com/mongodb/mongodb-vapor-template/
cd MyProject
-
If you're using Linux The driver vendors and wraps the MongoDB C driver (
libmongoc
), which depends on a number of external C libraries when built in Linux environments. As a result, these libraries must be installed on your system in order to build the driver. To install those libraries, please follow the instructions fromlibmongoc
's documentation. -
Set up a MongoDB deployment to connect to. You can install MongoDB, and (in a new terminal window) start up a standalone server locally (this will run on the default host/port,
localhost:27017
):
mongod --dbpath /data/path/here
Alternatively, you can use MongoDB Atlas to set up a fully managed MongoDB deployment in the cloud.
- If using Atlas, or a local host/port besides the default
localhost:27017
, set your MongoDB connection string via environment variable:
export MONGODB_URI='connection-string-here'
- Load some sample data into the database via the MongoDB shell:
mongo $MONGODB_URI home --eval "db.kittens.insert([{name:\"roscoe\",color:\"orange\", createdAt: new Date()},{name:\"chester\",color:\"tan\", createdAt: new Date()}])"
If using Atlas, you could also do this via Atlas Data Explorer.
- Build and run the application (this might take a while on the first time):
swift build
swift run
-
If using Leaf, go to
http://localhost:8080
to see your app in action and load a list of kittens! Else, you can runcurl http://localhost:8080
to see the resulting JSON. -
To add a new kitten: if using Leaf, use the form at
http://localhost:8080
. Else, you can add new data viacurl
:curl -d "name=Bob&color=white" http://localhost:8080
.
- MongoDBVapor README
- MongoDBVapor API documentation
- MongoDB + Vapor example application
- Vapor documentation
- MongoDB server documentation
Notice any issues with this template, or have ideas for how to make it more useful? Feel free to open a pull request or GitHub issue, or reach out to us on the SWIFT Jira project.