VaporMongo

Vapor project with MongoDB

6
2
Swift

VaporMongo

This project uses the Swift Web framework Vapor with MongoDB to create a generic API for consuming users and objects of any type with any data without having to update the API code or the DB schema.

MongoDB connection

In order to connect to MongoDB you must add a mongo.json file inside of the folder Config/secrets.

The mongo.json file must contain the following structure:

{
    "user": "",
    "password": "",
    "database": "local",
    "port": "27017",
    "host": ""
}

User

The user is used for authentication and authorization.

  • The user’s collection in mongo must be users

User structure

{
    "id" : "581f3d7d1940cb25cb1b1f2d",
    "username" : "gabriel",
    "password" : "12345678",
    "data" : {
        ...
    },
    "access_token" : "...",
}
  • The data property can also be an array.

Object

The object is used to manage generic objects.

  • The objects’s collection in mongo must be objects

Object structure

{
    "id" : "581f3d7d1940cb25cb1b1f2d",
    "type" : "object type, used for filtering",
    "data" : {
        ...
    }
}
  • The data property can also be an array.

  • This project also contain posts which is the example from Vapor example.

Route

Registration

The path for registration is /api/users/register and all the neccessary attributes for user like username and password should be setted.

Authorization

Every path of the app needs the Bearer access_token setted in Authorization header of the request in order to access the data or perform an action.

The access_token is setted by the path /api/users/token where you should provide the username and password of the user. In a future commit the access_token will also be returned in the registration process.

Help

Vapor

Vapor is the most used web framework for Swift. It provides a beautifully expressive and easy to use foundation for your next website, API, or cloud project.
Website

📖 Documentation

Visit the Vapor web framework’s documentation for instructions on how to use this package.

💧 Community

Join the welcoming community of fellow Vapor developers in slack.

🔧 Compatibility

Vapor package has been tested on macOS and Ubuntu.

MongoDB

MongoDB (from humongous) is a free and open-source cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schemas. MongoDB is developed by MongoDB Inc. and is free and open-source, published under a combination of the GNU Affero General Public License and the Apache License.

Ubuntu Installation