@pbvision/firestore-orm 0.0.26 → 0.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pbvision/firestore-orm",
3
- "version": "0.0.26",
3
+ "version": "0.0.27",
4
4
  "description": "Firestore ORM Library",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
package/src/context.js CHANGED
@@ -405,10 +405,23 @@ export class Context {
405
405
  await model.__write(this)
406
406
  }
407
407
 
408
- __throwIfWritesNotAllowed (data) {
409
- if (this.options.readOnly) {
410
- throw new WriteAttemptedInReadOnlyTxError(data)
408
+ __throwIfWritesNotAllowed (modelOrKey) {
409
+ if (!this.options.readOnly) {
410
+ return
411
+ }
412
+ let key = modelOrKey
413
+ let fieldNames = []
414
+ if (modelOrKey instanceof Model) {
415
+ key = modelOrKey.__key
416
+ fieldNames = Object.values(modelOrKey.__cached_attrs)
417
+ .filter(field => field.hasChangesToCommit(false))
418
+ .map(field => field.name)
411
419
  }
420
+ // delete() checks for read-only before it checks its args
421
+ const doc = key instanceof Key
422
+ ? `${key.Cls.collectionName} _id=${key.encodedKey}`
423
+ : 'an unknown doc'
424
+ throw new WriteAttemptedInReadOnlyTxError(doc, fieldNames)
412
425
  }
413
426
 
414
427
  /**
package/src/errors.js CHANGED
@@ -135,12 +135,20 @@ export class DeletedTwiceError extends GenericModelError {
135
135
 
136
136
  /**
137
137
  * Thrown when a tx tries to write when it was marked read-only.
138
+ *
139
+ * Names the doc and the fields it would have changed, never their values: a
140
+ * model can hold secrets, and this message ends up in logs and error reports
141
+ * (and, through an error handler that returns crash messages, in API
142
+ * responses).
138
143
  * @memberof Errors
139
144
  */
140
145
  export class WriteAttemptedInReadOnlyTxError extends Error {
141
- constructor (dataToWrite) {
142
- super(`Tried to write model in a read-only transaction with the following
143
- changes ${JSON.stringify(dataToWrite)}`)
146
+ constructor (doc, fieldNames) {
147
+ const fields = fieldNames.length
148
+ ? ` (changed fields: ${fieldNames.join(', ')})`
149
+ : ''
150
+ super(`Tried to write ${doc} in a read-only transaction${fields}`)
151
+ this.name = this.constructor.name
144
152
  }
145
153
  }
146
154