Week 7 & 8

News

RC2 was tagged for all packages last week, and with it, NIO now handles all non-blocking async for Vapor! 🎉🎉🎉 If you want to know more about the impact of NIO, check out Tanner on Swift by Sundell and Week 5 of Vapor Nation. Many API docs were also released this week. API docs are less like the tutorial style you’d find at docs.vapor.codes and more like traditional docs you’d find for other libraries and frameworks.

New Features

Adds Ability To Execute Shell Commands

adds a new Process.execute method for executing shell commands.

let result = try Process.execute("echo", "hi")
print(result) /// "hi"

vapor/core#100

Simplify The Services System

Simplifies the service system and internal caching.

vapor/service#18

Use foundation base64 + async deprecations

vapor/core#103

MySQL Query Pipelining

MySQL now supports multiple queries being enqueued when one is still running

vapor/mysql#145
vapor/fluent#411
vapor/fluent#413

New SQL Query Operators

vapor/fluent#423
vapor/fluent#424

Adds Command To Revert Migrations

vapor/fluent#421

Use Environment For Command Line Arguments

vapor/vapor#1570

Services and Database Connections Are Now Extendable

Extendable is a protocol which allows you to store data on instances of the protocol. DatabaseConnection and Service are now Extendable. This will allow higher-level libraries to store things on a connection without resorting to global caches which may leak memory.

vapor/service#19 vapor/database-kit#19

Decoding Errors Result In 400 Response

vapor/vapor#1563

NIO

HTTP Pipelining

HTTP pipelining is a relatively new technique that’s only supported by a couple browsers at the present, but it has huge potential for performance gains. You can find the benchmarks yielding around 3x performance gains in a comment a ways down the thread.

apple/swift-nio#62

Discussion On Performance Improvements For HTTP

Vapor lost some performance when it switched to NIO, but optimizing NIO’s HTTP parser should improve that by a lot.

apple/swift-nio#141

Discussion On flatMap vs then For Future Interaction

flatMap comes from the functional programming world and is more technically correct, but then is more consistent with other promise/future implementations. What do you think?

apple/swift-nio#213

Community Contributions

Tests That A Date’s Milliseconds Are Persisted

@MrMage

vapor/fluent-postgresql#27

Prevent Foreign Keys From Being Created Twice

@MrMage

vapor/database-kit#17

Adds Microsecond Support To MySQL and PostgreSQL

@MrMage

vapor/mysql#141
vapor/postgresql#29

Release The First Awaiter When A Promise Gets Fulfilled

@MrMage

vapor/async#79

Console Can Now Parse Short Flags Properly

@siemensikkema

vapor/console#59

Fix DateMiddleWare cached timestamp return logic

@grundoon noted that timestamps were being cached and never recalculated.

vapor/vapor#1554

Default Postgres Port to 5432 When Not Supplied

@0xTim simplified PSQL config by defaulting to the port most setups of PSQL will be using.

vapor/postgresql#34

Fix a memory leak in PostgreSQLConnection

@MrMage -

PostgreSQLTableNameCache holds a PostgreSQLConnection future. Once that future is fulfilled, it strongly holds a reference to PostgreSQLConnection. That connection in turn holds the table name cache, so we have a reference cycle. This causes the table name cache and its connection to never get released. This is relevant when testing, if the tests are set up such that each test creates a new Database.

vapor/postgresql#31

Avoid double encoding of query params when using FoundationClient [Vapor 2]

@steffendsommer

https://github.com/vapor/engine/pull/244

Remove an unused argument in PostgreSQLDatabase.init

@MrMage cleaned up PostgreSQLDatabase.init

vapor/postgresql#38

Close a PostgreSQLConnection’s channel on deinit

@MrMage

vapor/postgresql#36

Fixing join mapping

@rafiki270

vapor/fluent#410

Added connection string parsing for PostgreSQLDatabaseConfig

@pedantix enabled PostgreSQLDatabaseConfig to be initialized with a PSQL URL. This is typically the format you’ll get your credentials in in you’re using a service like Heroku

vapor/postgresql#25

Correctly fetch LAST_INSERT_ID() on model creation

@jseibert allows Fluent to handle MySQL’s LAST_INSERT_ID

vapor/fluent-mysql#85

Make BenchmarkTimestampable care more about fractional-second parts

@MrMage made benchmarking more precise

if tanner.createdAt?.isWithin(seconds: 0.1, of: Date()) != true {
  self.fail("timestamps should be current")
}

vapor/fluent#406

Use microsecond precision on new time and datetime columns

@MrMage made MySQL’s time types more precise

vapor/fluent-mysql#84

Made FluentError init method public

@calebkleveter

guard let id = model.id else {
    throw FluentError(identifier: "noID", reason: "A model must have an ID to be connected to a pivot"
}

vapor/fluent#408

Created QueryBuilder.set method

@calebkleveter’s PR adds a .set(_:to:) method to the QueryBuilder type, allowing you to run SET queries on a model.

Player.query(on: request).filter(\.points == 10_000).set(\.rank, to: 10)

vapor/fluent#394

Adapt To Updates In Crypto

@pedantix updated the JWT package to work with the most recent updates to Crypto

vapor/jwt#86

Adding Group By

@rafiki270

vapor/fluent#403

Made QueryBuilder.connection Property Public

@calebkleveter

vapor/fluent#414

Export Async To Avoid An Import When Conforming To Fluent Protocols

@rafiki270

vapor/fluent#417

Changes Default Connector In Name Of Pivot Table From ‘+’ To ‘_’

@abbasmousavi addressed an issue where the + symbol was giving a few databases a hard time.

vapor/fluent#422

Check to see if path is a directory as well

@rafiki270 fixed a bug where requests were being intercepted by FileMiddleware.

vapor/vapor#1560

Articles

Caleb Kleveter On Moving From Vapor 2 -> Vapor 3 In Production At Skelpo

https://www.skelpo.com/blog/vapor2-to-vapor3/

How To Perform Fluent Queries

This tutorial is an introduction for how to perform simple filter/sort queries on a database using Vapor’s ORM, Fluent.

https://www.vaporforums.io/thread/37

How To Write Models Using Fluent

This tutorial will show you how to implement a simple user model, store it to the database, get it back from the database and pass it to the view.

https://medium.com/@martinlasek/tutorial-how-to-write-models-using-fluent-e9482d335a5f

Persisting Data In Vapor 3

In this tutorial, we will be covering how to connect to a relational database (both PostgreSQL and MySQL) and then storing data in it.

https://theswiftwebdeveloper.com/diving-into-vapor-part-2-persisting-data-in-vapor-3-c927638301e8

Talks

Tanner Nelson On Swift by Sundell

Tanner talks with John Sundell about NIO, where he sees the future of Vapor, his own background, the inspiration for Vapor 3, and more!

https://www.swiftbysundell.com/podcast/18

Writing Credits

@twof
@Yasumoto