Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted by pokstad ( 14 years ago )
//
//  main.m
//  couchdb
//
//  Created by Paul Okstad on 9/30/12.
//  Copyright (c) 2012 Paul Okstad. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <CouchCocoa/CouchCocoa.h>
#import <CouchCocoa/CouchTouchDBServer.h>
#import <TouchDB/TouchDB.h>

#define DB_NAME @"mydb"

int main(int argc, const char * argv[])
{
    
    @autoreleasepool {
        
        // insert code here...
        NSLog(@"Main bundle directory: %@", [NSBundle mainBundle].resourcePath);
        // Connect to local Linux VM hosting CouchDB server
        NSURL* testServerURL = [NSURL URLWithString: @"http://172.16.106.145:5984"];
        CouchServer *testServer = [[CouchServer alloc] initWithURL:testServerURL];
        CouchDatabase *testDB = [testServer databaseNamed: DB_NAME];
        // Show all docs from production server
        CouchQuery* allDocs = [testDB getAllDocuments];
        NSLog(@"Test server has %ld docs. Printing all of their ID's:", allDocs.rows.count);
        for (CouchQueryRow* row in allDocs.rows) {
            NSLog(@"test server doc: %@", row.documentID);
        }
        // Create a TouchDB server
        CouchTouchDBServer* tdbserver = [CouchTouchDBServer sharedInstance];
        if (tdbserver.error) {
            NSLog(@"ERROR: %@", [tdbserver.error localizedDescription]);
        }
        // Create database
        CouchDatabase *localDB = [tdbserver databaseNamed:DB_NAME];
        NSError *error = Nil;
        if (![localDB ensureCreated:&error;]) {
            NSLog(@"ERROR: %@", [error localizedDescription]);
        }
        // Get some info about this server
        NSLog(@"TouchDB databases: %@", tdbserver.getDatabases);
        
        // Now replicate: pull from test server to touchdb
        NSLog(@"Pulling from prod to test");
        CouchReplication *pull = [localDB pullFromDatabaseAtURL:testDB.URL];
        pull.continuous = FALSE;
        RESTOperation *op = [pull start];
        if (op != Nil) {
            NSLog(@"Waiting for operation to finish: %d to replicate", pull.total);
            [op wait];
            NSLog(@"Operation has finished.");
        }
        allDocs = [localDB getAllDocuments];
        NSLog(@"Printing all %ld docs in touchdb", allDocs.rows.count);
        for (CouchQueryRow* row in allDocs.rows) {
            NSLog(@"Test server doc: %@", row.documentID);
        }
        NSLog(@"Finished printing all docs in touchdb");
    }
    return 0;
}

 

Revise this Paste

Your Name: Code Language: