https://github.com/danielde/swiftyrecord
https://github.com/danielde/swiftyrecord
activerecord sqlite swift
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/danielde/swiftyrecord
- Owner: DanielDe
- Created: 2020-02-06T19:00:42.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-07T18:17:13.000Z (over 6 years ago)
- Last Synced: 2026-01-13T03:41:02.751Z (5 months ago)
- Topics: activerecord, sqlite, swift
- Language: Swift
- Size: 24.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# SwiftyRecord
A Swift ORM heavily inspired by Rails' ActiveRecord.
Still a work in progress.
## Example:
```swift
// Migration:
let usersMigration = SwiftyRecordMigration("add users table") {
CreateTableOperation("users") {
TableColumn("name", String.self)
TableColumn("age", Int.self)
}
}
// Execute migration...
struct User: SwiftyRecord {
static let tableName = "users"
let id: Int64?
var name: String
var age: Int
}
// Insertion:
let user = try! User(name: "Winnie Harvey", age: 5).save()
assert(user.id != nil, "Serial ID generated by database upon insertion")
// Querying with operator overloading enabled DSL:
let youngUsers = try! User.findAll(where: ["age" <= 10])
print("Num young users: \(try! youngUsers.count())")
// Relationships:
let booksMigration = SwiftyRecordMigration("add books table") {
CreateTableOperation("books") {
TableColumn("title", String.self)
TableColumn("authorId", Int.self)
Relationship(.belongsTo, "user", via: "authorId")
}
}
// Execute migration...
struct Book: SwiftyRecord {
static let tableName = "books"
let id: Int64?
var title: String
var authorId: Int64?
@BelongsTo var author: User?
}
assert(user.id == 1, "Users's id is 1")
let book = try! Book(title: "Surely You're Joking", authorId: 1).save()
assert(book.author!.name == "Winnie Harvey", "Author is the above user")
```