Skip to content

An sqlite variant of raywenderlich/vapor-til. Uses Vapor 3 & Swift 4

License

Notifications You must be signed in to change notification settings

VaporExamplesLab/vapor-til-sqlite

Repository files navigation

Vapor TIL SQLite

Overview | PostgreSQL → SQLite | Resources

Overview

The vapor-til-sqlite is based on raywenderlich/vapor-til. Some key aspects of vapor-til-sqlite are:

  • SQLite is used instead of PostgreSQL.
  • SQLite pivot id type is Int instead of UUID.
  • TBD: Tests pass on Linux 16.04?
  • Swift script to populate database.
  • Imperial package and related files for OAth login are commented out.
  • Demo user: admin:password

PostgreSQL → SQLite Conversion

Direct find & replace.

PostgreSQL SQLite
FluentPostgreSQL FluentSQLite
FluentPostgreSQLProvider FluentSQLiteProvider
PostgreSQLModel SQLiteModel
PostgreSQLUUIDModel SQLiteUUIDModel
PostgreSQLConnection SQLiteConnection
.psql .sqlite

Rewritten sections.

PostgreSQL SQLite
PostgreSQLDatabase SQLiteDatabase
PostgreSQLDatabaseConfig SQLiteStorage
PostgreSQLUUIDPivot SQLitePivot

configure.swift PostgreSQLDatabaseConfig, PostgreSQLDatabase

// Configure a database
var databases = DatabasesConfig()
let databaseConfig: PostgreSQLDatabaseConfig
if let url = Environment.get("DATABASE_URL") {
  databaseConfig = try PostgreSQLDatabaseConfig(url: url)
} else {
  let databaseName: String
  let databasePort: Int
  if (env == .testing) {
    databaseName = "vapor-test"
    if let testPort = Environment.get("DATABASE_PORT") {
      databasePort = Int(testPort) ?? 5433
    } else {
      databasePort = 5433
    }
  }
  else {
    databaseName = Environment.get("DATABASE_DB") ?? "vapor"
    databasePort = 5432
  }

  let hostname = Environment.get("DATABASE_HOSTNAME") ?? "localhost"
  let username = Environment.get("DATABASE_USER") ?? "vapor"
  let password = Environment.get("DATABASE_PASSWORD") ?? "password"
  databaseConfig = PostgreSQLDatabaseConfig(hostname: hostname, port: databasePort, username: username, database: databaseName, password: password)
}
let database = PostgreSQLDatabase(config: databaseConfig)
databases.add(database: database, as: .psql)
services.register(databases)

configure.swift SQLiteStorage, SQLiteDatabase

// Configure a database
var databases = DatabasesConfig()
var storage: SQLiteStorage!  
switch env {
case .development:
    // default: "/tmp/db.sqlite"
    let sqlitePath = "/Volumes/gMediaHD/VaporProjects/workspace/databases/vapor-til-sqlite.sqlite"
    storage = .file(path: sqlitePath)
case .testing:
    // "/tmp/_swift-tmp.sqlite"
    storage = .memory
case .production:
    storage = .memory
default:
    // includes custom
    storage = .memory
}
let sqliteDb = try SQLiteDatabase(storage:  storage)
databases.add(database: sqliteDb, as: .sqlite) 
services.register(databases)

Notes

vapor-til-sqlite has not yet been tested on Linux.

Resources

About

An sqlite variant of raywenderlich/vapor-til. Uses Vapor 3 & Swift 4

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages