Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/meniny/sqlable

Swift ORM framework.
https://github.com/meniny/sqlable

databse orm sqlite

Last synced: about 1 month ago
JSON representation

Swift ORM framework.

Awesome Lists containing this project

README

        





Version
Author
Build Passing
Swift


Platforms
MIT


Cocoapods
Carthage
SPM

## 🏵 Introduction

**Sqlable** is a tiny library for ORM written in Swift.

## 📋 Requirements

- iOS 8.0+
- macOS 10.10+
- tvOS 9.1+
- watchOS 2.2+
- Xcode 9.0+ with Swift 4.0+

## 📲 Installation

`Sqlable` is available on [CocoaPods](https://cocoapods.org):

```ruby
use_frameworks!
pod 'Sqlable'
```

## ❤️ Contribution

You are welcome to fork and submit pull requests.

## 🔖 License

`Sqlable` is open-sourced software, licensed under the `MIT` license.

## 💫 Usage

First, create a model:

```swift
struct User {
var name: String
}
```

then, extend the model to confirm `Sqlable` protocol:

```swift
extension User: Sqlable {

}
```

and, we need to create the database columns:

```swift
extension User: Sqlable {
// create your columns:

static let id = SQLColumn("id", .integer, PrimaryKey(autoincrement: true))
static let name = SQLColumn("name", .text)
static var tableLayout: [SQLColumn] = [id, name]

// implement there two functions:

func valueForColumn(_ column: SQLColumn) -> SQLValue? {
switch column {
case User.name:
return self.name
default:
return nil
}
}

init(row: SQLReadRow) throws {
name = try row.get(User.name)
}
}
```

now, get your database:

```swift
let doc = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
let database = try SQLiteDatabase.init(filepath: doc.appendingPathComponent("User.db").path)
```

create table if not exists:

```swift
try database.create(table: User.self)
```

do your work, let's `insert` for example:

```swift
try user.insert(into: database)
```
query:

```swift
try User.query(in: database)
```