1 /**
2 * Copyright © DiamondMVC 2019
3 * License: MIT (https://github.com/DiamondMVC/Diamond/blob/master/LICENSE)
4 * Author: Jacob Jensen (bausshf)
5 */
6 module diamond.database.mongo.operations;
7 
8 import diamond.core.apptype;
9 
10 static if (isWeb)
11 {
12   import vibe.data.json;
13 
14   import diamond.errors.checks;
15   import diamond.database.mongo;
16 
17   /**
18   * Finds a single document.
19   * Params:
20   *   collection = The mongo db collection.
21   *   query =      The find query.
22   * Returns:
23   *   Returns a single document result if found.
24   */
25   T findSingle(T,TQuery)(string collection, TQuery query)
26   {
27     enforce(client !is null, "Mongodb has not been configured properly.");
28 
29     auto result = client.getCollection(collection).findOne!T(query);
30 
31     if (!result.isNull)
32     {
33       return result.get!T;
34     }
35 
36     return T.ini;
37   }
38 
39   /**
40   * Finds a set of documents.
41   * Params:
42   *   collection = The mongo db collection.
43   *   query =      The find query.
44   * Returns:
45   *   Returns an array of the document results found.
46   */
47   T[] findMany(T,TQuery)(string collection, TQuery query)
48   {
49     enforce(client !is null, "Mongodb has not been configured properly.");
50 
51     import std.array : array;
52 
53     return client.getCollection(collection).find!T(query).array;
54   }
55 
56   /**
57   * Inserts a single document.
58   * Params:
59   *   collection = The mongo db collection.
60   *   document =   The document to insert.
61   */
62   void insertSingle(T)(string collection, T document)
63   {
64     enforce(client !is null, "Mongodb has not been configured properly.");
65 
66     client.getCollection(collection).insert(document);
67   }
68 
69   /**
70   * Inserts a set of documents.
71   * Params:
72   *   collection = The mongo db collection.
73   *   documents =   The documents to insert.
74   */
75   void insertMany(T)(string collection, T[] documents)
76   {
77     enforce(client !is null, "Mongodb has not been configured properly.");
78 
79     client.getCollection(collection).insert(documents);
80   }
81 
82   /**
83   * Updates a set of documents based on a query.
84   * Params:
85   *   collection = The mongo db collection.
86   *   query =      The query of the update.
87   *   update =     The data to update the document(s) with.
88   */
89   void update(TQuery,TUpdate)(string collection, TQuery query, TUpdate update)
90   {
91     enforce(client !is null, "Mongodb has not been configured properly.");
92 
93     client.getCollection(collection).update(query, update);
94   }
95 
96   /**
97   * Removes a set of documents based on a query.
98   * Params:
99   *   collection = The mongo db collection.
100   *   query =      The query of the update.
101   */
102   void remove(TQuery,TUpdate)(string collection, TQuery query)
103   {
104     enforce(client !is null, "Mongodb has not been configured properly.");
105 
106     client.getCollection(collection).remove(query);
107   }
108 }