0
点赞
收藏
分享

微信扫一扫

【Vapor】04 Chapter 6:Configuring a Database


0x00 Chapter 6:Configuring a Database

1.​​Vapor​​​ has official, Swift-native ​​drivers​​ for:

  • SQLite
  • MySQL
  • PostgreSQL
  • MongoDB

2.数据库类型:
关系型(relational):​​​MySQL​​​ and ​​PostgreSQL​​​ 非关系型(non-relational):​​MongoDB​

​SQLite​​​ 是一个简单的、基于​​文件​​​的关系数据库系统。
它旨在嵌入到应用程序中,对于单进程应用程序(例如 iOS 应用程序)很有用

​MySQL​​​ become the ​​most popular​​​ database due to its ​​ease of use​​ and support from most cloud providers and website builders

​PostgreSQL​​​ 专为​​企业​​使用而设计。还原生支持几何图元,例如坐标

​MongoDB​​​ 旨在处理大量​​非结构化​​数据并具有极高的​​可扩展性​

3.使用 ​​SQLite​​​,需要的配置
​​​SQLite​doesn’t require you to run a ​​database server​​​ since SQLite uses a ​​local​​ file

​Package.swift​​ 文件的配置

a.添加依赖

dependencies: [
.package(url: "https://github.com/vapor/fluent-sqlite-driver.git", from: "4.0.0")
]

b.给名为 ​​App​​​ 的 ​​target​​ 添加依赖:

dependencies: [
.product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver")
]

完整的内容:

// swift-tools-version:5.5
import PackageDescription

let package = Package(
name: "TILApp",
platforms: [
.macOS(.v12)
],
dependencies: [
// 💧 A server-side Swift web framework.
.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0"),
// SQLite
.package(url: "https://github.com/vapor/fluent-sqlite-driver.git", from: "4.0.0")
],
targets: [
.target(
name: "App",
dependencies: [
.product(name: "FluentSQLiteDriver", package: "fluent-sqlite-driver")
.product(name: "Vapor", package: "vapor")
],
swiftSettings: [
.unsafeFlags(["-cross-module-optimization"], .when(configuration: .release))
]
),
.executableTarget(name: "Run", dependencies: [.target(name: "App")]),
.testTarget(name: "AppTests", dependencies: [
.target(name: "App"),
.product(name: "XCTVapor", package: "vapor"),
])
]
)

​configure.swift​​ 文件的配置

a.导入 ​​FluentSQLiteDriver​

import FluentSQLiteDriver

b.​​configure​​ 方法内添加

app.databases.use(.sqlite(.memory), as: .sqlite)

放在

app.migrations.add(CreateAcronym())

前面

如果要保存到文件

app.databases.use(.sqlite(.file("db.aqlite")), as: .sqlite)

4.使用 ​​MySQL​​​,需要的配置
run the ​​​MySQL server​​​ in a ​​Docker​​ container

在终端运行一个 ​​MySQL server​

docker run --name mysql \
-e MYSQL_USER=vapor_username \
-e MYSQL_PASSWORD=vapor_password \
-e MYSQL_DATABASE=vapor_database \
-e MYSQL_RANDOM_ROOT_PASSWORD=yes \
-p 3306:3306 \
-d mysql

检查数据库是否运行起来:
​​​docker ps​

​Package.swift​​ 文件的配置

a.添加依赖

dependencies: [
.package(url: "https://github.com/vapor/fluent-mysql-driver.git", from: "4.0.0")
]

b.给名为 ​​App​​​ 的 ​​target​​ 添加依赖:

dependencies: [
.product(name: "FluentMySQLDriver", package: "fluent-mysql-driver")
]

​configure.swift​​ 文件的配置

a.导入 ​​FluentMySQLDriver​

import FluentMySQLDriver

b.​​configure​​ 方法内添加

app.databases.use(.mysql(
hostname: Environment.get("DATABASE_HOST") ?? "localhost",
username: Environment.get("DATABASE_USERNAME") ?? "vapor_username",
password: Environment.get("DATABASE_PASSWORD") ?? "vapor_password",
database: Environment.get("DATABASE_NAME") ?? "vapor_database",
tlsConfiguration: .forClient(certificateVerification: .none)), as: .mysql)

放在

app.migrations.add(CreateAcronym())

前面

5.使用 ​​MongoDB​​​,需要的配置
run the ​​​MongoDB server​​​ in a ​​Docker​​​ container
在终端运行一个 ​​​MySQL server​

docker run --name mongo \
-e MONGO_INITDB_DATABASE=vapor \
-p 27017:27017 \
-d mongo

​Package.swift​​ 文件的配置

a.添加依赖

dependencies: [
.package(url: "https://github.com/vapor/fluent-mongo-driver.git", from: "1.0.0")
]

b.给名为 ​​App​​​ 的 ​​target​​ 添加依赖:

dependencies: [
.product(name: "FluentMongoDriver", package: "fluent-mongo-driver")
]

​configure.swift​​ 文件的配置

a.导入 ​​FluentMongoDriver​

import FluentMongoDriver

b.​​configure​​ 方法内添加

try app.databases.use(.mongo(connectionString: "mongodb://localhost:27017/vapor"), as: .mongo)

放在

app.migrations.add(CreateAcronym())

前面

6.使用 ​​PostgreSQL​​​,需要的配置
run the ​​​PostgreSQL server​​​ in a ​​Docker​​ container

docker run --name postgres \
-e POSTGRES_DB=vapor_database \
-e POSTGRES_USER=vapor_username \
-e POSTGRES_PASSWORD=vapor_password \
-p 5432:5432 \
-d postgres

​Package.swift​​ 文件的配置

a.添加依赖

dependencies: [
.package(url: "https://github.com/vapor/fluent-postgres-driver.git", from: "2.0.0")
]

b.给名为 ​​App​​​ 的 ​​target​​ 添加依赖:

dependencies: [
.product(name: "FluentPostgresDriver", package: "fluent-postgres-driver")
]

​configure.swift​​ 文件的配置

a.导入 ​​FluentPostgresDriver​

import FluentPostgresDriver

b.​​configure​​ 方法内添加

app.databases.use(.postgres(
hostname: Environment.get("DATABASE_HOST") ?? "localhost",
username: Environment.get("DATABASE_USERNAME") ?? "vapor_username",
password: Environment.get("DATABASE_PASSWORD") ?? "vapor_password",
database: Environment.get("DATABASE_NAME") ?? "vapor_database"
), as: .psql)

放在

app.migrations.add(CreateAcronym())

前面

7.依赖对比

【Vapor】04 Chapter 6:Configuring a Database_mysql

0x01 我的作品



举报

相关推荐

0 条评论