@pbvision/firestore-orm 0.0.2 → 0.0.4

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,9 +1,13 @@
1
1
  {
2
2
  "name": "@pbvision/firestore-orm",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "Firestore ORM Library",
5
5
  "license": "Apache-2.0",
6
- "main": "src/default-db.js",
6
+ "type": "module",
7
+ "engines": {
8
+ "node": ">=20"
9
+ },
10
+ "exports": "./src/default-db.js",
7
11
  "scripts": {
8
12
  "build-doc": "./docs/build.sh",
9
13
  "check-markdown-link": "./scripts/ci/markdown-link.sh",
@@ -12,7 +16,7 @@
12
16
  "restart-local-db": "./scripts/start-local-db.sh --force-recreate",
13
17
  "setup": "yarn install --frozen-lockfile && pip install -r requirements.txt",
14
18
  "start-local-db": "./scripts/start-local-db.sh",
15
- "test": "./node_modules/jest/bin/jest.js --coverage --config=./jest.config.json"
19
+ "test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --coverage --config=./jest.config.json"
16
20
  },
17
21
  "contributors": [
18
22
  "Yu Guan",
@@ -36,21 +40,19 @@
36
40
  ],
37
41
  "dependencies": {
38
42
  "@google-cloud/firestore": "^7.1.0",
43
+ "@pbvision/schema": "^0.2.15",
39
44
  "deep-object-diff": "^1.1.7",
45
+ "eslint-import-resolver-webpack": "^0.13.8",
40
46
  "fast-deep-equal": "^3.1.3",
41
47
  "fast-json-stable-stringify": "^2.1.0",
42
48
  "rfdc": "^1.1.4",
43
49
  "uuid": "^8.3.2"
44
50
  },
45
- "peerDependencies": {
46
- "@pbvision/schema": "^0.1.4"
47
- },
48
51
  "devDependencies": {
49
52
  "@babel/core": "^7.17.12",
50
53
  "@babel/eslint-parser": "^7.17.0",
51
54
  "@babel/preset-env": "^7.17.12",
52
- "@pbvision/schema": "^0.1.4",
53
- "@pbvision/jest-unit-test": "^0.2.0",
55
+ "@pbvision/jest-unit-test": "^0.2.3",
54
56
  "eslint": "^8.22.0",
55
57
  "eslint-config-standard": "15.0.0",
56
58
  "eslint-plugin-import": "^2.22.0",
@@ -64,7 +66,8 @@
64
66
  "remark-validate-links": "^10.0.2",
65
67
  "snazzy": "^8.0.0",
66
68
  "standard": "^15.0.0",
67
- "tui-jsdoc-template": "^1.2.2"
69
+ "tui-jsdoc-template": "^1.2.2",
70
+ "webpack": "^5.89.0"
68
71
  },
69
72
  "standard": {
70
73
  "envs": [
@@ -1,6 +1,6 @@
1
- const uuidv4 = require('uuid').v4
1
+ import { v4 as uuidv4 } from 'uuid'
2
2
 
3
- class AsyncEmitter {
3
+ export class AsyncEmitter {
4
4
  constructor () {
5
5
  this.handlers = {}
6
6
  }
@@ -46,5 +46,3 @@ class AsyncEmitter {
46
46
  }
47
47
  }
48
48
  }
49
-
50
- module.exports = AsyncEmitter
package/src/context.js CHANGED
@@ -1,12 +1,12 @@
1
- const assert = require('assert')
1
+ import assert from 'node:assert'
2
2
 
3
- const { Transaction } = require('@google-cloud/firestore')
4
- const { detailedDiff } = require('deep-object-diff')
3
+ import { Transaction } from '@google-cloud/firestore'
4
+ import { detailedDiff } from 'deep-object-diff'
5
5
 
6
- const AsyncEmitter = require('./async-emitter')
7
- const { Data } = require('./data')
8
- const DBError = require('./db-error')
9
- const {
6
+ import { AsyncEmitter } from './async-emitter.js'
7
+ import { Data } from './data.js'
8
+ import { DBError } from './db-error.js'
9
+ import {
10
10
  InvalidOptionsError,
11
11
  InvalidParameterError,
12
12
  DeletedTwiceError,
@@ -15,12 +15,12 @@ const {
15
15
  ModelTrackedTwiceError,
16
16
  ModelAlreadyExistsError,
17
17
  TransactionLockTimeoutError
18
- } = require('./errors')
19
- const { Key } = require('./key')
20
- const { Model } = require('./models')
21
- const { sleep, loadOptionDefaults } = require('./utils')
18
+ } from './errors.js'
19
+ import { Key } from './key.js'
20
+ import { Model } from './models.js'
21
+ import { sleep, loadOptionDefaults } from './utils.js'
22
22
 
23
- async function getWithArgs (args, callback) {
23
+ export async function getWithArgs (args, callback) {
24
24
  if (!args || !(args instanceof Array) || args.length === 0) {
25
25
  throw new InvalidParameterError('args', 'must be a non-empty array')
26
26
  }
@@ -70,7 +70,7 @@ async function getWithArgs (args, callback) {
70
70
  * reads across multiple docs).
71
71
  * @public
72
72
  */
73
- class Context {
73
+ export class Context {
74
74
  /**
75
75
  * Options for interacting with Firestore.
76
76
  * @typedef {Object} ContextOptions
@@ -670,8 +670,3 @@ function parseFirestoreErrorPath (err) {
670
670
  }
671
671
  }
672
672
  }
673
-
674
- module.exports = {
675
- Context,
676
- getWithArgs
677
- }
package/src/data.js CHANGED
@@ -1,10 +1,10 @@
1
- const { Key } = require('./key')
1
+ import { Key } from './key.js'
2
2
 
3
3
  /**
4
4
  * Data includes a model's key and non-key fields.
5
5
  * @param {Object} [fields] field (non-key) values
6
6
  */
7
- class Data extends Key {
7
+ export class Data extends Key {
8
8
  constructor (Cls, encodedKey, keyComponents, fields) {
9
9
  super(Cls, encodedKey, keyComponents)
10
10
  this.data = fields
@@ -18,7 +18,3 @@ class Data extends Key {
18
18
  return { ...this.keyComponents, ...this.data }
19
19
  }
20
20
  }
21
-
22
- module.exports = {
23
- Data
24
- }
package/src/db-error.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // istanbul ignore file
2
- class DBError extends Error {
2
+ export class DBError extends Error {
3
3
  constructor (action, e) {
4
4
  super(`Failed to ${action} with error ${e}`)
5
5
  this.code = e.code
@@ -8,5 +8,3 @@ class DBError extends Error {
8
8
  this.allErrors = e.allErrors
9
9
  }
10
10
  }
11
-
12
- module.exports = DBError
package/src/default-db.js CHANGED
@@ -1,8 +1,8 @@
1
1
  // Convenient helper to setup our db connection using environment variables.
2
2
  // The constructed db instance will be cached by NodeJS.
3
- const Firestore = require('@google-cloud/firestore')
3
+ import Firestore from '@google-cloud/firestore'
4
4
 
5
- const setup = require('./firestore')
5
+ import setup from './firestore.js'
6
6
 
7
7
  // make Firestore look like the Transaction object (it already has getAll but
8
8
  // does not have these others)
@@ -18,4 +18,4 @@ const firestoreClient = new Firestore({
18
18
  ignoreUndefinedProperties: true
19
19
  })
20
20
 
21
- module.exports = setup(firestoreClient)
21
+ export default setup(firestoreClient)
package/src/errors.js CHANGED
@@ -8,7 +8,7 @@
8
8
  * @access public
9
9
  * @memberof Errors
10
10
  */
11
- class InvalidOptionsError extends Error {
11
+ export class InvalidOptionsError extends Error {
12
12
  constructor (option, expectation) {
13
13
  super(`Invalid option value for ${option}. ${expectation}.`)
14
14
  this.name = this.constructor.name
@@ -21,7 +21,7 @@ class InvalidOptionsError extends Error {
21
21
  * @access public
22
22
  * @memberof Errors
23
23
  */
24
- class InvalidParameterError extends Error {
24
+ export class InvalidParameterError extends Error {
25
25
  constructor (param, expectation) {
26
26
  super(`Invalid parameter ${param}. ${expectation}.`)
27
27
  this.name = this.constructor.name
@@ -34,7 +34,7 @@ class InvalidParameterError extends Error {
34
34
  * @access public
35
35
  * @memberof Errors
36
36
  */
37
- class InvalidFieldError extends Error {
37
+ export class InvalidFieldError extends Error {
38
38
  constructor (field, reason) {
39
39
  super(`${field} ${reason}`)
40
40
  this.name = this.constructor.name
@@ -51,7 +51,7 @@ class InvalidFieldError extends Error {
51
51
  * @access public
52
52
  * @memberof Errors
53
53
  */
54
- class TransactionFailedError extends Error {
54
+ export class TransactionFailedError extends Error {
55
55
  constructor (msg, originalException) {
56
56
  super(msg)
57
57
  this.name = this.constructor.name
@@ -62,7 +62,7 @@ class TransactionFailedError extends Error {
62
62
  }
63
63
  }
64
64
 
65
- class TransactionLockTimeoutError extends TransactionFailedError {
65
+ export class TransactionLockTimeoutError extends TransactionFailedError {
66
66
  constructor (reason, original) {
67
67
  super(reason, original)
68
68
  this.name = this.constructor.name
@@ -74,7 +74,7 @@ class TransactionLockTimeoutError extends TransactionFailedError {
74
74
  * Thrown when there's some error with a particular model.
75
75
  * @memberof Errors
76
76
  */
77
- class GenericModelError extends Error {
77
+ export class GenericModelError extends Error {
78
78
  constructor (msg, collection, _id) {
79
79
  super(`${msg}: ${collection} _id=${_id}`)
80
80
  this.name = this.constructor.name
@@ -87,7 +87,7 @@ class GenericModelError extends Error {
87
87
  * same key.
88
88
  * @memberof Errors
89
89
  */
90
- class ModelAlreadyExistsError extends GenericModelError {
90
+ export class ModelAlreadyExistsError extends GenericModelError {
91
91
  constructor (collection, _id) {
92
92
  super('Tried to recreate an existing model', collection, _id)
93
93
  }
@@ -96,7 +96,7 @@ class ModelAlreadyExistsError extends GenericModelError {
96
96
  /**
97
97
  * Thrown when model is tracked more than once inside a transaction.
98
98
  */
99
- class ModelTrackedTwiceError extends GenericModelError {
99
+ export class ModelTrackedTwiceError extends GenericModelError {
100
100
  constructor (key, trackedModel) {
101
101
  const msg = 'Model tracked twice'
102
102
  super(msg, key.Cls.collectionName, key.encodedKey)
@@ -109,7 +109,7 @@ class ModelTrackedTwiceError extends GenericModelError {
109
109
  * Thrown when a model is being deleted more than once.
110
110
  * @memberof Errors
111
111
  */
112
- class DeletedTwiceError extends GenericModelError {
112
+ export class DeletedTwiceError extends GenericModelError {
113
113
  constructor (collectionName, _id) {
114
114
  super('Tried to delete model twice in the same transaction',
115
115
  collectionName, _id)
@@ -120,30 +120,16 @@ class DeletedTwiceError extends GenericModelError {
120
120
  * Thrown when a tx tries to write when it was marked read-only.
121
121
  * @memberof Errors
122
122
  */
123
- class WriteAttemptedInReadOnlyTxError extends Error {
123
+ export class WriteAttemptedInReadOnlyTxError extends Error {
124
124
  constructor (dataToWrite) {
125
125
  super(`Tried to write model in a read-only transaction with the following
126
126
  changes ${JSON.stringify(dataToWrite)}`)
127
127
  }
128
128
  }
129
129
 
130
- class NotImplementedError extends Error {
130
+ export class NotImplementedError extends Error {
131
131
  constructor (reason) {
132
132
  super(reason)
133
133
  this.name = this.constructor.name
134
134
  }
135
135
  }
136
-
137
- module.exports = {
138
- DeletedTwiceError,
139
- GenericModelError,
140
- InvalidFieldError,
141
- InvalidOptionsError,
142
- InvalidParameterError,
143
- ModelAlreadyExistsError,
144
- ModelTrackedTwiceError,
145
- NotImplementedError,
146
- TransactionFailedError,
147
- TransactionLockTimeoutError,
148
- WriteAttemptedInReadOnlyTxError
149
- }
package/src/fields.js CHANGED
@@ -1,11 +1,11 @@
1
- const assert = require('assert')
1
+ import assert from 'node:assert'
2
2
 
3
- const { FieldValue } = require('@google-cloud/firestore')
4
- const deepeq = require('fast-deep-equal')
5
- const deepcopy = require('rfdc')()
3
+ import { FieldValue } from '@google-cloud/firestore'
4
+ import deepeq from 'fast-deep-equal'
5
+ import deepcopy from 'rfdc/default'
6
6
 
7
- const { InvalidFieldError, InvalidOptionsError, NotImplementedError } = require('./errors')
8
- const { validateValue } = require('./utils')
7
+ import { InvalidFieldError, InvalidOptionsError, NotImplementedError } from './errors.js'
8
+ import { validateValue } from './utils.js'
9
9
 
10
10
  /**
11
11
  * Abstract class representing a field / property of a Model.
@@ -13,7 +13,7 @@ const { validateValue } = require('./utils')
13
13
  * @private
14
14
  * @memberof Internal
15
15
  */
16
- class __FieldInterface {
16
+ export class __FieldInterface {
17
17
  constructor () {
18
18
  if (this.constructor === __FieldInterface) {
19
19
  throw new Error('Can not instantiate abstract class')
@@ -96,7 +96,7 @@ class __BaseField extends __FieldInterface {
96
96
  * @private
97
97
  * @memberof Internal
98
98
  */
99
- class __Field extends __BaseField {
99
+ export class __Field extends __BaseField {
100
100
  static __validateFieldOptions (modelName, isKey, fieldName, schema) {
101
101
  if (fieldName.startsWith('_')) {
102
102
  throw new InvalidFieldError(
@@ -358,7 +358,7 @@ class __Field extends __BaseField {
358
358
  * @memberof Internal.Fields
359
359
  * @private
360
360
  */
361
- class NumberField extends __Field {
361
+ export class NumberField extends __Field {
362
362
  constructor (options) {
363
363
  super(options)
364
364
  this.__diff = undefined
@@ -453,7 +453,7 @@ class NumberField extends __Field {
453
453
  * @memberof Internal.Fields
454
454
  * @private
455
455
  */
456
- class StringField extends __Field {
456
+ export class StringField extends __Field {
457
457
  get mutated () {
458
458
  return this.__mayHaveMutated && super.mutated
459
459
  }
@@ -464,7 +464,7 @@ class StringField extends __Field {
464
464
  * @memberof Internal.Fields
465
465
  * @private
466
466
  */
467
- class ObjectField extends __Field {
467
+ export class ObjectField extends __Field {
468
468
  /**
469
469
  * This method checks for equality deeply against the initial
470
470
  * value so use it as sparsely as possible. It is primarily meant to be
@@ -483,14 +483,14 @@ class ObjectField extends __Field {
483
483
  * @memberof Internal.Fields
484
484
  * @private
485
485
  */
486
- class BooleanField extends __Field {}
486
+ export class BooleanField extends __Field {}
487
487
 
488
488
  /**
489
489
  * @extends Internal.__Field
490
490
  * @memberof Internal.Fields
491
491
  * @private
492
492
  */
493
- class ArrayField extends __Field {
493
+ export class ArrayField extends __Field {
494
494
  /**
495
495
  * This method checks for equality deeply against the initial
496
496
  * value so use it as sparsely as possible. It is primarily meant to be
@@ -504,7 +504,7 @@ class ArrayField extends __Field {
504
504
  }
505
505
  }
506
506
 
507
- const SCHEMA_TYPE_TO_FIELD_CLASS_MAP = {
507
+ export const SCHEMA_TYPE_TO_FIELD_CLASS_MAP = {
508
508
  array: ArrayField,
509
509
  boolean: BooleanField,
510
510
  float: NumberField,
@@ -513,14 +513,3 @@ const SCHEMA_TYPE_TO_FIELD_CLASS_MAP = {
513
513
  object: ObjectField,
514
514
  string: StringField
515
515
  }
516
-
517
- module.exports = {
518
- __FieldInterface,
519
- __Field,
520
- NumberField,
521
- ArrayField,
522
- BooleanField,
523
- StringField,
524
- ObjectField,
525
- SCHEMA_TYPE_TO_FIELD_CLASS_MAP
526
- }
package/src/firestore.js CHANGED
@@ -1,10 +1,10 @@
1
- const S = require('@pbvision/schema')
1
+ import S from '@pbvision/schema'
2
2
 
3
- const {
3
+ import {
4
4
  getWithArgs,
5
5
  Context
6
- } = require('./context')
7
- const {
6
+ } from './context.js'
7
+ import {
8
8
  DeletedTwiceError,
9
9
  InvalidFieldError,
10
10
  InvalidOptionsError,
@@ -13,17 +13,17 @@ const {
13
13
  ModelTrackedTwiceError,
14
14
  TransactionFailedError,
15
15
  WriteAttemptedInReadOnlyTxError
16
- } = require('./errors')
17
- const {
16
+ } from './errors.js'
17
+ import {
18
18
  __Field,
19
19
  ArrayField,
20
20
  BooleanField,
21
21
  NumberField,
22
22
  ObjectField,
23
23
  StringField
24
- } = require('./fields')
25
- const { Key, UniqueKeyList } = require('./key')
26
- const { Model } = require('./models')
24
+ } from './fields.js'
25
+ import { Key, UniqueKeyList } from './key.js'
26
+ import { Model } from './models.js'
27
27
 
28
28
  /**
29
29
  * @module firestore
@@ -37,7 +37,7 @@ const { Model } = require('./models')
37
37
  * @returns {Object} Symbols that clients of this library can use.
38
38
  * @private
39
39
  */
40
- function setup (firestoreDB) {
40
+ export default function setup (firestoreDB) {
41
41
  Key.firestoreDB = firestoreDB
42
42
  const toExport = {
43
43
  S,
@@ -71,5 +71,3 @@ function setup (firestoreDB) {
71
71
  }
72
72
  return toExport
73
73
  }
74
-
75
- module.exports = setup
package/src/key.js CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * Key uniquely identifies a model.
3
3
  */
4
- class Key {
4
+ export class Key {
5
5
  /**
6
6
  * @param {Model} Cls a Model class
7
7
  * @param {Object} encodedKey encoded key
@@ -26,7 +26,7 @@ class Key {
26
26
  * This is useful because key equality cannot be checked with the
27
27
  * built-in equality operator.
28
28
  */
29
- class UniqueKeyList extends Array {
29
+ export class UniqueKeyList extends Array {
30
30
  constructor (...keys) {
31
31
  super(...keys)
32
32
  const hashes = keys.map(key => this.constructor.getKeyHash(key))
@@ -59,8 +59,3 @@ class UniqueKeyList extends Array {
59
59
  return ret
60
60
  }
61
61
  }
62
-
63
- module.exports = {
64
- Key,
65
- UniqueKeyList
66
- }
package/src/models.js CHANGED
@@ -1,26 +1,26 @@
1
- const assert = require('assert')
1
+ import assert from 'node:assert'
2
2
 
3
- const { Transaction } = require('@google-cloud/firestore')
4
- const S = require('@pbvision/schema')
5
- const stableStringify = require('fast-json-stable-stringify')
3
+ import { Transaction } from '@google-cloud/firestore'
4
+ import S from '@pbvision/schema'
5
+ import stableStringify from 'fast-json-stable-stringify'
6
6
 
7
- const { Data } = require('./data')
8
- const {
7
+ import { Data } from './data.js'
8
+ import {
9
9
  InvalidFieldError,
10
10
  InvalidParameterError,
11
11
  GenericModelError
12
- } = require('./errors')
13
- const { __Field, SCHEMA_TYPE_TO_FIELD_CLASS_MAP } = require('./fields')
14
- const { Key } = require('./key')
15
- const {
12
+ } from './errors.js'
13
+ import { __Field, SCHEMA_TYPE_TO_FIELD_CLASS_MAP } from './fields.js'
14
+ import { Key } from './key.js'
15
+ import {
16
16
  validateValue,
17
17
  SCHEMA_TYPE_TO_JS_TYPE_MAP
18
- } = require('./utils')
18
+ } from './utils.js'
19
19
 
20
20
  /**
21
21
  * The base class for modeling data.
22
22
  */
23
- class Model {
23
+ export class Model {
24
24
  /**
25
25
  * Create a representation of a database Doc. Should only be used by the
26
26
  * library.
@@ -86,7 +86,7 @@ class Model {
86
86
  opts,
87
87
  val: vals[name],
88
88
  valIsFromDB: !this.isNew && !this.__isPartial,
89
- valSpecified: valSpecified,
89
+ valSpecified,
90
90
  isForUpdate: this.__isPartial
91
91
  })
92
92
  Object.seal(field)
@@ -536,7 +536,3 @@ class Model {
536
536
  return ret
537
537
  }
538
538
  }
539
-
540
- module.exports = {
541
- Model
542
- }
package/src/utils.js CHANGED
@@ -1,10 +1,10 @@
1
- const {
1
+ import {
2
2
  InvalidFieldError,
3
3
  InvalidOptionsError,
4
4
  InvalidParameterError
5
- } = require('./errors')
5
+ } from './errors.js'
6
6
 
7
- function validateValue (fieldName, opts, val) {
7
+ export function validateValue (fieldName, opts, val) {
8
8
  const schema = opts.schema
9
9
  const valueType = SCHEMA_TYPE_TO_JS_TYPE_MAP[schema.type]
10
10
 
@@ -22,7 +22,7 @@ function validateValue (fieldName, opts, val) {
22
22
  return valueType
23
23
  }
24
24
 
25
- const SCHEMA_TYPE_TO_JS_TYPE_MAP = {
25
+ export const SCHEMA_TYPE_TO_JS_TYPE_MAP = {
26
26
  array: Array,
27
27
  boolean: Boolean,
28
28
  integer: Number,
@@ -32,13 +32,13 @@ const SCHEMA_TYPE_TO_JS_TYPE_MAP = {
32
32
  string: String
33
33
  }
34
34
 
35
- async function sleep (millis) {
35
+ export async function sleep (millis) {
36
36
  return new Promise((resolve, reject) => {
37
37
  setTimeout(resolve, millis)
38
38
  })
39
39
  }
40
40
 
41
- function checkUnexpectedOptions (options, defaults) {
41
+ export function checkUnexpectedOptions (options, defaults) {
42
42
  if (typeof options !== 'object') {
43
43
  throw new InvalidParameterError('options', 'must be an object')
44
44
  }
@@ -58,18 +58,10 @@ function checkUnexpectedOptions (options, defaults) {
58
58
  })
59
59
  }
60
60
 
61
- function loadOptionDefaults (options, defaults) {
61
+ export function loadOptionDefaults (options, defaults) {
62
62
  // istanbul ignore next
63
63
  options = options || {}
64
64
  checkUnexpectedOptions(options, defaults)
65
65
  const retOptions = Object.assign({}, defaults)
66
66
  return Object.assign(retOptions, options)
67
67
  }
68
-
69
- module.exports = {
70
- checkUnexpectedOptions,
71
- loadOptionDefaults,
72
- SCHEMA_TYPE_TO_JS_TYPE_MAP,
73
- sleep,
74
- validateValue
75
- }