BRORM
Another Objective-C SQLite ORM
Install / Use
/learn @brototyp/BRORMREADME
BRORM
Another Objective-C SQLite ORM
Usage
Installation
You can install BRORM via Cocoapod. Just add the following line to your Podfile.
pod 'BRORM', '~> 0.4'
Setup
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
tableNameis the underscored classname. Override+ (NSString*)getTableNameif you want to change it. - Per default the
idColumnisidentifier. Override+ (NSString*)idColumnif 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
Read
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];
Create
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];
Write
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];
Delete
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
Person *p = (Person*)[w findOne:@"2"];
BOOL success = [p destroy];
Filter
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];
Relationships
- 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];
Limit
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
w.limit = @1;
NSArray *testentries = [w findMany];
Offset
BROrmWrapper *w = [BROrmWrapper factoryForClassName:@"Person"];
[w orderBy:@"int" withOrdering:@"ASC"];
w.limit = @1;
w.offset = @1;
NSArray *justOne = [w findMany];
Group By & Having
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];
Todo
- write better documentation
Related Skills
feishu-drive
339.5k|
things-mac
339.5kManage Things 3 via the `things` CLI on macOS (add/update projects+todos via URL scheme; read/search/list from the local Things database)
clawhub
339.5kUse the ClawHub CLI to search, install, update, and publish agent skills from clawhub.com
yu-ai-agent
2.0k编程导航 2025 年 AI 开发实战新项目,基于 Spring Boot 3 + Java 21 + Spring AI 构建 AI 恋爱大师应用和 ReAct 模式自主规划智能体YuManus,覆盖 AI 大模型接入、Spring AI 核心特性、Prompt 工程和优化、RAG 检索增强、向量数据库、Tool Calling 工具调用、MCP 模型上下文协议、AI Agent 开发(Manas Java 实现)、Cursor AI 工具等核心知识。用一套教程将程序员必知必会的 AI 技术一网打尽,帮你成为 AI 时代企业的香饽饽,给你的简历和求职大幅增加竞争力。
