@prisma/orm-target-mongo 8.0.0-rc.19-dev.7 → 8.0.0-rc.19-dev.9
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/dist/adapter.mjs +1 -1
- package/dist/adapter__control.mjs +2 -2
- package/dist/adapter__runtime.mjs +2 -2
- package/dist/{codecs-CylyDRI5-DdzCZHek.mjs → codecs-B1ab7vfj-DncFJY2Q.mjs} +3 -3
- package/dist/{codecs-CylyDRI5-DdzCZHek.mjs.map → codecs-B1ab7vfj-DncFJY2Q.mjs.map} +1 -1
- package/dist/{control-DBGCkxF_.mjs → control-DRifdPhB.mjs} +3 -3
- package/dist/{control-DBGCkxF_.mjs.map → control-DRifdPhB.mjs.map} +1 -1
- package/dist/{data-types-BS2Wy4k-.d.mts → data-types-BUXk_WEa.d.mts} +9 -7
- package/dist/data-types-BUXk_WEa.d.mts.map +1 -0
- package/dist/{data-types-BUNJnoE9-S7P2K-7r.mjs → data-types-hoKLBrYZ-CkFtgHfm.mjs} +269 -200
- package/dist/data-types-hoKLBrYZ-CkFtgHfm.mjs.map +1 -0
- package/dist/descriptor-meta-DTj807jv-C0Hq7wyV.mjs +146 -0
- package/dist/descriptor-meta-DTj807jv-C0Hq7wyV.mjs.map +1 -0
- package/dist/{descriptor-meta-runtime-DeRjCidG-BzMW-qkN.mjs → descriptor-meta-runtime-BaRbjvD7-MOhJcdky.mjs} +4 -4
- package/dist/{descriptor-meta-runtime-DeRjCidG-BzMW-qkN.mjs.map → descriptor-meta-runtime-BaRbjvD7-MOhJcdky.mjs.map} +1 -1
- package/dist/driver.mjs +1 -1
- package/dist/driver__control.mjs +1 -1
- package/dist/driver__control.mjs.map +1 -1
- package/dist/{mongo-adapter-Bo6ciURK-Dvq-oZQ9.mjs → mongo-adapter-Bo6ciURK-CGfjLOPh.mjs} +2 -2
- package/dist/{mongo-adapter-Bo6ciURK-Dvq-oZQ9.mjs.map → mongo-adapter-Bo6ciURK-CGfjLOPh.mjs.map} +1 -1
- package/dist/{mongo-driver-CvdBCKtz-WuUuJ-wy.mjs → mongo-driver-q-IqfhuV-CouLzDHp.mjs} +3 -3
- package/dist/{mongo-driver-CvdBCKtz-WuUuJ-wy.mjs.map → mongo-driver-q-IqfhuV-CouLzDHp.mjs.map} +1 -1
- package/dist/{pack-CDr8LNkg.d.mts → pack-DYR2mrvW.d.mts} +3 -1
- package/dist/{pack-CDr8LNkg.d.mts.map → pack-DYR2mrvW.d.mts.map} +1 -1
- package/dist/{prisma6-binding-L_x2s5FV.mjs → prisma6-binding-WOZ8vURx.mjs} +2 -2
- package/dist/{prisma6-binding-L_x2s5FV.mjs.map → prisma6-binding-WOZ8vURx.mjs.map} +1 -1
- package/dist/target.d.mts +3 -3
- package/dist/target.mjs +5 -5
- package/dist/target__codecs.mjs +1 -1
- package/dist/target__control.mjs +1 -1
- package/dist/target__data-types.d.mts +2 -2
- package/dist/target__data-types.mjs +2 -2
- package/dist/target__pack.d.mts +1 -1
- package/dist/target__pack.mjs +1 -1
- package/dist/target__pack.mjs.map +1 -1
- package/dist/target__prisma6-binding.mjs +1 -1
- package/dist/target__runtime.mjs +1 -1
- package/dist/target__runtime.mjs.map +1 -1
- package/package.json +9 -9
- package/dist/data-types-BS2Wy4k-.d.mts.map +0 -1
- package/dist/data-types-BUNJnoE9-S7P2K-7r.mjs.map +0 -1
- package/dist/descriptor-meta-Dtl7SqQv-D50zqWiV.mjs +0 -34
- package/dist/descriptor-meta-Dtl7SqQv-D50zqWiV.mjs.map +0 -1
|
@@ -1,23 +1,25 @@
|
|
|
1
|
-
import { DataType } from "@prisma/orm-framework/components/codec";
|
|
1
|
+
import { DataType, DataTypeId } from "@prisma/orm-framework/components/codec";
|
|
2
2
|
//#region ../../../3-mongo-target/1-mongo-target/dist/data-types.d.mts
|
|
3
3
|
//#region src/core/data-types.d.ts
|
|
4
4
|
/** The vector's dimension: the TypeScript type `Vector<n>` and the type's reader read it. */
|
|
5
5
|
declare const mongoVectorParams: import("arktype/internal/variants/object.ts").ObjectType<{
|
|
6
6
|
length?: number;
|
|
7
7
|
}, {}>;
|
|
8
|
-
declare const mongoObjectId: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
9
8
|
declare const mongoString: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
10
|
-
declare const mongoDouble: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
11
9
|
declare const mongoInt32: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
10
|
+
declare const mongoInt64: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
11
|
+
declare const mongoDecimal128: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
12
|
+
declare const mongoDouble: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
13
|
+
declare const mongoObjectId: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
12
14
|
declare const mongoBool: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
13
15
|
declare const mongoDate: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
14
16
|
declare const mongoVector: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
15
|
-
declare const mongoInt64: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
16
|
-
declare const mongoDecimal128: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
17
17
|
declare const mongoBinary: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
18
18
|
declare const mongoJson: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
19
19
|
declare const mongoBson: import("@prisma/orm-family-mongo/contract/data-type").MongoDataType;
|
|
20
20
|
declare const mongoDataTypes: readonly DataType[];
|
|
21
|
+
/** MongoDB's text data type: `string`. */
|
|
22
|
+
declare const mongoTextDataTypes: readonly DataTypeId[];
|
|
21
23
|
//#endregion
|
|
22
|
-
export { mongoDate as a, mongoInt32 as c, mongoObjectId as d, mongoString as f, mongoDataTypes as i, mongoInt64 as l,
|
|
23
|
-
//# sourceMappingURL=data-types-
|
|
24
|
+
export { mongoDate as a, mongoInt32 as c, mongoObjectId as d, mongoString as f, mongoVectorParams as h, mongoDataTypes as i, mongoInt64 as l, mongoVector as m, mongoBool as n, mongoDecimal128 as o, mongoTextDataTypes as p, mongoBson as r, mongoDouble as s, mongoBinary as t, mongoJson as u };
|
|
25
|
+
//# sourceMappingURL=data-types-BUXk_WEa.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"data-types-BUXk_WEa.d.mts","names":[],"sources":["../../../../3-mongo-target/1-mongo-target/dist/data-types.d.mts"],"mappings":";;;;cAGc,iEAAiE;EAC7E;;cAEY,0DAA0D;cAC1D,yDAAyD;cACzD,yDAAyD;cACzD,8DAA8D;cAC9D,0DAA0D;cAC1D,4DAA4D;cAC5D,wDAAwD;cACxD,wDAAwD;cACxD,0DAA0D;cAC1D,0DAA0D;cAC1D,wDAAwD;cACxD,wDAAwD;cACxD,yBAAyB;;cAEzB,6BAA6B"}
|
|
@@ -1,180 +1,14 @@
|
|
|
1
1
|
import { r as MONGO_BSON_CODEC_ID } from "./codec-ids-WdrumsYA-BkQfspmL.mjs";
|
|
2
2
|
import { t as mongoTargetError } from "./mongo-target-errors-Btf3ML1w-B5EMYbz1.mjs";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { isDeepStrictEqual } from "node:util";
|
|
6
|
-
import { Binary, Code, Decimal128, Double, EJSON, Long, MinKey, ObjectId } from "bson";
|
|
3
|
+
import { structuredError } from "@prisma/orm-framework/utils/structured-error";
|
|
4
|
+
import { INT32_RANGE, INT64_RANGE, SAFE_INTEGER_BIGINT_RANGE, SAFE_INTEGER_RANGE, asFloat, asNumeralText, castUnchanged, decimalTextEqualityKey, isIntegerIn, readJsonBoolean, readJsonFloat, readJsonInteger, readJsonIntegerText, readJsonMatching, readJsonString, refuseJsonValue, refuseWrongShape } from "@prisma/orm-framework/components/codec";
|
|
7
5
|
import { counted } from "@prisma/orm-framework/utils/text";
|
|
6
|
+
import { Binary, Code, Decimal128, Double, EJSON, Long, MinKey, ObjectId } from "bson";
|
|
8
7
|
import { mongoDataType } from "@prisma/orm-family-mongo/contract/data-type";
|
|
9
8
|
import { type } from "arktype";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
function child(path, key) {
|
|
15
|
-
return path === "" ? String(key) : `${path}.${key}`;
|
|
16
|
-
}
|
|
17
|
-
function isPlainObject(value) {
|
|
18
|
-
const prototype = Object.getPrototypeOf(value);
|
|
19
|
-
return prototype === Object.prototype || prototype === null;
|
|
20
|
-
}
|
|
21
|
-
function isPlainArray(value) {
|
|
22
|
-
return Array.isArray(value) && Object.getPrototypeOf(value) === Array.prototype;
|
|
23
|
-
}
|
|
24
|
-
const BSON_VERSION = Symbol.for("@@mdb.bson.version");
|
|
25
|
-
/** The major version of the `bson` this package loads, which every `bson` class instance it can serialise carries. */
|
|
26
|
-
const BSON_MAJOR = Reflect.get(new MinKey(), BSON_VERSION);
|
|
27
|
-
/** Whether a `bson` class instance comes from the same major version of `bson`, rather than being a look-alike or from another version. */
|
|
28
|
-
function createdByBsonMajor(value) {
|
|
29
|
-
return Reflect.get(value, BSON_VERSION) === BSON_MAJOR;
|
|
30
|
-
}
|
|
31
|
-
function bsonTypeTag(value) {
|
|
32
|
-
const tag = Reflect.get(value, "_bsontype");
|
|
33
|
-
return typeof tag === "string" ? tag : void 0;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* The `_bsontype` tag of a `bson` class instance. A stored subdocument is read as a plain object even when it has a `_bsontype` key, so a plain object has no tag.
|
|
37
|
-
*/
|
|
38
|
-
function bsonClassTag(value) {
|
|
39
|
-
return isPlainObject(value) ? void 0 : bsonTypeTag(value);
|
|
40
|
-
}
|
|
41
|
-
function constructorName(value) {
|
|
42
|
-
const name = Reflect.get(value, "constructor")?.name;
|
|
43
|
-
return typeof name === "string" && name !== "" ? name : "object";
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* The `bson` library reads any subdocument with a string `$ref` and an `$id` as a `DBRef`. It was stored as a plain object; these are its entries in stored order, `$ref`, `$id`, `$db` when present, then the other fields.
|
|
47
|
-
*/
|
|
48
|
-
function dbRefEntries(value) {
|
|
49
|
-
const ref = blindCast(value);
|
|
50
|
-
const entries = [["$ref", ref.collection], ["$id", ref.oid]];
|
|
51
|
-
if (ref.db !== void 0) entries.push(["$db", ref.db]);
|
|
52
|
-
entries.push(...Object.entries(ref.fields));
|
|
53
|
-
return entries;
|
|
54
|
-
}
|
|
55
|
-
const BSON_VALUE_TAGS = /* @__PURE__ */ new Set([
|
|
56
|
-
"ObjectId",
|
|
57
|
-
"Long",
|
|
58
|
-
"Decimal128",
|
|
59
|
-
"Binary",
|
|
60
|
-
"BSONRegExp",
|
|
61
|
-
"Timestamp",
|
|
62
|
-
"Int32",
|
|
63
|
-
"Double",
|
|
64
|
-
"Code",
|
|
65
|
-
"MinKey",
|
|
66
|
-
"MaxKey",
|
|
67
|
-
"BSONSymbol"
|
|
68
|
-
]);
|
|
69
|
-
const ENCODE_FIX_BY_RECEIVED = { DBRef: "Write it as a { $ref, $id } document instead." };
|
|
70
|
-
function encodeRefused(received, path) {
|
|
71
|
-
const fix = ENCODE_FIX_BY_RECEIVED[received];
|
|
72
|
-
throw mongoTargetError("RUNTIME.ENCODE_FAILED", `${MONGO_BSON_CODEC_ID} value must be a BSON value; received ${received} at ${where(path)}${fix === void 0 ? "" : `. ${fix}`}`, { meta: {
|
|
73
|
-
codecId: MONGO_BSON_CODEC_ID,
|
|
74
|
-
received,
|
|
75
|
-
valuePath: path
|
|
76
|
-
} });
|
|
77
|
-
}
|
|
78
|
-
function assertBsonValue(value, path, ancestors) {
|
|
79
|
-
if (value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number") return;
|
|
80
|
-
if (typeof value !== "object") {
|
|
81
|
-
encodeRefused(typeof value, path);
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
const tag = bsonTypeTag(value);
|
|
85
|
-
if (tag !== void 0) {
|
|
86
|
-
if (!BSON_VALUE_TAGS.has(tag)) encodeRefused(tag, path);
|
|
87
|
-
if (!createdByBsonMajor(value)) encodeRefused(`${tag} not created by bson ${String(BSON_MAJOR)}`, path);
|
|
88
|
-
return;
|
|
89
|
-
}
|
|
90
|
-
if (value instanceof Date || value instanceof RegExp || value instanceof Uint8Array) return;
|
|
91
|
-
if (!isPlainArray(value) && !isPlainObject(value)) {
|
|
92
|
-
encodeRefused(constructorName(value), path);
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
if (ancestors.has(value)) encodeRefused("circular reference", path);
|
|
96
|
-
ancestors.add(value);
|
|
97
|
-
if (Array.isArray(value)) for (let index = 0; index < value.length; index++) {
|
|
98
|
-
if (!(index in value)) encodeRefused("sparse array hole", child(path, index));
|
|
99
|
-
assertBsonValue(value[index], child(path, index), ancestors);
|
|
100
|
-
}
|
|
101
|
-
else for (const [key, entry] of Object.entries(value)) assertBsonValue(entry, child(path, key), ancestors);
|
|
102
|
-
ancestors.delete(value);
|
|
103
|
-
}
|
|
104
|
-
/**
|
|
105
|
-
* Returns `value` unchanged when it is a BSON value at every depth, and throws `RUNTIME.ENCODE_FAILED` naming the first value that is not, with its path.
|
|
106
|
-
*/
|
|
107
|
-
function encodeBsonValue(value) {
|
|
108
|
-
assertBsonValue(value, "", /* @__PURE__ */ new Set());
|
|
109
|
-
return value;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* A `Code` value with a scope, recognised by its tag: the driver reads with its own load of `bson`, so its `Code` is not an instance of the class imported here.
|
|
113
|
-
*/
|
|
114
|
-
function codeWithScope(value) {
|
|
115
|
-
if (bsonClassTag(value) !== "Code") return void 0;
|
|
116
|
-
const code = Reflect.get(value, "code");
|
|
117
|
-
const scope = Reflect.get(value, "scope");
|
|
118
|
-
return typeof code === "string" && typeof scope === "object" && scope !== null ? {
|
|
119
|
-
code,
|
|
120
|
-
scope
|
|
121
|
-
} : void 0;
|
|
122
|
-
}
|
|
123
|
-
function decodeEntries(entries) {
|
|
124
|
-
return Object.fromEntries(entries.map(([key, entry]) => [key, decodeValue(entry)]));
|
|
125
|
-
}
|
|
126
|
-
function decodeValue(value) {
|
|
127
|
-
if (typeof value !== "object" || value === null) return value;
|
|
128
|
-
if (bsonClassTag(value) === "DBRef") return decodeEntries(dbRefEntries(value));
|
|
129
|
-
if (Array.isArray(value)) {
|
|
130
|
-
const decoded = value.map(decodeValue);
|
|
131
|
-
return decoded.some((entry, index) => entry !== value[index]) ? decoded : value;
|
|
132
|
-
}
|
|
133
|
-
const code = codeWithScope(value);
|
|
134
|
-
if (code !== void 0) {
|
|
135
|
-
const scope = decodeValue(code.scope);
|
|
136
|
-
return scope === code.scope ? value : new Code(code.code, blindCast(scope));
|
|
137
|
-
}
|
|
138
|
-
if (!isPlainObject(value)) return value;
|
|
139
|
-
const entries = Object.entries(value);
|
|
140
|
-
const decoded = decodeEntries(entries);
|
|
141
|
-
return entries.some(([key, entry]) => decoded[key] !== entry) ? decoded : value;
|
|
142
|
-
}
|
|
143
|
-
/**
|
|
144
|
-
* Returns the wire value as the driver produced it, except that a `DBRef` the `bson` library read from a `{ $ref, $id }` subdocument becomes that document again, `{ $ref, $id[, $db], ...fields }`, with its members' BSON types kept, at any depth and inside a `Code` scope. A value holding no `DBRef` is returned as the same object.
|
|
145
|
-
*/
|
|
146
|
-
function decodeBsonValue(wire) {
|
|
147
|
-
return blindCast(decodeValue(wire));
|
|
148
|
-
}
|
|
149
|
-
function asDriverWrites(value) {
|
|
150
|
-
if (typeof value === "number") return Number.isInteger(value) && (value < INT32_RANGE.min || value > INT32_RANGE.max) ? new Double(value) : value;
|
|
151
|
-
if (typeof value !== "object" || value === null) return value;
|
|
152
|
-
if (value instanceof Uint8Array) return new Binary(value);
|
|
153
|
-
if (Array.isArray(value)) return value.map(asDriverWrites);
|
|
154
|
-
if (isPlainObject(value)) return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, asDriverWrites(entry)]));
|
|
155
|
-
const code = codeWithScope(value);
|
|
156
|
-
if (code !== void 0) return new Code(code.code, blindCast(asDriverWrites(code.scope)));
|
|
157
|
-
return value;
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Canonical Extended JSON for a BSON value, recording each JavaScript number and `Uint8Array` as the BSON type the driver writes for it: an integer outside the int32 range as a `double`, and bytes as `binData`.
|
|
161
|
-
*/
|
|
162
|
-
function bsonToJson(value) {
|
|
163
|
-
return blindCast(EJSON.serialize(asDriverWrites(value), { relaxed: false }));
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Reads the canonical Extended JSON `bsonToJson` writes. The `bson` reader also takes forms that are not canonical, some of them silently wrong (`{ "$numberInt": "abc" }` reads as 0), so a value that does not write back to the same JSON is refused, naming `owner`.
|
|
167
|
-
*/
|
|
168
|
-
function readBsonJson(owner, json) {
|
|
169
|
-
let value;
|
|
170
|
-
try {
|
|
171
|
-
value = EJSON.deserialize(blindCast(json), { relaxed: false });
|
|
172
|
-
} catch {
|
|
173
|
-
return refuseJsonValue(owner, "canonical Extended JSON", json);
|
|
174
|
-
}
|
|
175
|
-
if (!isDeepStrictEqual(EJSON.serialize(value, { relaxed: false }), json)) return refuseJsonValue(owner, "canonical Extended JSON", json);
|
|
176
|
-
return value;
|
|
177
|
-
}
|
|
9
|
+
import { isDeepStrictEqual } from "node:util";
|
|
10
|
+
import { blindCast } from "@prisma/orm-framework/utils/casts";
|
|
11
|
+
//#region ../../../3-mongo-target/1-mongo-target/dist/data-types-hoKLBrYZ.mjs
|
|
178
12
|
const STORED_DECIMAL_TEXT = /^(?:-?\d+(?:\.\d+)?|NaN|-?Infinity)$/;
|
|
179
13
|
const DECIMAL128_TEXT = /^(-?)(\d+)(?:\.(\d+))?(?:E([+-]\d+))?$/;
|
|
180
14
|
const BASE64_TEXT = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/;
|
|
@@ -274,6 +108,10 @@ function objectIdToJson(codecId, value) {
|
|
|
274
108
|
if (typeof value !== "string" || !OBJECT_ID_TEXT.test(value)) encodeFailed(codecId, "value must be 24 hexadecimal digits", value);
|
|
275
109
|
return value;
|
|
276
110
|
}
|
|
111
|
+
/** Whether `text` is an ObjectId's 24 hexadecimal digits, in either case. */
|
|
112
|
+
function isObjectIdText(text) {
|
|
113
|
+
return OBJECT_ID_TEXT.test(text);
|
|
114
|
+
}
|
|
277
115
|
/** Reads ObjectId hex in either case; {@link spellObjectIdJson} writes the lower case the type stores. */
|
|
278
116
|
function readObjectIdJson(owner, json) {
|
|
279
117
|
return readJsonMatching(owner, json, OBJECT_ID_TEXT, "24 hexadecimal digits");
|
|
@@ -308,6 +146,7 @@ function readVectorJson(owner, json, length) {
|
|
|
308
146
|
const numbers = [];
|
|
309
147
|
for (const element of json) {
|
|
310
148
|
if (typeof element !== "number") return refuseJsonValue(owner, expected, element);
|
|
149
|
+
if (!Number.isFinite(element)) return refuseJsonValue(owner, "an array of finite numbers", json);
|
|
311
150
|
numbers.push(element);
|
|
312
151
|
}
|
|
313
152
|
return numbers;
|
|
@@ -427,18 +266,34 @@ function decimal128Decode(codecId, wire) {
|
|
|
427
266
|
function decimal128ToJson(codecId, value) {
|
|
428
267
|
return requireStoredDecimalText(codecId, value);
|
|
429
268
|
}
|
|
269
|
+
/** Decimal text without its leading zeros, as MongoDB writes it; the sign of zero and trailing zeros stay. */
|
|
270
|
+
function decimalTextWithoutLeadingZeros(text) {
|
|
271
|
+
return text.replace(/^(-?)0+(?=\d)/, "$1");
|
|
272
|
+
}
|
|
273
|
+
function withoutSignOfZero(text) {
|
|
274
|
+
return /^-[0.]+$/.test(text) ? text.slice(1) : text;
|
|
275
|
+
}
|
|
276
|
+
/** Whether a Decimal128 holds the decimal text exactly: the text it writes back is the same number, digit for digit. */
|
|
277
|
+
function decimal128HoldsExactly(text) {
|
|
278
|
+
let written;
|
|
279
|
+
try {
|
|
280
|
+
written = Decimal128.fromString(text).toString();
|
|
281
|
+
} catch {
|
|
282
|
+
return false;
|
|
283
|
+
}
|
|
284
|
+
return decimalTextWithoutExponent(written) === withoutSignOfZero(decimalTextWithoutLeadingZeros(text));
|
|
285
|
+
}
|
|
430
286
|
/**
|
|
431
|
-
* The stored form is what the codec writes, so it follows the encode rule: decimal text without an exponent, or `NaN`, `Infinity` or `-Infinity`, that a Decimal128 holds exactly.
|
|
287
|
+
* The stored form is what the codec writes, so it follows the encode rule: decimal text without an exponent, or `NaN`, `Infinity` or `-Infinity`, that a Decimal128 holds exactly. A value a Decimal128 would round is refused.
|
|
432
288
|
*/
|
|
433
289
|
function readDecimal128Json(owner, json) {
|
|
434
290
|
const expected = "decimal text without an exponent that a Decimal128 holds exactly, or NaN, Infinity or -Infinity";
|
|
435
291
|
const text = readJsonMatching(owner, json, STORED_DECIMAL_TEXT, expected);
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
return text;
|
|
292
|
+
return decimal128HoldsExactly(text) ? text : refuseJsonValue(owner, expected, json);
|
|
293
|
+
}
|
|
294
|
+
/** The decimal text without leading zeros, as MongoDB writes it, keeping negative zero. */
|
|
295
|
+
function spellDecimal128Json(json) {
|
|
296
|
+
return typeof json === "string" ? decimalTextWithoutLeadingZeros(json) : json;
|
|
442
297
|
}
|
|
443
298
|
function binaryEncode(codecId, value) {
|
|
444
299
|
if (!(value instanceof Uint8Array)) encodeFailed(codecId, "value must be a Uint8Array", value);
|
|
@@ -455,6 +310,10 @@ function binaryDecode(codecId, wire) {
|
|
|
455
310
|
function binaryToBase64(value) {
|
|
456
311
|
return Buffer.from(value).toString("base64");
|
|
457
312
|
}
|
|
313
|
+
/** Whether `text` is base64, with or without padding bits set. */
|
|
314
|
+
function isBase64Text(text) {
|
|
315
|
+
return BASE64_TEXT.test(text);
|
|
316
|
+
}
|
|
458
317
|
/** Reads base64 with or without padding bits set; {@link spellBase64Json} writes the base64 the type stores. */
|
|
459
318
|
function readBinaryJson(owner, json) {
|
|
460
319
|
return readJsonMatching(owner, json, BASE64_TEXT, "base64 text");
|
|
@@ -466,27 +325,235 @@ function spellBase64Json(json) {
|
|
|
466
325
|
function binaryFromBase64(base64) {
|
|
467
326
|
return new Uint8Array(Buffer.from(base64, "base64"));
|
|
468
327
|
}
|
|
328
|
+
function where(path) {
|
|
329
|
+
return path === "" ? "the root" : path;
|
|
330
|
+
}
|
|
331
|
+
function child(path, key) {
|
|
332
|
+
return path === "" ? String(key) : `${path}.${key}`;
|
|
333
|
+
}
|
|
334
|
+
function isPlainObject(value) {
|
|
335
|
+
const prototype = Object.getPrototypeOf(value);
|
|
336
|
+
return prototype === Object.prototype || prototype === null;
|
|
337
|
+
}
|
|
338
|
+
function isPlainArray(value) {
|
|
339
|
+
return Array.isArray(value) && Object.getPrototypeOf(value) === Array.prototype;
|
|
340
|
+
}
|
|
341
|
+
const BSON_VERSION = Symbol.for("@@mdb.bson.version");
|
|
342
|
+
/** The major version of the `bson` this package loads, which every `bson` class instance it can serialise carries. */
|
|
343
|
+
const BSON_MAJOR = Reflect.get(new MinKey(), BSON_VERSION);
|
|
344
|
+
/** Whether a `bson` class instance comes from the same major version of `bson`, rather than being a look-alike or from another version. */
|
|
345
|
+
function createdByBsonMajor(value) {
|
|
346
|
+
return Reflect.get(value, BSON_VERSION) === BSON_MAJOR;
|
|
347
|
+
}
|
|
348
|
+
function bsonTypeTag(value) {
|
|
349
|
+
const tag = Reflect.get(value, "_bsontype");
|
|
350
|
+
return typeof tag === "string" ? tag : void 0;
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* The `_bsontype` tag of a `bson` class instance. A stored subdocument is read as a plain object even when it has a `_bsontype` key, so a plain object has no tag.
|
|
354
|
+
*/
|
|
355
|
+
function bsonClassTag(value) {
|
|
356
|
+
return isPlainObject(value) ? void 0 : bsonTypeTag(value);
|
|
357
|
+
}
|
|
358
|
+
function constructorName(value) {
|
|
359
|
+
const name = Reflect.get(value, "constructor")?.name;
|
|
360
|
+
return typeof name === "string" && name !== "" ? name : "object";
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* The `bson` library reads any subdocument with a string `$ref` and an `$id` as a `DBRef`. It was stored as a plain object; these are its entries in stored order, `$ref`, `$id`, `$db` when present, then the other fields.
|
|
364
|
+
*/
|
|
365
|
+
function dbRefEntries(value) {
|
|
366
|
+
const ref = blindCast(value);
|
|
367
|
+
const entries = [["$ref", ref.collection], ["$id", ref.oid]];
|
|
368
|
+
if (ref.db !== void 0) entries.push(["$db", ref.db]);
|
|
369
|
+
entries.push(...Object.entries(ref.fields));
|
|
370
|
+
return entries;
|
|
371
|
+
}
|
|
372
|
+
const BSON_VALUE_TAGS = /* @__PURE__ */ new Set([
|
|
373
|
+
"ObjectId",
|
|
374
|
+
"Long",
|
|
375
|
+
"Decimal128",
|
|
376
|
+
"Binary",
|
|
377
|
+
"BSONRegExp",
|
|
378
|
+
"Timestamp",
|
|
379
|
+
"Int32",
|
|
380
|
+
"Double",
|
|
381
|
+
"Code",
|
|
382
|
+
"MinKey",
|
|
383
|
+
"MaxKey",
|
|
384
|
+
"BSONSymbol"
|
|
385
|
+
]);
|
|
386
|
+
const ENCODE_FIX_BY_RECEIVED = { DBRef: "Write it as a { $ref, $id } document instead." };
|
|
387
|
+
function encodeRefused(received, path) {
|
|
388
|
+
const fix = ENCODE_FIX_BY_RECEIVED[received];
|
|
389
|
+
throw mongoTargetError("RUNTIME.ENCODE_FAILED", `${MONGO_BSON_CODEC_ID} value must be a BSON value; received ${received} at ${where(path)}${fix === void 0 ? "" : `. ${fix}`}`, { meta: {
|
|
390
|
+
codecId: MONGO_BSON_CODEC_ID,
|
|
391
|
+
received,
|
|
392
|
+
valuePath: path
|
|
393
|
+
} });
|
|
394
|
+
}
|
|
395
|
+
function assertBsonValue(value, path, ancestors) {
|
|
396
|
+
if (value === null || typeof value === "string" || typeof value === "boolean" || typeof value === "number") return;
|
|
397
|
+
if (typeof value !== "object") {
|
|
398
|
+
encodeRefused(typeof value, path);
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
const tag = bsonTypeTag(value);
|
|
402
|
+
if (tag !== void 0) {
|
|
403
|
+
if (!BSON_VALUE_TAGS.has(tag)) encodeRefused(tag, path);
|
|
404
|
+
if (!createdByBsonMajor(value)) encodeRefused(`${tag} not created by bson ${String(BSON_MAJOR)}`, path);
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (value instanceof Date || value instanceof RegExp || value instanceof Uint8Array) return;
|
|
408
|
+
if (!isPlainArray(value) && !isPlainObject(value)) {
|
|
409
|
+
encodeRefused(constructorName(value), path);
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
if (ancestors.has(value)) encodeRefused("circular reference", path);
|
|
413
|
+
ancestors.add(value);
|
|
414
|
+
if (Array.isArray(value)) for (let index = 0; index < value.length; index++) {
|
|
415
|
+
if (!(index in value)) encodeRefused("sparse array hole", child(path, index));
|
|
416
|
+
assertBsonValue(value[index], child(path, index), ancestors);
|
|
417
|
+
}
|
|
418
|
+
else for (const [key, entry] of Object.entries(value)) assertBsonValue(entry, child(path, key), ancestors);
|
|
419
|
+
ancestors.delete(value);
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Returns `value` unchanged when it is a BSON value at every depth, and throws `RUNTIME.ENCODE_FAILED` naming the first value that is not, with its path.
|
|
423
|
+
*/
|
|
424
|
+
function encodeBsonValue(value) {
|
|
425
|
+
assertBsonValue(value, "", /* @__PURE__ */ new Set());
|
|
426
|
+
return value;
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* A `Code` value with a scope, recognised by its tag: the driver reads with its own load of `bson`, so its `Code` is not an instance of the class imported here.
|
|
430
|
+
*/
|
|
431
|
+
function codeWithScope(value) {
|
|
432
|
+
if (bsonClassTag(value) !== "Code") return void 0;
|
|
433
|
+
const code = Reflect.get(value, "code");
|
|
434
|
+
const scope = Reflect.get(value, "scope");
|
|
435
|
+
return typeof code === "string" && typeof scope === "object" && scope !== null ? {
|
|
436
|
+
code,
|
|
437
|
+
scope
|
|
438
|
+
} : void 0;
|
|
439
|
+
}
|
|
440
|
+
function decodeEntries(entries) {
|
|
441
|
+
return Object.fromEntries(entries.map(([key, entry]) => [key, decodeValue(entry)]));
|
|
442
|
+
}
|
|
443
|
+
function decodeValue(value) {
|
|
444
|
+
if (typeof value !== "object" || value === null) return value;
|
|
445
|
+
if (bsonClassTag(value) === "DBRef") return decodeEntries(dbRefEntries(value));
|
|
446
|
+
if (Array.isArray(value)) {
|
|
447
|
+
const decoded = value.map(decodeValue);
|
|
448
|
+
return decoded.some((entry, index) => entry !== value[index]) ? decoded : value;
|
|
449
|
+
}
|
|
450
|
+
const code = codeWithScope(value);
|
|
451
|
+
if (code !== void 0) {
|
|
452
|
+
const scope = decodeValue(code.scope);
|
|
453
|
+
return scope === code.scope ? value : new Code(code.code, blindCast(scope));
|
|
454
|
+
}
|
|
455
|
+
if (!isPlainObject(value)) return value;
|
|
456
|
+
const entries = Object.entries(value);
|
|
457
|
+
const decoded = decodeEntries(entries);
|
|
458
|
+
return entries.some(([key, entry]) => decoded[key] !== entry) ? decoded : value;
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Returns the wire value as the driver produced it, except that a `DBRef` the `bson` library read from a `{ $ref, $id }` subdocument becomes that document again, `{ $ref, $id[, $db], ...fields }`, with its members' BSON types kept, at any depth and inside a `Code` scope. A value holding no `DBRef` is returned as the same object.
|
|
462
|
+
*/
|
|
463
|
+
function decodeBsonValue(wire) {
|
|
464
|
+
return blindCast(decodeValue(wire));
|
|
465
|
+
}
|
|
466
|
+
function asDriverWrites(value) {
|
|
467
|
+
if (typeof value === "number") return Number.isInteger(value) && (value < INT32_RANGE.min || value > INT32_RANGE.max) ? new Double(value) : value;
|
|
468
|
+
if (typeof value !== "object" || value === null) return value;
|
|
469
|
+
if (value instanceof Uint8Array) return new Binary(value);
|
|
470
|
+
if (Array.isArray(value)) return value.map(asDriverWrites);
|
|
471
|
+
if (isPlainObject(value)) return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, asDriverWrites(entry)]));
|
|
472
|
+
const code = codeWithScope(value);
|
|
473
|
+
if (code !== void 0) return new Code(code.code, blindCast(asDriverWrites(code.scope)));
|
|
474
|
+
return value;
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Canonical Extended JSON for a BSON value, recording each JavaScript number and `Uint8Array` as the BSON type the driver writes for it: an integer outside the int32 range as a `double`, and bytes as `binData`.
|
|
478
|
+
*/
|
|
479
|
+
function bsonToJson(value) {
|
|
480
|
+
return blindCast(EJSON.serialize(asDriverWrites(value), { relaxed: false }));
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Reads the canonical Extended JSON `bsonToJson` writes. The `bson` reader also takes forms that are not canonical, some of them silently wrong (`{ "$numberInt": "abc" }` reads as 0), so a value that does not write back to the same JSON is refused, naming `owner`.
|
|
484
|
+
*/
|
|
485
|
+
function readBsonJson(owner, json) {
|
|
486
|
+
let value;
|
|
487
|
+
try {
|
|
488
|
+
value = EJSON.deserialize(blindCast(json), { relaxed: false });
|
|
489
|
+
} catch {
|
|
490
|
+
return refuseJsonValue(owner, "canonical Extended JSON", json);
|
|
491
|
+
}
|
|
492
|
+
if (!isDeepStrictEqual(EJSON.serialize(value, { relaxed: false }), json)) return refuseJsonValue(owner, "canonical Extended JSON", json);
|
|
493
|
+
return value;
|
|
494
|
+
}
|
|
469
495
|
/** The vector's dimension: the TypeScript type `Vector<n>` and the type's reader read it. */
|
|
470
496
|
const mongoVectorParams = type({ "length?": "number.integer >= 1" });
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
}
|
|
497
|
+
/** Text as the ObjectId it names; {@link spellObjectIdJson} gives it the lower case the type stores. */
|
|
498
|
+
const asObjectId = (value) => {
|
|
499
|
+
if (typeof value !== "string") return refuseWrongShape(value, "text");
|
|
500
|
+
if (isObjectIdText(value)) return value;
|
|
501
|
+
throw structuredError("CONTRACT.CAST_REFUSED", `"${value}" is not an ObjectId: write 24 hexadecimal digits.`, {
|
|
502
|
+
why: "An ObjectId is 12 bytes, written as 24 hexadecimal digits.",
|
|
503
|
+
fix: "Write the ObjectId as 24 hexadecimal digits, as in \"507f1f77bcf86cd799439011\"."
|
|
504
|
+
});
|
|
505
|
+
};
|
|
506
|
+
/** The numbers of a written list as a vector stores them, each finite. */
|
|
507
|
+
function vectorElements(elements) {
|
|
508
|
+
return elements.map((element, index) => {
|
|
509
|
+
const number = Number(element);
|
|
510
|
+
if (Number.isFinite(number)) return number;
|
|
511
|
+
throw structuredError("CONTRACT.CAST_REFUSED", `a vector element is a finite number, and element ${index + 1} is ${String(element)}.`, {
|
|
512
|
+
why: "A vector holds finite numbers, as a vector search index reads them.",
|
|
513
|
+
fix: "Write each element as a finite number."
|
|
514
|
+
});
|
|
515
|
+
});
|
|
516
|
+
}
|
|
476
517
|
const mongoString = mongoDataType("mongo/string", {
|
|
477
518
|
read: (json) => readJsonString("mongo/string", json),
|
|
478
519
|
bsonTypes: ["string"]
|
|
479
520
|
});
|
|
521
|
+
const mongoInt32 = mongoDataType("mongo/int32", {
|
|
522
|
+
read: (json) => readInt32Json("mongo/int32", json),
|
|
523
|
+
bsonTypes: ["int"]
|
|
524
|
+
});
|
|
525
|
+
const mongoInt64 = mongoDataType("mongo/int64", {
|
|
526
|
+
read: (json) => readInt64Json("mongo/int64", json),
|
|
527
|
+
bsonTypes: ["long"],
|
|
528
|
+
casts: { [mongoInt32.id]: asNumeralText }
|
|
529
|
+
});
|
|
530
|
+
const mongoDecimal128 = mongoDataType("mongo/decimal128", {
|
|
531
|
+
read: (json) => readDecimal128Json("mongo/decimal128", json),
|
|
532
|
+
spell: spellDecimal128Json,
|
|
533
|
+
equalityKey: decimalTextEqualityKey,
|
|
534
|
+
bsonTypes: ["decimal"],
|
|
535
|
+
casts: {
|
|
536
|
+
[mongoInt32.id]: asNumeralText,
|
|
537
|
+
[mongoInt64.id]: castUnchanged
|
|
538
|
+
}
|
|
539
|
+
});
|
|
480
540
|
const mongoDouble = mongoDataType("mongo/double", {
|
|
481
541
|
read: (json) => {
|
|
482
542
|
readJsonFloat("mongo/double", json);
|
|
483
543
|
return json;
|
|
484
544
|
},
|
|
485
|
-
bsonTypes: ["double"]
|
|
545
|
+
bsonTypes: ["double"],
|
|
546
|
+
casts: {
|
|
547
|
+
[mongoInt32.id]: asFloat,
|
|
548
|
+
[mongoInt64.id]: asFloat,
|
|
549
|
+
[mongoDecimal128.id]: asFloat
|
|
550
|
+
}
|
|
486
551
|
});
|
|
487
|
-
const
|
|
488
|
-
read: (json) =>
|
|
489
|
-
|
|
552
|
+
const mongoObjectId = mongoDataType("mongo/objectid", {
|
|
553
|
+
read: (json) => readObjectIdJson("mongo/objectid", json),
|
|
554
|
+
spell: spellObjectIdJson,
|
|
555
|
+
bsonTypes: ["objectId"],
|
|
556
|
+
casts: { [mongoString.id]: asObjectId }
|
|
490
557
|
});
|
|
491
558
|
const mongoBool = mongoDataType("mongo/bool", {
|
|
492
559
|
read: (json) => readJsonBoolean("mongo/bool", json),
|
|
@@ -499,16 +566,16 @@ const mongoDate = mongoDataType("mongo/date", {
|
|
|
499
566
|
const mongoVector = mongoDataType("mongo/vector", {
|
|
500
567
|
read: (json, params) => readVectorJson("mongo/vector", json, typeof params["length"] === "number" ? params["length"] : void 0),
|
|
501
568
|
bsonTypes: ["array"],
|
|
502
|
-
params: mongoVectorParams
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
569
|
+
params: mongoVectorParams,
|
|
570
|
+
listCast: {
|
|
571
|
+
of: [
|
|
572
|
+
mongoInt32.id,
|
|
573
|
+
mongoInt64.id,
|
|
574
|
+
mongoDecimal128.id,
|
|
575
|
+
mongoDouble.id
|
|
576
|
+
],
|
|
577
|
+
cast: vectorElements
|
|
578
|
+
}
|
|
512
579
|
});
|
|
513
580
|
const mongoBinary = mongoDataType("mongo/binary", {
|
|
514
581
|
read: (json) => readBinaryJson("mongo/binary", json),
|
|
@@ -549,7 +616,9 @@ const mongoDataTypes = [
|
|
|
549
616
|
mongoJson,
|
|
550
617
|
mongoBson
|
|
551
618
|
];
|
|
619
|
+
/** MongoDB's text data type: `string`. */
|
|
620
|
+
const mongoTextDataTypes = [mongoString.id];
|
|
552
621
|
//#endregion
|
|
553
|
-
export {
|
|
622
|
+
export { where as $, isBase64Text as A, mongoInt32 as B, int64Decode as C, int64NumberFromDigits as D, int64NumberEncode as E, mongoBson as F, mongoTextDataTypes as G, mongoJson as H, mongoDataTypes as I, objectIdEncode as J, mongoVector as K, mongoDate as L, isPlainObject as M, mongoBinary as N, int64NumberToJson as O, mongoBool as P, vectorEncode as Q, mongoDecimal128 as R, int32ToJson as S, int64NumberDecode as T, mongoObjectId as U, mongoInt64 as V, mongoString as W, readBsonJson as X, objectIdToJson as Y, stringEncode as Z, decimal128ToJson as _, booleanEncode as a, encodeBsonValue as b, bsonTypeTag as c, dateEncode as d, dateToJson as f, decimal128HoldsExactly as g, decimal128Encode as h, binaryToBase64 as i, isPlainArray as j, int64ToJson as k, child as l, decimal128Decode as m, binaryEncode as n, bsonClassTag as o, dbRefEntries as p, mongoVectorParams as q, binaryFromBase64 as r, bsonToJson as s, binaryDecode as t, constructorName as u, decodeBsonValue as v, int64Encode as w, int32Encode as x, doubleEncode as y, mongoDouble as z };
|
|
554
623
|
|
|
555
|
-
//# sourceMappingURL=data-types-
|
|
624
|
+
//# sourceMappingURL=data-types-hoKLBrYZ-CkFtgHfm.mjs.map
|