@firestore-emulator/server
Version:
This package is the implementation of the Firestore emulator. It is a Node.js
406 lines • 12.7 kB
JavaScript
// src/FirestoreState/field.ts
import { NullValue } from "@firestore-emulator/proto/dist/google/protobuf/struct";
var FirestoreStateDocumentStringField = class _FirestoreStateDocumentStringField {
constructor(value) {
this.value = value;
}
type = "string_value";
toJSON() {
return { type: this.type, value: this.value };
}
toV1ValueObject() {
return { string_value: this.value };
}
eq(other) {
return other instanceof _FirestoreStateDocumentStringField && this.value === other.value;
}
lt(other) {
return other instanceof _FirestoreStateDocumentStringField && this.value < other.value;
}
lte(other) {
return other instanceof _FirestoreStateDocumentStringField && this.value <= other.value;
}
gt(other) {
return other instanceof _FirestoreStateDocumentStringField && this.value > other.value;
}
gte(other) {
return other instanceof _FirestoreStateDocumentStringField && this.value >= other.value;
}
};
var FirestoreStateDocumentNullField = class {
type = "null_value";
value = null;
toJSON() {
return { type: this.type, value: null };
}
toV1ValueObject() {
return { null_value: NullValue.NULL_VALUE };
}
eq(other) {
return other.type === this.type;
}
lt(_other) {
return false;
}
lte(_other) {
return false;
}
gt(_other) {
return false;
}
gte(_other) {
return false;
}
};
var FirestoreStateDocumentBooleanField = class _FirestoreStateDocumentBooleanField {
constructor(value) {
this.value = value;
}
type = "boolean_value";
toJSON() {
return { type: this.type, value: this.value };
}
toV1ValueObject() {
return { boolean_value: this.value };
}
eq(other) {
return other instanceof _FirestoreStateDocumentBooleanField && this.value === other.value;
}
lt(_other) {
return false;
}
lte(_other) {
return false;
}
gt(_other) {
return false;
}
gte(_other) {
return false;
}
};
var FirestoreStateDocumentIntegerField = class _FirestoreStateDocumentIntegerField {
constructor(value) {
this.value = value;
if (!Number.isInteger(value)) {
throw new Error(`value must be integer. value=${value}`);
}
}
type = "integer_value";
toJSON() {
return { type: this.type, value: this.value };
}
toV1ValueObject() {
return { integer_value: this.value };
}
eq(other) {
return other instanceof _FirestoreStateDocumentIntegerField && this.value === other.value;
}
lt(other) {
return other instanceof _FirestoreStateDocumentIntegerField && this.value < other.value;
}
lte(other) {
return other instanceof _FirestoreStateDocumentIntegerField && this.value <= other.value;
}
gt(other) {
return other instanceof _FirestoreStateDocumentIntegerField && this.value > other.value;
}
gte(other) {
return other instanceof _FirestoreStateDocumentIntegerField && this.value >= other.value;
}
add(other) {
if (other instanceof _FirestoreStateDocumentIntegerField) {
return new _FirestoreStateDocumentIntegerField(this.value + other.value);
}
if (other instanceof FirestoreStateDocumentDoubleField) {
return new FirestoreStateDocumentDoubleField(this.value + other.value);
}
throw new Error(`unsupported type. other=${other}`);
}
};
var FirestoreStateDocumentDoubleField = class _FirestoreStateDocumentDoubleField {
constructor(value) {
this.value = value;
}
type = "double_value";
toJSON() {
return { type: this.type, value: this.value };
}
toV1ValueObject() {
return { double_value: this.value };
}
eq(other) {
return other instanceof _FirestoreStateDocumentDoubleField && this.value === other.value;
}
lt(other) {
return other instanceof _FirestoreStateDocumentDoubleField && this.value < other.value;
}
lte(other) {
return other instanceof _FirestoreStateDocumentDoubleField && this.value <= other.value;
}
gt(other) {
return other instanceof _FirestoreStateDocumentDoubleField && this.value > other.value;
}
gte(other) {
return other instanceof _FirestoreStateDocumentDoubleField && this.value >= other.value;
}
add(other) {
if (other instanceof FirestoreStateDocumentIntegerField) {
return new _FirestoreStateDocumentDoubleField(this.value + other.value);
}
if (other instanceof _FirestoreStateDocumentDoubleField) {
return new _FirestoreStateDocumentDoubleField(this.value + other.value);
}
throw new Error(`unsupported type. other=${other}`);
}
};
var FirestoreStateDocumentTimestampField = class _FirestoreStateDocumentTimestampField {
constructor(value) {
this.value = value;
}
type = "timestamp_value";
static fromDate(date) {
return new _FirestoreStateDocumentTimestampField({
nanos: date.getTime() % 1e3 * 1e6,
seconds: Math.floor(date.getTime() / 1e3)
});
}
toJSON() {
return { type: this.type, value: this.value };
}
toV1ValueObject() {
return { timestamp_value: this.value };
}
eq(other) {
return other instanceof _FirestoreStateDocumentTimestampField && this.value.seconds === other.value.seconds && this.value.nanos === other.value.nanos;
}
lt(other) {
return other instanceof _FirestoreStateDocumentTimestampField && (this.value.seconds < other.value.seconds || this.value.seconds === other.value.seconds && this.value.nanos < other.value.nanos);
}
lte(other) {
return other instanceof _FirestoreStateDocumentTimestampField && (this.value.seconds < other.value.seconds || this.value.seconds === other.value.seconds && this.value.nanos <= other.value.nanos);
}
gt(other) {
return other instanceof _FirestoreStateDocumentTimestampField && (this.value.seconds > other.value.seconds || this.value.seconds === other.value.seconds && this.value.nanos > other.value.nanos);
}
gte(other) {
return other instanceof _FirestoreStateDocumentTimestampField && (this.value.seconds > other.value.seconds || this.value.seconds === other.value.seconds && this.value.nanos >= other.value.nanos);
}
};
var FirestoreStateDocumentBytesField = class _FirestoreStateDocumentBytesField {
constructor(value) {
this.value = value;
}
type = "bytes_value";
toJSON() {
return { type: this.type, value: this.value };
}
toV1ValueObject() {
return { bytes_value: this.value };
}
eq(other) {
return other instanceof _FirestoreStateDocumentBytesField && this.value.toString() === other.value.toString();
}
lt(other) {
return other instanceof _FirestoreStateDocumentBytesField && this.value.toString() < other.value.toString();
}
lte(other) {
return other instanceof _FirestoreStateDocumentBytesField && this.value.toString() <= other.value.toString();
}
gt(other) {
return other instanceof _FirestoreStateDocumentBytesField && this.value.toString() > other.value.toString();
}
gte(other) {
return other instanceof _FirestoreStateDocumentBytesField && this.value.toString() >= other.value.toString();
}
};
var FirestoreStateDocumentReferenceField = class _FirestoreStateDocumentReferenceField {
constructor(value) {
this.value = value;
}
type = "reference_value";
toJSON() {
return { type: this.type, value: this.value };
}
toV1ValueObject() {
return { reference_value: this.value };
}
eq(other) {
return other instanceof _FirestoreStateDocumentReferenceField && this.value === other.value;
}
lt(_other) {
return false;
}
lte(_other) {
return false;
}
gt(_other) {
return false;
}
gte(_other) {
return false;
}
};
var FirestoreStateDocumentGeoPointField = class _FirestoreStateDocumentGeoPointField {
constructor(value) {
this.value = value;
}
type = "geo_point_value";
toJSON() {
return { type: this.type, value: this.value };
}
toV1ValueObject() {
return { geo_point_value: this.value };
}
eq(other) {
return other instanceof _FirestoreStateDocumentGeoPointField && this.value.latitude === other.value.latitude && this.value.longitude === other.value.longitude;
}
lt(other) {
return other instanceof _FirestoreStateDocumentGeoPointField && (this.value.latitude < other.value.latitude || this.value.longitude < other.value.longitude);
}
lte(other) {
return other instanceof _FirestoreStateDocumentGeoPointField && (this.value.latitude <= other.value.latitude || this.value.longitude <= other.value.longitude);
}
gt(other) {
return other instanceof _FirestoreStateDocumentGeoPointField && (this.value.latitude > other.value.latitude || this.value.longitude > other.value.longitude);
}
gte(other) {
return other instanceof _FirestoreStateDocumentGeoPointField && (this.value.latitude >= other.value.latitude || this.value.longitude >= other.value.longitude);
}
};
var FirestoreStateDocumentArrayField = class _FirestoreStateDocumentArrayField {
constructor(value) {
this.value = value;
}
type = "array_value";
toJSON() {
return {
type: this.type,
value: this.value.map((v) => v.toJSON())
};
}
toV1ValueObject() {
return {
array_value: { values: this.value.map((v) => v.toV1ValueObject()) }
};
}
eq(other) {
return other instanceof _FirestoreStateDocumentArrayField && this.value.length === other.value.length && this.value.every((v, i) => {
const item = other.value[i];
if (!item) return false;
return item.eq(v);
});
}
lt(_other) {
return false;
}
lte(_other) {
return false;
}
gt(_other) {
return false;
}
gte(_other) {
return false;
}
};
var FirestoreStateDocumentMapField = class _FirestoreStateDocumentMapField {
constructor(value) {
this.value = value;
}
type = "map_value";
toJSON() {
return {
type: this.type,
value: Object.fromEntries(
Object.entries(this.value).map(([k, v]) => [k, v.toJSON()])
)
};
}
toV1ValueObject() {
return {
map_value: {
fields: Object.fromEntries(
Object.entries(this.value).map(([k, v]) => [k, v.toV1ValueObject()])
)
}
};
}
eq(other) {
return other instanceof _FirestoreStateDocumentMapField && Object.keys(this.value).length === Object.keys(other.value).length && Object.entries(this.value).every(([k, v]) => {
const item = other.value[k];
if (!item) return false;
return item.eq(v);
});
}
lt(_other) {
return false;
}
lte(_other) {
return false;
}
gt(_other) {
return false;
}
gte(_other) {
return false;
}
};
var convertV1DocumentField = (field) => {
if (field.has_string_value)
return new FirestoreStateDocumentStringField(field.string_value);
if (field.has_null_value) return new FirestoreStateDocumentNullField();
if (field.has_boolean_value)
return new FirestoreStateDocumentBooleanField(field.boolean_value);
if (field.has_integer_value)
return new FirestoreStateDocumentIntegerField(field.integer_value);
if (field.has_double_value)
return new FirestoreStateDocumentDoubleField(field.double_value);
if (field.has_timestamp_value)
return new FirestoreStateDocumentTimestampField({
nanos: field.timestamp_value.nanos,
seconds: field.timestamp_value.seconds
});
if (field.has_bytes_value)
return new FirestoreStateDocumentBytesField(field.bytes_value);
if (field.has_reference_value)
return new FirestoreStateDocumentReferenceField(field.reference_value);
if (field.has_geo_point_value)
return new FirestoreStateDocumentGeoPointField({
latitude: field.geo_point_value.latitude,
longitude: field.geo_point_value.longitude
});
if (field.has_array_value)
return new FirestoreStateDocumentArrayField(
field.array_value.values.map(convertV1DocumentField)
);
if (field.has_map_value)
return new FirestoreStateDocumentMapField(
Object.fromEntries(
Array.from(field.map_value.fields.entries()).map(([k, v]) => [
k,
convertV1DocumentField(v)
])
)
);
throw new Error(`unknown field type. field=${JSON.stringify(field)}`);
};
var convertV1Value = (value) => {
return convertV1DocumentField(value).toV1ValueObject();
};
export {
FirestoreStateDocumentArrayField,
FirestoreStateDocumentBooleanField,
FirestoreStateDocumentBytesField,
FirestoreStateDocumentDoubleField,
FirestoreStateDocumentGeoPointField,
FirestoreStateDocumentIntegerField,
FirestoreStateDocumentMapField,
FirestoreStateDocumentNullField,
FirestoreStateDocumentReferenceField,
FirestoreStateDocumentStringField,
FirestoreStateDocumentTimestampField,
convertV1DocumentField,
convertV1Value
};
//# sourceMappingURL=field.mjs.map