-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathbson.ts
98 lines (90 loc) · 2.66 KB
/
bson.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import type { OperationParent } from './operations/command';
// import type * as _BSON from 'bson';
// let BSON: typeof _BSON = require('bson');
// try {
// BSON = require('bson-ext');
// } catch {} // eslint-disable-line
// export = BSON;
export {
Long,
Binary,
ObjectId,
Timestamp,
Code,
MinKey,
MaxKey,
Decimal128,
Int32,
Double,
DBRef,
deserialize,
serialize,
calculateObjectSize
} from 'bson';
/** @public */
export interface Document {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[key: string]: any;
}
import type { SerializeOptions } from 'bson';
// TODO: Remove me when types from BSON are updated
/**
* BSON Serialization options.
* @public
*/
export interface BSONSerializeOptions extends Omit<SerializeOptions, 'index'> {
/** Return document results as raw BSON buffers */
fieldsAsRaw?: { [key: string]: boolean };
/** Promotes BSON values to native types where possible, set to false to only receive wrapper types */
promoteValues?: boolean;
/** Promotes Binary BSON values to native Node Buffers */
promoteBuffers?: boolean;
/** Promotes long values to number if they fit inside the 53 bits resolution */
promoteLongs?: boolean;
/** Serialize functions on any object */
serializeFunctions?: boolean;
/** Specify if the BSON serializer should ignore undefined fields */
ignoreUndefined?: boolean;
raw?: boolean;
}
export function pluckBSONSerializeOptions(options: BSONSerializeOptions): BSONSerializeOptions {
const {
fieldsAsRaw,
promoteValues,
promoteBuffers,
promoteLongs,
serializeFunctions,
ignoreUndefined,
raw
} = options;
return {
fieldsAsRaw,
promoteValues,
promoteBuffers,
promoteLongs,
serializeFunctions,
ignoreUndefined,
raw
};
}
/**
* Merge the given BSONSerializeOptions, preferring options over the parent's options, and
* substituting defaults for values not set.
*
* @internal
*/
export function resolveBSONOptions(
options?: BSONSerializeOptions,
parent?: OperationParent
): BSONSerializeOptions {
const parentOptions = parent?.bsonOptions;
return {
raw: options?.raw ?? parentOptions?.raw ?? false,
promoteLongs: options?.promoteLongs ?? parentOptions?.promoteLongs ?? true,
promoteValues: options?.promoteValues ?? parentOptions?.promoteValues ?? true,
promoteBuffers: options?.promoteBuffers ?? parentOptions?.promoteBuffers ?? false,
ignoreUndefined: options?.ignoreUndefined ?? parentOptions?.ignoreUndefined ?? false,
serializeFunctions: options?.serializeFunctions ?? parentOptions?.serializeFunctions ?? false,
fieldsAsRaw: options?.fieldsAsRaw ?? parentOptions?.fieldsAsRaw ?? {}
};
}