ORM Allowing to modify documents in the Mongo db very quickly and easily with class fields and optimized reflection.
By 360matt (
https://github.com/360matt)
⁉️ 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 11 KB (against 17 KB in v1)
What's new in v2 ?
- ⌛ Better performance
- ⌛ Better reflection system (with cache)
- ♻️ Less class, less instantiation
- ♻️ Generic Type removed
- ♻️ Less code needed, much simpler
- Can now use multiple (infinite?) database at the same time
Dependencies:
- I only use the official Mongo driver in the latest version (4.2.3)
Benchmarks:
Code (Text):
With FX8300 overclocked 4Ghz / DDR3 overclocked 1600Mhz.
With 500_000 documents.
Action: load (doc->field), modify (field) and save (field->doc).
Time elapsed: 160ms.
Every document: 0,000318ms (= 318ns)
⭐ Beginning:
- You must define the connection to the database:
Code (Java):
// with url
Database database
=
new Datebase
(
"database",
"url connection"
)
;
// ________________________________________________________________
Database.
Auth auth
=
new Database.
Auth
(
)
;
auth.
database
=
""
;
auth.
host
=
""
;
auth.
password
=
""
;
auth.
port
=
0
;
auth.
user
=
""
;
// with Auth instance.
Database database
=
new Datebase
( auth
)
;
Code (Java):
database.
close
(
)
;
// close your database connection.
database.
setDefault
(
)
;
// Used to define this database as default.
// The first instantiation is already defined as default.
Legacy references:
Code (Java):
Database#getMongoClient
(
)
// MongoClient
Database#getMongoDatabase
(
)
// MongoDatabase
Manager#getMongoCollection
(
)
// MongoCollection<Document>
⚡ Collection Manager:
Code (Java):
Manager man
=
new Manager
(
"name"
)
;
// get collection Manager for default (first instantied) database
Manager man
=
new Manager
(
"name", Database
)
;
// get collection Manager for chosen database
Features:
- Modify default field id name:
Code (Java):
man.
setFieldID
(
"_id"
)
;
// Define the name of the default field used by the API when instantiating data classes.
Code (Java):
man.
drop
(
)
;
- Check if document exist by id:
Code (Java):
booelan state
= man.
existObject
(
"name"
)
;
- Remove a document by id staticly:
Code (Java):
man.
remove
(
"name"
)
;
- It is possible to classify documents by order according to one or more fields:
Code (Java):
Sort sort
= man.
buildSort
(
"",
"",
"",
"",
"",
""
)
;
// you can set ascending fields here
Sort sort
= man.
buildSort
(
)
;
// or keep empty
sort.
ascending
(
"one",
"two",
"variadic"
)
;
sort.
descending
(
"three",
"fourth",
"variadic"
)
;
sort.
skip
(
10
)
;
// ignore the 10 firsts documents
sort.
setLimit
(
50
)
;
// maximum 50 documents returned
You can return documents:
Code (Java):
List
<Document
> list
= sort.
getDocuments
(
)
;
You can return instances:
Code (Java):
Function
<
Document,
****> instantiate
=
(doc
)
->
{
return
new
****
(doc
)
;
}
;
// this function permit to instantiate your data-class instances.
// You must declare your own constructor, call it ``super(document, manager);``
// ___________________________________________________________________________
List
<****> list
= sort.
getStructure
(instantiate
)
;
You can forEach instances:
Code (Java):
Function
<
Document,
****> instantiate
=
(doc
)
->
{
return
new
****
(doc
)
;
}
;
// this function permit to instantiate your data-class instances.
// You must declare your own constructor, call it ``super(document, manager);``
// ___________________________________________________________________________
sort.
foreachStructure
(instantiate,
(yourInstances of
****
)
->
{
// do stuff
}
)
;
Yours Class-Data instances (represents a document):
The instances each represent a document whether it is fictitious or not.
it is thanks to an instance that we can handle a document in the DB (create, modify, delete)
Example blank class-file:
Code (Java):
public
class
****
extends Structure
{
public
static
final Manager manager
=
new Manager
(
"****"
)
;
public
****
(
final
Object id
)
{
super
(id, manager
)
;
}
public
****
(
final
Document doc
)
{
super
(doc, manager
)
;
}
}
Features:
- Get current document equivalent:
Code (Java):
**** instance
=
?;
Document doc
= instance.
fieldToDoc
(
)
;
// must catch java.lang.IllegalAccessException
- Set from document equivalent:
Code (Java):
**** instance
=
?;
instance.
docToField
( doc
)
;
// must catch java.lang.IllegalAccessException
Extra:
For more help:
- You can add me on Discord: Matteow#6953
About Me:
- I am a 17 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)