Reputation: 27
I have already gone through the proper way of the fluent provider to build a relation for my model (Swift) using vapor (server side) and PostgreSQL provider (database), I follow the general method of fluent, but I don't know where I am doing mistake, in extension for modelName preparation, below the code of my modelName.swift and main.swift.
import Foundation
import Vapor
import FluentProvider
import PostgreSQLProvider
final class modelName: Model {
let storage = Storage()
var id: Node?
var name:String
var displayName:String
public var content: String
init(content: String, displayName:String, name:String) {
self.content = content
self.displayName = displayName
self.name = name
}
func forDataBase() {
let array:[berichtDataPoint] = [intDataPoint(), boolDataPoint(), doubleDataPoint()]
let _ = array[0] as! intDataPoint
let _ = array[1] as! doubleDataPoint
for point in array {
switch point {
case is intDataPoint:
print("int")
case is doubleDataPoint:
print("double")
case is boolDataPoint:
print("bool")
default:
print("error")
}
}
}
func makeRow() throws -> Row {
var row = Row()
try row.set("id", idKey)
try row.set("displayName", displayName)
try row.set("name", name)
return row
}
init(row: Row) throws {
content = try row.get("content")
displayName = try row.get("displayName")
name = try row.get("name")
}
func makeNode(context: Context) throws -> Node {
return try Node(node: [
"id": id,
"content": content,
"displayName": displayName,
"name": name
])
}
}
extension modelName: Preparation {
static func prepare(_ database: Database) throws {
try database.create(self) { modelName in
modelName.id()
modelName.string("displayName")
modelName.string("name")
}
}
static func revert(_ database: Database) throws {
try database.delete(self)
}
}
main.swift
import App
import Vapor
import FluentProvider
import PostgreSQLProvider
/// We have isolated all of our App's logic into
/// the App module because it makes our app
/// more testable.
///
/// In general, the executable portion of our App
/// shouldn't include much more code than is presented
/// here.
///
/// We simply initialize our Droplet, optionally
/// passing in values if necessary
/// Then, we pass it to our App's setup function
/// this should setup all the routes and special
/// features of our app
///
/// .run() runs the Droplet's commands,
/// if no command is given, it will default to "serve"
let config = try Config()
config.preparations.append(modelName.self) \\error is here '(Use of
unresolved identifier 'modelName')
let drop = try Droplet(config)
try drop.setup()
try drop.run()
Upvotes: 0
Views: 396
Reputation: 26
I think the root cause is that modules are separated.
If you created vapor project as vapor new
, main.swift
is in Run
module, modelName.swift
is in App
module.
// Package.swift
let package = Package(
name: "hello",
targets: [
Target(name: "App"),
Target(name: "Run", dependencies: ["App"]),
],
When access to other module class, target class's access level is must use open
or public
.
// modelName.swift
public class moduleName: Model {
...
Please note that you must also modify other method declarations according to this change.
Thanks.
Upvotes: 1