Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 committed Apr 9, 2020
1 parent c9771ea commit 22a79a4
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Sources/Fluent/Deprecated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ extension Application.Fluent.Sessions {
) -> Middleware
where User: SessionAuthenticatable, User: Model, User.SessionID == User.IDValue
{
User.sessionAuthenticator(databaseID: databaseID)
User.sessionAuthenticator(databaseID)
}
}
61 changes: 36 additions & 25 deletions Sources/Fluent/Fluent+Sessions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,22 @@ extension Application.Fluent {
}
}

public protocol ModelSessionAuthenticatable: Model, SessionAuthenticatable
where Self.SessionID == Self.IDValue
{ }

extension ModelSessionAuthenticatable {
public var sessionID: SessionID {
guard let id = self.id else {
fatalError("Cannot persist unsaved model to session.")
}
return id
}
}

extension Model where Self: SessionAuthenticatable, Self.SessionID == Self.IDValue {
public static func sessionAuthenticator(
databaseID: DatabaseID? = nil
_ databaseID: DatabaseID? = nil
) -> Authenticator {
DatabaseSessionAuthenticator<Self>(databaseID: databaseID)
}
Expand Down Expand Up @@ -45,28 +58,28 @@ private struct DatabaseSessions: SessionDriver {

func createSession(_ data: SessionData, for request: Request) -> EventLoopFuture<SessionID> {
let id = self.generateID()
return Session(key: id, data: data)
return SessionRecord(key: id, data: data)
.create(on: request.db(self.databaseID))
.map { id }
}

func readSession(_ sessionID: SessionID, for request: Request) -> EventLoopFuture<SessionData?> {
return Session.query(on: request.db(self.databaseID))
SessionRecord.query(on: request.db(self.databaseID))
.filter(\.$key == sessionID)
.first()
.map { $0?.data }
}

func updateSession(_ sessionID: SessionID, to data: SessionData, for request: Request) -> EventLoopFuture<SessionID> {
return Session.query(on: request.db(self.databaseID))
SessionRecord.query(on: request.db(self.databaseID))
.filter(\.$key == sessionID)
.set(\.$data, to: data)
.update()
.map { sessionID }
}

func deleteSession(_ sessionID: SessionID, for request: Request) -> EventLoopFuture<Void> {
return Session.query(on: request.db(self.databaseID))
SessionRecord.query(on: request.db(self.databaseID))
.filter(\.$key == sessionID)
.delete()
}
Expand Down Expand Up @@ -94,14 +107,28 @@ private struct DatabaseSessionAuthenticator<User>: SessionAuthenticator
}
}

public final class Session: Model {
public final class SessionRecord: Model {
public static let schema = "_fluent_sessions"

private struct _Migration: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema("_fluent_sessions")
.id()
.field("key", .string, .required)
.field("data", .json, .required)
.create()
}

func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema("_fluent_sessions").delete()
}
}

public static var migration: Migration {
CreateSession()
_Migration()
}

@ID(key: "id")
@ID(key: .id)
public var id: UUID?

@Field(key: "key")
Expand All @@ -110,27 +137,11 @@ public final class Session: Model {
@Field(key: "data")
public var data: SessionData

public init() {

}
public init() { }

public init(id: UUID? = nil, key: SessionID, data: SessionData) {
self.id = id
self.key = key
self.data = data
}
}

public struct CreateSession: Migration {
public func prepare(on database: Database) -> EventLoopFuture<Void> {
return database.schema("sessions")
.field("id", .uuid, .identifier(auto: false))
.field("key", .string, .required)
.field("data", .json, .required)
.create()
}

public func revert(on database: Database) -> EventLoopFuture<Void> {
return database.schema("sessions").delete()
}
}
15 changes: 6 additions & 9 deletions Tests/FluentTests/PaginationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,14 @@ final class PaginationTests: XCTestCase {
let app = Application(.testing)
defer { app.shutdown() }

let test = TestDatabase()
app.databases.use(test.configuration, as: .test)

app.get("todos") { req -> EventLoopFuture<Page<Todo>> in
Todo.query(on: req.db).paginate(for: req)
}

var rows: [TestOutput] = []
for i in 1...1_000 {
rows.append(TestOutput([
"id": i,
"title": "Todo #\(i)"
]))
}

test.use { query in
let test = CallbackTestDatabase { query in
XCTAssertEqual(query.schema, "todos")
let result: [TestOutput]
if let limit = query.limits.first?.value, let offset = query.offsets.first?.value {
Expand All @@ -39,6 +31,11 @@ final class PaginationTests: XCTestCase {
return result
}
}
app.databases.use(test.configuration, as: .test)

app.get("todos") { req -> EventLoopFuture<Page<Todo>> in
Todo.query(on: req.db).paginate(for: req)
}

try app.test(.GET, "todos") { res in
XCTAssertEqual(res.status, .ok)
Expand Down
2 changes: 1 addition & 1 deletion Tests/FluentTests/RepositoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final class RepositoryTests: XCTestCase {
let app = Application(.testing)
defer { app.shutdown() }

let test = TestDatabase()
let test = ArrayTestDatabase()
app.databases.use(test.configuration, as: .test)

app.posts.use { req in
Expand Down
23 changes: 22 additions & 1 deletion Tests/FluentTests/SessionTests.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import XCTFluent
import XCTVapor
import Fluent
import Vapor

final class SessionTests: XCTestCase {
func testSessions() throws {
let app = Application(.testing)
defer { app.shutdown() }

// Setup test db.
let test = TestDatabase()
let test = ArrayTestDatabase()
app.databases.use(test.configuration, as: .test)
app.migrations.add(SessionRecord.migration)

// Configure sessions.
app.sessions.use(.fluent)
Expand Down Expand Up @@ -58,6 +60,25 @@ final class SessionTests: XCTestCase {
}
}

final class User: Model {
static let schema = "users"

@ID(key: .id)
var id: UUID?

@Field(key: "name")
var name: String

init() { }

init(id: UUID? = nil, name: String) {
self.id = id
self.name = name
}
}

extension User: ModelSessionAuthenticatable { }

extension DatabaseID {
static var test: Self {
.init(string: "test")
Expand Down

0 comments on commit 22a79a4

Please sign in to comment.