Open Noobitious opened 7 years ago
In case it helps, here is the full code for my "model" (in case I didn't make it correctly?)... (ps, why doesn't this format nicely as "code" in github?)
` // // ImportDB.swift // DB_Server //
import Foundation import Vapor import VaporMongo
final class ImportDB: Model { var id: Node? // ??? var exists: Bool = false var importstage: String var owner: String var compressed: Bool var needstransforming: Bool var dashborgname: String var dashbunchname: String // should be unique var linkoriginal: String var linkdecompressed: String var linkfinal: String init(linkoriginal: String) { //self.id = UUID().uuidString.makeNode() // ??? self.id = nil self.importstage = String() self.owner = String() self.compressed = Bool() self.needstransforming = Bool() self.dashborgname = String() self.dashbunchname = String() self.linkoriginal = String() self.linkdecompressed = String() self.linkfinal = String() }
init(node: Node, in context: Context) throws {
id = try node.extract("id")
importstage = try node.extract("importstage")
owner = try node.extract("owner")
compressed = try node.extract("compressed")
needstransforming = try node.extract("needstransforming")
dashborgname = try node.extract("dashborgname")
dashbunchname = try node.extract("dashbunchname")
linkoriginal = try node.extract("linkoriginal")
linkdecompressed = try node.extract("linkdecompressed")
linkfinal = try node.extract("linkfinal")
}
func makeNode(context: Context) throws -> Node {
return try Node(node: [
"id": id,
"importstage": importstage,
"owner": owner,
"compressed": compressed,
"needstransforming": needstransforming,
"dashborgname": dashborgname,
"dashbunchname": dashbunchname,
"linkoriginal": linkoriginal,
"linkdecompressed": linkdecompressed,
"linkfinal": linkfinal
])
}
static func prepare(_ database: Database) throws {
// Let's assume we are using a SQL database. To prepare the database for our User class, we need to create a table. If you are using a database like Mongo, you can leave this method unimplemented.
try database.create("importdbs") { importdbs in
importdbs.id()
importdbs.string("importstage")
importdbs.string("owner")
importdbs.bool("compressed")
importdbs.bool("needstransforming")
importdbs.string("dashborgname")
importdbs.string("dashbunchname")
importdbs.string("linkoriginal")
importdbs.string("linkdecompressed")
importdbs.string("linkfinal")
}
}
static func revert(_ database: Database) throws {
// An optional preparation reversion. This will be run if vapor run prepare --revert is called
try database.delete("importdbs")
}
} `
@Noobitious take a look at https://github.com/vapor/mongo-driver/issues/27#issuecomment-266738711
I have made a model and saved it to a Mongo DB. My model creates "var id: Node?" and init to "self.id = nil". However, when the record is saved I notice Mongo creates a different "_id" with values like this "ObjectId("31943c580047b1f729e088f5")". The "id" field is left null as you can see here:
However, if I do the same thing but save the model twice then it seems to populate the "id" field as you can see here:
I believe this should have been done on the first save, but if you have multiple documents, it will arbitrarily update the id of the first document it finds (i.e., not necessarily the correct one)