Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joannis/simple
https://github.com/joannis/simple
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/joannis/simple
- Owner: Joannis
- Created: 2016-05-09T19:42:14.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-05-15T22:58:07.000Z (over 8 years ago)
- Last Synced: 2024-05-02T05:30:25.134Z (7 months ago)
- Language: Swift
- Size: 12.7 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Simple
A library that makes it very simple to set up a backend using MongoDB, Vapor (HTTP) routing and Models using Genome.
## Simple example
```swift
import Simplelet app = try! SimpleApplication(databaseUrl: "mongodb://127.0.0.1:27017", database: "simple")
struct User: Model {
static let collection = app.collection("users")
var id: ObjectId
var username: String
var age: Int
var metadata: Document
func serialize() throws -> Document {
return [
"username": ~username,
"age": ~age
]
}
init(with map: Map) throws {
id = try ObjectId(try map.extract("_id"))
username = try map.extract("username")
age = try map.extract("age")
metadata = ["special": true]
}
}app.get("user", User.self) { request, user in
return try user.toJson().serialize()
}app.start(port: 8080)
```## Featuring RequestInitializable
```swift
enum LoginError: ErrorProtocol {
case missingAuthenticationDetails
}class LoginRequest: RequestInitializable {
let request: Request
let user: User?
var authenticated = falserequired init(request: Request) throws {
self.request = request
guard let username = request.data["username"].string, let password = request.data["password"].string else {
throw LoginError.missingAuthenticationDetails
}self.user = try User.findOne(matching: "username" == ~username)
if let user = user {
authenticated = user.password == password
}
}
}app.get("login") { (loginRequest: LoginRequest) in
if let user = loginRequest.user where loginRequest.authenticated {
return "Welcome \(user.username)"
}return "Login failed"
}
```## Supports reflection to reduce boilerplate
```swift
struct User: ReflectableModel {
static let collection = app.collection("users")
var _id: ObjectId
var metadata: Document
var username: String
var age: Int
// Plain text for example purposes ONLY
var password: String
init(with map: Map) throws {
_id = try ObjectId(try map.extract("_id"))
username = try map.extract("username")
age = try map.extract("age")
password = try map.extract("password")
metadata = ["special": true]
}
}
```