Skip to content

Commit

Permalink
Apply consistency to code examples / fix a typo
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlasek committed Aug 20, 2018
1 parent f4b4511 commit e30e683
Showing 1 changed file with 59 additions and 53 deletions.
112 changes: 59 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ In your Package.swift file, add the line
```swift
.package(url: "https://github.com/vapor-community/ferno.git", from: "0.2.0")
```

Also make sure you add `Ferno` as a dependency

```swift
Expand All @@ -22,7 +23,6 @@ dependencies: ["Vapor", ..., "Ferno"]
## Setup

1. Ferno uses an access token to read and write to your database. First we will need to get your service account information.

* Log into the Firebase console
* Click the settings gear next to `Project Overview`
* Select `Project settings`
Expand Down Expand Up @@ -86,95 +86,101 @@ There are 6 functions that allow you to interact with your Firebase realtime dat

### GET
There are two functions that allow you get your data.
```swift
client.ferno.retrieve(req: Request, queryItems: [FernoQuery], appendedPath: [String])
```
```swift
client.ferno.retrieveMany(req: Request, queryItems: [FernoQuery], appendedPath: [String])
```
```swift
client.ferno.retrieve(req: Request, queryItems: [FernoQuery], appendedPath: [String])
```
```swift
client.ferno.retrieveMany(req: Request, queryItems: [FernoQuery], appendedPath: [String])
```
The only difference between `retrieve` and `retrieveMany` is the return type.
- `retrive` returns -> `F` where `F` is of type `Decodable`
- `retrieve` returns -> `F` where `F` is of type `Decodable`
- `retrieveMany` returns -> `[String: F]` where `F` is of type `Decodable` and `String` is the key

#### Example
1. Define the value you want the data converted.
```swift
struct Developer: Content {
var name: String
var favLanguage: String
var age: Int
}
```
2. Make the request. Make sure you set the type of the response so Ferno knows what to convert.
```swift
let developers: Future<[String: Developer]> = try client.ferno.retrieveMany(req: request, queryItems: [], appendedPath: ["developers"])
let developer: Future<Developer> = try client.ferno.retrieve(req: request, queryItems: [], appendedPath: ["developers", "dev1"])
```
1. Define the value you want the data converted.
```swift
struct Developer: Content {
var name: String
var favLanguage: String
var age: Int
}
```

2. Make the request. Make sure you set the type of the response so Ferno knows what to convert.
```swift
let developers: Future<[String: Developer]> = try client.ferno.retrieveMany(req: request, queryItems: [], appendedPath: ["developers"])
let developer: Future<Developer> = try client.ferno.retrieve(req: request, queryItems: [], appendedPath: ["developers", "dev1"])
```

### POST
Used to create a new entry in your database
```swift
client.ferno.create(req: Request, appendedPath: [String], body: T) -> Future<FernoChild>
client.ferno.create(req: Request, appendedPath: [String], body: T) -> Future<FernoChild>
```

- `body: T` is of type `Content`.
- `FernoChild` is a struct:
```swift
struct FernoChild: Content {
var name: String
```

```swift
struct FernoChild: Content {
var name: String
}
```

- `FernoChild` is returned, because the API request returns the key from the newly created child.

#### Example
```swift
let newDeveloper = Developer(name: "Elon", favLanguage: "Python", age: 46) //conforms to Content
let newDeveloperKey: Future<FernoChild> = try client.ferno.create(req: request, appendedPath: ["developers"], body: newDeveloper)
```
```swift
let newDeveloper = Developer(name: "Elon", favLanguage: "Python", age: 46) //conforms to Content
let newDeveloperKey: Future<FernoChild> = try client.ferno.create(req: request, appendedPath: ["developers"], body: newDeveloper)
```

### DELETE
Used to delete an entry in your database
```swift
client.ferno.delete(req: Request, appendedPath: [String]) -> Future<Bool>
client.ferno.delete(req: Request, appendedPath: [String]) -> Future<Bool>
```
- the delete method will return a boolean depending on if the delete was successful

#### Example
```swift
let successfulDelete: Future<Bool> = try client.ferno.delete(req: request, appendedPath: ["developers", "dev-1"])
```
```swift
let successfulDelete: Future<Bool> = try client.ferno.delete(req: request, appendedPath: ["developers", "dev-1"])
```

### PATCH
update values at a specific location, but omitted values won't get removed
```swift
client.ferno.update(req: Request, appendedPath: [String], body: T -> Future<T>
client.ferno.update(req: Request, appendedPath: [String], body: T -> Future<T>
```
- the update method will return the body

### Example
```swift
struct UpdateDeveloperName: Content {
var name: String
}
let newDeveloperName = UpdateDeveloperName(name: "Kimbal") //conforms to Content
let updatedDeveloperName: Future<UpdateDeveloperName> = try client.ferno.update(req: request, appendedPath: ["developers", newDeveloperKey.name], body: newDeveloper) //newDeveloperKey.name comes from the create method
```
```swift
struct UpdateDeveloperName: Content {
var name: String
}

let newDeveloperName = UpdateDeveloperName(name: "Kimbal") //conforms to Content
let updatedDeveloperName: Future<UpdateDeveloperName> = try client.ferno.update(req: request, appendedPath: ["developers", newDeveloperKey.name], body: newDeveloper) //newDeveloperKey.name comes from the create method
```

### PUT
overwrite the current location with data you are passing in
```swift
client.ferno.overwrite(req: Request, appendedPath: [String], body: T -> Future<T>
client.ferno.overwrite(req: Request, appendedPath: [String], body: T -> Future<T>
```

#### Example
```swift
struct LeadDeveloper: Content {
var name: String
var company: String
var age: Int
}
let leadDeveloper = LeadDeveloper(name: "Ashley", company: "Bio-Fit", age: 20)
let leadDevResponse: Future<LeadDeveloper> = try client.ferno.overwrite(req: request, appendedPath: ["developers", newDeveloperKey.name], body: leadDeveloper)
```
```swift
struct LeadDeveloper: Content {
var name: String
var company: String
var age: Int
}

let leadDeveloper = LeadDeveloper(name: "Ashley", company: "Bio-Fit", age: 20)
let leadDevResponse: Future<LeadDeveloper> = try client.ferno.overwrite(req: request, appendedPath: ["developers", newDeveloperKey.name], body: leadDeveloper)
```

## Testing

Expand Down

0 comments on commit e30e683

Please sign in to comment.