Vapor 2.0 - Fluent入門指南(Getting Started with Fluent)

前往 Vapor 2.0 - 文檔目錄

Fluent提供了一種簡單、簡單、安全的API,用于處理持久數(shù)據(jù)。每個數(shù)據(jù)庫表/集合都由一個可用于與數(shù)據(jù)交互的Model表示。Fluent支持常見的操作,如創(chuàng)建、讀取、更新和刪除模型。它還支持更高級的操作,比如連接、關(guān)聯(lián)和軟刪除。

筆記
不要忘記將import FluentProvider(或您的其他數(shù)據(jù)庫提供程序)添加到您的Swift文件的頂部。

默認(rèn)情況下,F(xiàn)luent自帶SQLite。您可以通過SQLite快速地使用它提供的內(nèi)存數(shù)據(jù)庫來搭建應(yīng)用程序。默認(rèn)情況下,這是默認(rèn)啟用的。要了解更多關(guān)于配置數(shù)據(jù)庫的信息,請查看可用的驅(qū)動程序(在本篇最后)。

創(chuàng)建一個模型(Creating a Model)

模型是數(shù)據(jù)庫中Swift數(shù)據(jù)的展示。因此,它們對于大多數(shù)Fluent的api來說都是至關(guān)重要的。

讓我們來看看一個簡單的模型是什么樣子的。

final class Pet: Model {
    var name: String
    var age: Int
    let storage = Storage()

    init(row: Row) throws {
        name = try row.get("name")
        age = try row.get("age")
    }

    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }

    func makeRow() throws -> Row {
        var row = Row()
        try row.set("name", name)
        try row.set("age", age)
        return row
    }
}

在這里,我們創(chuàng)建了一個簡單的類寵物(Pet),名字和年齡。我們將添加一個簡單的init方法來創(chuàng)建新的寵物對象。

儲存(Storage)

storage屬性允許Fluent在模型中存儲額外的信息——比如模型的數(shù)據(jù)庫id。

行(Row)

Row結(jié)構(gòu)體表示一個數(shù)據(jù)庫行。您的模型應(yīng)該能夠?qū)?shù)據(jù)庫行進(jìn)行解析和序列化。

解析(Parse)

下面是從數(shù)據(jù)庫中解析Pet的代碼。

final class Pet: Model {
    ...

    init(row: Row) throws {
        name = try row.get("name")
        age = try row.get("age")
    }
}
序列化(Serialize)

下面是將Pet序列化到數(shù)據(jù)庫的代碼。

final class Pet: Model {
    ...

    func makeRow() throws -> Row {
        var row = Row()
        try row.set("name", name)
        try row.set("age", age)
        return row
    }
}

準(zhǔn)備數(shù)據(jù)庫(Preparing the Database)

為了使用您的模型,您可能需要使用適當(dāng)?shù)哪J絹頊?zhǔn)備數(shù)據(jù)庫。

準(zhǔn)備(Preparation)

你可以通過將你的模型繼承Preparation來做到這一點。

extension Pet: Preparation {
    static func prepare(_ database: Database) throws {
        try database.create(self) { pets in
            pets.id()
            pets.string("name")
            pets.int("age")
        }
    } 

    static func revert(_ database: Database) throws {
        try database.delete(self)
    }
}

在這里,我們創(chuàng)建了一個簡單的表,看起來如下:

id name age
<database id type> string int

添加到Droplet(Add to Droplet)

現(xiàn)在,您可以將模型添加到配置的準(zhǔn)備中,以便在應(yīng)用程序啟動時準(zhǔn)備好數(shù)據(jù)庫。

import Vapor
import FluentProvider

let config = try Config()
config.preparations.append(Pet.self)
let drop = try Droplet(config)

...

使用模型(Using Models)

現(xiàn)在我們已經(jīng)創(chuàng)建了模型并準(zhǔn)備好了數(shù)據(jù)庫,我們可以使用它來保存和從數(shù)據(jù)庫中獲取數(shù)據(jù)。

保存(Save)

要保存一個模型,請調(diào)用.save()。將自動創(chuàng)建模型的新標(biāo)識符。

let dog = Pet(name: "Spud", age: 5)
try dog.save()
print(dog.id) // the newly saved pet's id

查找(Find)

您可以使用它的ID從數(shù)據(jù)庫中獲取一個模型。

guard let dog = try Pet.find(42) else {
    throw Abort.notFound
}

print(dog.name) // the name of the dog with id 42

過濾器(Filter)

您還可以使用過濾器搜索模型。

let dogs = try Pet.makeQuery().filter("age", .greaterThan, 2).all()
print(dogs) // all dogs older than 2

驅(qū)動(Drivers)

查看數(shù)據(jù)庫(database)部分,了解更多關(guān)于不同數(shù)據(jù)庫驅(qū)動程序的信息,您可以使用Fluent的數(shù)據(jù)庫驅(qū)動程序。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容