Giter VIP home page Giter VIP logo

smodel's Introduction

sModel

sModel is a Swift framework written on top of FMDB to provide:

  • Simple management of your database schema (including database updates)
  • Simple mapping of database rows to Swift objects
  • Batch updates for improved performance on large updates

The included test project has examples of how to use all of the different features of sModel.

DB Schema Management

sModel will take a list of sql files (sorted alphabetically) and execute them against your db. Each sql file is guaranteed to run once and only once for the lifetime of your app's installation on a device. Simply add a new sql file to adjust your schema as your app requires and the next time the app runs, sModel will update your db schema.

sModel comes with a set of helpers to open/close your database and to load your sql files.

var paths = DBManager.getDBDefFiles(bundle: nil)!
paths.sort() //You can sort the files however you would like, just stay consistent.

try? DBManager.open(nil, dbDefFilePaths: paths)

Example SQL files

CREATE TABLE "Thing" (
  "tid" TEXT PRIMARY KEY,
  "name" TEXT
);

Object Mapping

sModel will read data out of your database and map it into your model object. For this to work, you simply subclass BaseModel and adopt the ModelDef protocol. Here's an example domain object to match our db schema definition above.

class Thing: BaseModel {
  var tid = ""
  var name: String? = nil

  static let sqlTableName = "Thing"
  static let columns = [
    ColumnMeta(name: "tid", type: .text, constraint: .primary),
    ColumnMeta(name: "name", type: .text)
  ]
}

extension Thing: ModelDef {
  typealias ModelType = Thing
}

Inserts/Updates

Inserting an object into the database is as simple as creating an instance, populating it with data, and calling save.

let thing = Thing()
thing.tid = "tid1"
thing.name = "thing 1"
thing.save()

To update an existing object, just modify it's properties and call save.

Queries

Querying data out of the database is also very straightforward. Each model object has a set of class methods that can be used to query the database.

let things = Thing.allInstances() //Returns Array<Thing> that holds every instance of `Thing`

// Where clauses
let thing = Thing.firstInstanceWhere("tid = ?", "tid1") //Returns a Thing?
let someThings = Thing.instancesWhere("tid in (?, ?)", "tid1", "tid2") //Return Array<Thing> for each `Thing` that matches the where clause

smodel's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.