-
Notifications
You must be signed in to change notification settings - Fork 5
/
Daikiri.h
137 lines (112 loc) · 3.48 KB
/
Daikiri.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
//
// Daikiri.h
// daikiri
//
// Created by Jordi Puigdellívol on 15/4/16.
// Copyright © 2016 revo. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import "DaikiriJSON.h"
#import "QueryBuilder.h"
@interface Daikiri : DaikiriJSON{
NSManagedObject *_managed;
Daikiri *_pivot;
}
@property(strong,nonatomic) NSNumber* id;
// -----------------------------------------
// Create / Save / Update / Destroy
// -----------------------------------------
/**
* Creates a new object to de database
* It MUST have the id field filled or it won't be inserted
* If the object already exists, the db one will be returned
*/
+(id)create:(Daikiri*)object;
/**
* The save function uses the model ID for checking if it already
* exists so it will do an update, or if model ID is null or can't
* find the object, it will create a new one and save it to the
* database
*/
-(bool)save;
/**
* Updates the object in the database (needs the id the be filled)
*/
-(bool)update;
/**
* Deletes the object from the database. Id field is required
*/
-(bool)destroy;
// -----------------------------------------
// Convenience methods
// -----------------------------------------
+(id)createWith:(NSDictionary*)dict;
+(bool)updateWith:(NSDictionary*)dict;
+(bool)destroyWith:(NSNumber*)id;
// -----------------------------------------
// Eloquent like
// -----------------------------------------
/**
* Returns a base queryBuilder instance for the model that can be used to add query filters
*/
+(QueryBuilder*)query;
/**
* Returns the model in the database that matches the id or `nil` if not found
*/
+(id)find:(NSNumber*)id;
/**
* Returns all the models that the id is in the identifiers array
*/
+(NSArray*)findIn:(NSArray*)identifiers;
/**
* Returns the first model found
*/
+(id)first;
/**
* Returns the first model found sorted by
*/
+(id)first:(NSString*)sort;
/**
* Returns all the models in the database
*/
+(NSArray*)all;
/**
* Returns all the models in the database sorted by key
*/
+(NSArray*)all:(NSString*)sort;
/**
* Returns the parent model related with the localKey
*/
-(Daikiri*)belongsTo:(NSString*)model localKey:(NSString*)localKey;
/**
* Retuns all the models that have self as parent with the foreingKey
*/
-(NSArray*)hasMany:(NSString*)model foreignKey:(NSString*)foreignKey;
/**
* Retuns all the models that have self as parent with the foreingKey
*/
-(NSArray*)hasMany:(NSString*)model foreignKey:(NSString*)foreignKey sort:(NSString*)sort;
/**
* Get the related models when there is a pivot table between them,
* you can acces the pivot information with the `pivot` method of the returning results
*/
-(NSArray*)belongsToMany:(NSString*)model pivot:(NSString*)pivotModel localKey:(NSString*)localKey foreignKey:(NSString*)foreingKey;
/**
* Get the related models when there is a pivot table between them,
* you can acces the pivot information with the `pivot` method of the returning results
* sorted by sort
*/
-(NSArray*)belongsToMany:(NSString*)model pivot:(NSString*)pivotModel localKey:(NSString*)localKey foreignKey:(NSString*)foreingKey pivotSort:(NSString*)pivotSort;
/**
* In `belongsToMany` relationships you can access to the pivot model with
* this method, otherwise, it will be nil
*/
-(Daikiri*)pivot;
-(void)setPivot:(Daikiri*)pivot;
/**
* Overridable function so each model can define its managedObjectContext
* By default it returns the DaikiriCoredata.manager
*/
+(NSManagedObjectContext*)managedObjectContext;
@end