Skip to content

A simple Mongo API reducing development time and easy to learn and allows any evolution of the project. You can use class as a structure thanks to reflection.

Notifications You must be signed in to change notification settings

360matt-archives/FastMongo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

💥 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)

About

A simple Mongo API reducing development time and easy to learn and allows any evolution of the project. You can use class as a structure thanks to reflection.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages