@atscript/db-mongo 0.1.172 → 0.1.174
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/index.cjs +72 -27
- package/dist/index.d.cts +4 -1
- package/dist/index.d.mts +4 -1
- package/dist/index.mjs +73 -28
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -50,19 +50,69 @@ async function wrapInvalidQuery(fn) {
|
|
|
50
50
|
try {
|
|
51
51
|
return await fn();
|
|
52
52
|
} catch (error) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
53
|
+
const code = serverErrorCode(error);
|
|
54
|
+
if (code === 31249 || code === 31254) throw new _atscript_db.DbError("INVALID_QUERY", [{
|
|
55
|
+
path: "$select",
|
|
56
|
+
message: error.message
|
|
57
|
+
}]);
|
|
58
|
+
if (code === UNKNOWN_TIME_ZONE) {
|
|
59
|
+
const message = error.message;
|
|
60
|
+
const zone = /time zone identifier:\s*"([^"]*)"/.exec(message)?.[1];
|
|
61
|
+
throw (0, _atscript_db.bucketTimeZoneUnavailable)(zone ? `MongoDB does not recognize time zone "${zone}" — its time zone database may be outdated` : `MongoDB does not recognize a bucket time zone: ${message}`);
|
|
62
62
|
}
|
|
63
63
|
throw error;
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
|
+
/** The numeric server code of a driver error, `undefined` for anything else. */
|
|
67
|
+
function serverErrorCode(error) {
|
|
68
|
+
if (!(error instanceof Error)) return void 0;
|
|
69
|
+
const code = error.code;
|
|
70
|
+
return typeof code === "number" ? code : void 0;
|
|
71
|
+
}
|
|
72
|
+
/** `DuplicateKey` (E11000) and its legacy update form `DuplicateKeyValue` (E11001). */
|
|
73
|
+
function isDuplicateKeyCode(code) {
|
|
74
|
+
return code === 11e3 || code === 11001;
|
|
75
|
+
}
|
|
76
|
+
/** A bulk error's per-statement write errors (`MongoBulkWriteError.writeErrors`) — `[]` for other errors. */
|
|
77
|
+
function bulkWriteErrors(error) {
|
|
78
|
+
if (!(error instanceof Error)) return [];
|
|
79
|
+
return [error.writeErrors].flat().filter((w) => !!w);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Whether a driver error carries a write-concern failure (`MongoWriteConcernError`,
|
|
83
|
+
* or a bulk error whose result holds one): the write's durability is unknown, so it
|
|
84
|
+
* is never read as a duplicate key, whatever its write errors say.
|
|
85
|
+
*/
|
|
86
|
+
function hasWriteConcernError(error) {
|
|
87
|
+
const e = error;
|
|
88
|
+
return Boolean(e.writeConcernError || e.result?.getWriteConcernError?.() || e.result?.writeConcernErrors?.length);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The duplicate-key violation a write failed with, or `undefined` when the error
|
|
92
|
+
* is anything else — for every shape the driver reports one in:
|
|
93
|
+
* - a `MongoServerError` (single-document writes: `code` 11000, `keyPattern`);
|
|
94
|
+
* - a `MongoBulkWriteError` (`insertMany` / `bulkWrite` — the table layer sends
|
|
95
|
+
* every insert through `insertMany`): 11000 on top and in `writeErrors`, or in
|
|
96
|
+
* `writeErrors` only — a bulk error counts only when EVERY write error is a
|
|
97
|
+
* duplicate key (another failure in the batch is not a conflict);
|
|
98
|
+
* - a bare `WriteError` (`code` getter, `errmsg`).
|
|
99
|
+
*
|
|
100
|
+
* A write-concern failure is never a duplicate key (see {@link hasWriteConcernError}).
|
|
101
|
+
*/
|
|
102
|
+
function duplicateKeyViolation(error) {
|
|
103
|
+
if (error === null || typeof error !== "object" || hasWriteConcernError(error)) return void 0;
|
|
104
|
+
const e = error;
|
|
105
|
+
const writeErrors = bulkWriteErrors(error);
|
|
106
|
+
if (!(writeErrors.length > 0 ? writeErrors.map((w) => w.code) : [e.code]).every(isDuplicateKeyCode)) return void 0;
|
|
107
|
+
return {
|
|
108
|
+
message: [
|
|
109
|
+
e.message,
|
|
110
|
+
e.errmsg,
|
|
111
|
+
writeErrors[0]?.errmsg
|
|
112
|
+
].find((m) => typeof m === "string" && m !== "") ?? "E11000 duplicate key error",
|
|
113
|
+
keyPattern: e.keyPattern
|
|
114
|
+
};
|
|
115
|
+
}
|
|
66
116
|
//#endregion
|
|
67
117
|
//#region src/lib/collection-patcher.ts
|
|
68
118
|
/**
|
|
@@ -2279,7 +2329,7 @@ async function probe(client) {
|
|
|
2279
2329
|
try {
|
|
2280
2330
|
reply = await admin.command({ hello: 1 });
|
|
2281
2331
|
} catch (error) {
|
|
2282
|
-
if (
|
|
2332
|
+
if (serverErrorCode(error) !== 59) throw error;
|
|
2283
2333
|
reply = await admin.command({ isMaster: 1 });
|
|
2284
2334
|
}
|
|
2285
2335
|
return reply.setName != null || reply.isreplicaset === true || reply.msg === "isdbgrid";
|
|
@@ -2309,11 +2359,6 @@ function objectIdFromStorage(value) {
|
|
|
2309
2359
|
* predicates (see {@link MongoAdapter.supportsRelationFilters}).
|
|
2310
2360
|
*/
|
|
2311
2361
|
const REL_WRITE_BATCH = 1e3;
|
|
2312
|
-
/** Whether a bulk-write error carries a write-concern failure besides (or instead of) write errors. */
|
|
2313
|
-
function hasWriteConcernError(error) {
|
|
2314
|
-
const e = error;
|
|
2315
|
-
return Boolean(e.writeConcernError || e.result?.getWriteConcernError?.() || e.result?.writeConcernErrors?.length);
|
|
2316
|
-
}
|
|
2317
2362
|
var MongoAdapter = class MongoAdapter extends _atscript_db.BaseDbAdapter {
|
|
2318
2363
|
db;
|
|
2319
2364
|
client;
|
|
@@ -3087,12 +3132,16 @@ var MongoAdapter = class MongoAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
3087
3132
|
return this._mapConstraintError(error);
|
|
3088
3133
|
}
|
|
3089
3134
|
}
|
|
3090
|
-
/**
|
|
3135
|
+
/**
|
|
3136
|
+
* Rethrows a duplicate-key error as `CONFLICT` — whatever driver error class
|
|
3137
|
+
* carries it (see {@link duplicateKeyViolation}) — anything else as is.
|
|
3138
|
+
*/
|
|
3091
3139
|
_mapConstraintError(error) {
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3140
|
+
const duplicate = duplicateKeyViolation(error);
|
|
3141
|
+
if (duplicate) throw (0, _atscript_db.constraintViolation)("CONFLICT", {
|
|
3142
|
+
columns: duplicate.keyPattern ? Object.keys(duplicate.keyPattern) : void 0,
|
|
3143
|
+
name: duplicate.message.match(/index: (\S+) dup key/)?.[1]
|
|
3144
|
+
}, duplicate.message);
|
|
3096
3145
|
throw error;
|
|
3097
3146
|
}
|
|
3098
3147
|
/** An update-shaped write under {@link _wrapDuplicateKeyError}, reduced to `TDbUpdateResult`. */
|
|
@@ -3163,13 +3212,9 @@ var MongoAdapter = class MongoAdapter extends _atscript_db.BaseDbAdapter {
|
|
|
3163
3212
|
...this._getSessionOpts()
|
|
3164
3213
|
});
|
|
3165
3214
|
} catch (error) {
|
|
3166
|
-
|
|
3167
|
-
|
|
3168
|
-
|
|
3169
|
-
for (const writeError of writeErrors) {
|
|
3170
|
-
if (writeError.code !== 11e3) throw error;
|
|
3171
|
-
skipped.add(writeError.index);
|
|
3172
|
-
}
|
|
3215
|
+
const writeErrors = bulkWriteErrors(error);
|
|
3216
|
+
if (writeErrors.length === 0 || !duplicateKeyViolation(error)) this._mapConstraintError(error);
|
|
3217
|
+
for (const writeError of writeErrors) skipped.add(writeError.index);
|
|
3173
3218
|
}
|
|
3174
3219
|
return data.map((item, i) => skipped.has(i) ? null : { insertedId: idOf(item) });
|
|
3175
3220
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -576,7 +576,10 @@ declare class MongoAdapter extends BaseDbAdapter {
|
|
|
576
576
|
* so a unique-index violation is a `CONFLICT` whichever statement hit it.
|
|
577
577
|
*/
|
|
578
578
|
private _wrapDuplicateKeyError;
|
|
579
|
-
/**
|
|
579
|
+
/**
|
|
580
|
+
* Rethrows a duplicate-key error as `CONFLICT` — whatever driver error class
|
|
581
|
+
* carries it (see {@link duplicateKeyViolation}) — anything else as is.
|
|
582
|
+
*/
|
|
580
583
|
private _mapConstraintError;
|
|
581
584
|
/** An update-shaped write under {@link _wrapDuplicateKeyError}, reduced to `TDbUpdateResult`. */
|
|
582
585
|
private _wrapUpdate;
|
package/dist/index.d.mts
CHANGED
|
@@ -576,7 +576,10 @@ declare class MongoAdapter extends BaseDbAdapter {
|
|
|
576
576
|
* so a unique-index violation is a `CONFLICT` whichever statement hit it.
|
|
577
577
|
*/
|
|
578
578
|
private _wrapDuplicateKeyError;
|
|
579
|
-
/**
|
|
579
|
+
/**
|
|
580
|
+
* Rethrows a duplicate-key error as `CONFLICT` — whatever driver error class
|
|
581
|
+
* carries it (see {@link duplicateKeyViolation}) — anything else as is.
|
|
582
|
+
*/
|
|
580
583
|
private _mapConstraintError;
|
|
581
584
|
/** An update-shaped write under {@link _wrapDuplicateKeyError}, reduced to `TDbUpdateResult`. */
|
|
582
585
|
private _wrapUpdate;
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { a as buildMongoQuery, c as planStages, f as sortStages, h as queryNodeToExpr, i as buildMongoFilter, l as correlate, m as orNull, n as distinctCountExpr, o as collationOfAdapter, p as exprToMongo, r as REL_FILTER_TEMP_PREFIX, s as mongoFilterStages, t as buildAccumulator, u as hasNullsPlacement } from "./mongo-accumulator-D4drEz0-.mjs";
|
|
2
2
|
import { t as dynamicSearchEncryptedMessage } from "./search-messages-mQSpJ4GD.mjs";
|
|
3
3
|
import { ALL_AGGREGATE_FNS, ALL_BUCKET_UNITS, ALL_VIEW_CAPABILITIES, BaseDbAdapter, DbError, DbSpace, andFilters, bucketTimeZoneUnavailable, computeInsights, constraintViolation, containsRelationFilter, createChangeRecorder, createFailureCollector, defaultFulltextIndex, geoIndexNotFoundMessage, getDbFieldOp, getKeyProps, getPath, isAtscriptDbView, isFieldRef, isPlainObject, isViewType, parseDefaultLiteral, queryReadColumns, relationStaticFilter, resolveDesignType, searchIndexNotFoundMessage, searchTermInteger, tableNameOf, translateQueryTree, uniqueKeyTuple, vectorIndexNotFoundMessage } from "@atscript/db";
|
|
4
|
-
import {
|
|
4
|
+
import { MongoClient, ObjectId } from "mongodb";
|
|
5
5
|
import { resolveAggregateSearch } from "@atscript/db/agg";
|
|
6
6
|
//#region src/lib/path-utils.ts
|
|
7
7
|
function joinPath(prefix, segment) {
|
|
@@ -49,19 +49,69 @@ async function wrapInvalidQuery(fn) {
|
|
|
49
49
|
try {
|
|
50
50
|
return await fn();
|
|
51
51
|
} catch (error) {
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
52
|
+
const code = serverErrorCode(error);
|
|
53
|
+
if (code === 31249 || code === 31254) throw new DbError("INVALID_QUERY", [{
|
|
54
|
+
path: "$select",
|
|
55
|
+
message: error.message
|
|
56
|
+
}]);
|
|
57
|
+
if (code === UNKNOWN_TIME_ZONE) {
|
|
58
|
+
const message = error.message;
|
|
59
|
+
const zone = /time zone identifier:\s*"([^"]*)"/.exec(message)?.[1];
|
|
60
|
+
throw bucketTimeZoneUnavailable(zone ? `MongoDB does not recognize time zone "${zone}" — its time zone database may be outdated` : `MongoDB does not recognize a bucket time zone: ${message}`);
|
|
61
61
|
}
|
|
62
62
|
throw error;
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
|
+
/** The numeric server code of a driver error, `undefined` for anything else. */
|
|
66
|
+
function serverErrorCode(error) {
|
|
67
|
+
if (!(error instanceof Error)) return void 0;
|
|
68
|
+
const code = error.code;
|
|
69
|
+
return typeof code === "number" ? code : void 0;
|
|
70
|
+
}
|
|
71
|
+
/** `DuplicateKey` (E11000) and its legacy update form `DuplicateKeyValue` (E11001). */
|
|
72
|
+
function isDuplicateKeyCode(code) {
|
|
73
|
+
return code === 11e3 || code === 11001;
|
|
74
|
+
}
|
|
75
|
+
/** A bulk error's per-statement write errors (`MongoBulkWriteError.writeErrors`) — `[]` for other errors. */
|
|
76
|
+
function bulkWriteErrors(error) {
|
|
77
|
+
if (!(error instanceof Error)) return [];
|
|
78
|
+
return [error.writeErrors].flat().filter((w) => !!w);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Whether a driver error carries a write-concern failure (`MongoWriteConcernError`,
|
|
82
|
+
* or a bulk error whose result holds one): the write's durability is unknown, so it
|
|
83
|
+
* is never read as a duplicate key, whatever its write errors say.
|
|
84
|
+
*/
|
|
85
|
+
function hasWriteConcernError(error) {
|
|
86
|
+
const e = error;
|
|
87
|
+
return Boolean(e.writeConcernError || e.result?.getWriteConcernError?.() || e.result?.writeConcernErrors?.length);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The duplicate-key violation a write failed with, or `undefined` when the error
|
|
91
|
+
* is anything else — for every shape the driver reports one in:
|
|
92
|
+
* - a `MongoServerError` (single-document writes: `code` 11000, `keyPattern`);
|
|
93
|
+
* - a `MongoBulkWriteError` (`insertMany` / `bulkWrite` — the table layer sends
|
|
94
|
+
* every insert through `insertMany`): 11000 on top and in `writeErrors`, or in
|
|
95
|
+
* `writeErrors` only — a bulk error counts only when EVERY write error is a
|
|
96
|
+
* duplicate key (another failure in the batch is not a conflict);
|
|
97
|
+
* - a bare `WriteError` (`code` getter, `errmsg`).
|
|
98
|
+
*
|
|
99
|
+
* A write-concern failure is never a duplicate key (see {@link hasWriteConcernError}).
|
|
100
|
+
*/
|
|
101
|
+
function duplicateKeyViolation(error) {
|
|
102
|
+
if (error === null || typeof error !== "object" || hasWriteConcernError(error)) return void 0;
|
|
103
|
+
const e = error;
|
|
104
|
+
const writeErrors = bulkWriteErrors(error);
|
|
105
|
+
if (!(writeErrors.length > 0 ? writeErrors.map((w) => w.code) : [e.code]).every(isDuplicateKeyCode)) return void 0;
|
|
106
|
+
return {
|
|
107
|
+
message: [
|
|
108
|
+
e.message,
|
|
109
|
+
e.errmsg,
|
|
110
|
+
writeErrors[0]?.errmsg
|
|
111
|
+
].find((m) => typeof m === "string" && m !== "") ?? "E11000 duplicate key error",
|
|
112
|
+
keyPattern: e.keyPattern
|
|
113
|
+
};
|
|
114
|
+
}
|
|
65
115
|
//#endregion
|
|
66
116
|
//#region src/lib/collection-patcher.ts
|
|
67
117
|
/**
|
|
@@ -2278,7 +2328,7 @@ async function probe(client) {
|
|
|
2278
2328
|
try {
|
|
2279
2329
|
reply = await admin.command({ hello: 1 });
|
|
2280
2330
|
} catch (error) {
|
|
2281
|
-
if (
|
|
2331
|
+
if (serverErrorCode(error) !== 59) throw error;
|
|
2282
2332
|
reply = await admin.command({ isMaster: 1 });
|
|
2283
2333
|
}
|
|
2284
2334
|
return reply.setName != null || reply.isreplicaset === true || reply.msg === "isdbgrid";
|
|
@@ -2308,11 +2358,6 @@ function objectIdFromStorage(value) {
|
|
|
2308
2358
|
* predicates (see {@link MongoAdapter.supportsRelationFilters}).
|
|
2309
2359
|
*/
|
|
2310
2360
|
const REL_WRITE_BATCH = 1e3;
|
|
2311
|
-
/** Whether a bulk-write error carries a write-concern failure besides (or instead of) write errors. */
|
|
2312
|
-
function hasWriteConcernError(error) {
|
|
2313
|
-
const e = error;
|
|
2314
|
-
return Boolean(e.writeConcernError || e.result?.getWriteConcernError?.() || e.result?.writeConcernErrors?.length);
|
|
2315
|
-
}
|
|
2316
2361
|
var MongoAdapter = class MongoAdapter extends BaseDbAdapter {
|
|
2317
2362
|
db;
|
|
2318
2363
|
client;
|
|
@@ -3086,12 +3131,16 @@ var MongoAdapter = class MongoAdapter extends BaseDbAdapter {
|
|
|
3086
3131
|
return this._mapConstraintError(error);
|
|
3087
3132
|
}
|
|
3088
3133
|
}
|
|
3089
|
-
/**
|
|
3134
|
+
/**
|
|
3135
|
+
* Rethrows a duplicate-key error as `CONFLICT` — whatever driver error class
|
|
3136
|
+
* carries it (see {@link duplicateKeyViolation}) — anything else as is.
|
|
3137
|
+
*/
|
|
3090
3138
|
_mapConstraintError(error) {
|
|
3091
|
-
|
|
3092
|
-
|
|
3093
|
-
|
|
3094
|
-
|
|
3139
|
+
const duplicate = duplicateKeyViolation(error);
|
|
3140
|
+
if (duplicate) throw constraintViolation("CONFLICT", {
|
|
3141
|
+
columns: duplicate.keyPattern ? Object.keys(duplicate.keyPattern) : void 0,
|
|
3142
|
+
name: duplicate.message.match(/index: (\S+) dup key/)?.[1]
|
|
3143
|
+
}, duplicate.message);
|
|
3095
3144
|
throw error;
|
|
3096
3145
|
}
|
|
3097
3146
|
/** An update-shaped write under {@link _wrapDuplicateKeyError}, reduced to `TDbUpdateResult`. */
|
|
@@ -3162,13 +3211,9 @@ var MongoAdapter = class MongoAdapter extends BaseDbAdapter {
|
|
|
3162
3211
|
...this._getSessionOpts()
|
|
3163
3212
|
});
|
|
3164
3213
|
} catch (error) {
|
|
3165
|
-
|
|
3166
|
-
|
|
3167
|
-
|
|
3168
|
-
for (const writeError of writeErrors) {
|
|
3169
|
-
if (writeError.code !== 11e3) throw error;
|
|
3170
|
-
skipped.add(writeError.index);
|
|
3171
|
-
}
|
|
3214
|
+
const writeErrors = bulkWriteErrors(error);
|
|
3215
|
+
if (writeErrors.length === 0 || !duplicateKeyViolation(error)) this._mapConstraintError(error);
|
|
3216
|
+
for (const writeError of writeErrors) skipped.add(writeError.index);
|
|
3172
3217
|
}
|
|
3173
3218
|
return data.map((item, i) => skipped.has(i) ? null : { insertedId: idOf(item) });
|
|
3174
3219
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atscript/db-mongo",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.174",
|
|
4
4
|
"description": "Mongodb plugin for atscript.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"atscript",
|
|
@@ -46,19 +46,19 @@
|
|
|
46
46
|
"access": "public"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@atscript/core": "^0.2.
|
|
50
|
-
"@atscript/typescript": "^0.2.
|
|
49
|
+
"@atscript/core": "^0.2.9",
|
|
50
|
+
"@atscript/typescript": "^0.2.9",
|
|
51
51
|
"@uniqu/core": "^0.2.1",
|
|
52
52
|
"mongodb": "^6.17.0",
|
|
53
53
|
"mongodb-memory-server-core": "^10.0.0",
|
|
54
|
-
"unplugin-atscript": "^0.2.
|
|
54
|
+
"unplugin-atscript": "^0.2.9"
|
|
55
55
|
},
|
|
56
56
|
"peerDependencies": {
|
|
57
|
-
"@atscript/core": "^0.2.
|
|
58
|
-
"@atscript/typescript": "^0.2.
|
|
57
|
+
"@atscript/core": "^0.2.9",
|
|
58
|
+
"@atscript/typescript": "^0.2.9",
|
|
59
59
|
"@uniqu/core": "^0.2.1",
|
|
60
60
|
"mongodb": "^6.17.0",
|
|
61
|
-
"@atscript/db": "^0.1.
|
|
61
|
+
"@atscript/db": "^0.1.174"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
64
|
"build": "vp pack",
|