├── .gitignore
├── README.md
├── pom.xml
└── src
└── main
└── java
└── dev
└── quosty
└── mongo
├── MongoClientInitializer.java
├── MongodbWrapperImpl.java
├── annotation
└── MongodbEntity.java
├── helper
└── GsonHelper.java
└── impl
└── MongodbWrapper.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # Created by https://www.toptal.com/developers/gitignore/api/java,jetbrains,jetbrains+all,jetbrains+iml,maven,intellij,intellij+all,intellij+iml
2 | # Edit at https://www.toptal.com/developers/gitignore?templates=java,jetbrains,jetbrains+all,jetbrains+iml,maven,intellij,intellij+all,intellij+iml
3 |
4 | ### Intellij ###
5 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
6 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
7 |
8 | # User-specific stuff
9 | .idea/**/workspace.xml
10 | .idea/**/tasks.xml
11 | .idea/**/usage.statistics.xml
12 | .idea/**/dictionaries
13 | .idea/**/shelf
14 |
15 | # AWS User-specific
16 | .idea/**/aws.xml
17 |
18 | # Generated files
19 | .idea/**/contentModel.xml
20 |
21 | # Sensitive or high-churn files
22 | .idea/**/dataSources/
23 | .idea/**/dataSources.ids
24 | .idea/**/dataSources.local.xml
25 | .idea/**/sqlDataSources.xml
26 | .idea/**/dynamic.xml
27 | .idea/**/uiDesigner.xml
28 | .idea/**/dbnavigator.xml
29 |
30 | # Gradle
31 | .idea/**/gradle.xml
32 | .idea/**/libraries
33 |
34 | # Gradle and Maven with auto-import
35 | # When using Gradle or Maven with auto-import, you should exclude module files,
36 | # since they will be recreated, and may cause churn. Uncomment if using
37 | # auto-import.
38 | # .idea/artifacts
39 | # .idea/compiler.xml
40 | # .idea/jarRepositories.xml
41 | # .idea/modules.xml
42 | # .idea/*.iml
43 | # .idea/modules
44 | # *.iml
45 | # *.ipr
46 |
47 | # CMake
48 | cmake-build-*/
49 |
50 | # Mongo Explorer plugin
51 | .idea/**/mongoSettings.xml
52 |
53 | # File-based project format
54 | *.iws
55 |
56 | # IntelliJ
57 | out/
58 |
59 | # mpeltonen/sbt-idea plugin
60 | .idea_modules/
61 |
62 | # JIRA plugin
63 | atlassian-ide-plugin.xml
64 |
65 | # Cursive Clojure plugin
66 | .idea/replstate.xml
67 |
68 | # SonarLint plugin
69 | .idea/sonarlint/
70 |
71 | # Crashlytics plugin (for Android Studio and IntelliJ)
72 | com_crashlytics_export_strings.xml
73 | crashlytics.properties
74 | crashlytics-build.properties
75 | fabric.properties
76 |
77 | # Editor-based Rest Client
78 | .idea/httpRequests
79 |
80 | # Android studio 3.1+ serialized cache file
81 | .idea/caches/build_file_checksums.ser
82 |
83 | ### Intellij Patch ###
84 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
85 |
86 | # *.iml
87 | # modules.xml
88 | # .idea/misc.xml
89 | # *.ipr
90 |
91 | # Sonarlint plugin
92 | # https://plugins.jetbrains.com/plugin/7973-sonarlint
93 | .idea/**/sonarlint/
94 |
95 | # SonarQube Plugin
96 | # https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
97 | .idea/**/sonarIssues.xml
98 |
99 | # Markdown Navigator plugin
100 | # https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
101 | .idea/**/markdown-navigator.xml
102 | .idea/**/markdown-navigator-enh.xml
103 | .idea/**/markdown-navigator/
104 |
105 | # Cache file creation bug
106 | # See https://youtrack.jetbrains.com/issue/JBR-2257
107 | .idea/$CACHE_FILE$
108 |
109 | # CodeStream plugin
110 | # https://plugins.jetbrains.com/plugin/12206-codestream
111 | .idea/codestream.xml
112 |
113 | # Azure Toolkit for IntelliJ plugin
114 | # https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
115 | .idea/**/azureSettings.xml
116 |
117 | ### Intellij+all ###
118 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
119 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
120 |
121 | # User-specific stuff
122 |
123 | # AWS User-specific
124 |
125 | # Generated files
126 |
127 | # Sensitive or high-churn files
128 |
129 | # Gradle
130 |
131 | # Gradle and Maven with auto-import
132 | # When using Gradle or Maven with auto-import, you should exclude module files,
133 | # since they will be recreated, and may cause churn. Uncomment if using
134 | # auto-import.
135 | # .idea/artifacts
136 | # .idea/compiler.xml
137 | # .idea/jarRepositories.xml
138 | # .idea/modules.xml
139 | # .idea/*.iml
140 | # .idea/modules
141 | # *.iml
142 | # *.ipr
143 |
144 | # CMake
145 |
146 | # Mongo Explorer plugin
147 |
148 | # File-based project format
149 |
150 | # IntelliJ
151 |
152 | # mpeltonen/sbt-idea plugin
153 |
154 | # JIRA plugin
155 |
156 | # Cursive Clojure plugin
157 |
158 | # SonarLint plugin
159 |
160 | # Crashlytics plugin (for Android Studio and IntelliJ)
161 |
162 | # Editor-based Rest Client
163 |
164 | # Android studio 3.1+ serialized cache file
165 |
166 | ### Intellij+all Patch ###
167 | # Ignore everything but code style settings and run configurations
168 | # that are supposed to be shared within teams.
169 |
170 | .idea/*
171 |
172 | !.idea/codeStyles
173 | !.idea/runConfigurations
174 |
175 | ### Intellij+iml ###
176 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
177 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
178 |
179 | # User-specific stuff
180 |
181 | # AWS User-specific
182 |
183 | # Generated files
184 |
185 | # Sensitive or high-churn files
186 |
187 | # Gradle
188 |
189 | # Gradle and Maven with auto-import
190 | # When using Gradle or Maven with auto-import, you should exclude module files,
191 | # since they will be recreated, and may cause churn. Uncomment if using
192 | # auto-import.
193 | # .idea/artifacts
194 | # .idea/compiler.xml
195 | # .idea/jarRepositories.xml
196 | # .idea/modules.xml
197 | # .idea/*.iml
198 | # .idea/modules
199 | # *.iml
200 | # *.ipr
201 |
202 | # CMake
203 |
204 | # Mongo Explorer plugin
205 |
206 | # File-based project format
207 |
208 | # IntelliJ
209 |
210 | # mpeltonen/sbt-idea plugin
211 |
212 | # JIRA plugin
213 |
214 | # Cursive Clojure plugin
215 |
216 | # SonarLint plugin
217 |
218 | # Crashlytics plugin (for Android Studio and IntelliJ)
219 |
220 | # Editor-based Rest Client
221 |
222 | # Android studio 3.1+ serialized cache file
223 |
224 | ### Intellij+iml Patch ###
225 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023
226 |
227 | *.iml
228 | modules.xml
229 | .idea/misc.xml
230 | *.ipr
231 |
232 | ### Java ###
233 | # Compiled class file
234 | *.class
235 |
236 | # Log file
237 | *.log
238 |
239 | # BlueJ files
240 | *.ctxt
241 |
242 | # Mobile Tools for Java (J2ME)
243 | .mtj.tmp/
244 |
245 | # Package Files #
246 | *.jar
247 | *.war
248 | *.nar
249 | *.ear
250 | *.zip
251 | *.tar.gz
252 | *.rar
253 |
254 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
255 | hs_err_pid*
256 | replay_pid*
257 |
258 | ### JetBrains ###
259 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
260 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
261 |
262 | # User-specific stuff
263 |
264 | # AWS User-specific
265 |
266 | # Generated files
267 |
268 | # Sensitive or high-churn files
269 |
270 | # Gradle
271 |
272 | # Gradle and Maven with auto-import
273 | # When using Gradle or Maven with auto-import, you should exclude module files,
274 | # since they will be recreated, and may cause churn. Uncomment if using
275 | # auto-import.
276 | # .idea/artifacts
277 | # .idea/compiler.xml
278 | # .idea/jarRepositories.xml
279 | # .idea/modules.xml
280 | # .idea/*.iml
281 | # .idea/modules
282 | # *.iml
283 | # *.ipr
284 |
285 | # CMake
286 |
287 | # Mongo Explorer plugin
288 |
289 | # File-based project format
290 |
291 | # IntelliJ
292 |
293 | # mpeltonen/sbt-idea plugin
294 |
295 | # JIRA plugin
296 |
297 | # Cursive Clojure plugin
298 |
299 | # SonarLint plugin
300 |
301 | # Crashlytics plugin (for Android Studio and IntelliJ)
302 |
303 | # Editor-based Rest Client
304 |
305 | # Android studio 3.1+ serialized cache file
306 |
307 | ### JetBrains Patch ###
308 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
309 |
310 | # *.iml
311 | # modules.xml
312 | # .idea/misc.xml
313 | # *.ipr
314 |
315 | # Sonarlint plugin
316 | # https://plugins.jetbrains.com/plugin/7973-sonarlint
317 |
318 | # SonarQube Plugin
319 | # https://plugins.jetbrains.com/plugin/7238-sonarqube-community-plugin
320 |
321 | # Markdown Navigator plugin
322 | # https://plugins.jetbrains.com/plugin/7896-markdown-navigator-enhanced
323 |
324 | # Cache file creation bug
325 | # See https://youtrack.jetbrains.com/issue/JBR-2257
326 |
327 | # CodeStream plugin
328 | # https://plugins.jetbrains.com/plugin/12206-codestream
329 |
330 | # Azure Toolkit for IntelliJ plugin
331 | # https://plugins.jetbrains.com/plugin/8053-azure-toolkit-for-intellij
332 |
333 | ### JetBrains+all ###
334 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
335 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
336 |
337 | # User-specific stuff
338 |
339 | # AWS User-specific
340 |
341 | # Generated files
342 |
343 | # Sensitive or high-churn files
344 |
345 | # Gradle
346 |
347 | # Gradle and Maven with auto-import
348 | # When using Gradle or Maven with auto-import, you should exclude module files,
349 | # since they will be recreated, and may cause churn. Uncomment if using
350 | # auto-import.
351 | # .idea/artifacts
352 | # .idea/compiler.xml
353 | # .idea/jarRepositories.xml
354 | # .idea/modules.xml
355 | # .idea/*.iml
356 | # .idea/modules
357 | # *.iml
358 | # *.ipr
359 |
360 | # CMake
361 |
362 | # Mongo Explorer plugin
363 |
364 | # File-based project format
365 |
366 | # IntelliJ
367 |
368 | # mpeltonen/sbt-idea plugin
369 |
370 | # JIRA plugin
371 |
372 | # Cursive Clojure plugin
373 |
374 | # SonarLint plugin
375 |
376 | # Crashlytics plugin (for Android Studio and IntelliJ)
377 |
378 | # Editor-based Rest Client
379 |
380 | # Android studio 3.1+ serialized cache file
381 |
382 | ### JetBrains+all Patch ###
383 | # Ignore everything but code style settings and run configurations
384 | # that are supposed to be shared within teams.
385 |
386 |
387 |
388 | ### JetBrains+iml ###
389 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
390 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
391 |
392 | # User-specific stuff
393 |
394 | # AWS User-specific
395 |
396 | # Generated files
397 |
398 | # Sensitive or high-churn files
399 |
400 | # Gradle
401 |
402 | # Gradle and Maven with auto-import
403 | # When using Gradle or Maven with auto-import, you should exclude module files,
404 | # since they will be recreated, and may cause churn. Uncomment if using
405 | # auto-import.
406 | # .idea/artifacts
407 | # .idea/compiler.xml
408 | # .idea/jarRepositories.xml
409 | # .idea/modules.xml
410 | # .idea/*.iml
411 | # .idea/modules
412 | # *.iml
413 | # *.ipr
414 |
415 | # CMake
416 |
417 | # Mongo Explorer plugin
418 |
419 | # File-based project format
420 |
421 | # IntelliJ
422 |
423 | # mpeltonen/sbt-idea plugin
424 |
425 | # JIRA plugin
426 |
427 | # Cursive Clojure plugin
428 |
429 | # SonarLint plugin
430 |
431 | # Crashlytics plugin (for Android Studio and IntelliJ)
432 |
433 | # Editor-based Rest Client
434 |
435 | # Android studio 3.1+ serialized cache file
436 |
437 | ### JetBrains+iml Patch ###
438 | # Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-249601023
439 |
440 |
441 | ### Maven ###
442 | target/
443 | pom.xml.tag
444 | pom.xml.releaseBackup
445 | pom.xml.versionsBackup
446 | pom.xml.next
447 | release.properties
448 | dependency-reduced-pom.xml
449 | buildNumber.properties
450 | .mvn/timing.properties
451 | # https://github.com/takari/maven-wrapper#usage-without-binary-jar
452 | .mvn/wrapper/maven-wrapper.jar
453 |
454 | # Eclipse m2e generated files
455 | # Eclipse Core
456 | .project
457 | # JDT-specific (Eclipse Java Development Tools)
458 | .classpath
459 |
460 | # End of https://www.toptal.com/developers/gitignore/api/java,jetbrains,jetbrains+all,jetbrains+iml,maven,intellij,intellij+all,intellij+iml
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Example Java mongodb-wrapper
2 |
3 | ---
4 |
5 | #### Repository(Maven)
6 | ```xml
7 |
8 | quosty-repository-releases
9 | quosty-dev
10 | https://repo.quosty.dev/releases
11 |
12 | ```
13 | #### Dependency(Maven)
14 | ```xml
15 |
16 | dev.quosty
17 | mongodb-wrapper
18 | 2.0.1
19 |
20 | ```
21 |
22 | #### Example use:
23 |
24 | ```java
25 | import com.mongodb.client.model.Filters;
26 |
27 | import java.util.List;
28 |
29 | public class ExampleApplication {
30 |
31 | public static void main(String[] args) {
32 | //Create a new instance of the MongoClientInitializer and connect to the database
33 | MongoClientInitializer initializer = new MongoClientInitializer("mongodb://localhost:27017");
34 | //new MongoClientInitializer("uri", gson);
35 | //new MongoClientInitializer(mongoClient);
36 | //new MongoClientInitializer(mongoClient, gson)
37 |
38 | MongodbWrapperImpl wrapper = initializer.getMongodbWrapper();
39 |
40 | //Create example object
41 | ExampleObject exampleObject = new ExampleObject("test");
42 |
43 | //Insert one object to database.
44 | wrapper.insertOne(exampleObject);
45 |
46 | //Insert list of objects to database.
47 | wrapper.insertMany(List.of(exampleObject, exampleObject));
48 |
49 | //Delete none object from database.
50 | wrapper.deleteOne(exampleObject);
51 |
52 | //Delete list of objects from database.
53 | wrapper.deleteMany(exampleObject, Filters.eq("key"));
54 |
55 | //Delete list of objects from database with custom filters.
56 | wrapper.deleteMany(List.of(exampleObject, exampleObject), Filters.eq("key", value));
57 |
58 | //Update one object in database.
59 | wrapper.updateOne(exampleObject);
60 |
61 | //Update object in database with custom filters.
62 | wrapper.updateOne(exampleObject, Filters.eq("key", value));
63 |
64 | //Update list of objects in database.
65 | wrapper.updateMany(List.of(exampleObject, exampleObject));
66 |
67 | //Update list of objects in database with custom filters.
68 | wrapper.updateMany(List.of(exampleObject, exampleObject), Filters.eq("key", value));
69 |
70 | //Find object in database with custom filter
71 | ExampleObject object = wrapper.findOne(ExampleObject.class, Filters.eq("key", value));
72 |
73 | //Find list of objects with custom filter
74 | List objects = wrapper.findMany(ExampleObject.class, Filters.eq("key", value));
75 |
76 | //Checks if the element exists
77 | boolean status = wrapper.isExists(ExampleObject.class, Filters.eq("key", value));
78 | }
79 | }
80 | ```
81 |
82 | #### Example Object
83 | ```java
84 | @MongodbEntity(
85 | database = "database",
86 | collection = "collection"
87 | )
88 | public class ExampleObject {
89 |
90 | @SerializedName("_id")
91 | private final String userName;
92 |
93 | public ExampleObject(String userName) {
94 | this.userName = userName;
95 | }
96 | }
97 | ```
98 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | dev.quosty
8 | mongodb-wrapper
9 | 2.0.1
10 |
11 |
12 | 17
13 | 17
14 |
15 |
16 |
17 |
18 | quosty-repository-releases
19 | quosty-dev
20 | https://repo.quosty.dev/releases
21 |
22 |
23 |
24 |
25 |
26 | org.mongodb
27 | mongodb-driver-sync
28 | 5.2.0
29 | provided
30 |
31 |
32 |
33 | com.google.code.gson
34 | gson
35 | 2.9.1
36 | provided
37 |
38 |
39 |
--------------------------------------------------------------------------------
/src/main/java/dev/quosty/mongo/MongoClientInitializer.java:
--------------------------------------------------------------------------------
1 | package dev.quosty.mongo;
2 |
3 | import com.google.gson.Gson;
4 | import com.mongodb.client.MongoClient;
5 | import com.mongodb.client.MongoClients;
6 | import dev.quosty.mongo.helper.GsonHelper;
7 |
8 | public class MongoClientInitializer {
9 |
10 | private final Gson gson;
11 | private final MongoClient mongoClient;
12 | private final MongodbWrapperImpl mongodbWrapper;
13 |
14 | public MongoClientInitializer(MongoClient mongoClient, Gson gson) {
15 | this.mongoClient = mongoClient;
16 | this.gson = gson;
17 | this.mongodbWrapper = new MongodbWrapperImpl(this.gson, this.mongoClient);
18 | }
19 |
20 | public MongoClientInitializer(MongoClient mongoClient) {
21 | this(mongoClient, GsonHelper.getGson());
22 | }
23 |
24 | public MongoClientInitializer(String mongodbUri) {
25 | this(MongoClients.create(mongodbUri), GsonHelper.getGson());
26 | }
27 |
28 | public MongoClientInitializer(String mongodbUri, Gson gson) {
29 | this(MongoClients.create(mongodbUri), gson);
30 | }
31 |
32 | public Gson getGson() {
33 | return gson;
34 | }
35 |
36 | public MongoClient getMongoClient() {
37 | return mongoClient;
38 | }
39 |
40 | public MongodbWrapperImpl getMongodbWrapper() {
41 | return mongodbWrapper;
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/src/main/java/dev/quosty/mongo/MongodbWrapperImpl.java:
--------------------------------------------------------------------------------
1 | package dev.quosty.mongo;
2 |
3 | import com.google.gson.Gson;
4 | import com.mongodb.client.FindIterable;
5 | import com.mongodb.client.MongoClient;
6 | import com.mongodb.client.MongoCollection;
7 | import com.mongodb.client.model.Filters;
8 | import dev.quosty.mongo.annotation.MongodbEntity;
9 | import dev.quosty.mongo.impl.MongodbWrapper;
10 | import org.bson.Document;
11 | import org.bson.conversions.Bson;
12 |
13 | import java.util.ArrayList;
14 | import java.util.List;
15 |
16 | public class MongodbWrapperImpl implements MongodbWrapper {
17 |
18 | private final Gson gson;
19 | private final MongoClient client;
20 |
21 | public MongodbWrapperImpl(Gson gson, MongoClient client) {
22 | this.gson = gson;
23 | this.client = client;
24 | }
25 |
26 | @Override
27 | public void insertOne(T value) {
28 | this.getDatabaseCollection(value.getClass()).insertOne(Document.parse(this.gson.toJson(value)));
29 | }
30 |
31 | @Override
32 | public void insertMany(List value) {
33 | value.forEach(this::insertOne);
34 | }
35 |
36 | @Override
37 | public void deleteOne(T value) {
38 | this.getDatabaseCollection(value.getClass()).findOneAndDelete(
39 | Filters.eq("_id", Document.parse(this.gson.toJson(value)).get("_id")));
40 | }
41 |
42 | @Override
43 | public void deleteMany(T value, Bson filters) {
44 | this.getDatabaseCollection(value.getClass()).deleteMany(filters);
45 | }
46 |
47 | @Override
48 | public void updateOne(T value) {
49 | this.getDatabaseCollection(value.getClass()).findOneAndUpdate(
50 | Filters.eq("_id", Document.parse(this.gson.toJson(value)).get("_id")),
51 | Document.parse(this.gson.toJson(value))
52 | );
53 | }
54 |
55 | @Override
56 | public void updateOne(T value, Bson filters) {
57 | this.getDatabaseCollection(value.getClass()).findOneAndUpdate(filters, Document.parse(this.gson.toJson(value)));
58 | }
59 |
60 | @Override
61 | public void updateMany(List value) {
62 | value.forEach(this::updateOne);
63 | }
64 |
65 | @Override
66 | public void updateMany(List value, Bson filters) {
67 | value.forEach(object -> this.updateOne(object, filters));
68 | }
69 |
70 | @Override
71 | public List findMany(Class clazz, Bson filters) {
72 | FindIterable documents = this.getDatabaseCollection(clazz)
73 | .find(filters);
74 | List result = new ArrayList<>();
75 | for (Document document : documents) {
76 | T instance = this.gson.fromJson(document.toJson(), clazz);
77 | result.add(instance);
78 | }
79 | return result;
80 | }
81 |
82 | @Override
83 | public T findOne(Class clazz, Bson filters) {
84 | Document document = this.getDatabaseCollection(clazz)
85 | .find(filters)
86 | .first();
87 | if (document == null) {
88 | return null;
89 | }
90 | return this.gson.fromJson(document.toJson(), clazz);
91 | }
92 |
93 | @Override
94 | public boolean isExists(Class clazz, Bson filters) {
95 | return this.getDatabaseCollection(clazz).countDocuments(filters) > 0;
96 | }
97 |
98 | @Override
99 | public MongoCollection getDatabaseCollection(Class> value) {
100 | return !value.isAnnotationPresent(MongodbEntity.class) ? null : this.getMongodbClient()
101 | .getDatabase(value.getAnnotation(MongodbEntity.class).database())
102 | .getCollection(value.getAnnotation(MongodbEntity.class).collection());
103 | }
104 |
105 | public MongoClient getMongodbClient() {
106 | return this.client;
107 | }
108 | }
109 |
--------------------------------------------------------------------------------
/src/main/java/dev/quosty/mongo/annotation/MongodbEntity.java:
--------------------------------------------------------------------------------
1 | package dev.quosty.mongo.annotation;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Retention;
5 | import java.lang.annotation.RetentionPolicy;
6 | import java.lang.annotation.Target;
7 |
8 | @Retention(RetentionPolicy.RUNTIME)
9 | @Target(ElementType.TYPE)
10 | public @interface MongodbEntity {
11 |
12 | String collection() default "example";
13 |
14 | String database() default "example";
15 |
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/dev/quosty/mongo/helper/GsonHelper.java:
--------------------------------------------------------------------------------
1 | package dev.quosty.mongo.helper;
2 |
3 | import com.google.gson.Gson;
4 | import com.google.gson.GsonBuilder;
5 |
6 | public class GsonHelper {
7 |
8 | protected static final Gson gson = new GsonBuilder()
9 | .disableHtmlEscaping()
10 | .serializeNulls()
11 | .create();
12 |
13 | public static Gson getGson() {
14 | return gson;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/main/java/dev/quosty/mongo/impl/MongodbWrapper.java:
--------------------------------------------------------------------------------
1 | package dev.quosty.mongo.impl;
2 |
3 | import com.mongodb.client.MongoCollection;
4 | import org.bson.Document;
5 | import org.bson.conversions.Bson;
6 |
7 | import java.util.List;
8 |
9 | public interface MongodbWrapper {
10 |
11 | void insertOne(T value);
12 |
13 | void insertMany(List value);
14 |
15 | void deleteOne(T value);
16 |
17 | void deleteMany(T value, Bson filters);
18 |
19 | void updateOne(T value);
20 |
21 | void updateOne(T value, Bson filters);
22 |
23 | void updateMany(List value);
24 |
25 | void updateMany(List value, Bson filters);
26 |
27 | T findOne(Class clazz, Bson filters);
28 |
29 | List findMany(Class clazz, Bson filters);
30 |
31 | boolean isExists(Class clazz, Bson filters);
32 |
33 | MongoCollection getDatabaseCollection(Class> value);
34 |
35 |
36 | }
--------------------------------------------------------------------------------