Manually modifying model property values in vapor 4 response - swift

I have a vapor 4 application. I do a query from database for getting some items and I want to perform some manual calculation based on the returned values before finishing the request. here a sample code of what I am trying to achieve.
final class Todo: Model, Content {
static var schema: String = "todos"
#ID(custom: .id)
var id: Int?
#Field(key: "title")
var title: String
var someValue: Int?
}
/// Allows `Todo` to be used as a dynamic migration.
struct CreateTodo: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema(Todo.schema)
.field("id", .int, .identifier(auto: true))
.field("title", .string, .required)
.create()
}
func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema(Todo.schema).delete()
}
}
final class TodoController:RouteCollection{
func boot(routes: RoutesBuilder) throws {
routes.get("tmp", use: temp)
}
func temp(_ req:Request) throws -> EventLoopFuture<[Todo]> {
Todo.query(on: req.db).all().map { todos in
todos.map {
$0.someValue = (0...10).randomElement()!
return $0
}
}
}
}
The problem is that those manual changes, aren't available in response. In this case someValue property.
Thanks.
[
{
"title": "item 1",
"id": 1
},
{
"title": "item 2",
"id": 2
}
]

The problem you're hitting is that Models override the Codable implementations. This allows you to do things like passing around parents and not adding children etc.
However, this breaks your case. What you should do, is create a new type if you want to return a Todo with another field that isn't stored in the database, something like:
struct TodoResponse: Content {
let id: Int
let title: String
let someValue: Int
}
And then convert from your database type to your response type in your route handler (this is a pretty common pattern and the recommended way to do it in Vapor)

Related

Vapor 4 Unable to Update Optional Parent with Put HTPP Request

I have a model called jurisdictions that houses political jurisdictions. Now theres jurisdictions can sit within other jurisdictions, so I have an optional parent jurisdiction...
Now I can create new jurisdictions just fine, but if I attempt to use a put request to update a jurisdictions parent, I get the following error:
FluentKit/OptionalParent.swift:20: Fatal error: OptionalParent relation OptionalParent<Jurisdiction, Jurisdiction>(key: parent_jurisdiction_id) is get-only.
2022-05-18 13:39:12.421882-0700 Run[32583:5555712] FluentKit/OptionalParent.swift:20: Fatal error: OptionalParent relation OptionalParent<Jurisdiction, Jurisdiction>(key: parent_jurisdiction_id) is get-only.```
Jurisdictions Model
import Fluent
import Vapor
final class Jurisdiction: Model, Content {
static let schema = "jurisdictions"
#ID(key: .id)
var id: UUID?
#OptionalParent(key: "parent_jurisdiction_id")
var jurisdiction: Jurisdiction?
#Field(key: "name")
var name: String
#Field(key: "scope")
var scope: String
#Children(for: \.$jurisdiction)
var jurisdictions: [Jurisdiction]
init() { }
init(name:String, scope:String,parentJurisdictionID:UUID? = nil) {
self.name = name
self.scope = scope
self.$jurisdiction.id = parentJurisdictionID
}
}
Jurisdiction Migration
import Foundation
import Vapor
import Fluent
import FluentPostgresDriver
struct CreateJurisdiction: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema("jurisdictions")
.id()
//parent fields
//attribute fields
.field("name", .string, .required)
.field("scope",.string, .required)
//make unique on:
.unique(on: "name", "scope")
//create scheme
.create()
}
func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema("jurisdictions").delete()
}
}
struct AddParentJurisdictionToJurisdictions: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema("jurisdictions")
.field("parent_jurisdiction_id", .uuid, .references("jurisdictions", "id"))
.update()
}
func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema("jurisdictions")
.deleteField("parent_jurisdiction_id")
.delete()
}
}
Jurisdiction Controller
import Foundation
import Vapor
import Fluent
import FluentPostgresDriver
struct JurisdictionsController: RouteCollection {
func boot(routes: RoutesBuilder) throws {
let jurisdictions = routes.grouped("jurisdictions")
jurisdictions.put(use: update)
}
}
//
func update(req:Request) throws -> EventLoopFuture<HTTPStatus> {
let jurisdiction = try req.content.decode(Jurisdiction.self)
return Jurisdiction.find(jurisdiction.id,on: req.db)
.unwrap(or:Abort(.notFound))
.flatMap {
$0.name = jurisdiction.name
$0.scope = jurisdiction.scope
$0.jurisdiction = $0.jurisdiction //if I remove this line it works
return $0.update(on: req.db).transform(to: .ok)
}
}
}
using a put http request looking like this: http://localhost:8080/jurisdictions with the following content produces the error...don't know what to do here.
{
"scope": "locasl",
"id": "0C6D5F6E-0244-4ABE-847F-2AF89CA27C30",
"jurisdiction": {
"id": "128133B0-25FE-4B6C-B211-CE01AA236AF9"
},
"name": "local"
}
With Fluent and relations if you're trying to set properties you need to do it via the property wrapper rather than the property itself. So in your case it should be
$0.$jurisdiction.id = jurisdiction.id
This allows you to update the relation with only the parent ID rather than requiring the whole parent, in the same way as the initialiser.

Using vapor-fluent to upsert models

I am currently struggling with doing an upsert with vapor/fluent. I have a model something like this:
struct DeviceToken: PostgreSQLModel {
var id: Int?
var token: String
var updatedAt: Date = Date()
init(id: Int? = nil, token: String, updatedAt: Date = Date()) {
self.id = id
self.token = token
self.updatedAt = updatedAt
}
}
struct Account: PostgreSQLModel {
var id: Int?
let username: String
let service: String
...
let deviceTokenId: DeviceToken.ID
init(id: Int? = nil, service: String, username: String, ..., deviceTokenId: DeviceToken.ID) {
self.id = id
self.username = username
....
self.deviceTokenId = deviceTokenId
}
}
From the client something like
{
"deviceToken": {
"token": "ab123",
"updatedAt": "01-01-2019 10:10:10"
},
"account": {
"username": "user1",
"service": "some service"
}
}
is send.
What I'd like to do is to insert the new models if they do not exist else update them. I saw the create(orUpdate:) method however this will only update if the id is the same (in my understanding). Since the client does not send the id i am not quite sure how to handle this.
Also I can't decode the model since the account is send without the deviceTokenId and therefore the decoding will fail. I guess I can address the latter problem by overriding NodeCovertible or by using two different models (one for decoding the json without the id and the actual model from above). However the first problem still remains.
What I exactly want to do is:
Update a DeviceToken if an entry with token already exists else create it
If an account with the combination of username and service already exists update its username, service and deviceTokenId else create it. DeviceTokenId is the id returned from 1.
Any chance you can help me out here ?
For everyone who is interested:
I solved it by writing an extension on PostgreSQLModel to supply an upsert method. I added a gist for you to have a look at: here.
Since these kind of links sometimes are broken when you need the information here a quick overview:
Actual upsert implementation:
extension QueryBuilder
where Result: PostgreSQLModel, Result.Database == Database {
/// Creates the model or updates it depending on whether a model
/// with the same ID already exists.
internal func upsert(_ model: Result,
columns: [PostgreSQLColumnIdentifier]) -> Future<Result> {
let row = SQLQueryEncoder(PostgreSQLExpression.self).encode(model)
/// remove id from row if not available
/// otherwise the not-null constraint will break
row = row.filter { (key, value) -> Bool in
if key == "id" && value.isNull { return false }
return true
}
let values = row
.map { row -> (PostgreSQLIdentifier, PostgreSQLExpression) in
return (.identifier(row.key), row.value)
}
self.query.upsert = .upsert(columns, values)
return create(model)
}
}
Convenience methods
extension PostgreSQLModel {
/// Creates the model or updates it depending on whether a model
/// with the same ID already exists.
internal func upsert(on connection: DatabaseConnectable) -> Future<Self> {
return Self
.query(on: connection)
.upsert(self, columns: [.keyPath(Self.idKey)])
}
internal func upsert<U>(on connection: DatabaseConnectable,
onConflict keyPath: KeyPath<Self, U>) -> Future<Self> {
return Self
.query(on: connection)
.upsert(self, columns: [.keyPath(keyPath)])
}
....
}
I solved the other problem I had that my database model could not be decoded since the id was not send from the client, by using a inner struct which would hold only the properties the client would send. The id and other database generated properties are in the outer struct. Something like:
struct DatabaseModel: PostgreSQLModel {
var id: Int?
var someProperty: String
init(id: Int? = nil, form: DatabaseModelForm) {
self.id = id
self.someProperty = form.someProperty
}
struct DatabaseModelForm: Content {
let someProperty: String
}
}

Handle different kinds of json on the same route with vapor 3

I'm trying to add a POST endpoint on my Vapor 3 server to create a new resource, but I want to have 3 different JSON formats that can create that resource. So I have 3 different Content structs that are each fairly similar.
struct IDJson: Content, DeckConvertible {
var name: String?
var format: Int
var hero: Int
var cards: [Int]
}
struct NameJson: Content, DeckConvertible {
var name: String?
var format: Int
var hero: String
var cards: [String]
}
struct DeckstringJson: Content, DeckConvertible {
var name: String?
var deckstring: String
}
And I would like to add them all to my router like this
class DeckRouteController: RouteCollection {
func boot(router: Router) throws {
router.post(IDJson.self, at: "user", "collection", use: createDeckHandler)
router.post(NameJson.self, at: "user", "collection", use: createDeckHandler)
router.post(DeckstringJson.self, at: "user", "collection", use: createDeckHandler)
}
}
...
private extension DeckRouteController {
func createDeckHandler(_ request: Request, container: DeckConvertible) throws -> Future<Response> {
// Create deck
}
}
Is this possible? at the moment only the last one gets used but is there a way I can do this with some middleware handler or something?
You can handle the decoding in the function itself, ie:
router.post("user", "collection", use: createDeckHandler)
func createDeckHandler(_ request: Request) throws -> String {
let asId = try? request.content.syncDecode(IDJson.self)
let asName = try? request.content.syncDecode(NameJson.self)
if let asId = asId {
// do stuff
} else if let asName = asName {
// do something else
}
}

Vapor 3, Fluent 3 and Many-to-Many relations not working as expected

Just started with Vapor 3 along with a MySQL database and I am having hard time figuring out the Relations part.
I have created 2 models so far: Movie and Actor.
A Movie can have many Actors and an Actor can have many Movies.
Movie Model:
import Vapor
import FluentMySQL
final class Movie: Codable {
var id: Int?
var name: String
var synopsis: String
var dateReleased: Date
var totalGrossed: Float
init(id: Int? = nil, name: String, synopsis: String, dateReleased: Date, totalGrossed: Float) {
self.id = id
self.name = name
self.synopsis = synopsis
self.dateReleased = dateReleased
self.totalGrossed = totalGrossed
}
}
extension Movie {
var actors: Siblings<Movie, Actor, MovieActor> {
return siblings()
}
}
extension Movie: Content {}
extension Movie: Parameter {}
extension Movie: MySQLModel {}
extension Movie: MySQLMigration {
static func prepare(on conn: MySQLConnection) -> Future<Void> {
return MySQLDatabase.create(self, on: conn) { builder in
builder.field(for: \.id, isIdentifier: true)
builder.field(for: \.name)
builder.field(for: \.synopsis)
builder.field(for: \.dateReleased, type: .date)
builder.field(for: \.totalGrossed, type: .float)
}
}
}
Actor Model:
import Vapor
import FluentMySQL
final class Actor: Codable {
var id: Int?
var firstName: String
var lastName: String
var fullName: String {
return firstName + " " + lastName
}
var dateOfBirth: Date
var story: String
init(id: Int? = nil, firstName: String, lastName: String, dateOfBirth: Date, story: String) {
self.id = id
self.firstName = firstName
self.lastName = lastName
self.dateOfBirth = dateOfBirth
self.story = story
}
}
extension Actor {
var actors: Siblings<Actor, Movie, MovieActor> {
return siblings()
}
}
extension Actor: Content {}
extension Actor: Parameter {}
extension Actor: MySQLModel {}
extension Actor: MySQLMigration {
static func prepare(on conn: MySQLConnection) -> Future<Void> {
return MySQLDatabase.create(self, on: conn) { builder in
builder.field(for: \.id, isIdentifier: true)
builder.field(for: \.firstName)
builder.field(for: \.lastName)
builder.field(for: \.dateOfBirth, type: .date)
builder.field(for: \.story, type: .text)
}
}
}
And I have also created a MovieActor model as a MySQLPivot for the relationship:
import Vapor
import FluentMySQL
final class MovieActor: MySQLPivot {
typealias Left = Movie
typealias Right = Actor
static var leftIDKey: LeftIDKey = \.movieID
static var rightIDKey: RightIDKey = \.actorID
var id: Int?
var movieID: Int
var actorID: Int
init(movieID: Int, actorID: Int) {
self.movieID = movieID
self.actorID = actorID
}
}
extension MovieActor: MySQLMigration {}
And have added them to the migration section in the configure.swift file:
var migrations = MigrationConfig()
migrations.add(model: Movie.self, database: .mysql)
migrations.add(model: Actor.self, database: .mysql)
migrations.add(model: MovieActor.self, database: .mysql)
services.register(migrations)
All the tables in the database are being created just fine, but I am not receiving the relationship when calling the get all movies service. I am just receiving the Movie's properties:
final class MoviesController {
func all(request: Request) throws -> Future<[Movie]> {
return Movie.query(on: request).all()
}
}
[
{
"id": 1,
"dateReleased": "2017-11-20T00:00:00Z",
"totalGrossed": 0,
"name": "Star Wars: The Last Jedi",
"synopsis": "Someone with a lightsaber kills another person with a lightsaber"
},
{
"id": 3,
"dateReleased": "1970-07-20T00:00:00Z",
"totalGrossed": 0,
"name": "Star Wars: A New Hope",
"synopsis": "Someone new has been discovered by the force and he will kill the dark side with his awesome lightsaber and talking skills."
},
{
"id": 4,
"dateReleased": "2005-12-20T00:00:00Z",
"totalGrossed": 100000000,
"name": "Star Wars: Revenge of the Sith",
"synopsis": "Anakin Skywalker being sliced by Obi-Wan Kenobi in an epic dual of fates"
}
]
Your help would be appreciated! Thank you very much :)
So I believe you're expecting the relationship to be reflected in what is returned when you query for a Movie model. So for example you expect something like this to be returned for a Movie:
{
"id": 1,
"dateReleased": "2017-11-20T00:00:00Z",
"totalGrossed": 0,
"name": "Star Wars: The Last Jedi",
"synopsis": "Someone with a lightsaber kills another person with a lightsaber",
"actors": [
"id": 1,
"firstName": "Leonardo",
"lastName": "DiCaprio",
"dateOfBirth": "1974-11-11T00:00:00Z",
"story": "Couldn't get an Oscar until wrestling a bear for the big screen."
]
}
However, connecting the Movie and Actor models as siblings simply just gives you the convenience of being able to query the actors from a movie as if the actors were a property of the Movie model:
movie.actors.query(on: request).all()
That line above returns: Future<[Actor]>
This works vice versa for accessing the movies from an Actor object:
actor.movies.query(on: request).all()
That line above returns: Future<[Movie]>
If you wanted it to return both the movie and its actors in the same response like how I assumed you wanted it to work above, I believe the best way to do this would be creating a Content response struct like this:
struct MovieResponse: Content {
let movie: Movie
let actors: [Actor]
}
Your "all" function would now look like this:
func all(_ request: Request) throws -> Future<[MovieResponse]> {
return Movie.query(on: request).all().flatMap { movies in
let movieResponseFutures = try movies.map { movie in
try movie.actors.query(on: request).all().map { actors in
return MovieResponse(movie: movie, actors: actors)
}
}
return movieResponseFutures.flatten(on: request)
}
}
This function queries all of the movies and then iterates through each movie and then uses the "actors" sibling relation to query for that movie's actors. This actors query returns a Future<[Actor]> for each movie it queries the actors for. Map what is returned from the that relation so that you can access the actors as [Actor] instead of Future<[Actor]>, and then return that combined with the movie as a MovieResponse.
What this movieResponseFutures actually consists of is an array of MovieResponse futures:
[Future<[MovieResponse]>]
To turn that array of futures into a single future that consists of an array you use flatten(on:). This waits waits for each of those individual futures to finish and then returns them all as a single future.
If you really wanted the Actor's array inside of the Movie object json, then you could structure the MovieResponse struct like this:
struct MovieResponse: Content {
let id: Int?
let name: String
let synopsis: String
let dateReleased: Date
let totalGrossed: Float
let actors: [Actor]
init(movie: Movie, actors: [Actor]) {
self.id = movie.id
self.name = movie.name
self.synopsis = movie.synopsis
self.dateReleased = movie.dateReleased
self.totalGrossed = movie.totalGrossed
self.actors = actors
}
}
So the underlying issue here is that computed properties aren't provided in a Codable response. What you need to do is define a new type MoviesWithActors and populate that and return that. Or provide a second endpoint, something like /movies/1/actors/ that gets all the actors for a particular movie. That fits better with REST but it depends on your use case, as you may not want the extra requests etc

RealmSwift + ObjectMapper managing String Array (tags)

What I need to represent in RealmSwift is the following JSON scheme:
{
"id": 1234,
"title": "some value",
"tags": [ "red", "blue", "green" ]
}
Its a basic string array that I'm stumbling on. I'm guessing in Realm I need to represent "tags" as
dynamic id: Int = 0
dynamic title: String = ""
let tags = List<MyTagObject>()
making tags its own table in Realm, but how to map it with ObjectMapper? This is how far I got...
func mapping(map: Map) {
id <- map["id"]
title <- map["title"]
tags <- map["tags"]
}
... but the tags line doesn't compile of course because of the List and Realm cannot use a [String] type.
This feels like a somewhat common problem and I'm hoping someone who has faced this can comment or point to a post with a suggestion.
UPDATE 1
The MyTagObject looks like the following:
class MyTagObject: Object {
dynamic var name: String = ""
}
UPDATE 2
I found this post which deals with the realm object but assumes the array has named elements rather than a simple string.
https://gist.github.com/Jerrot/fe233a94c5427a4ec29b
My solution is to use an ObjectMapper TransformType as a custom method to map the JSON to a Realm List<String> type. No need for 2 Realm models.
Going with your example JSON:
{
"id": 1234,
"title": "some value",
"tags": [ "red", "blue", "green" ]
}
First, create an ObjectMapper TransformType object:
import Foundation
import ObjectMapper
import RealmSwift
public struct StringArrayTransform: TransformType {
public init() { }
public typealias Object = List<String>
public typealias JSON = [String]
public func transformFromJSON(_ value: Any?) -> List<String>? {
guard let value = value else {
return nil
}
let objects = value as! [String]
let list = List<String>()
list.append(objectsIn: objects)
return list
}
public func transformToJSON(_ value: Object?) -> JSON? {
return value?.toArray()
}
}
Create your 1 Realm model used to store the JSON data:
import Foundation
import RealmSwift
import ObjectMapper
class MyObjectModel: Object, Mappable {
#objc dynamic id: Int = 0
#objc dynamic title: String = ""
let tags = List<MyTagObject>()
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
id <- map["id"]
title <- map["title"]
tags <- (map["tags"], StringArrayTransform())
}
}
Done!
This line is the magic: tags <- (map["tags"], StringArrayTransform()). This tells ObjectMapper to use our custom StringArrayTransform I showed above which takes the JSON String array and transforms it into a Realm List<String>.
First of all we should assume that our model extends both Object and Mappable.
Let's create a wrapper model to store the primitive (String) type:
class StringObject: Object {
dynamic var value = ""
}
Then we describe corresponding properties and mapping rules for the root model (not the wrapper one):
var tags = List<StringObject>()
var parsedTags: [String] {
var result = [String]()
for tag in tags {
result.append(tag.value)
}
return result
}
override static func ignoredProperties() -> [String] {
return ["parsedTags"]
}
func mapping(map: Map) {
if let unwrappedTags = map.JSON["tags"] as? [String] {
for tag in unwrappedTags {
let tagObject = StringObject()
tagObject.value = tag
tags.append(tagObject)
}
}
}
We need a tags property to store and obtain the data about tags from Realm.
Then a parsedTags property simplifies extraction of tags in the usual array format.
An ignoredProperties definition allows to avoid some failures with Realm while data savings (because of course we can't store non-Realm datatypes in the Realm).
And at last we are manually parsing our tags in the mapping function to store it in the Realm.
It will work if your tags array will contains a Dictionary objects with a key: "name"
{
"id": 1234,
"title": "some value",
"tags": [ ["name" : "red"], ... ]
}
If you cannot modify JSON object, I recommend you to map json to realm programmatically.
for tagName in tags {
let tagObject = MyTagObject()
tagObject.name = tagName
myObject.tags.append(tagObject)
}
Follow this code
import ObjectMapper
import RealmSwift
//Create a Model Class
class RSRestaurants:Object, Mappable {
#objc dynamic var status: String?
var data = List<RSData>()
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
status <- map["status"]
data <- (map["data"], ListTransform<RSData>())
}
}
//Use this for creating Array
class ListTransform<T:RealmSwift.Object> : TransformType where T:Mappable {
typealias Object = List<T>
typealias JSON = [AnyObject]
let mapper = Mapper<T>()
func transformFromJSON(_ value: Any?) -> Object? {
let results = List<T>()
if let objects = mapper.mapArray(JSONObject: value) {
for object in objects {
results.append(object)
}
}
return results
}
func transformToJSON(_ value: Object?) -> JSON? {
var results = [AnyObject]()
if let value = value {
for obj in value {
let json = mapper.toJSON(obj)
results.append(json as AnyObject)
}
}
return results
}
}