Giter VIP home page Giter VIP logo

jongo-dao's Introduction

jongo-dao

A generic data access library for jongo

Summary

Encapsulates frequently used boiler plate, so models don't have to rewrite the same boring code again and again.

Author

Mohammad Nasir Rasul < nasir [at] rasul [dot] ca>

Website: http://mnrasul.github.io/jongo-dao/

Issue Tracker: On Github - https://github.com/mnrasul/jongo-dao/issues

Code: https://github.com/mnrasul/jongo-dao

Mailing List: None exists yet. You can email me. If there is sufficient traction, I'll set one up.

Wishlist

  • Provide support for aggregation queries

Usage

Have a look at the test class provided for an example usage.

Helpful Tip : Use mvn dependency:copy-dependencies -DoutputDirectory=directory to copy all jars to a target directory of your choice if needed

Advantages

I find it tedious to rewrite the same kind of queries for similar objects. I built this DAO for myself. Perhaps someone will find it useful.

Feel free to modify it to suit your needs.

It saves me the effort of writing same queries again and again. Instead, I only need to write queries which are rather unique. Overtime, I expect, to flesh this out more as I observe my usage patterns.

Usage

  • Create a model
  • Subclass DAO to provide specific initialization vectors
  • Use DAO

Persisting History/versions of documents

You can have full copies of documents stored as a new version.

  • Model should implement HistoryAwareModel interface.
  • Call the super.update method in your DAO i.e PersonDAO
  • ensure version is incremented by calling $inc on version
  • that is pretty much it
  • at some point I will try and implement a smarter history tracking
@Override
public void update(ObjectId id, Person person) {
        super.update(id,person);
        collection.update(id).with("{$set: {fn: #, ln: #, createdAt: #, createdBy: #}, $inc: {version: 1}}", person.firstName, person.lastName, new Date(), "anon");
}

Create a model

public class Person implements Model{
    @Id
    ObjectId id;
    
    @JsonProperty("fn")
    String firstName;
    
    @JsonProperty("ln")
    String lastName;
    
    @JsonProperty("a")
    Address address;
    
    @JsonProperty("l")
    List<String> tags = new ArrayList<String>(3);
    
    public ObjectId getId() {
        return this.id;
    }

    public String getHash() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}

Subclass DAO

public class PersonDAO extends DAO<Person>{

    public PersonDAO(String connectionURL, String dbname, String collectionName, Class<Person> type) throws UnknownHostException {
        super(connectionURL, dbname, collectionName, Person.class);
    }
    
    public PersonDAO() throws UnknownHostException{
        super("localhost:27017", "jongo-dao-test", "person", Person.class);
    }
    
}

Use DAO

    @Test
    public void testCRUD() throws UnknownHostException{
        Person p = new Person();
        p.lastName = "Aladin";
        p.firstName = "Prince";
        Address address = new Address();
        address.street = "Palace";
        address.city = "City";
        address.postal = "12345";
        address.state = "Kingdom";
        address.country = "Magicland";
        p.address = address;
        p.tags.add("flying carpet");
        p.tags.add("magic lamp");
        p.tags.add("fantasy");
        
        PersonDAO dao = new PersonDAO();
        dao.save(p);
        ObjectId id = p.id;
        Person find = dao.find(id);
        
        Assert.assertEquals(p.firstName, find.firstName);
        Assert.assertEquals(p.lastName, find.lastName);
        Assert.assertEquals(p.address.city, find.address.city);
        
        p.firstName = "Genie";
        dao.update(p);
        Assert.assertNotEquals(p.firstName, find.firstName);
        
        find = dao.find(id);
        Assert.assertEquals(p.firstName, find.firstName);
        
        dao.delete(id);
        find = dao.find(id);
        Assert.assertNull(find);
        
    }

jongo-dao's People

Contributors

mnrasul avatar

Watchers

James Cloos avatar  avatar

jongo-dao's Issues

Add support for counting

One should be able to count the number of records in a collection, or number of records matching a specific query.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.