Skip to content

Commit e59d11d

Browse files
authored
Merge pull request #56 from qutheory/fluent-8
node, joins, and relationships
2 parents d40f3af + dae6fe4 commit e59d11d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1407
-791
lines changed

.swift-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
DEVELOPMENT-SNAPSHOT-2016-06-06-a
1+
DEVELOPMENT-SNAPSHOT-2016-07-25-a
22

.travis.yml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ os:
44
language: generic
55
sudo: required
66
dist: trusty
7-
osx_image: xcode7.3
7+
osx_image: xcode8
88
install:
9-
- eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/02090c7ede5a637b76e6df1710e83cd0bbe7dcdf/swiftenv-install.sh)"
9+
- eval "$(curl -sL swift.qutheory.io/travis)"
1010
script:
11-
# Build Fluent
1211
- swift build
1312
- swift build --configuration release
14-
# Test Fluent
1513
- swift test

Package.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@ import PackageDescription
33
let package = Package(
44
name: "Fluent",
55
dependencies: [
6-
//Standards package. Contains protocols for cross-project compatability.
7-
.Package(url: "https://github.com/open-swift/C7.git", majorVersion: 0, minor: 9),
8-
9-
// Syntax for easily accessing values from generic data.
10-
.Package(url: "https://github.com/qutheory/polymorphic.git", majorVersion: 0, minor: 2),
11-
12-
// Syntax for easily indexing arrays and dictionaries.
13-
.Package(url: "https://github.com/qutheory/path-indexable.git", majorVersion: 0, minor: 2)
6+
// Data structure for converting between multiple representations
7+
.Package(url: "https://github.com/qutheory/node.git", majorVersion: 0, minor: 2)
148
]
159
)
10+

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ Clone the [Example](https://github.com/qutheory/fluent-example) project to start
2121

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

24+
## 🌏 Environment
25+
26+
|Fluent|Xcode|Swift|
27+
|:-:|:-:|:-:|
28+
|0.8.x|8.0 Beta **3**|DEVELOPMENT-SNAPSHOT-2016-07-25-a|
29+
|0.7.x|7.3.x|DEVELOPMENT-SNAPSHOT-2016-06-06-a|
30+
|0.6.x|7.3.x|DEVELOPMENT-SNAPSHOT-2016-06-06-a|
31+
|0.5.x|7.3.x|DEVELOPMENT-SNAPSHOT-2016-06-06-a|
32+
|0.4.x|7.3.x|3.0-preview-1-SNAPSHOT-2016-05-31-a|
33+
|0.3.x|7.3.x|DEVELOPMENT-SNAPSHOT-2016-05-03-a|
34+
|0.2.x|7.3.x|DEVELOPMENT-SNAPSHOT-2016-03-01-a|
35+
2436
## 📖 Documentation
2537

2638
Visit the [Documentation](http://docs.qutheory.io) for extensive information on getting setup, using, and deploying Vapor.

Sources/Fluent/Database/Database.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Database {
1515
Creates a `Database` with the supplied
1616
`Driver`. This cannot be changed later.
1717
*/
18-
public init(driver: Driver) {
18+
public init(_ driver: Driver) {
1919
self.driver = driver
2020
}
2121

@@ -30,5 +30,5 @@ public class Database {
3030
/**
3131
The default database for all `Model` types.
3232
*/
33-
public static var `default`: Database = Database(driver: PrintDriver())
33+
public static var `default`: Database?
3434
}

Sources/Fluent/Database/Driver.swift

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,29 @@ public protocol Driver {
2121
created, or updated by the action.
2222
*/
2323
@discardableResult
24-
func query<T: Model>(_ query: Query<T>) throws -> [[String: Value]]
24+
func query<T: Entity>(_ query: Query<T>) throws -> Node
2525

2626
/**
2727
Creates the `Schema` indicated
2828
by the `Builder`.
2929
*/
3030
func schema(_ schema: Schema) throws
31+
32+
/**
33+
Drivers that support raw querying
34+
accept string queries and parameterized values.
35+
36+
This allows Fluent extensions to be written that
37+
can support custom querying behavior.
38+
*/
39+
@discardableResult
40+
func raw(_ raw: String, _ values: [Node]) throws -> Node
41+
}
42+
43+
extension Driver {
44+
@discardableResult
45+
public func raw(_ raw: String, _ values: [NodeRepresentable] = []) throws -> Node {
46+
let nodes = try values.map { try $0.makeNode() }
47+
return try self.raw(raw, nodes)
48+
}
3149
}

Sources/Fluent/Database/PrintDriver.swift

Lines changed: 0 additions & 28 deletions
This file was deleted.

Sources/Fluent/Entity/Entity.swift

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import Foundation
2+
3+
/**
4+
Represents an entity that can be
5+
stored and retrieved from the `Database`.
6+
*/
7+
public protocol Entity: Preparation, NodeConvertible {
8+
/**
9+
The collection or table name
10+
for this entity.
11+
*/
12+
static var entity: String { get }
13+
14+
/**
15+
The entity's primary identifier.
16+
This is the same value used for
17+
`find(:_)`.
18+
*/
19+
var id: Node? { get set }
20+
}
21+
22+
//MARK: Defaults
23+
24+
extension Entity {
25+
/**
26+
The default entity is the
27+
lowercase model pluralized.
28+
*/
29+
public static var entity: String {
30+
return name + "s"
31+
}
32+
33+
public static var name: String {
34+
return String(self).lowercased()
35+
}
36+
}
37+
38+
//MARK: CRUD
39+
40+
extension Entity {
41+
/**
42+
Persists the entity into the
43+
data store and sets the `id` property.
44+
*/
45+
public mutating func save() throws {
46+
try Self.query().save(&self)
47+
}
48+
49+
/**
50+
Deletes the entity from the data
51+
store if the `id` property is set.
52+
*/
53+
public func delete() throws {
54+
try Self.query().delete(self)
55+
}
56+
57+
/**
58+
Returns all entities for this `Model`.
59+
*/
60+
public static func all() throws -> [Self] {
61+
return try Self.query().all()
62+
}
63+
64+
/**
65+
Finds the entity with the given `id`.
66+
*/
67+
public static func find(_ id: NodeRepresentable) throws -> Self? {
68+
guard let idKey = database?.driver.idKey else {
69+
return nil
70+
}
71+
72+
return try Self.query().filter(idKey, .equals, id).first()
73+
}
74+
75+
/**
76+
Creates a `Query` instance for this `Model`.
77+
*/
78+
public static func query() throws -> Query<Self> {
79+
guard let db = database else {
80+
throw EntityError.noDatabase
81+
}
82+
return Query(db)
83+
}
84+
}
85+
86+
public enum EntityError: Error {
87+
case noDatabase
88+
}
89+
90+
//MARK: Database
91+
92+
extension Entity {
93+
/**
94+
Fetches or sets the `Database` for this
95+
`Model` from the static database map.
96+
*/
97+
public static var database: Database? {
98+
get {
99+
if let db = Database.map[Self.name] {
100+
return db
101+
} else {
102+
return Database.default
103+
}
104+
}
105+
set {
106+
Database.map[Self.name] = newValue
107+
}
108+
}
109+
}

Sources/Fluent/Model/Model.swift

Lines changed: 0 additions & 154 deletions
This file was deleted.

0 commit comments

Comments
 (0)