Skip to content

DatabaseKeyMappingStrategy

Josh Wright edited this page Jan 14, 2021 · 4 revisions

DatabaseKeyMappingStrategy

Represents the mapping between your type's property names and their corresponding database column.

public enum DatabaseKeyMappingStrategy

For example, you might be using a PostgreSQL database which has a snake_case naming convention. Your users table might have fields id, email, first_name, and last_name.

Since Swift's naming convention is camelCase, your corresponding database model will probably look like this:

struct User: Model {
    var id: Int?
    let email: String
    let firstName: String // doesn't match database field of `first_name`
    let lastName: String // doesn't match database field of `last_name`
}

By overriding the keyMappingStrategy on User, you can customize the mapping between the property names and database columns. Note that in the example above you won't need to override, since keyMappingStrategy is, by default, convertToSnakeCase.

Enumeration Cases

useDefaultKeys

Use the literal name for all properties on an object as its corresponding database column.

case useDefaultKeys

convertToSnakeCase

Convert property names from camelCase to snake_case for the database columns.

case convertToSnakeCase

e.g. someGreatString -> some_great_string

custom

A custom mapping of property name to database column name.

case custom(: (String) -> String)

Methods

map(input:)

Given the strategy, map from an input string to an output string.

public func map(input: String) -> String

Parameters

  • input: The input string, representing the name of the swift type's property

Returns

The output string, representing the column of the database's table.

Alchemy
Types
Protocols
Global Typealiases
Global Variables
Global Functions
Fusion
Types
Protocols
Papyrus
Types
Protocols
Clone this wiki locally