def save(self): """ Saves the model instance to the database (blocking), unlike insert() this is an instance method so needs to be called by and instantiated model instance:: from models import MyTestModel my_data = { 'this_field': "Hello World", 'another_field': 42, 'whatever_field': True } model_instance = MyTestModel(my_data) model_instance.save() """ from Mojo.ServerHelpers.RunServer import BACKEND_COLLECTION, DATABASE if DATABASE: if BACKEND_COLLECTION: this_collection = BACKEND_COLLECTION(DATABASE, self) self.validate() ret_obj = this_collection.save(self) return ret_obj
def insert(klass, to_insert): """ Insert a model into the database (blocking). **Usage:** :: from models import MyTestModel my_data = { 'this_field': "Hello World", 'another_field': 42, 'whatever_field': True } MyTestModel.insert(MyTestModel(my_data)) This will validate the fields before committing them to the database, without having to instantiate a new model instance. """ from Mojo.ServerHelpers.RunServer import BACKEND_COLLECTION, DATABASE if DATABASE: if BACKEND_COLLECTION: this_collection = BACKEND_COLLECTION(DATABASE, klass) self.validate() ret_obj = this_collection.insert(to_insert) return ret_obj
def delete(self): """ Removes an item form the database, takes a model as input (blocking). """ from Mojo.ServerHelpers.RunServer import BACKEND_COLLECTION, DATABASE if DATABASE: if BACKEND_COLLECTION: this_collection = BACKEND_COLLECTION(DATABASE, self) ret_obj = this_collection.delete([self]) return ret_obj
def delete_bulk(klass, to_delete): """ Delete items in a batch operation (blocking) - takes a list of models as input """ from Mojo.ServerHelpers.RunServer import BACKEND_COLLECTION, DATABASE if DATABASE: if BACKEND_COLLECTION: this_collection = BACKEND_COLLECTION(DATABASE, klass) ret_obj = this_collection.delete(to_delete) return ret_obj
def insert_async(klass, to_insert, *args, **kwargs): """ Insert a model into the database (non-blocking). **Usage:** :: from models import MyTestModel my_data = { 'this_field': "Hello World", 'another_field': 42, 'whatever_field': True } yield gen.Task(MyTestModel.insert_async, MyTestModel(my_data)) This will validate the fields before committing them to the database, without having to instantiate a new model instance. """ from Mojo.ServerHelpers.RunServer import BACKEND_COLLECTION, DATABASE cb = kwargs['callback'] del (kwargs['callback']) if DATABASE: if BACKEND_COLLECTION: this_collection = BACKEND_COLLECTION(DATABASE, klass) ret_obj = yield gen.Task(this_collection.insert, to_insert, *args, **kwargs) cb(ret_obj)
def find_one(klass, *args, **kwargs): """ Find a single object in the database, takes standard PyMongo-style input parameters (blocking):: thisUser = User.find_one({'username':username}) #Gets a specific user from the DB """ from Mojo.ServerHelpers.RunServer import BACKEND_COLLECTION, DATABASE klass.collection_name = klass.__name__ if DATABASE: if BACKEND_COLLECTION: this_collection = BACKEND_COLLECTION(DATABASE, klass) ret_obj = this_collection.find_one(*args, **kwargs) return ret_obj
def find(klass, *args, **kwargs): """ Find a list of objects in the database, takes standard PyMongo-style input parameters (blocking):: users = User.find({}) #gets all users in the database print users """ from Mojo.ServerHelpers.RunServer import BACKEND_COLLECTION, DATABASE klass.collection_name = klass.__name__ if DATABASE: if BACKEND_COLLECTION: this_collection = BACKEND_COLLECTION(DATABASE, klass) ret_obj = this_collection.find(*args, **kwargs) return ret_obj
def delete_bulk_async(klass, to_delete, callback): """ Delete items in a batch operation (non-blocking) - takes a list of models as input """ from Mojo.ServerHelpers.RunServer import BACKEND_COLLECTION, DATABASE if DATABASE: if BACKEND_COLLECTION: this_collection = BACKEND_COLLECTION(DATABASE, klass) ret_obj = yield gen.Task(this_collection.delete, to_delete) callback(ret_obj)
def delete_async(self, callback): """ Removes an item form the database, takes a model as input (non-blocking). """ from Mojo.ServerHelpers.RunServer import BACKEND_COLLECTION, DATABASE if DATABASE: if BACKEND_COLLECTION: this_collection = BACKEND_COLLECTION(DATABASE, self) ret_obj = yield gen.Task(this_collection.delete, [self]) callback(ret_obj)
def find_one_async(klass, *args, **kwargs): """ Find a single object in the database, takes standard PyMongo-style input parameters (non-blocking):: thisUser = yield gen.Task(User.find_one,{'username':username}) #Gets a specific user from the DB """ from Mojo.ServerHelpers.RunServer import BACKEND_COLLECTION, DATABASE klass.collection_name = klass.__name__ cb = kwargs['callback'] del (kwargs['callback']) if DATABASE: if BACKEND_COLLECTION: this_collection = BACKEND_COLLECTION(DATABASE, klass) ret_obj = yield gen.Task(this_collection.find_one, *args, **kwargs) cb(ret_obj)
def find_async(klass, *args, **kwargs): """ Find a list of objects in the database, takes standard PyMongo-style input paramaters (non-blocking):: users = yield gen.Task(User.find_async,{}) #gets all users in the database print users """ from Mojo.ServerHelpers.RunServer import BACKEND_COLLECTION, DATABASE klass.collection_name = klass.__name__ cb = kwargs['callback'] del (kwargs['callback']) if DATABASE: if BACKEND_COLLECTION: this_collection = BACKEND_COLLECTION(DATABASE, klass) ret_obj = yield gen.Task(this_collection.find, *args, **kwargs) cb(ret_obj)