Permalink
Browse files

added ability to optionally enable an automatic update timestamp "_up"

with each insert and update.
  • Loading branch information...
1 parent 3a7307b commit 0a79387153838a52fff1058325dfdc9ce7302542 @timburks committed Dec 12, 2011
Showing with 15 additions and 0 deletions.
  1. +3 −0 objc/NuMongoDB.h
  2. +12 −0 objc/NuMongoDB.m
View
3 objc/NuMongoDB.h
@@ -98,4 +98,7 @@ limitations under the License.
/*! Close a database connection. */
- (void) close;
+/*! Automatically record update times by inserting an _up NSDate with each update. */
++ (void) setEnableUpdateTimestamps:(BOOL) enable;
+
@end
View
12 objc/NuMongoDB.m
@@ -70,6 +70,12 @@ - (NSMutableArray *) arrayValueWithLimit:(int) limit
@implementation NuMongoDB
+static BOOL enableUpdateTimestamps = NO;
+
++ (void) setEnableUpdateTimestamps:(BOOL) enable {
+ enableUpdateTimestamps = YES;
+}
+
- (int) connectWithOptions:(NSDictionary *) options
{
id host = options ? [options objectForKey:@"host"] : nil;
@@ -151,6 +157,9 @@ - (id) insertObject:(id) insert intoCollection:(NSString *) collection
insert = [[insert mutableCopy] autorelease];
[insert setObject:[NuBSONObjectID objectID] forKey:@"_id"];
}
+ if (enableUpdateTimestamps) {
+ [insert setObject:[NSDate date] forKey:@"_up"];
+ }
bson *b = bson_for_object(insert);
if (b) {
mongo_insert(conn, [collection cStringUsingEncoding:NSUTF8StringEncoding], b);
@@ -165,6 +174,9 @@ - (id) insertObject:(id) insert intoCollection:(NSString *) collection
- (void) updateObject:(id) update inCollection:(NSString *) collection
withCondition:(id) condition insertIfNecessary:(BOOL) insertIfNecessary updateMultipleEntries:(BOOL) updateMultipleEntries
{
+ if (enableUpdateTimestamps) {
+ [update setObject:[NSDate date] forKey:@"_up"];
+ }
bson *bupdate = bson_for_object(update);
bson *bcondition = bson_for_object(condition);
if (bupdate && bcondition) {

0 comments on commit 0a79387

Please sign in to comment.