SwiftKnex can evaluate comparison formula as function.
If you input "id" == 1 in where clause, it is evaluated as function(Not BOOL) and tnrasform to SQL Comparison Literal.
SwiftKnex
Mysql
where(“id” == 1)
where id = 1
where(“id” > 1)
where id > 1
where(“id” >= 1)
where id >= 1
where(“id” < 1)
where id < 1
where(“id” <= 1)
where id <= 1
where(“id” != 1)
where id != 1
Conditions
SwiftKnex provides conditions as function. Here are list of conditions available now.
4. Create main.swift in the {$PROJ}/Sources/Migration
Create main.swift in the {$PROJ}/Sources/Migration directory that is created by Migrate create at previous section. And copy/paste the following code into your {$PROJ}/Sources/Migration/main.swift and then, replace the class names in the knexMigrations array to correct names, and change the database configuration depending on your environment.
You need to add the new class name(s) to the knexMigrations at every migration resource created.
SwiftKnex
A Mysql Native Client and Query Builder that works on Mac and Linux.
This library is powerded by Prorsum
SwiftKnex is aimed to use at production services at my company
Features
TODO
Contributing
All developers should feel welcome and encouraged to contribute to SwiftKnex.
To contribute a feature or idea to SwiftKnex please submit an issue! If you find bugs, of course you can create the PR(s) directory.
Query Builder Reference
Initializing the Library
Query Builder
Select
where equal
where between and limit and offset
You can chain the conditional clauses like SQL and it’s amazing expressive.
join
count
where based on priority
Sub Query
fetch all rows from the results that taken by subquery.
Use ids taken by subquery as argument of in clause
Operators, Conditions and Clauses
Operators
SwiftKnex can evaluate comparison formula as function. If you input
"id" == 1
inwhere
clause, it is evaluated asfunction
(Not BOOL) and tnrasform to SQL Comparison Literal.id
= 1id
> 1id
>= 1id
< 1id
<= 1id
!= 1Conditions
SwiftKnex provides conditions as function. Here are list of conditions available now.
where(_ filter: ConditionFilter)
knex().where(like("name", "%a%"))
knex().where(in("id", [1, 2, 3]))
knex().where(notIn("id", [1, 2, 3]))
knex().where(between("date", "2017-01-01", "2017-01-31"))
knex().where(notBetween("date", "2017-01-01", "2017-01-31"))
knex().where(isNull("deleted_at"))
knex().where(isNotNull("deleted_at"))
knex().where(raw("id = ?", [1]))
or(_ filter: ConditionFilter)
knex().where(in("id", [1, 2, 3])).or(in("id", [4, 5, 6]))
Clauses
Of cource it supports other clauses. You can use them with
knex()
‘s method chain.knex().table("a").join("b")
knex().table("a").leftJoin("b")
knex().table("a").rightJoin("b")
knex().table("a").innerJoin("b")
knex().table("a").join("b").on("a.id" == "b.a_id")
knex().limit(10)
knex().limit(10).offset(100)
knex().order(by: "created_at", .desc)
knex().group(by: "company")
knex().group(by: "company").having(in("name", ["Google", "Apple"]))
TypeSafe Querying with Entity protocol
Define Your Entity with confirming
Entity
protocol and fetch rows as your specified type.Insert
Update
Delete
Transaction
DDL
Create
Schema.Field Reference
Schema Types Comparison
Functions for adding field attributes
default(as value: Any)
after(for name: String)
asPrimaryKey()
asAutoIncrement()
asNotNullable()
asUnsigned()
charset(_ char: Charset)
asUnique()
asIndex()
Drop
Raw
You can perform raw sql with SwiftKnex
Migration
SwiftKnex supports database migration and rollback features.
Flows
1. Install SwiftKnex into your project.
Package.swift
run swift build
and then,
SwiftKnexMigration
executable binary was created in the .build/debug directory.2. Create Migration file
the next step is creating migration class file into your
Sources/Migration
directory with./build/debug/Migration create {ResourceName}
here is an example for creating
CreateUser
migration file3. Edit Your Migration File
After create migration class file, edit it like following.
The created migration class has following methods.
up
Performed on migrate:latest
down
Performed on migrate:rollback
4. Create
main.swift
in the{$PROJ}/Sources/Migration
Create
main.swift
in the{$PROJ}/Sources/Migration
directory that is created byMigrate create
at previous section.And copy/paste the following code into your
{$PROJ}/Sources/Migration/main.swift
and then, replace the class names in theknexMigrations
array to correct names, and change the database configuration depending on your environment.You need to add the new class name(s) to the
knexMigrations
at every migration resource created.main.swif example
after editing main.swift, run
swift build
5. Perform Migration and Rollback
After that, you only need to run the migration
Current supporting commands are
migrate:latest
: Perform to migrate recent unmigrated files.migrate:rollback
: Rollback the migrations recent performed.(The rollback unit is grouped bybatch
number)Try to perform Migration
Try to perform Rollback
Seed
TODO
Working with Prorsum
Go Style async query performing and syncronization with Prorsum
Mysql Library
The base Connection and Querying library that used in SwiftKnex.
TODO
License
Prorsum is released under the MIT license. See LICENSE for details.