Skip to content

Instantly share code, notes, and snippets.

@hyouuu
Created May 16, 2018 09:48
Show Gist options
  • Save hyouuu/8e9e5dc1e3c397d1f72b6abf49b8f514 to your computer and use it in GitHub Desktop.
Save hyouuu/8e9e5dc1e3c397d1f72b6abf49b8f514 to your computer and use it in GitHub Desktop.
Thread 1: Fatal error: Error raised at top level: ⚠️ PostgreSQL Error: Could not parse Date from binary data type: UNKNOWN 1184. - id: PostgreSQLError.date
// Fluent 2 model - note that in fluent.json I have:
// "idType": "uuid",
// "keyNamingConvention": "camelCase"
import Foundation
import Vapor
import FluentProvider
import HTTP
final class User: Model {
let storage = Storage()
// MARK: Properties and database keys
var uname: String
var password: String
var name: String
var intro: String?
var imageKey: String?
// Could be OneSignal player ID or SNS id or device token etc
var pushSubscriptionId: String?
var storyLikeCount: Int = 0
var msgLikeCount: Int = 0
var followingCount: Int = 0
var followerCount: Int = 0
/// Creates a new User
init(uname: String,
password: String,
name: String,
intro: String?,
imageKey: String?,
pushSubscriptionId: String?,
storyLikeCount: Int = 0,
msgLikeCount: Int = 0,
followingCount: Int = 0,
followerCount: Int = 0
)
{
self.uname = uname
self.password = password
self.name = name
self.intro = intro
self.imageKey = imageKey
self.pushSubscriptionId = pushSubscriptionId
self.storyLikeCount = storyLikeCount
self.msgLikeCount = msgLikeCount
self.followingCount = followingCount
self.followerCount = followerCount
}
// MARK: Fluent Serialization
/// Initializes User from the database row
init(row: Row) throws {
uname = try row.get(Attr.uname)
password = try row.get(Attr.password)
name = try row.get(Attr.name)
intro = try row.get(Attr.intro)
imageKey = try row.get(Attr.imageKey)
pushSubscriptionId = try row.get(Attr.pushSubscriptionId)
storyLikeCount = try row.get(Attr.storyLikeCount)
msgLikeCount = try row.get(Attr.msgLikeCount)
followingCount = try row.get(Attr.followingCount)
followerCount = try row.get(Attr.followerCount)
}
// Serializes the Post to the database
func makeRow() throws -> Row {
var row = Row()
try row.set(Attr.uname, uname)
try row.set(Attr.password, password)
try row.set(Attr.name, name)
try row.set(Attr.intro, intro)
try row.set(Attr.imageKey, imageKey)
try row.set(Attr.pushSubscriptionId, pushSubscriptionId)
try row.set(Attr.storyLikeCount, storyLikeCount)
try row.set(Attr.msgLikeCount, msgLikeCount)
try row.set(Attr.followingCount, followingCount)
try row.set(Attr.followerCount, followerCount)
return row
}
}
// adds createdAt & updatedAt of type Date?
extension User: Timestampable { }
extension User: SoftDeletable { }
extension User: Preparation {
/// Prepares a table/collection in the database
static func prepare(_ database: Database) throws {
try database.create(self) { builder in
builder.id()
builder.string(Attr.uname)
builder.string(Attr.password)
builder.string(Attr.name)
builder.string(Attr.intro, optional: true)
builder.string(Attr.imageKey, optional: true)
builder.string(Attr.pushSubscriptionId, optional: true)
builder.int(Attr.storyLikeCount, default: 0)
builder.int(Attr.msgLikeCount, default: 0)
builder.int(Attr.followingCount, default: 0)
builder.int(Attr.followerCount, default: 0)
}
}
/// Undoes what was done in `prepare`
static func revert(_ database: Database) throws {
try database.delete(self)
}
}
// Fluent 3 Model - standard migration block
// var migrations = MigrationConfig()
// migrations.add(model: User.self, database: .psql)
// services.register(migrations)
import FluentPostgreSQL
import Vapor
/// A simple user.
final class User: PostgreSQLUUIDModel {
var id: UUID?
/// Timestampable
var createdAt: Date?
var updatedAt: Date?
/// SoftDeletable
var deletedAt: Date?
var uname: String
var password: String
var name: String
var intro: String?
var imageKey: String?
// Could be OneSignal player ID or SNS id or device token etc
var pushSubscriptionId: String?
var storyLikeCount: Int = 0
var msgLikeCount: Int = 0
var followingCount: Int = 0
var followerCount: Int = 0
/// Creates a new user.
init(id: UUID?,
uname: String,
password: String,
name: String,
intro: String?,
imageKey: String?,
pushSubscriptionId: String?,
storyLikeCount: Int = 0,
msgLikeCount: Int = 0,
followingCount: Int = 0,
followerCount: Int = 0)
{
self.id = id
self.uname = uname
self.password = password
self.name = name
self.intro = intro
self.imageKey = imageKey
self.pushSubscriptionId = pushSubscriptionId
self.storyLikeCount = storyLikeCount
self.msgLikeCount = msgLikeCount
self.followingCount = followingCount
self.followerCount = followerCount
}
}
extension User: Timestampable {
static var createdAtKey: WritableKeyPath<User, Date?> {
return \.createdAt
}
static var updatedAtKey: WritableKeyPath<User, Date?> {
return \.updatedAt
}
}
extension User: SoftDeletable {
static var deletedAtKey: WritableKeyPath<User, Date?> {
return \.deletedAt
}
}
/// Allows `User` to be used as a dynamic migration.
extension User: Migration { }
/// Allows `User` to be encoded to and decoded from HTTP messages.
extension User: Content { }
/// Allows `User` to be used as a dynamic parameter in route definitions.
extension User: Parameter { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment