Skip to content

Commit 11463dc

Browse files
committed
Added DB example
1 parent 300b2bb commit 11463dc

4 files changed

Lines changed: 97 additions & 4 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@
22

33
An empty shell to serve as a starting point for using Vapor.
44

5+
* To serve files from the /Public folder, search for "UNCOMMENT-PUBLIC" and uncomment
6+
* For a database example, search for "UNCOMMENT-DATABASE" and uncomment.
7+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
VaporShell provides a minimal framework for starting Vapor projects.
3+
Copyright (C) 2021, 2022 CoderMerlin.com
4+
This program is free software: you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation, either version 3 of the License, or
7+
(at your option) any later version.
8+
This program is distributed in the hope that it will be useful,
9+
but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
GNU General Public License for more details.
12+
You should have received a copy of the GNU General Public License
13+
along with this program. If not, see <https://www.gnu.org/licenses/>.
14+
*/
15+
16+
import Vapor
17+
import Fluent
18+
import FluentMySQLDriver
19+
20+
// UNCOMMENT-DATABASE to configure database example
21+
// // Content conformance will ensure that the object can be encoded and decoded from HTTP messages.
22+
// final class Employee: Model, Content {
23+
// // Name of the table or collection.
24+
// static let schema = "employees"
25+
26+
// // Unique identifier for this Employee.
27+
// @ID(custom: "emp_no", generatedBy: .database)
28+
// var id: Int?
29+
30+
// // Additional fields for this Employee.
31+
// @Field(key: "first_name")
32+
// var firstName: String
33+
34+
// @Field(key: "last_name")
35+
// var lastName: String
36+
37+
// @Field(key: "gender")
38+
// var gender: String
39+
40+
// @Field(key: "birth_date")
41+
// var birthDate: Date
42+
43+
// @Field(key: "hire_date")
44+
// var hireDate: Date
45+
46+
// // Creates a new, empty Employee.
47+
// init() { }
48+
// }
49+

Sources/VaporShell/App/configure.swift

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
VaporShell provides a minimal framework for starting Igis projects.
3-
Copyright (C) 2021 CoderMerlin.com
2+
VaporShell provides a minimal framework for starting Vapor projects.
3+
Copyright (C) 2021, 2022 CoderMerlin.com
44
This program is free software: you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
66
the Free Software Foundation, either version 3 of the License, or
@@ -15,11 +15,27 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
1515

1616
import Vapor
1717

18+
// UNCOMMENT to use database
19+
import Fluent
20+
import FluentMySQLDriver
21+
1822
// configures your application
1923
public func configure(_ app: Application) throws {
20-
// uncomment to serve files from /Public folder
24+
// UNCOMMENT-PUBLIC to serve files from /Public folder
2125
// app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
2226

27+
// UNCOMMENT-DATABASE to configure database example
28+
// var tls = TLSConfiguration.makeClientConfiguration()
29+
// tls.certificateVerification = .none
30+
// app.databases.use(.mysql(
31+
// hostname: "db",
32+
// port: MySQLConfiguration.ianaPortNumber,
33+
// username: "employees_user",
34+
// password: "tAn?*4YKX3,xk?PH",
35+
// database: "employees",
36+
// tlsConfiguration: tls
37+
// ), as: .mysql)
38+
2339
// Set local port
2440
guard let portString = Environment.get("VAPOR_LOCAL_PORT"),
2541
let port = Int(portString) else {

Sources/VaporShell/App/routes.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
VaporShell provides a minimal framework for starting Igis projects.
3-
Copyright (C) 2021 CoderMerlin.com
3+
Copyright (C) 2021, 2022 CoderMerlin.com
44
This program is free software: you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
66
the Free Software Foundation, either version 3 of the License, or
@@ -14,9 +14,34 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
1414
*/
1515

1616
import Vapor
17+
import Fluent
18+
import FluentMySQLDriver
1719

1820
func routes(_ app: Application) throws {
21+
1922
app.get { req in
2023
return "It works!"
2124
}
25+
26+
// UNCOMMENT-DATABASE to configure database example
27+
// // Find an employee with the specified ID
28+
// app.get ("employees", ":id") { req -> Employee in
29+
// guard let id = req.parameters.get("id", as: Int.self) else {
30+
// throw Abort(.badRequest)
31+
// }
32+
33+
// guard let employee = try await Employee.query(on: req.db)
34+
// .filter(\.$id == id)
35+
// .first() else {
36+
// throw Abort(.notFound)
37+
// }
38+
// return employee
39+
// }
40+
41+
// // List all employees using paging
42+
// app.get("employees") { req -> Page<Employee> in
43+
// let employees = try await Employee.query(on: req.db)
44+
// .paginate(for: req)
45+
// return employees
46+
// }
2247
}

0 commit comments

Comments
 (0)