# \[VAPOR\] FluentMySQL Querying and Saving to DB

**URL:** <https://forums.kodeco.com/t/vapor-fluentmysql-querying-and-saving-to-db/89576>\
**Category:** Swift\
**Created:** [November 28, 2019, 4:08am UTC](https://forums.kodeco.com/t/vapor-fluentmysql-querying-and-saving-to-db/89576 "2019-11-28T04:08:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![lganti](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lganti/32/103565_2.png) [@lganti](https://forums.kodeco.com/u/lganti)\
**Post date:** [November 28, 2019, 4:08am UTC](https://forums.kodeco.com/t/vapor-fluentmysql-querying-and-saving-to-db/89576/1 "2019-11-28T04:08:51Z")

</div>

This is a question regarding HOW/WHERE

**This is what I am trying to accomplish:**  
Interact with Google Sheets API, fetch the columns and parse as per my needs and provide endpoints so that both Android and iOS clients can use it (all in an attempt to standardize localization via Server Side Swift)

**I currently have three models:**  
`Sheet`, `Language`, `SheetData` ([https://gist.github.com/lahariganti/2cc0afe477017fe0293eac8676e7e78c](https://gist.github.com/lahariganti/2cc0afe477017fe0293eac8676e7e78c))

**Able to successfully fetch the sheet data via:**

```
func getSheet(_ req: Request) throws -> Future<Sheet> {
    guard let endpoint = Endpoint.sheetValues(), let url = endpoint.url else {
        // TODO: throw the appropriate error using Middleware maybe
        throw Abort(.notFound)
    }
    
    let headers = HTTPHeaders(dictionaryLiteral: ("Content-Type", "application/json"))
    let sheet = try req.client().get(url, headers: headers).map(to: Sheet.self) { response in
        return try response.content.syncDecode(Sheet.self)
    }
    
    return sheet.save(on: req)
}

```

**Will be querying `Language` of the sheet via something like this:**

```
func fetchLocales(_ req: Request) throws -> Future<[Language]> {
    return Language.query(on: req).all()
}

```

Where do I save Language objects? Have another function execute pre `fetchLocales` that would retrieve `Sheet` objects and save `Language` objects?

---

<div class="post-metadata">

**Author:** ![0xtim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/0xtim/32/111111_2.png) [@0xtim](https://forums.kodeco.com/u/0xtim)\
**Post date:** [November 28, 2019, 11:04am UTC](https://forums.kodeco.com/t/vapor-fluentmysql-querying-and-saving-to-db/89576/2 "2019-11-28T11:04:42Z")

</div>

At what point do you get all the languages? Because you should probably save them then!

---

<div class="post-metadata">

**Author:** ![lganti](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lganti/32/103565_2.png) [@lganti](https://forums.kodeco.com/u/lganti)\
**Post date:** [December 3, 2019, 4:00am UTC](https://forums.kodeco.com/t/vapor-fluentmysql-querying-and-saving-to-db/89576/3 "2019-12-03T04:00:06Z")

</div>

The`fetchLocales` function is essentially just going to parse the data obtained from google sheet (which is being saved to db) and provide it as an endpoint as per desired format and I would eventually want to filter out per language for something like `/locale/en`

Wanted `SheetController` to only deal with the sheet data and now wondering if I should just have a mechanism (am not saving `Language` objects to db anywhere as it is pretty much the data obtained from `Sheet`) which does `query.all()` instead of `Future<[Language]>`

```
func fetchLocales(_ req: Request) throws -> Future<[Language]> {
    return try sheetController.getSheet(req).map(to: [Language].self) { sheet in
        let decodedSheetValues = sheet.values
        var data: [String: [String: Any]] = [:]
        var allLanguages: [Language] = []
        
        for language in self.languages {
           var sheetData: [String: Any] = [:]
           sheetData["isoCode"] = language
           sheetData["data"] = []

           data[language] = sheetData
        }
        
        for i in 1..<decodedSheetValues.count {
            let languageKey = decodedSheetValues[i][0]
            var allSheetData = [SheetData]()
            
            for j in 1..<decodedSheetValues[i].count {
                if data.keys.contains(languageKey) {
                    let sheetData = SheetData(key: decodedSheetValues[0][j],
                    value: decodedSheetValues[i][j],
                    description: decodedSheetValues[decodedSheetValues.count - 2][j],
                    section: decodedSheetValues[decodedSheetValues.count - 1][j])
                    
                    allSheetData.append(sheetData)
                }
            }

            data[languageKey]?["data"] = allSheetData
        }
        
        for (_, value) in data {
            var languageObject = Language()
            if let isoCode = value["isoCode"] as? String {
                languageObject.isoCode = isoCode
            }
            
            if let sheetData = value["data"] as? [SheetData] {
                languageObject.data = sheetData
            }
            
            allLanguages.append(languageObject)
        }
        
        
        return allLanguages
    }
}

```

---

<div class="post-metadata">

**Author:** ![0xtim](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/0xtim/32/111111_2.png) [@0xtim](https://forums.kodeco.com/u/0xtim)\
**Post date:** [December 4, 2019, 1:39pm UTC](https://forums.kodeco.com/t/vapor-fluentmysql-querying-and-saving-to-db/89576/4 "2019-12-04T13:39:59Z")

</div>

You could put this in `boot()` if it can happen at app startup?

---

<div class="post-metadata">

**Author:** ![lganti](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lganti/32/103565_2.png) [@lganti](https://forums.kodeco.com/u/lganti)\
**Post date:** [December 9, 2019, 9:04am UTC](https://forums.kodeco.com/t/vapor-fluentmysql-querying-and-saving-to-db/89576/5 "2019-12-09T09:04:19Z")

</div>

Figure it out, thanks for the responses Tim!

**For anyone who might be interested:**

My use case is a bit weird as all this API does is fetch data from another API and provide the same data in a different format; it needs to check if data in the goole sheet has changed, doesn’t quite make sense to save non `Sheet` data into the database → but this might provide with other API versioning problems to solve down the line, I guess - idk, may be

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [January 13, 2020, 6:06am UTC](https://forums.kodeco.com/t/vapor-fluentmysql-querying-and-saving-to-db/89576/6 "2020-01-13T06:06:30Z")

</div>

@lganti Thank you for sharing this - much appreciated!

---

<div class="post-metadata">

**Author:** ![system](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@system](https://forums.kodeco.com/u/system)\
**Post date:** [May 12, 2020, 8:12pm UTC](https://forums.kodeco.com/t/vapor-fluentmysql-querying-and-saving-to-db/89576/7 "2020-05-12T20:12:38Z")

</div>

This topic was automatically closed after 166 days. New replies are no longer allowed.
