Another Objective-C SQLite ORM
You can install BRORM
via Cocoapod. Just add the following line to your Podfile.
pod 'BRORM', '~> 0.4'
I am using FMDB as SQLite wrapper.
NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"database.sqlite"];
_databaseQueue = [FMDatabaseQueue databaseQueueWithPath:databasePath];
[BROrm setDefaultQueue:_databaseQueue];
Add your migrations for the database.
[BROrm executeUpdate:@"CREATE TABLE IF NOT EXISTS default_class (identifier INTEGER PRIMARY KEY AUTOINCREMENT, string TEXT, int INTEGER, default_class_identifier INTEGER);" withArgumentsInArray:NULL];
Create a subclass of BRModel for each Model u want to use.
- Per default the
tableName
is the underscored classname. Override+ (NSString*)getTableName
if you want to change it. - Per default the
idColumn
isidentifier
. Override+ (NSString*)idColumn
if you want to change it.
If you want do change the table name and or the id column globally you can just subclass BRModel
, override it in there an then subclass your subclass for each of your models.
@interface Person : BRModel
@end
@implementation BRTesttable
+ (NSString*)getTableName{
return @"person";
}
+ (NSString*)idColumn{
return @"id";
}
@end
To read or write from the database you have to use a BROrmWrapper with the class name of your model.
Find Many simply selects many results from the database.
w.limit
: default nonew.tableAlias
: default is the same as the tableNamew.distinct
: default no
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
NSArray *persons = [w findMany];
Find One sets the limit of the query to 1 and returns the result.
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
Person *person = (Person*)[w findOne];
Find One By Id
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
Person *person = (Person*)[w findOne:@(2)];
Count
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
int personCount = [w count];
Creates a new Object. You can either assign an NSDictionary to hydrate the data or change the data on demand.
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
Person *p = [w create:@{@"prename":@"Jason",@"age":@(27)}];
p[@"surname"] = @"Keller";
BOOL success = [p save];
Assigning data to an object only sets them as dirty if they changed. The save method only lazy saves the values that got changed. If nothing changed the wouldn't be any update.
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
Person *p = (Person*)[w findOne:@"2"];
[p setFromDictionary:@{@"street":@"Some Ave. 1234",@"city":@"somecity"}]
p[@"prename"] = @"Jason";
BOOL success = [p save];
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
Person *p = (Person*)[w findOne:@"2"];
BOOL success = [p destroy];
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
[w whereEquals:@"prename" value:@"Jason"];
// [w whereNotEquals:@"prename" value:@"Jason"];
// [w whereLike:@"prename" value:@"Ja%"];
// [w whereNotLike:@"prename" value:@"Ja%"];
// [w whereIdIs:@(1)];
// [w whereRaw:@"`prename` = `Ja%`"]
NSArray *jasons = [w findMany];
- HasOneOrMany
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
Person *t = (Person*)[w findOne:@"1"];
NSArray *customer = [[t hasOneOrMany:@"Customer"] findMany];
- hasAndBelongsToMany
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
Person *t = (Person*)[w findOne:@"1"];
NSArray *customer = [[t hasMany:@"Customer" through:@"customer_person" withForeignKey:@"customer_identifier" andBaseKey:@"person_identifier"] findMany];
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
w.limit = @1;
NSArray *testentries = [w findMany];
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
[w orderBy:@"int" withOrdering:@"ASC"];
w.limit = @1;
w.offset = @1;
NSArray *justOne = [w findMany];
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
[w select:@"prename" as:@"prename"];
[w select:@"count(*)" as:@"count"];
[w groupBy:@"prename"];
[w having:@"age > 21"];
NSArray *allOverTwentyoneByPrename = [w findMany];
- write better documentation