Skip to content

Commit

Permalink
Merge pull request #56 from qutheory/fluent-8
Browse files Browse the repository at this point in the history
node, joins, and relationships
  • Loading branch information
tanner0101 authored Jul 27, 2016
2 parents d40f3af + dae6fe4 commit e59d11d
Show file tree
Hide file tree
Showing 52 changed files with 1,407 additions and 791 deletions.
2 changes: 1 addition & 1 deletion .swift-version
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

6 changes: 2 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ os:
language: generic
sudo: required
dist: trusty
osx_image: xcode7.3
osx_image: xcode8
install:
- eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/02090c7ede5a637b76e6df1710e83cd0bbe7dcdf/swiftenv-install.sh)"
- eval "$(curl -sL swift.qutheory.io/travis)"
script:
# Build Fluent
- swift build
- swift build --configuration release
# Test Fluent
- swift test
11 changes: 3 additions & 8 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ import PackageDescription
let package = Package(
name: "Fluent",
dependencies: [
//Standards package. Contains protocols for cross-project compatability.
.Package(url: "https://github.com/open-swift/C7.git", majorVersion: 0, minor: 9),

// Syntax for easily accessing values from generic data.
.Package(url: "https://github.com/qutheory/polymorphic.git", majorVersion: 0, minor: 2),

// Syntax for easily indexing arrays and dictionaries.
.Package(url: "https://github.com/qutheory/path-indexable.git", majorVersion: 0, minor: 2)
// Data structure for converting between multiple representations
.Package(url: "https://github.com/qutheory/node.git", majorVersion: 0, minor: 2)
]
)

12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@ Clone the [Example](https://github.com/qutheory/fluent-example) project to start

You must have Swift 3 or later installed. You can learn more about Swift 3 at [Swift.org](http://swift.org)

## 🌏 Environment

|Fluent|Xcode|Swift|
|:-:|:-:|:-:|
|0.8.x|8.0 Beta **3**|DEVELOPMENT-SNAPSHOT-2016-07-25-a|
|0.7.x|7.3.x|DEVELOPMENT-SNAPSHOT-2016-06-06-a|
|0.6.x|7.3.x|DEVELOPMENT-SNAPSHOT-2016-06-06-a|
|0.5.x|7.3.x|DEVELOPMENT-SNAPSHOT-2016-06-06-a|
|0.4.x|7.3.x|3.0-preview-1-SNAPSHOT-2016-05-31-a|
|0.3.x|7.3.x|DEVELOPMENT-SNAPSHOT-2016-05-03-a|
|0.2.x|7.3.x|DEVELOPMENT-SNAPSHOT-2016-03-01-a|

## 📖 Documentation

Visit the [Documentation](http://docs.qutheory.io) for extensive information on getting setup, using, and deploying Vapor.
Expand Down
4 changes: 2 additions & 2 deletions Sources/Fluent/Database/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Database {
Creates a `Database` with the supplied
`Driver`. This cannot be changed later.
*/
public init(driver: Driver) {
public init(_ driver: Driver) {
self.driver = driver
}

Expand All @@ -30,5 +30,5 @@ public class Database {
/**
The default database for all `Model` types.
*/
public static var `default`: Database = Database(driver: PrintDriver())
public static var `default`: Database?
}
20 changes: 19 additions & 1 deletion Sources/Fluent/Database/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,29 @@ public protocol Driver {
created, or updated by the action.
*/
@discardableResult
func query<T: Model>(_ query: Query<T>) throws -> [[String: Value]]
func query<T: Entity>(_ query: Query<T>) throws -> Node

/**
Creates the `Schema` indicated
by the `Builder`.
*/
func schema(_ schema: Schema) throws

/**
Drivers that support raw querying
accept string queries and parameterized values.
This allows Fluent extensions to be written that
can support custom querying behavior.
*/
@discardableResult
func raw(_ raw: String, _ values: [Node]) throws -> Node
}

extension Driver {
@discardableResult
public func raw(_ raw: String, _ values: [NodeRepresentable] = []) throws -> Node {
let nodes = try values.map { try $0.makeNode() }
return try self.raw(raw, nodes)
}
}
28 changes: 0 additions & 28 deletions Sources/Fluent/Database/PrintDriver.swift

This file was deleted.

109 changes: 109 additions & 0 deletions Sources/Fluent/Entity/Entity.swift
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
}
}
}
154 changes: 0 additions & 154 deletions Sources/Fluent/Model/Model.swift

This file was deleted.

Loading

0 comments on commit e59d11d

Please sign in to comment.