@pbvision/firestore-orm 0.0.2
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/LICENSE +204 -0
- package/README.md +860 -0
- package/environment.js +2 -0
- package/package.json +81 -0
- package/requirements.txt +1 -0
- package/scripts/ci/lint-format-js.sh +13 -0
- package/scripts/ci/markdown-link.sh +8 -0
- package/scripts/start-local-db.sh +2 -0
- package/src/async-emitter.js +50 -0
- package/src/context.js +677 -0
- package/src/data.js +24 -0
- package/src/db-error.js +12 -0
- package/src/default-db.js +21 -0
- package/src/errors.js +149 -0
- package/src/fields.js +526 -0
- package/src/firestore.js +75 -0
- package/src/key.js +66 -0
- package/src/models.js +542 -0
- package/src/utils.js +75 -0
package/src/utils.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
const {
|
|
2
|
+
InvalidFieldError,
|
|
3
|
+
InvalidOptionsError,
|
|
4
|
+
InvalidParameterError
|
|
5
|
+
} = require('./errors')
|
|
6
|
+
|
|
7
|
+
function validateValue (fieldName, opts, val) {
|
|
8
|
+
const schema = opts.schema
|
|
9
|
+
const valueType = SCHEMA_TYPE_TO_JS_TYPE_MAP[schema.type]
|
|
10
|
+
|
|
11
|
+
// handle omitted value
|
|
12
|
+
if (val === undefined) {
|
|
13
|
+
if (opts.optional) {
|
|
14
|
+
return valueType
|
|
15
|
+
} else {
|
|
16
|
+
throw new InvalidFieldError(fieldName, 'missing required value')
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// validate the value against the provided schema
|
|
21
|
+
opts.assertValid(val)
|
|
22
|
+
return valueType
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const SCHEMA_TYPE_TO_JS_TYPE_MAP = {
|
|
26
|
+
array: Array,
|
|
27
|
+
boolean: Boolean,
|
|
28
|
+
integer: Number,
|
|
29
|
+
number: Number,
|
|
30
|
+
float: Number,
|
|
31
|
+
object: Object,
|
|
32
|
+
string: String
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
async function sleep (millis) {
|
|
36
|
+
return new Promise((resolve, reject) => {
|
|
37
|
+
setTimeout(resolve, millis)
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function checkUnexpectedOptions (options, defaults) {
|
|
42
|
+
if (typeof options !== 'object') {
|
|
43
|
+
throw new InvalidParameterError('options', 'must be an object')
|
|
44
|
+
}
|
|
45
|
+
Object.keys(options).forEach(opt => {
|
|
46
|
+
if (!Object.prototype.hasOwnProperty.call(defaults, opt)) {
|
|
47
|
+
throw new InvalidOptionsError(opt, 'Unexpected option. ' +
|
|
48
|
+
`Valid options are ${Object.keys(defaults)}`)
|
|
49
|
+
}
|
|
50
|
+
const optionVal = options[opt]
|
|
51
|
+
const defaultVal = defaults[opt]
|
|
52
|
+
if (optionVal !== undefined &&
|
|
53
|
+
defaultVal !== undefined &&
|
|
54
|
+
typeof optionVal !== typeof defaultVal) {
|
|
55
|
+
throw new InvalidOptionsError(opt, 'Unexpected option. ' +
|
|
56
|
+
`Invalid type for option ${opt}. Expected ${typeof defaultVal}`)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function loadOptionDefaults (options, defaults) {
|
|
62
|
+
// istanbul ignore next
|
|
63
|
+
options = options || {}
|
|
64
|
+
checkUnexpectedOptions(options, defaults)
|
|
65
|
+
const retOptions = Object.assign({}, defaults)
|
|
66
|
+
return Object.assign(retOptions, options)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
module.exports = {
|
|
70
|
+
checkUnexpectedOptions,
|
|
71
|
+
loadOptionDefaults,
|
|
72
|
+
SCHEMA_TYPE_TO_JS_TYPE_MAP,
|
|
73
|
+
sleep,
|
|
74
|
+
validateValue
|
|
75
|
+
}
|