Skip to content

Commit

Permalink
Merge pull request #181 from vapor/storable
Browse files Browse the repository at this point in the history
[WIP] - Storable
  • Loading branch information
loganwright authored Feb 17, 2017
2 parents c7ef296 + a574f56 commit 99d959c
Show file tree
Hide file tree
Showing 35 changed files with 219 additions and 207 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ os:
language: generic
sudo: required
dist: trusty
osx_image: xcode8
osx_image: xcode8.3
script:
- eval "$(curl -sL https://swift.vapor.sh/ci)"
- eval "$(curl -sL https://swift.vapor.sh/ci-3.1)"
- eval "$(curl -sL https://swift.vapor.sh/codecov)"
58 changes: 45 additions & 13 deletions Sources/Fluent/Entity/Entity.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,43 @@
public final class Storage {
public init() {}

fileprivate var exists: Bool = false
fileprivate var id: Node? = nil
}

public protocol Storable: class {
/// General implementation should just be `let storage = Storage()`
var storage: Storage { get }
}

extension Storable {
/// Whether or not entity was retrieved from database.
///
/// This value shouldn't be interacted w/ external users
/// w/o explicit knowledge.
///
public internal(set) var exists: Bool {
get {
return storage.exists
}
set {
storage.exists = newValue
}
}

public var id: Node? {
get {
return storage.id
}
set {
storage.id = newValue
}
}
}

/// Represents an entity that can be
/// stored and retrieved from the `Database`.
public protocol Entity: Preparation, NodeConvertible {
// DELETE ME
var id: Node? { get set }
var exists: Bool { get set }
public protocol Entity: class, Preparation, NodeConvertible, Storable {

/// The plural relational name of this model.
/// Used as the collection or table name.
Expand Down Expand Up @@ -68,8 +102,8 @@ extension Entity {
extension Entity {
/// Persists the entity into the
/// data store and sets the `id` property.
public mutating func save() throws {
try Self.query().save(&self)
public func save() throws {
try Self.query().save(self)
}

/// Deletes the entity from the data
Expand Down Expand Up @@ -98,17 +132,15 @@ extension Entity {
}
}

// MARK: Deprecated
// MARK: Relatable

extension Entity {
public var exists: Bool {
// TODO: Implement me
get { return false }
set { }
/// See Entity.idKey -- instance implementation of static var
public var idKey: String {
return type(of: self).idKey
}
}

// MARK: Relatable

extension Entity {
/// See Entity.entity
public static var entity: String {
Expand Down
9 changes: 4 additions & 5 deletions Sources/Fluent/Pivot/Pivot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ public final class Pivot<
return entity
}

public var id: Node?
public var leftId: Node
public var rightId: Node
public var exists = false
public let storage = Storage()

public init(_ left: Left, _ right: Right) throws {
guard left.exists else {
Expand All @@ -49,15 +48,15 @@ public final class Pivot<
}

public init(node: Node, in context: Context) throws {
id = try node.extract(type(of: self).idKey)

leftId = try node.extract(Left.foreignIdKey)
rightId = try node.extract(Right.foreignIdKey)

id = try node.extract(idKey)
}

public func makeNode(context: Context) throws -> Node {
return try Node(node: [
type(of: self).idKey: id,
idKey: id,
Left.foreignIdKey: leftId,
Right.foreignIdKey: rightId,
])
Expand Down
2 changes: 1 addition & 1 deletion Sources/Fluent/Pivot/PivotProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ extension PivotProtocol where Self: Entity {
public static func attach(_ left: Left, _ right: Right) throws {
_ = try assertSaved(left, right)

var pivot = try Pivot<Left, Right>(left, right)
let pivot = try Pivot<Left, Right>(left, right)
try pivot.save()
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Fluent/Preparation/Database+Preparation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extension Database {


// record that this preparation has run
var migration = Migration(name: preparation.name)
let migration = Migration(name: preparation.name)
try migration.save()
}
}
Expand Down
8 changes: 3 additions & 5 deletions Sources/Fluent/Preparation/Migration.swift
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
final class Migration: Entity {
static var entity = "fluent"
var exists: Bool = false

var id: Node?
let storage = Storage()
var name: String

init(name: String) {
self.name = name
}

init(node: Node, in context: Context) throws {
id = try node.extract("id")
name = try node.extract("name")
id = try node.extract(idKey)
}

func makeNode(context: Context = EmptyNode) throws -> Node {
return try Node(node: [
"id": id,
idKey: id,
"name": name
])
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Fluent/Query/Query.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public final class Query<T: Entity> {

for result in array {
do {
var model = try T(node: result, in: _context)
let model = try T(node: result, in: _context)
if case .object(let dict) = result {
model.id = dict[T.idKey]
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Fluent/Query/QueryRepresentable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extension QueryRepresentable {
query.action = .fetch
query.limit = Limit(count: 1)

var model = try query.run().first
let model = try query.run().first
model?.exists = true

return model
Expand All @@ -25,7 +25,7 @@ extension QueryRepresentable {

let models = try query.run()
models.forEach { model in
var model = model
let model = model
model.exists = true
}

Expand Down Expand Up @@ -69,7 +69,7 @@ extension QueryRepresentable {

/// Attempts to save a supplied entity
/// and updates its identifier if successful.
public func save(_ model: inout T) throws {
public func save(_ model: T) throws {
let query = try makeQuery()

if let _ = model.id, model.exists {
Expand Down Expand Up @@ -135,7 +135,7 @@ extension QueryRepresentable {
try query.run()
model.didDelete()

var model = model
let model = model
model.exists = false
}
}
Expand Down
11 changes: 5 additions & 6 deletions Sources/FluentTester/Atom.swift
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
import Fluent

public final class Atom: Entity {
public var id: Node?

public var name: String
public var protons: Int
public var weight: Double

public var exists: Bool = false
public let storage = Storage()

public init(id: Node?, name: String, protons: Int, weight: Double) {
self.id = id
self.name = name
self.protons = protons
self.weight = weight
self.id = id
}

public init(node: Node, in context: Context) throws {
id = try node.extract(type(of: self).idKey)
name = try node.extract("name")
protons = try node.extract("protons")
weight = try node.extract("weight")

id = try node.extract(idKey)
}

public func makeNode(context: Context) throws -> Node {
return try Node(node: [
type(of: self).idKey: id,
idKey: id,
"name": name,
"protons": protons,
"weight": weight
Expand Down
9 changes: 4 additions & 5 deletions Sources/FluentTester/Compound.swift
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import Fluent

public final class Compound: Entity {
public var id: Node?
public var name: String
public var exists = false
public let storage = Storage()

public init(id: Node?, name: String) {
self.id = id
self.name = name
self.id = id
}

public init(node: Node, in context: Context) throws {
id = try node.extract("id")
name = try node.extract("name")
id = try node.extract(idKey)
}

public func makeNode(context: Context) throws -> Node {
return try Node(node: [
"id": id,
idKey: id,
"name": name
])
}
Expand Down
9 changes: 4 additions & 5 deletions Sources/FluentTester/Student.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import Fluent

final class Student: Entity {
var id: Node?

var name: String
var age: Int
var ssn: String
var donor: Bool
var meta: Node

var exists = false
let storage = Storage()

init(name: String, age: Int, ssn: String, donor: Bool, meta: Node) {
self.name = name
Expand All @@ -20,17 +18,18 @@ final class Student: Entity {
}

init(node: Node, in context: Context) throws {
id = try node.extract("id")
name = try node.extract("name")
age = try node.extract("age")
ssn = try node.extract("ssn")
donor = try node.extract("donor")
meta = try node.extract("meta")

id = try node.extract(idKey)
}

func makeNode(context: Context) throws -> Node {
return try Node(node: [
"id": id,
idKey: id,
"name": name,
"age": age,
"ssn": ssn,
Expand Down
2 changes: 1 addition & 1 deletion Sources/FluentTester/Tester+InsertAndFind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extension Tester {
}
Atom.database = database

var hydrogen = Atom(id: nil, name: "Hydrogen", protons: 1, weight: 1.007)
let hydrogen = Atom(id: nil, name: "Hydrogen", protons: 1, weight: 1.007)

guard hydrogen.exists == false else {
throw Error.failed("Exists should be false since not yet saved.")
Expand Down
22 changes: 11 additions & 11 deletions Sources/FluentTester/Tester+PivotsAndRelations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ extension Tester {
Compound.database = database
Pivot<Atom, Compound>.database = database

var hydrogen = Atom(id: nil, name: "Hydrogen", protons: 1, weight: 1.007)
let hydrogen = Atom(id: nil, name: "Hydrogen", protons: 1, weight: 1.007)
try hydrogen.save()

var carbon = Atom(id: nil, name: "Carbon", protons: 6, weight: 12.011)
let carbon = Atom(id: nil, name: "Carbon", protons: 6, weight: 12.011)
try carbon.save()

var oxygen = Atom(id: nil, name: "Oxygen", protons: 8, weight: 15.999)
let oxygen = Atom(id: nil, name: "Oxygen", protons: 8, weight: 15.999)
try oxygen.save()

var water = Compound(id: nil, name: "Water")
let water = Compound(id: nil, name: "Water")
try water.save()
try Pivot<Atom, Compound>.attach(hydrogen, water)
try Pivot<Atom, Compound>.attach(oxygen, water)

var sugar = Compound(id: nil, name: "Sugar")
let sugar = Compound(id: nil, name: "Sugar")
try sugar.save()
try Pivot<Atom, Compound>.attach(hydrogen, sugar)
try Pivot<Atom, Compound>.attach(oxygen, sugar)
Expand Down Expand Up @@ -72,13 +72,13 @@ extension Tester {
Pivot<Atom, Compound>.database = database
Pivot<Pivot<Atom, Compound>, Student>.database = database

var hydrogen = Atom(id: nil, name: "Hydrogen", protons: 1, weight: 1.007)
let hydrogen = Atom(id: nil, name: "Hydrogen", protons: 1, weight: 1.007)
try hydrogen.save()

var water = Compound(id: nil, name: "Water")
let water = Compound(id: nil, name: "Water")
try water.save()

var vapor = Student(
let vapor = Student(
name: "Vapor",
age: 2,
ssn: "123",
Expand All @@ -87,10 +87,10 @@ extension Tester {
)
try vapor.save()

var hydrogenWater = try Pivot<Atom, Compound>(hydrogen, water)
let hydrogenWater = try Pivot<Atom, Compound>(hydrogen, water)
try hydrogenWater.save()

var hwVapor = try Pivot<Pivot<Atom, Compound>, Student>(hydrogenWater, vapor)
let hwVapor = try Pivot<Pivot<Atom, Compound>, Student>(hydrogenWater, vapor)
try hwVapor.save()

let pivot1 = Pivot<Student, Pivot<Atom, Compound>>.self
Expand All @@ -113,7 +113,7 @@ extension Tester {
throw Error.failed("Pivot relation failed.")
}

var helium = Atom(id: nil, name: "Helium", protons: 2, weight: 2.007)
let helium = Atom(id: nil, name: "Helium", protons: 2, weight: 2.007)
try helium.save()

let result3 = try pivot1.related(
Expand Down
2 changes: 1 addition & 1 deletion Sources/FluentTester/Tester+Schema.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ extension Tester {
try? Student.revert(database)
}

var bob = Student(
let bob = Student(
name: "Bob",
age: 22,
ssn: "382482",
Expand Down
Loading

0 comments on commit 99d959c

Please sign in to comment.