@atscript/db-mongo 0.1.187 → 0.1.189

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/plugin.cjs CHANGED
@@ -75,6 +75,84 @@ const searchStrategies = [
75
75
  const strategyDescription = "How `search()` matches a term against this index. Locks the query shape into the index — there is no query-time mode switching.\n\n- `compound` (default) → rank exact-word hits above prefix hits: a wildcard `text` clause **plus** one `autocomplete` clause per autocomplete field. Degrades to plain `text` when the index has no autocomplete field.\n- `autocomplete` → **prefix/typeahead only** — query just the autocomplete fields, no word-match ranking clause.\n- `text` → **word matching only** — a single `text` operator over all string-mapped fields (autocomplete fields are matched via their companion `string` mapping).\n\nTo use the same data with a different strategy, declare a second index and select it per request with `$index`.";
76
76
  const fuzzyDescription = "Maximum typo tolerance, applied **at query time** to the search operator.\n\n- `0` (default) → no fuzzy matching (exact tokens).\n- `1` → allows small typos (e.g., `\"mongo\"` ≈ `\"mango\"`).\n- `2` → more typo tolerance (e.g., `\"mongodb\"` ≈ `\"mangodb\"`).\n\nAtlas only accepts an edit distance of `1` or `2`; `0` simply disables fuzzy. Can be overridden per request via the `$fuzzy` query control.";
77
77
  /**
78
+ * What a declared type holds besides strings (`"a number"`, `"an object"`),
79
+ * or `undefined` when it holds strings only — `null` / `undefined` members
80
+ * aside — or does not resolve (reported elsewhere). The one rule of the
81
+ * Atlas Search field annotations that map a field as `string`: a string,
82
+ * string literals, or an array of them.
83
+ */
84
+ function nonStringValue(node, doc, depth = 0) {
85
+ if (!node || depth > 16) return void 0;
86
+ if (node.entity === "const") return node.token("identifier")?.type === "text" ? void 0 : `the literal ${node.id}`;
87
+ if ((0, _atscript_core.isPrimitive)(node)) {
88
+ if (node.tags.has("mongo") && node.tags.has("objectId")) return "a mongo.objectId";
89
+ const base = (0, _atscript_db_shared.primitiveBaseType)(node);
90
+ if (base === void 0 || base === "string" || base === "null" || base === "void") return;
91
+ if (base === "any") return "an unknown value";
92
+ if (base === "number" && node.tags.has("int")) return "an integer";
93
+ return `${/^[aeiou]/.test(base) ? "an" : "a"} ${base}`;
94
+ }
95
+ if ((0, _atscript_core.isRef)(node)) {
96
+ if (!node.hasChain && node.id === "null") return void 0;
97
+ return nonStringValue(doc.unwindType(node.id, node.chain)?.def, doc, depth + 1);
98
+ }
99
+ if ((0, _atscript_core.isArray)(node)) {
100
+ const item = nonStringValue(node.getDefinition(), doc, depth + 1);
101
+ return item && `${item} in an array`;
102
+ }
103
+ if (node.entity === "type") return nonStringValue(node.getDefinition(), doc, depth + 1);
104
+ if ((0, _atscript_core.isInterface)(node) || (0, _atscript_core.isStructure)(node)) return "an object";
105
+ if ((0, _atscript_core.isGroup)(node)) for (const member of node.unwrap()) {
106
+ const found = nonStringValue(member, doc, depth + 1);
107
+ if (found) return found;
108
+ }
109
+ }
110
+ /**
111
+ * The prop an annotation on `field` describes: the prop itself, or — for an
112
+ * `annotate` block entry — the prop the entry names (`undefined` when it names
113
+ * several, through a union).
114
+ */
115
+ function describedProp(field, doc) {
116
+ if (!(0, _atscript_core.isRef)(field)) return {
117
+ prop: field,
118
+ doc
119
+ };
120
+ const at = field.token("identifier")?.range.start;
121
+ const block = at && doc.annotateBlockAt(at.line, at.character);
122
+ const found = block && doc.unwindType(block.targetName, [field.id, ...field.chain.map((c) => c.text)]);
123
+ return found?.node ? {
124
+ prop: found.node,
125
+ doc: found.doc
126
+ } : void 0;
127
+ }
128
+ /**
129
+ * `validate` of a field annotation that maps the field as an Atlas Search
130
+ * `string`: the ciphertext refusal, then the rule that the field's declared
131
+ * type holds strings only.
132
+ */
133
+ function stringMappingValidate(annotation) {
134
+ return (token, _args, doc) => {
135
+ const messages = [];
136
+ const ciphertext = (0, _atscript_db_shared.encryptedSearchProblem)(token, doc, annotation);
137
+ if (ciphertext) messages.push(ciphertext);
138
+ const field = token.parentNode;
139
+ const target = doc.annotatedDefinition(field);
140
+ const held = target && nonStringValue(target.def, target.doc);
141
+ if (held) {
142
+ const described = describedProp(field, doc);
143
+ const verdict = described && (0, _atscript_db_shared.searchFieldVerdict)(described.prop, described.doc);
144
+ const instead = verdict && "kind" in verdict && verdict.kind === "integer" ? "to match it by exact number, list it in @db.index.fulltext; for text search, annotate a string field" : "annotate a string field instead";
145
+ const name = (0, _atscript_core.isRef)(field) ? [field.id, ...field.chain.map((c) => c.text)].join(".") : field.id;
146
+ messages.push({
147
+ message: `${annotation} on "${name}" needs a field whose declared type holds only strings (a string, string literals, or an array of them), and this field holds ${held}; ${instead}`,
148
+ severity: 1,
149
+ range: token.range
150
+ });
151
+ }
152
+ return messages;
153
+ };
154
+ }
155
+ /**
78
156
  * MongoDB-specific annotations.
79
157
  *
80
158
  * Merged into the global config under `{ db: { mongo: ... } }` so they
@@ -211,9 +289,9 @@ const annotations = {
211
289
  ]
212
290
  }),
213
291
  text: new _atscript_core.AnnotationSpec({
214
- description: (0, _atscript_core.annotationExample)("Marks a field to be **included in a MongoDB Atlas Search Index** defined by `@db.mongo.search.static`.\n\n- **The field has to reference an existing search index name**.\n- If index name is not defined, a new search index with default attributes will be created.", "@db.mongo.search.text \"lucene.english\", \"mySearchIndex\"", "firstName: string"),
292
+ description: (0, _atscript_core.annotationExample)("Marks a field to be **included in a MongoDB Atlas Search Index** defined by `@db.mongo.search.static`.\n\n- **The field has to reference an existing search index name**.\n- If index name is not defined, a new search index with default attributes will be created.\n- The field's declared type must hold only **strings** (a string, string literals, or an array of them); any other type is a compile error. To search an integer by exact number, use `@db.index.fulltext`.", "@db.mongo.search.text \"lucene.english\", \"mySearchIndex\"", "firstName: string"),
215
293
  nodeType: ["prop"],
216
- validate: (0, _atscript_db_shared.refuseCiphertext)("@db.mongo.search.text"),
294
+ validate: stringMappingValidate("@db.mongo.search.text"),
217
295
  passedWhenReferred: false,
218
296
  multiple: true,
219
297
  argument: [{
@@ -230,9 +308,9 @@ const annotations = {
230
308
  }]
231
309
  }),
232
310
  autocomplete: new _atscript_core.AnnotationSpec({
233
- description: (0, _atscript_core.annotationExample)("Marks a field for **prefix / typeahead (as-you-type)** matching in a MongoDB Atlas Search Index.\n\n- Indexes the field as the Atlas **`autocomplete`** type, **and** double-maps it as `string` so exact-word hits still rank.\n- Lets `search()` match partial words: with the default `edgeGram` tokenization, `\"art\"` matches `\"Artem\"` **as you type** (no whole word required).\n- Use `nGram` tokenization for true mid-word (infix/substring) matching at higher index cost.\n- Like `@db.mongo.search.text`, the field joins the index named by `indexName` (or the default index).", "@db.mongo.search.autocomplete \"users\"", "username: string"),
311
+ description: (0, _atscript_core.annotationExample)("Marks a field for **prefix / typeahead (as-you-type)** matching in a MongoDB Atlas Search Index.\n\n- Indexes the field as the Atlas **`autocomplete`** type, **and** double-maps it as `string` so exact-word hits still rank.\n- Lets `search()` match partial words: with the default `edgeGram` tokenization, `\"art\"` matches `\"Artem\"` **as you type** (no whole word required).\n- Use `nGram` tokenization for true mid-word (infix/substring) matching at higher index cost.\n- Like `@db.mongo.search.text`, the field joins the index named by `indexName` (or the default index).\n- The field's declared type must hold only **strings** (a string, string literals, or an array of them); any other type is a compile error.", "@db.mongo.search.autocomplete \"users\"", "username: string"),
234
312
  nodeType: ["prop"],
235
- validate: (0, _atscript_db_shared.refuseCiphertext)("@db.mongo.search.autocomplete"),
313
+ validate: stringMappingValidate("@db.mongo.search.autocomplete"),
236
314
  passedWhenReferred: false,
237
315
  multiple: true,
238
316
  argument: [
package/dist/plugin.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  import { t as dynamicSearchEncryptedMessage } from "./search-messages-mQSpJ4GD.mjs";
2
- import { AnnotationSpec, annotationExample, isInterface, isPrimitive, isRef, isStructure } from "@atscript/core";
3
- import { findStoredField, refuseCiphertext } from "@atscript/db/shared";
2
+ import { AnnotationSpec, annotationExample, isArray, isGroup, isInterface, isPrimitive, isRef, isStructure } from "@atscript/core";
3
+ import { encryptedSearchProblem, findStoredField, primitiveBaseType, searchFieldVerdict } from "@atscript/db/shared";
4
4
  //#region src/plugin/annotations.ts
5
5
  /**
6
6
  * Atlas Search built-in analyzers, offered by completion. Not a closed list:
@@ -71,6 +71,84 @@ const searchStrategies = [
71
71
  const strategyDescription = "How `search()` matches a term against this index. Locks the query shape into the index — there is no query-time mode switching.\n\n- `compound` (default) → rank exact-word hits above prefix hits: a wildcard `text` clause **plus** one `autocomplete` clause per autocomplete field. Degrades to plain `text` when the index has no autocomplete field.\n- `autocomplete` → **prefix/typeahead only** — query just the autocomplete fields, no word-match ranking clause.\n- `text` → **word matching only** — a single `text` operator over all string-mapped fields (autocomplete fields are matched via their companion `string` mapping).\n\nTo use the same data with a different strategy, declare a second index and select it per request with `$index`.";
72
72
  const fuzzyDescription = "Maximum typo tolerance, applied **at query time** to the search operator.\n\n- `0` (default) → no fuzzy matching (exact tokens).\n- `1` → allows small typos (e.g., `\"mongo\"` ≈ `\"mango\"`).\n- `2` → more typo tolerance (e.g., `\"mongodb\"` ≈ `\"mangodb\"`).\n\nAtlas only accepts an edit distance of `1` or `2`; `0` simply disables fuzzy. Can be overridden per request via the `$fuzzy` query control.";
73
73
  /**
74
+ * What a declared type holds besides strings (`"a number"`, `"an object"`),
75
+ * or `undefined` when it holds strings only — `null` / `undefined` members
76
+ * aside — or does not resolve (reported elsewhere). The one rule of the
77
+ * Atlas Search field annotations that map a field as `string`: a string,
78
+ * string literals, or an array of them.
79
+ */
80
+ function nonStringValue(node, doc, depth = 0) {
81
+ if (!node || depth > 16) return void 0;
82
+ if (node.entity === "const") return node.token("identifier")?.type === "text" ? void 0 : `the literal ${node.id}`;
83
+ if (isPrimitive(node)) {
84
+ if (node.tags.has("mongo") && node.tags.has("objectId")) return "a mongo.objectId";
85
+ const base = primitiveBaseType(node);
86
+ if (base === void 0 || base === "string" || base === "null" || base === "void") return;
87
+ if (base === "any") return "an unknown value";
88
+ if (base === "number" && node.tags.has("int")) return "an integer";
89
+ return `${/^[aeiou]/.test(base) ? "an" : "a"} ${base}`;
90
+ }
91
+ if (isRef(node)) {
92
+ if (!node.hasChain && node.id === "null") return void 0;
93
+ return nonStringValue(doc.unwindType(node.id, node.chain)?.def, doc, depth + 1);
94
+ }
95
+ if (isArray(node)) {
96
+ const item = nonStringValue(node.getDefinition(), doc, depth + 1);
97
+ return item && `${item} in an array`;
98
+ }
99
+ if (node.entity === "type") return nonStringValue(node.getDefinition(), doc, depth + 1);
100
+ if (isInterface(node) || isStructure(node)) return "an object";
101
+ if (isGroup(node)) for (const member of node.unwrap()) {
102
+ const found = nonStringValue(member, doc, depth + 1);
103
+ if (found) return found;
104
+ }
105
+ }
106
+ /**
107
+ * The prop an annotation on `field` describes: the prop itself, or — for an
108
+ * `annotate` block entry — the prop the entry names (`undefined` when it names
109
+ * several, through a union).
110
+ */
111
+ function describedProp(field, doc) {
112
+ if (!isRef(field)) return {
113
+ prop: field,
114
+ doc
115
+ };
116
+ const at = field.token("identifier")?.range.start;
117
+ const block = at && doc.annotateBlockAt(at.line, at.character);
118
+ const found = block && doc.unwindType(block.targetName, [field.id, ...field.chain.map((c) => c.text)]);
119
+ return found?.node ? {
120
+ prop: found.node,
121
+ doc: found.doc
122
+ } : void 0;
123
+ }
124
+ /**
125
+ * `validate` of a field annotation that maps the field as an Atlas Search
126
+ * `string`: the ciphertext refusal, then the rule that the field's declared
127
+ * type holds strings only.
128
+ */
129
+ function stringMappingValidate(annotation) {
130
+ return (token, _args, doc) => {
131
+ const messages = [];
132
+ const ciphertext = encryptedSearchProblem(token, doc, annotation);
133
+ if (ciphertext) messages.push(ciphertext);
134
+ const field = token.parentNode;
135
+ const target = doc.annotatedDefinition(field);
136
+ const held = target && nonStringValue(target.def, target.doc);
137
+ if (held) {
138
+ const described = describedProp(field, doc);
139
+ const verdict = described && searchFieldVerdict(described.prop, described.doc);
140
+ const instead = verdict && "kind" in verdict && verdict.kind === "integer" ? "to match it by exact number, list it in @db.index.fulltext; for text search, annotate a string field" : "annotate a string field instead";
141
+ const name = isRef(field) ? [field.id, ...field.chain.map((c) => c.text)].join(".") : field.id;
142
+ messages.push({
143
+ message: `${annotation} on "${name}" needs a field whose declared type holds only strings (a string, string literals, or an array of them), and this field holds ${held}; ${instead}`,
144
+ severity: 1,
145
+ range: token.range
146
+ });
147
+ }
148
+ return messages;
149
+ };
150
+ }
151
+ /**
74
152
  * MongoDB-specific annotations.
75
153
  *
76
154
  * Merged into the global config under `{ db: { mongo: ... } }` so they
@@ -207,9 +285,9 @@ const annotations = {
207
285
  ]
208
286
  }),
209
287
  text: new AnnotationSpec({
210
- description: annotationExample("Marks a field to be **included in a MongoDB Atlas Search Index** defined by `@db.mongo.search.static`.\n\n- **The field has to reference an existing search index name**.\n- If index name is not defined, a new search index with default attributes will be created.", "@db.mongo.search.text \"lucene.english\", \"mySearchIndex\"", "firstName: string"),
288
+ description: annotationExample("Marks a field to be **included in a MongoDB Atlas Search Index** defined by `@db.mongo.search.static`.\n\n- **The field has to reference an existing search index name**.\n- If index name is not defined, a new search index with default attributes will be created.\n- The field's declared type must hold only **strings** (a string, string literals, or an array of them); any other type is a compile error. To search an integer by exact number, use `@db.index.fulltext`.", "@db.mongo.search.text \"lucene.english\", \"mySearchIndex\"", "firstName: string"),
211
289
  nodeType: ["prop"],
212
- validate: refuseCiphertext("@db.mongo.search.text"),
290
+ validate: stringMappingValidate("@db.mongo.search.text"),
213
291
  passedWhenReferred: false,
214
292
  multiple: true,
215
293
  argument: [{
@@ -226,9 +304,9 @@ const annotations = {
226
304
  }]
227
305
  }),
228
306
  autocomplete: new AnnotationSpec({
229
- description: annotationExample("Marks a field for **prefix / typeahead (as-you-type)** matching in a MongoDB Atlas Search Index.\n\n- Indexes the field as the Atlas **`autocomplete`** type, **and** double-maps it as `string` so exact-word hits still rank.\n- Lets `search()` match partial words: with the default `edgeGram` tokenization, `\"art\"` matches `\"Artem\"` **as you type** (no whole word required).\n- Use `nGram` tokenization for true mid-word (infix/substring) matching at higher index cost.\n- Like `@db.mongo.search.text`, the field joins the index named by `indexName` (or the default index).", "@db.mongo.search.autocomplete \"users\"", "username: string"),
307
+ description: annotationExample("Marks a field for **prefix / typeahead (as-you-type)** matching in a MongoDB Atlas Search Index.\n\n- Indexes the field as the Atlas **`autocomplete`** type, **and** double-maps it as `string` so exact-word hits still rank.\n- Lets `search()` match partial words: with the default `edgeGram` tokenization, `\"art\"` matches `\"Artem\"` **as you type** (no whole word required).\n- Use `nGram` tokenization for true mid-word (infix/substring) matching at higher index cost.\n- Like `@db.mongo.search.text`, the field joins the index named by `indexName` (or the default index).\n- The field's declared type must hold only **strings** (a string, string literals, or an array of them); any other type is a compile error.", "@db.mongo.search.autocomplete \"users\"", "username: string"),
230
308
  nodeType: ["prop"],
231
- validate: refuseCiphertext("@db.mongo.search.autocomplete"),
309
+ validate: stringMappingValidate("@db.mongo.search.autocomplete"),
232
310
  passedWhenReferred: false,
233
311
  multiple: true,
234
312
  argument: [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atscript/db-mongo",
3
- "version": "0.1.187",
3
+ "version": "0.1.189",
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.15",
50
- "@atscript/typescript": "^0.2.15",
49
+ "@atscript/core": "^0.2.17",
50
+ "@atscript/typescript": "^0.2.17",
51
51
  "@uniqu/core": "^0.2.2",
52
52
  "mongodb": "^6.17.0",
53
53
  "mongodb-memory-server-core": "^10.0.0",
54
- "unplugin-atscript": "^0.2.15"
54
+ "unplugin-atscript": "^0.2.17"
55
55
  },
56
56
  "peerDependencies": {
57
- "@atscript/core": "^0.2.15",
58
- "@atscript/typescript": "^0.2.15",
57
+ "@atscript/core": "^0.2.17",
58
+ "@atscript/typescript": "^0.2.17",
59
59
  "@uniqu/core": "^0.2.2",
60
60
  "mongodb": "^6.17.0",
61
- "@atscript/db": "^0.1.187"
61
+ "@atscript/db": "^0.1.189"
62
62
  },
63
63
  "scripts": {
64
64
  "build": "vp pack",