Featured post

Creating API with Swift Perfect and MongoDB



MongoDB is from the company 10Gen, they've open sourced the project since their database component (which is the mongoDB) is really good
MongoDB is documented-oriented, compare to the traditional SQL which is Relational

SQLNo-SQL (which is MongoDB)
DatabaseDatabase
TableCollection
RowDocument

Use a Database: use database_name
Retrieve Collection: db.database_name.find()
Insert in Collection: db.database_name.insert({json_formatted_data})
Find a Document with parameter: db.database_name.find({key:value paramter})

Those are the basics of mongoDB

Also to run mongoDB from terminal, it's a bit tricky, just type and run mongod
and when you get this message "connection accepted" then open a new terminal and run mongo
the > indicates that you can type mongoDB code
Vincents-Mac-mini:PerfectTemplate vbacalso$ swift --version
try installing this one:
# after installing swiftenv from https://swiftenv.fuller.li/en/latest/swiftenv install https://swift.org/builds/swift-3.0.1-preview-1/xcode/swift-3.0.1-PREVIEW-1/swift-3.0.1-PREVIEW-1-osx.pkg
Also if the error persist, go into the PerfectTemplate directory and open for edit the .swift-version file
Generate the XCode Project File
Setup MongoDB

After you setup mongoDB and a little exercise using mongodb in terminal. You have to add in your Package.swift the mongoDB dependency

Usage in mongoDB:





Here's a guide for Creating API with Swift Perfect 2.0 and mongoDB
So I'll be compiling this as my personal note, since it was a pain in the ass for me when I set this up.
First of, get start here: https://github.com/PerfectlySoft/PerfectDocs/blob/master/guide/gettingStarted.md
Follow it through, and if ever you get this error:


swiftenv: version `DEVELOPMENT-SNAPSHOT-2016-09-05-a' (set by /Users/vbacalso/Desktop/PerfectTemplate/.swift-version) is not installed


just remove its contents. Then try building again thru swift build. If it works with you also, what this does most probably is, it matches your swift-version in your toolchain to what is written in the swift-version.

After build, generate an xcodeproj file to make it easier for you to develop your API project.

swift package generate-xcodeproj


take note, that every time you add dependency, you need to do a swift build and swift package generate-xcodeproj

What this does is to update your project to include the new dependencies, much like with pod install, every time you update your Podfile

Make sure to run mongoDB - this will be your Database
Run the Web Server - this is your Swift Perfect
Test your POST and GET Requests using Postman

Comments