-
-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from qutheory/fluent-8
node, joins, and relationships
- Loading branch information
Showing
52 changed files
with
1,407 additions
and
791 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
DEVELOPMENT-SNAPSHOT-2016-06-06-a | ||
DEVELOPMENT-SNAPSHOT-2016-07-25-a | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import Foundation | ||
|
||
/** | ||
Represents an entity that can be | ||
stored and retrieved from the `Database`. | ||
*/ | ||
public protocol Entity: Preparation, NodeConvertible { | ||
/** | ||
The collection or table name | ||
for this entity. | ||
*/ | ||
static var entity: String { get } | ||
|
||
/** | ||
The entity's primary identifier. | ||
This is the same value used for | ||
`find(:_)`. | ||
*/ | ||
var id: Node? { get set } | ||
} | ||
|
||
//MARK: Defaults | ||
|
||
extension Entity { | ||
/** | ||
The default entity is the | ||
lowercase model pluralized. | ||
*/ | ||
public static var entity: String { | ||
return name + "s" | ||
} | ||
|
||
public static var name: String { | ||
return String(self).lowercased() | ||
} | ||
} | ||
|
||
//MARK: CRUD | ||
|
||
extension Entity { | ||
/** | ||
Persists the entity into the | ||
data store and sets the `id` property. | ||
*/ | ||
public mutating func save() throws { | ||
try Self.query().save(&self) | ||
} | ||
|
||
/** | ||
Deletes the entity from the data | ||
store if the `id` property is set. | ||
*/ | ||
public func delete() throws { | ||
try Self.query().delete(self) | ||
} | ||
|
||
/** | ||
Returns all entities for this `Model`. | ||
*/ | ||
public static func all() throws -> [Self] { | ||
return try Self.query().all() | ||
} | ||
|
||
/** | ||
Finds the entity with the given `id`. | ||
*/ | ||
public static func find(_ id: NodeRepresentable) throws -> Self? { | ||
guard let idKey = database?.driver.idKey else { | ||
return nil | ||
} | ||
|
||
return try Self.query().filter(idKey, .equals, id).first() | ||
} | ||
|
||
/** | ||
Creates a `Query` instance for this `Model`. | ||
*/ | ||
public static func query() throws -> Query<Self> { | ||
guard let db = database else { | ||
throw EntityError.noDatabase | ||
} | ||
return Query(db) | ||
} | ||
} | ||
|
||
public enum EntityError: Error { | ||
case noDatabase | ||
} | ||
|
||
//MARK: Database | ||
|
||
extension Entity { | ||
/** | ||
Fetches or sets the `Database` for this | ||
`Model` from the static database map. | ||
*/ | ||
public static var database: Database? { | ||
get { | ||
if let db = Database.map[Self.name] { | ||
return db | ||
} else { | ||
return Database.default | ||
} | ||
} | ||
set { | ||
Database.map[Self.name] = newValue | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.