Code Monkey home page Code Monkey logo

fastmongo's Introduction

๐Ÿ’ฅ FastMongo BCH compliance

Being able to manipulate documents in the form of a personalized structure thanks to reflection.
Create any class, add as many fields to it as you want, set defaults to whatever you want, and you can finally use it to fetch and define documents.

โ‰๏ธ Why use this API ?

  • ๐Ÿ’ก As simple as possible, it is easy to learn
  • โŒ› Its use is very fast, even the migration
  • ๐ŸŽจ It is customizable, you can define many behavior in this API
  • ๐Ÿ’พ Your data is better structured
  • ๐Ÿšฆ You can develop your project and your structure
  • โ™ป๏ธ Very light, my code represents only 17 KB

๐Ÿ”— Dependencies:

  • I only use the official Mongo driver in the latest version (3.12.8)
  • For my first tests, I was using Fongo to emulate a Mongo server, but lastly I tested with a real production server.
    โš ๏ธ Fongo does not really work above 3.5.X excluded

โญ Beginning:

  1. You must define the connection to the database:
MongoIntegration.connect(new MongoIntegration.Auth() {{
        this.host = ;
        this.port = ;
        this.user = ;
        this.password = ;
        this.database = ;
}});
  1. If at any time you want to close the connection:
MongoIntegration.disconnect();
  1. You can cycle connect/disconnect as many times. But you can only connect to one database at a time for this API version

๐Ÿ’ค Legacy references:

MongoIntegration.client // static field
MongoIntegration.database // static field
CollectionManager#collection // instance field related of collection
Element#getDocument() // get legacy document of Element

โšก Collection Manager:

  • The collection is created in the DB each time CollectionManager is instantiated
 // getting Collection manager
CollectionManager man = new CollectionManager("collection name") {{

        updateStructure(Kangourou.class);
        // Optional: allows you to define new fields without deleting old ones from all existing documents 
        
        autoInsert(Kangourou.class);
        // Optional: allows to auto-insert the document with default values upon instantiation of Element
        
        setFieldID("UUID");
        // Optional: By default, IDs are mapped by the field _id, but you can use any other field name 
        
}};

๐Ÿ”จ Features:

  • It is possible to classify documents by order according to one or more fields:
    โš ๏ธ you can use any class you want, the return type adjusts itself.
    Only existing fields will be modified, without errors, but be reasonable on the utility.

You can return structures:

List<Kangourou> topPrice = man.buildSort("price")
        .setLimit(20) // limit to 20 elements
        .getRaws(Kangourou.class); // recover as structure

You can return documents:

List<Document> topPrice = man.buildSort("price")
        .setLimit(20) // limit to 20 elements
        .getDocuments(); // recover as legacy document

You can sort multiple fields:

Sort currentSort = man.buildSort("price", "age");
// ascending sort



Sort currentSort = man.buildSort(); // nothing in constructor
// It is possible to add classification rules after instantiation


currentSort.descending("disease", "etc ...");
currentSort.ascending("price", "age");
// create an empty pattern and add ascending/descending criteria ad infinitum 


List<Kangourou> topPriceWithBetterAge = currentSort.getRaws(Kangourou.class);
// recover as structure
List<Document> topPriceWithBetterAge = currentSort.getDocument();
// recover as legacy document
  • You can check if document exist by id:
    โš ๏ธ Set by default, "_id", you can change the name of the field id with setFieldID( NAME ) in manager initialiser.
booelan state = man.exist( "name" );
  • Get empty default structure (Utils):
Kangourou emptyWithDefault = man.getEmptyRaw( Kangourou.class )

๐Ÿ”“ Element (represents a document):

The elements each represent a document whether it is fictitious or not.
it is thanks to an element instance that we can handle a document in the DB (create, modify, delete)

There are two ways to get an Element instance:

  • Using the element's constructor directly (less popular but still possible):
Element element = new Element("name of document by ID", managerOfAnyCollection);
// Element represents a document (fictive or not) with chosen id and collection
  • Using the collection manager:
Element element = managerOfAnyCollection.getObject("name of document by ID");
// Element represents a document (fictive or not) in collection with chosen id

Features:

  • Get structure with current value from DB:
Kangourou struct = element.getRaw(Kangourou.class);
  • Set structure to create/edit the document:
    โš ๏ธ The other unedited fields appearing in the reset structure are those of the document
    Those which are not present in the structure but present in the document will be kept intact

โ„น๏ธ You can also change name/id of document

element.setRaw(new Kangourou() {{
  this.anyField = "newer value";
}});
  • Update document without structures:
element.update("key", "value");
// update single key

element.update(
  "key", "value",
  "key_2", "value_2"
);
// update multiples keys with variadic

element.update( new HashMap() {{
  put("key", "value");
}});
// update with hashmap
  • Increment / Decrement one or multiple fields:
element.increment("key", 3);
// will do +3

element.increment("key", -5);
// will do -5

element.increment(
  "key_1", 3,
  "key_2", -5
)
// [in/de]crement multiple fields

element.increment(new HashMap() {{
  put("key", 3);
}});
// will do +3
  • Get legacy document:
element.getDocument();
  • Get list List<?> from field:
List<?> list = element.getList("predators");
// You can try to cast after that
  • Get string list List from field:
    โ„น๏ธ is safety
List<String> list = element.getStringList(" field name ");
  • Get document list List from field:
List<Document> document = element.getListAsDocument(" field name" );
  • Push (add) entrie to a list:
element.push(" field ", new AnyObject());
// the value can be any type

element.push(" field ", new Document());
// the document will be parsed
  • Pull (remove) entrie from a list:
element.pull(" field ", new AnyObject());
// remove this object if exist

element.pull(" field ", new Document());
// can remove document from array/list

element.pullIndex(" field ", 0);
// can remove by index (here, the first element)

element.pullAll(" field ");
// can remove all entries from list

Extra:

  • You can retrieve the collection manager from a Element instance:
element.manager // is the collection manager
  • You can retrieve the ID of Element/Document:
element.id

๐Ÿ˜‡ For more help:

๐Ÿ‘ป About Me:

  • I am a 16 year old French developer.
  • I started around 12 years old with basic PHP, then around 14 years old I was doing Discord bots in JS with NodeJS.
  • I finally started Java around 15 (so it's been over a year), and my experience has improved over time (see the rest of my Github)

fastmongo's People

Contributors

360matt avatar snyk-bot avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Forkers

classicvalues

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.