summaryrefslogtreecommitdiff
path: root/dsa/FireBase/Offline/DatabaseExtensions.cs
blob: e7c4074e10e867bce89fab4c912f59f9a3c9a814 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using Firebase.Database.Query;

namespace Firebase.Database.Offline
{
    public static class DatabaseExtensions
    {
        /// <summary>
        ///     Create new instances of the <see cref="RealtimeDatabase{T}" />.
        /// </summary>
        /// <typeparam name="T"> Type of elements. </typeparam>
        /// <param name="filenameModifier"> Custom string which will get appended to the file name. </param>
        /// <param name="elementRoot"> Optional custom root element of received json items. </param>
        /// <param name="streamingOptions"> Realtime streaming options. </param>
        /// <param name="initialPullStrategy"> Specifies what strategy should be used for initial pulling of server data. </param>
        /// <param name="pushChanges">
        ///     Specifies whether changed items should actually be pushed to the server. It this is false,
        ///     then Put / Post / Delete will not affect server data.
        /// </param>
        /// <returns> The <see cref="RealtimeDatabase{T}" />. </returns>
        public static RealtimeDatabase<T> AsRealtimeDatabase<T>(this ChildQuery query, string filenameModifier = "",
            string elementRoot = "", StreamingOptions streamingOptions = StreamingOptions.LatestOnly,
            InitialPullStrategy initialPullStrategy = InitialPullStrategy.MissingOnly, bool pushChanges = true)
            where T : class
        {
            return new RealtimeDatabase<T>(query, elementRoot, query.Client.Options.OfflineDatabaseFactory,
                filenameModifier, streamingOptions, initialPullStrategy, pushChanges);
        }

        /// <summary>
        ///     Create new instances of the <see cref="RealtimeDatabase{T}" />.
        /// </summary>
        /// <typeparam name="T"> Type of elements. </typeparam>
        /// <typeparam name="TSetHandler"> Type of the custom <see cref="ISetHandler{T}" /> to use. </typeparam>
        /// <param name="filenameModifier"> Custom string which will get appended to the file name. </param>
        /// <param name="elementRoot"> Optional custom root element of received json items. </param>
        /// <param name="streamingOptions"> Realtime streaming options. </param>
        /// <param name="initialPullStrategy"> Specifies what strategy should be used for initial pulling of server data. </param>
        /// <param name="pushChanges">
        ///     Specifies whether changed items should actually be pushed to the server. It this is false,
        ///     then Put / Post / Delete will not affect server data.
        /// </param>
        /// <returns> The <see cref="RealtimeDatabase{T}" />. </returns>
        public static RealtimeDatabase<T> AsRealtimeDatabase<T, TSetHandler>(this ChildQuery query,
            string filenameModifier = "", string elementRoot = "",
            StreamingOptions streamingOptions = StreamingOptions.LatestOnly,
            InitialPullStrategy initialPullStrategy = InitialPullStrategy.MissingOnly, bool pushChanges = true)
            where T : class
            where TSetHandler : ISetHandler<T>, new()
        {
            return new RealtimeDatabase<T>(query, elementRoot, query.Client.Options.OfflineDatabaseFactory,
                filenameModifier, streamingOptions, initialPullStrategy, pushChanges,
                Activator.CreateInstance<TSetHandler>());
        }

        /// <summary>
        ///     Overwrites existing object with given key leaving any missing properties intact in firebase.
        /// </summary>
        /// <param name="key"> The key. </param>
        /// <param name="obj"> The object to set. </param>
        /// <param name="syncOnline"> Indicates whether the item should be synced online. </param>
        /// <param name="priority">
        ///     The priority. Objects with higher priority will be synced first. Higher number indicates higher
        ///     priority.
        /// </param>
        public static void Patch<T>(this RealtimeDatabase<T> db, string key, T obj, bool syncOnline = true,
            int priority = 1)
            where T : class
        {
            db.Set(key, obj, syncOnline ? SyncOptions.Patch : SyncOptions.None, priority);
        }

        /// <summary>
        ///     Overwrites existing object with given key.
        /// </summary>
        /// <param name="key"> The key. </param>
        /// <param name="obj"> The object to set. </param>
        /// <param name="syncOnline"> Indicates whether the item should be synced online. </param>
        /// <param name="priority">
        ///     The priority. Objects with higher priority will be synced first. Higher number indicates higher
        ///     priority.
        /// </param>
        public static void Put<T>(this RealtimeDatabase<T> db, string key, T obj, bool syncOnline = true,
            int priority = 1)
            where T : class
        {
            db.Set(key, obj, syncOnline ? SyncOptions.Put : SyncOptions.None, priority);
        }

        /// <summary>
        ///     Adds a new entity to the Database.
        /// </summary>
        /// <param name="obj"> The object to add.  </param>
        /// <param name="syncOnline"> Indicates whether the item should be synced online. </param>
        /// <param name="priority">
        ///     The priority. Objects with higher priority will be synced first. Higher number indicates higher
        ///     priority.
        /// </param>
        /// <returns> The generated key for this object. </returns>
        public static string Post<T>(this RealtimeDatabase<T> db, T obj, bool syncOnline = true, int priority = 1)
            where T : class
        {
            var key = FirebaseKeyGenerator.Next();

            db.Set(key, obj, syncOnline ? SyncOptions.Put : SyncOptions.None, priority);

            return key;
        }

        /// <summary>
        ///     Deletes the entity with the given key.
        /// </summary>
        /// <param name="key"> The key. </param>
        /// <param name="syncOnline"> Indicates whether the item should be synced online. </param>
        /// <param name="priority">
        ///     The priority. Objects with higher priority will be synced first. Higher number indicates higher
        ///     priority.
        /// </param>
        public static void Delete<T>(this RealtimeDatabase<T> db, string key, bool syncOnline = true, int priority = 1)
            where T : class
        {
            db.Set(key, null, syncOnline ? SyncOptions.Put : SyncOptions.None, priority);
        }

        /// <summary>
        ///     Do a Put for a nested property specified by <paramref name="propertyExpression" /> of an object with key
        ///     <paramref name="key" />.
        /// </summary>
        /// <typeparam name="T"> Type of the root elements. </typeparam>
        /// <typeparam name="TProperty"> Type of the property being modified</typeparam>
        /// <param name="db"> Database instance. </param>
        /// <param name="key"> Key of the root element to modify. </param>
        /// <param name="propertyExpression"> Expression on the root element leading to target value to modify. </param>
        /// <param name="value"> Value to put. </param>
        /// <param name="syncOnline"> Indicates whether the item should be synced online. </param>
        /// <param name="priority">
        ///     The priority. Objects with higher priority will be synced first. Higher number indicates higher
        ///     priority.
        /// </param>
        public static void Put<T, TProperty>(this RealtimeDatabase<T> db, string key,
            Expression<Func<T, TProperty>> propertyExpression, TProperty value, bool syncOnline = true,
            int priority = 1)
            where T : class
        {
            db.Set(key, propertyExpression, value, syncOnline ? SyncOptions.Put : SyncOptions.None, priority);
        }

        /// <summary>
        ///     Do a Patch for a nested property specified by <paramref name="propertyExpression" /> of an object with key
        ///     <paramref name="key" />.
        /// </summary>
        /// <typeparam name="T"> Type of the root elements. </typeparam>
        /// <typeparam name="TProperty"> Type of the property being modified</typeparam>
        /// <param name="db"> Database instance. </param>
        /// <param name="key"> Key of the root element to modify. </param>
        /// <param name="propertyExpression"> Expression on the root element leading to target value to modify. </param>
        /// <param name="value"> Value to patch. </param>
        /// <param name="syncOnline"> Indicates whether the item should be synced online. </param>
        /// <param name="priority">
        ///     The priority. Objects with higher priority will be synced first. Higher number indicates higher
        ///     priority.
        /// </param>
        public static void Patch<T, TProperty>(this RealtimeDatabase<T> db, string key,
            Expression<Func<T, TProperty>> propertyExpression, TProperty value, bool syncOnline = true,
            int priority = 1)
            where T : class
        {
            db.Set(key, propertyExpression, value, syncOnline ? SyncOptions.Patch : SyncOptions.None, priority);
        }

        /// <summary>
        ///     Delete a nested property specified by <paramref name="propertyExpression" /> of an object with key
        ///     <paramref name="key" />. This basically does a Put with null value.
        /// </summary>
        /// <typeparam name="T"> Type of the root elements. </typeparam>
        /// <typeparam name="TProperty"> Type of the property being modified</typeparam>
        /// <param name="db"> Database instance. </param>
        /// <param name="key"> Key of the root element to modify. </param>
        /// <param name="propertyExpression"> Expression on the root element leading to target value to modify. </param>
        /// <param name="value"> Value to put. </param>
        /// <param name="syncOnline"> Indicates whether the item should be synced online. </param>
        /// <param name="priority">
        ///     The priority. Objects with higher priority will be synced first. Higher number indicates higher
        ///     priority.
        /// </param>
        public static void Delete<T, TProperty>(this RealtimeDatabase<T> db, string key,
            Expression<Func<T, TProperty>> propertyExpression, bool syncOnline = true, int priority = 1)
            where T : class
            where TProperty : class
        {
            db.Set(key, propertyExpression, null, syncOnline ? SyncOptions.Put : SyncOptions.None, priority);
        }

        /// <summary>
        ///     Post a new entity into the nested dictionary specified by <paramref name="propertyExpression" /> of an object with
        ///     key <paramref name="key" />.
        ///     The key of the new entity is automatically generated.
        /// </summary>
        /// <typeparam name="T"> Type of the root elements. </typeparam>
        /// <typeparam name="TSelector"> Type of the dictionary being modified</typeparam>
        /// <typeparam name="TProperty"> Type of the value within the dictionary being modified</typeparam>
        /// <param name="db"> Database instance. </param>
        /// <param name="key"> Key of the root element to modify. </param>
        /// <param name="propertyExpression"> Expression on the root element leading to target value to modify. </param>
        /// <param name="value"> Value to put. </param>
        /// <param name="syncOnline"> Indicates whether the item should be synced online. </param>
        /// <param name="priority">
        ///     The priority. Objects with higher priority will be synced first. Higher number indicates higher
        ///     priority.
        /// </param>
        public static void Post<T, TSelector, TProperty>(this RealtimeDatabase<T> db, string key,
            Expression<Func<T, TSelector>> propertyExpression, TProperty value, bool syncOnline = true,
            int priority = 1)
            where T : class
            where TSelector : IDictionary<string, TProperty>
        {
            var nextKey = FirebaseKeyGenerator.Next();
            var expression = Expression.Lambda<Func<T, TProperty>>(
                Expression.Call(propertyExpression.Body,
                    typeof(TSelector).GetRuntimeMethod("get_Item", new[] {typeof(string)}),
                    Expression.Constant(nextKey)), propertyExpression.Parameters);
            db.Set(key, expression, value, syncOnline ? SyncOptions.Put : SyncOptions.None, priority);
        }

        /// <summary>
        ///     Delete an entity with key <paramref name="dictionaryKey" /> in the nested dictionary specified by
        ///     <paramref name="propertyExpression" /> of an object with key <paramref name="key" />.
        ///     The key of the new entity is automatically generated.
        /// </summary>
        /// <typeparam name="T"> Type of the root elements. </typeparam>
        /// <typeparam name="TSelector"> Type of the dictionary being modified</typeparam>
        /// <typeparam name="TProperty"> Type of the value within the dictionary being modified</typeparam>
        /// <param name="db"> Database instance. </param>
        /// <param name="key"> Key of the root element to modify. </param>
        /// <param name="propertyExpression"> Expression on the root element leading to target value to modify. </param>
        /// <param name="dictionaryKey"> Key within the nested dictionary to delete. </param>
        /// <param name="syncOnline"> Indicates whether the item should be synced online. </param>
        /// <param name="priority">
        ///     The priority. Objects with higher priority will be synced first. Higher number indicates higher
        ///     priority.
        /// </param>
        public static void Delete<T, TProperty>(this RealtimeDatabase<T> db, string key,
            Expression<Func<T, IDictionary<string, TProperty>>> propertyExpression, string dictionaryKey,
            bool syncOnline = true, int priority = 1)
            where T : class
        {
            var expression = Expression.Lambda<Func<T, TProperty>>(
                Expression.Call(propertyExpression.Body,
                    typeof(IDictionary<string, TProperty>).GetRuntimeMethod("get_Item", new[] {typeof(string)}),
                    Expression.Constant(dictionaryKey)), propertyExpression.Parameters);
            db.Set(key, expression, null, syncOnline ? SyncOptions.Put : SyncOptions.None, priority);
        }
    }
}