Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(firestore): add timestamp & fieldvalue management #677

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions packages/firestore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,9 @@ Remove all listeners for this plugin.

#### DocumentData

Document data (for use with {@link @firebase/firestore/lite#(setDoc:1)}) consists of fields mapped to
values.


#### SetDocumentOptions

Expand Down Expand Up @@ -844,6 +847,9 @@ Remove all listeners for this plugin.

#### QueryFilterConstraint

<a href="#queryfilterconstraint">`QueryFilterConstraint`</a> is a helper union type that represents
{@link <a href="#queryfieldfilterconstraint">QueryFieldFilterConstraint</a>} and {@link <a href="#querycompositefilterconstraint">QueryCompositeFilterConstraint</a>}.

<code><a href="#queryfieldfilterconstraint">QueryFieldFilterConstraint</a> | <a href="#querycompositefilterconstraint">QueryCompositeFilterConstraint</a></code>


Expand All @@ -854,11 +860,22 @@ Remove all listeners for this plugin.

#### QueryNonFilterConstraint

<a href="#querynonfilterconstraint">`QueryNonFilterConstraint`</a> is a helper union type that represents
QueryConstraints which are used to narrow or order the set of documents,
but that do not explicitly filter on a document field.
`QueryNonFilterConstraint`s are created by invoking {@link orderBy},
{@link (startAt:1)}, {@link (startAfter:1)}, {@link (endBefore:1)}, {@link (endAt:1)},
{@link limit} or {@link limitToLast} and can then be passed to {@link (query:1)}
to create a new query instance that also contains the `QueryConstraint`.

<code><a href="#queryorderbyconstraint">QueryOrderByConstraint</a> | <a href="#querylimitconstraint">QueryLimitConstraint</a> | <a href="#querystartatconstraint">QueryStartAtConstraint</a> | <a href="#queryendatconstraint">QueryEndAtConstraint</a></code>


#### OrderByDirection

The direction of a {@link orderBy} clause is specified as 'desc' or 'asc'
(descending or ascending).

<code>'desc' | 'asc'</code>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.constraints.QueryLimitConstraint;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.constraints.QueryOrderByConstraint;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.constraints.QueryStartAtConstraint;
import io.capawesome.capacitorjs.plugins.firebase.firestore.classes.fields.FirestoreField;
import io.capawesome.capacitorjs.plugins.firebase.firestore.interfaces.QueryNonFilterConstraint;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -45,7 +46,7 @@ public static JSObject createJSObjectFromMap(@Nullable Map<String, Object> map)
} else if (value instanceof Map) {
value = createJSObjectFromMap((Map<String, Object>) value);
}
object.put(key, value);
object.put(key, parseObject(value));
}
return object;
}
Expand All @@ -54,7 +55,7 @@ public static Object createObjectFromJSValue(Object value) throws JSONException
if (value.toString().equals("null")) {
return null;
} else if (value instanceof JSONObject) {
return createHashMapFromJSONObject((JSONObject) value);
return createObjectFromJSONObject((JSONObject) value);
} else if (value instanceof JSONArray) {
return createArrayListFromJSONArray((JSONArray) value);
} else {
Expand Down Expand Up @@ -103,12 +104,12 @@ public static QueryNonFilterConstraint[] createQueryNonFilterConstraintArrayFrom
}
}

private static ArrayList<Object> createArrayListFromJSONArray(JSONArray array) throws JSONException {
public static ArrayList<Object> createArrayListFromJSONArray(JSONArray array) throws JSONException {
ArrayList<Object> arrayList = new ArrayList<>();
for (int x = 0; x < array.length(); x++) {
Object value = array.get(x);
if (value instanceof JSONObject) {
value = createHashMapFromJSONObject((JSONObject) value);
value = createObjectFromJSONObject((JSONObject) value);
} else if (value instanceof JSONArray) {
value = createArrayListFromJSONArray((JSONArray) value);
}
Expand All @@ -123,7 +124,7 @@ private static JSArray createJSArrayFromArrayList(ArrayList arrayList) {
if (value instanceof Map) {
value = createJSObjectFromMap((Map<String, Object>) value);
}
array.put(value);
array.put(parseObject(value));
}
return array;
}
Expand All @@ -134,4 +135,27 @@ public static JSObject createSnapshotMetadataResult(DocumentSnapshot snapshot) {
obj.put("hasPendingWrites", snapshot.getMetadata().hasPendingWrites());
return obj;
}

private static Object createObjectFromJSONObject(@NonNull JSONObject object) throws JSONException {
if (FirestoreField.isFirestoreField(object)) {
FirestoreField field = FirestoreField.fromJSONObject(object);
return field.getField();
}

return createHashMapFromJSONObject(object);
}

/**
* Parse an object to return it's firestore field value. Else return the same object.
*/
private static Object parseObject(Object object) {
if (object == null) {
return null;
}
try {
return FirestoreField.fromObject(object).getJSObject();
} catch (Exception e) {
return object;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.capawesome.capacitorjs.plugins.firebase.firestore.classes.fields;

import static com.google.firebase.firestore.FieldValue.arrayRemove;
import static com.google.firebase.firestore.FieldValue.arrayUnion;
import static com.google.firebase.firestore.FieldValue.delete;
import static com.google.firebase.firestore.FieldValue.increment;
import static com.google.firebase.firestore.FieldValue.serverTimestamp;

import androidx.annotation.NonNull;
import io.capawesome.capacitorjs.plugins.firebase.firestore.FirebaseFirestoreHelper;
import io.capawesome.capacitorjs.plugins.firebase.firestore.enums.FieldValueMethod;
import org.json.JSONException;
import org.json.JSONObject;

public class FieldValue {

@NonNull
FieldValueMethod method;

Object[] args;

public FieldValue(@NonNull FieldValueMethod method, Object[] args) {
this.method = method;
this.args = args;
}

public static FieldValue fromJSONObject(@NonNull JSONObject value) throws JSONException {
return new FieldValue(
FieldValueMethod.fromString(value.getString("method")),
FirebaseFirestoreHelper.createArrayListFromJSONArray(value.getJSONArray("args")).toArray()
);
}

public Object getField() {
return switch (method) {
case ARRAY_REMOVE -> arrayRemove(this.args);
case ARRAY_UNION -> arrayUnion(this.args);
case DELETE_FIELD -> delete();
case INCREMENT -> this.args[0] instanceof Double
? increment((Double) this.args[0])
: increment(((Integer) this.args[0]).longValue());
case SERVER_TIMESTAMP -> serverTimestamp();
default -> null;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package io.capawesome.capacitorjs.plugins.firebase.firestore.classes.fields;

import androidx.annotation.NonNull;
import com.getcapacitor.JSObject;
import io.capawesome.capacitorjs.plugins.firebase.firestore.enums.FirestoreFieldType;
import org.json.JSONException;
import org.json.JSONObject;

public class FirestoreField {

@NonNull
private final FirestoreFieldType type;

@NonNull
private final JSONObject value;

private static final String FIRESTORE_FIELD_TYPE = "_capacitorFirestoreFieldType";
private static final String FIRESTORE_FIELD_VALUE = "_capacitorFirestoreFieldValue";

/**
* Is a JSONObject a serialized Firestore field
* @param firestoreFieldData
* @return
*/
public static boolean isFirestoreField(JSONObject firestoreFieldData) {
return firestoreFieldData.has(FIRESTORE_FIELD_TYPE);
}

public FirestoreField(@NonNull FirestoreFieldType type, @NonNull JSONObject value) {
this.type = type;
this.value = value;
}

public static FirestoreField fromJSONObject(JSONObject data) throws JSONException {
FirestoreFieldType type = FirestoreFieldType.fromString((String) data.get(FIRESTORE_FIELD_TYPE));
JSONObject value = (JSONObject) data.get(FIRESTORE_FIELD_VALUE);
return new FirestoreField(type, value);
}

public static FirestoreField fromObject(Object object) throws IllegalArgumentException {
if (object instanceof com.google.firebase.Timestamp) {
Timestamp timestamp = Timestamp.fromFirestore((com.google.firebase.Timestamp) object);
return new FirestoreField(FirestoreFieldType.TIMESTAMP, timestamp.getValue());
}
throw new IllegalArgumentException("The provided object is not a firestore field");
}

public Object getField() throws JSONException {
return switch (type) {
case FIELD_VALUE -> FieldValue.fromJSONObject(value).getField();
case TIMESTAMP -> Timestamp.fromJSONObject(value).getField();
default -> null;
};
}

public JSObject getJSObject() throws JSONException {
JSObject object = new JSObject();
object.put(FIRESTORE_FIELD_TYPE, type.toString());
object.put(FIRESTORE_FIELD_VALUE, JSObject.fromJSONObject(value));
return object;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.capawesome.capacitorjs.plugins.firebase.firestore.classes.fields;

import androidx.annotation.NonNull;
import org.json.JSONException;
import org.json.JSONObject;

public class Timestamp {

long seconds;

int nanoseconds;

public Timestamp(long seconds, int nanoseconds) {
this.seconds = seconds;
this.nanoseconds = nanoseconds;
}

public static Timestamp fromJSONObject(@NonNull JSONObject value) throws JSONException {
return new Timestamp(((Number) value.get("seconds")).longValue(), (int) value.get("nanoseconds"));
}

public static Timestamp fromFirestore(@NonNull com.google.firebase.Timestamp timestamp) {
return new Timestamp(timestamp.getSeconds(), timestamp.getNanoseconds());
}

@NonNull
public JSONObject getValue() {
JSONObject value = new JSONObject();
try {
value.put("seconds", seconds);
value.put("nanoseconds", nanoseconds);
} catch (JSONException e) {}
return value;
}

public com.google.firebase.Timestamp getField() throws JSONException {
return new com.google.firebase.Timestamp(seconds, nanoseconds);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.capawesome.capacitorjs.plugins.firebase.firestore.enums;

import androidx.annotation.NonNull;

public enum FieldValueMethod {
ARRAY_REMOVE("arrayRemove"),
ARRAY_UNION("arrayUnion"),
DELETE_FIELD("deleteField"),
INCREMENT("increment"),
SERVER_TIMESTAMP("serverTimestamp");

private final String value;

private FieldValueMethod(String value) {
this.value = value;
}

private String getValue() {
return value;
}

@NonNull
@Override
public String toString() {
return this.getValue();
}

public static FieldValueMethod fromString(String value) {
for (FieldValueMethod v : values()) if (v.getValue().equalsIgnoreCase(value)) return v;
throw new IllegalArgumentException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.capawesome.capacitorjs.plugins.firebase.firestore.enums;

import androidx.annotation.NonNull;

public enum FirestoreFieldType {
FIELD_VALUE("fieldvalue"),
TIMESTAMP("timestamp");

private final String value;

private FirestoreFieldType(String value) {
this.value = value;
}

private String getValue() {
return value;
}

@NonNull
@Override
public String toString() {
return this.getValue();
}

public static FirestoreFieldType fromString(String value) {
for (FirestoreFieldType v : values()) if (v.getValue().equalsIgnoreCase(value)) return v;
throw new IllegalArgumentException();
}
}
Loading
Loading