Skip to content

Commit 7f79860

Browse files
Improving SDK interoperability
1 parent 8788cb5 commit 7f79860

File tree

6 files changed

+83
-15
lines changed

6 files changed

+83
-15
lines changed

src/document.js

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,25 +1332,39 @@ function validateFieldValue(obj, options, depth) {
13321332
);
13331333
}
13341334

1335-
if (obj === FieldValue.DELETE_SENTINEL) {
1336-
if (!options.allowNestedDeletes && (!options.allowDeletes || depth > 1)) {
1337-
throw new Error(
1338-
'Deletes must appear at the top-level and can only be used in update() or set() with {merge:true}.'
1339-
);
1335+
if (DocumentTransform.isTransformSentinel(obj)) {
1336+
if (obj === FieldValue.DELETE_SENTINEL) {
1337+
if (!options.allowNestedDeletes && (!options.allowDeletes || depth > 1)) {
1338+
throw new Error(
1339+
'Deletes must appear at the top-level and can only be used in update() or set() with {merge:true}.'
1340+
);
1341+
}
13401342
}
1341-
}
1342-
1343-
if (isPlainObject(obj)) {
1343+
} else if (is.array(obj)) {
1344+
for (let prop of obj) {
1345+
validateFieldValue(obj[prop], options, depth + 1);
1346+
}
1347+
} else if (isPlainObject(obj)) {
13441348
for (let prop in obj) {
13451349
if (obj.hasOwnProperty(prop)) {
13461350
validateFieldValue(obj[prop], options, depth + 1);
13471351
}
13481352
}
1349-
}
1350-
if (is.array(obj)) {
1351-
for (let prop of obj) {
1352-
validateFieldValue(obj[prop], options, depth + 1);
1353+
} else if (is.object(obj)) {
1354+
if (is.instanceof(obj, DocumentReference)) {
1355+
return true;
1356+
} else if (is.instanceof(obj, FieldValue)) {
1357+
return true;
13531358
}
1359+
1360+
validate.throwVersionMismatchErrorIfType(obj, DocumentReference);
1361+
validate.throwVersionMismatchErrorIfType(obj, FieldValue);
1362+
1363+
throw new Error(
1364+
'Cannot use type (' +
1365+
Object.prototype.toString.call(obj) +
1366+
') as a Firestore Value'
1367+
);
13541368
}
13551369

13561370
return true;

src/field-value.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
* Sentinel value for a field delete.
2121
*
2222
*/
23-
const DELETE_SENTINEL = {};
23+
let DELETE_SENTINEL;
2424

2525
/*!
2626
* Sentinel value for a server timestamp.
2727
*
2828
*/
29-
const SERVER_TIMESTAMP_SENTINEL = {};
29+
let SERVER_TIMESTAMP_SENTINEL;
3030

3131
/**
3232
* Sentinel values that can be used when writing documents with set() or
@@ -35,6 +35,7 @@ const SERVER_TIMESTAMP_SENTINEL = {};
3535
* @class
3636
*/
3737
class FieldValue {
38+
constructor() {}
3839
/**
3940
* Returns a sentinel used with update() to mark a field for deletion.
4041
*
@@ -76,6 +77,18 @@ class FieldValue {
7677
}
7778
}
7879

80+
/*!
81+
* Sentinel value for a field delete.
82+
*
83+
*/
84+
DELETE_SENTINEL = new FieldValue();
85+
86+
/*!
87+
* Sentinel value for a server timestamp.
88+
*
89+
*/
90+
SERVER_TIMESTAMP_SENTINEL = new FieldValue();
91+
7992
module.exports = FieldValue;
8093
module.exports.DELETE_SENTINEL = DELETE_SENTINEL;
8194
module.exports.SERVER_TIMESTAMP_SENTINEL = SERVER_TIMESTAMP_SENTINEL;

src/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,31 @@ Transaction = require('./transaction')(Firestore);
11471147
* region_tag:firestore_quickstart
11481148
* Full quickstart example:
11491149
*/
1150+
1151+
const version = require('../package.json').version;
1152+
1153+
if (!is.defined(global.__FIRESTORE_DATA)) {
1154+
global.__FIRESTORE_DATA = {
1155+
version: version,
1156+
path: __dirname,
1157+
};
1158+
} else if (is.defined(global.__FIRESTORE_DATA)) {
1159+
if (global.__FIRESTORE_DATA.version !== version) {
1160+
console.warn(
1161+
`Importing multiple versions @google-cloud/firestore can cause unexpected ` +
1162+
`behavior. If you are using Firestore through a transitive ` +
1163+
`dependency (such as the Firebase Admin SDK), please ensure that your ` +
1164+
`dependency versions match (found ${
1165+
global.__FIRESTORE_DATA.version
1166+
} and ` +
1167+
`${version}). For more information, see http://goo.gl/...`
1168+
);
1169+
} else if (__dirname !== global.__FIRESTORE_DATA.path) {
1170+
module.exports = require(global.__FIRESTORE_DATA.path);
1171+
return;
1172+
}
1173+
}
1174+
11501175
module.exports = Firestore;
11511176
module.exports.default = Firestore;
11521177
module.exports.Firestore = Firestore;

src/path.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ class FieldPath extends Path {
483483
*/
484484
static validateFieldPath(fieldPath) {
485485
if (!is.instanceof(fieldPath, FieldPath)) {
486+
validate.throwVersionMismatchErrorIfType(fieldPath, FieldPath);
487+
486488
if (!is.string(fieldPath)) {
487489
throw new Error(`Paths must be strings or FieldPath objects.`);
488490
}

src/reference.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2089,7 +2089,11 @@ function validateComparisonOperator(str, val) {
20892089
* @returns 'true' is value is an instance of DocumentReference.
20902090
*/
20912091
function validateDocumentReference(value) {
2092-
return is.instanceof(value, DocumentReference);
2092+
if (is.instanceof(value, DocumentReference)) {
2093+
return true;
2094+
}
2095+
validate.verifyConstructorName(value, DocumentReference);
2096+
return false;
20932097
}
20942098

20952099
module.exports = FirestoreType => {

src/validate.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,15 @@ module.exports = validators => {
140140
return true;
141141
};
142142

143+
exports.throwVersionMismatchErrorIfType = (obj, classType) => {
144+
if (is.object(obj) && obj.constructor.name === classType.name) {
145+
throw new Error(
146+
`Detected an object of type '${
147+
obj.constructor.name
148+
}' that doesn't match the expected version for this Firestore instance. For more information, please visit: http://go...`
149+
);
150+
}
151+
};
152+
143153
return exports;
144154
};

0 commit comments

Comments
 (0)