-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.swift
60 lines (49 loc) · 1.54 KB
/
main.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import Fluent
import FluentSQLite
do {
let driver = try SQLiteDriver()
Database.default = Database(driver)
let results = try driver.raw("SELECT sqlite_version();")
if
let row = results.array?.first?.object,
let version = row["sqlite_version()"]?.string
{
print("SQLite Version: \(version)")
}
} catch {
print("Could not initialize driver: \(error)")
}
// Database preparation code from <https://github.com/vapor/fluent-provider/blob/master/Sources/Prepare.swift#L49-L62>
let preparations: [Preparation.Type] = [User.self]
if let database = Database.default {
do {
try preparations.forEach { preparation in
let name = "\(preparation.self)"
let hasPrepared = try database.hasPrepared(preparation)
// only prepare the unprepared
guard !hasPrepared else { return }
print("Preparing \(name)")
try database.prepare(preparation)
print("Prepared \(name)")
}
} catch {
print("Could not modify user: \(error)")
}
print("Database prepared")
}
do {
if var user = try User.find(1) {
print(user.name)
//increments the user's age with every run
user.age = user.age + 1
try user.save()
print("\(user.name) is \(user.age) iterations old")
} else {
print("Could not find any users")
var newUser = User(name: "vapor", age: 1)
try newUser.save()
print("Added a user named \(newUser.name)")
}
} catch {
print("Could not modify user: \(error)")
}