Skip to content

Instantly share code, notes, and snippets.

View cweinberger's full-sized avatar

Christian Weinberger cweinberger

View GitHub Profile
@cweinberger
cweinberger / mysql-drop-all-tables.sh
Created June 6, 2018 12:44
drops all tables of a specific db
#!/bin/bash
#usage: mysql-drop-all-tables -d database -u dbuser -p dbpass
TEMP_FILE_PATH='./drop_all_tables.sql'
while getopts d:u:p: option
do
case "${option}"
in
@cweinberger
cweinberger / EventLoopFuture+tryFlatMap.swift
Last active February 21, 2021 21:39
Vapor 4 Utilities
import NIO
/// Author: vzsg (Discord) - https://twitter.com/vzsg_dev
/// Source: https://discordapp.com/channels/431917998102675485/684159753189982218/684537099378098272
extension EventLoopFuture {
func tryFlatMap<NewValue>(
file: StaticString = #file,
line: UInt = #line,
_ callback: @escaping (Value) throws -> EventLoopFuture<NewValue>
@cweinberger
cweinberger / EventLoopFutureQueue-example.swift
Last active January 19, 2021 22:03
Vapor 4 EventLoopFutureQueue
/**
Assuming the route handler is attached to `http://localhost:8080/import-data` you can test the implementation with this request (cURL):
```
curl -X "POST" "http://localhost:8080/import-data" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{ "values": ["first", "foo", "bar", "last"] }'
```
Prints:
```
@cweinberger
cweinberger / let-default-value.swift
Created August 14, 2020 09:00
Let default value
/**
+ less boilerplate
+ default initializer
- you can alter `limit`
*/
struct MyRequestInput1 {
let searchTerm: String
var limit: Int = 10
}
import Foundation
enum IntOrString: Encodable {
case integer(Int)
case string(String)
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
switch self {
case .integer(let val): try container.encode(val)
@cweinberger
cweinberger / corsmiddleware.swift
Last active June 20, 2020 09:32
Vapor 4 TodoBackend - CORS Middleware
public func configure(_ app: Application) throws {
// Serves files from `Public/` directory
// app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
app.middleware.use(CORSMiddleware()) // Add CORS with default configuration
// Configure SQLite database
app.databases.use(.sqlite(.file("db.sqlite")), as: .sqlite)
// Configure migrations
@cweinberger
cweinberger / change-branches-github.sh
Last active June 18, 2020 07:19
Change branch from `master` to `main`
# if you are using GitHub, you can use this tool to change all repos at once
# https://github.com/mheap/github-default-branch
# Rename dev to develop
github-default-branch --pat <token> --repo user/repo --old dev --new develop
# Rename all repos owned by an org
github-default-branch --pat <token> --org my-org-name
# Rename all repos owned by a user
docker run -d \
--restart=always \
-p 33060:3306 \
-e MYSQL_ROOT_PASSWORD=root \
-v ~/docker/volumes/mysql:/var/lib/mysql \
--name mysql-5.7 \
mysql:5.7 \
--sql-mode="NO_ENGINE_SUBSTITUTION"
@cweinberger
cweinberger / MySQL+Database.swift
Last active May 31, 2020 12:48
Vapor 3 with Swift 5.2
import FluentMySQL
/*
Starting with Swift 5.2 the compiler is not able to resolve this automatically:
`migrations.add(model: Todo.self, database: .mysql)`
See: https://forums.swift.org/t/vapor-3-swift-5-2-regression/34764
Instead of adding this to each model, I added these extensions.
@cweinberger
cweinberger / 1-MySQLDataConvertibleEnum.swift
Last active April 13, 2020 15:54
Auto-conform your enums types that you want to store in MySQL to `MySQLDataConvertible` and `ReflectionDecodable`. Avoids a lot of boilerplate!
/* NOTE: check out 3-MySQLDataConvertibleEnum_Better_Version.swift. */
import Fluent
import FluentMySQL
import Vapor
protocol MySQLDataConvertibleEnum: MySQLDataConvertible, ReflectionDecodable, CaseIterable { }
extension MySQLDataConvertibleEnum where Self: RawRepresentable, Self.RawValue == String {
func convertToMySQLData() -> MySQLData {