@graphql-mesh/transport-mongo 0.0.0-stage → 0.1.0
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/CHANGELOG.md +8 -0
- package/dist/cjs/client.js +88 -0
- package/dist/cjs/execution.js +239 -0
- package/dist/cjs/filter.js +178 -0
- package/dist/cjs/index.js +16 -0
- package/dist/cjs/mapping.js +328 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/types.js +0 -0
- package/dist/esm/client.js +85 -0
- package/dist/esm/execution.js +236 -0
- package/dist/esm/filter.js +168 -0
- package/dist/esm/index.js +13 -0
- package/dist/esm/mapping.js +321 -0
- package/dist/esm/types.js +0 -0
- package/dist/package.json +49 -0
- package/dist/typings/client.d.cts +5 -0
- package/dist/typings/client.d.ts +5 -0
- package/dist/typings/execution.d.cts +9 -0
- package/dist/typings/execution.d.ts +9 -0
- package/dist/typings/filter.d.cts +24 -0
- package/dist/typings/filter.d.ts +24 -0
- package/dist/typings/index.d.cts +10 -0
- package/dist/typings/index.d.ts +10 -0
- package/dist/typings/mapping.d.cts +19 -0
- package/dist/typings/mapping.d.ts +19 -0
- package/dist/typings/types.d.cts +53 -0
- package/dist/typings/types.d.ts +53 -0
- package/package.json +55 -4
- package/src/client.ts +93 -0
- package/src/execution.ts +323 -0
- package/src/filter.ts +190 -0
- package/src/index.ts +18 -0
- package/src/mapping.ts +348 -0
- package/src/types.ts +86 -0
- package/test/execution.test.ts +111 -0
- package/test/filter.test.ts +67 -0
- package/test/mapping.test.ts +93 -0
- package/README.md +0 -3
package/src/filter.ts
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import { ObjectId } from 'mongodb';
|
|
2
|
+
import { parseMongoMapping, resolveMappingValue } from './mapping.js';
|
|
3
|
+
import type { ParsedMongoMapping } from './types.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Build a MongoDB filter document from `@mongo(filter:)` (or legacy `where:`) mapping.
|
|
7
|
+
*/
|
|
8
|
+
export function buildMongoFilter(
|
|
9
|
+
filterSource: string | null | undefined,
|
|
10
|
+
ctx: { args?: Record<string, unknown>; parent?: unknown },
|
|
11
|
+
): Record<string, unknown> {
|
|
12
|
+
const parsed = parseMongoMapping(filterSource);
|
|
13
|
+
return filterFromMapping(parsed, ctx);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function filterFromMapping(
|
|
17
|
+
parsed: ParsedMongoMapping,
|
|
18
|
+
ctx: { args?: Record<string, unknown>; parent?: unknown },
|
|
19
|
+
): Record<string, unknown> {
|
|
20
|
+
const filter: Record<string, unknown> = {};
|
|
21
|
+
|
|
22
|
+
for (const entry of parsed.entries) {
|
|
23
|
+
if (entry.spread && entry.value) {
|
|
24
|
+
const spreadVal = resolveMappingValue(entry.value, ctx);
|
|
25
|
+
if (spreadVal && typeof spreadVal === 'object' && !Array.isArray(spreadVal)) {
|
|
26
|
+
Object.assign(filter, graphqlFilterToMongo(spreadVal as Record<string, unknown>));
|
|
27
|
+
}
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Bare `$args.filter` / `$args.where` binder
|
|
32
|
+
if (
|
|
33
|
+
entry.value?.kind === 'args' &&
|
|
34
|
+
(entry.key.startsWith('$args') || entry.key === `$args.${entry.value.value}`)
|
|
35
|
+
) {
|
|
36
|
+
const val = resolveMappingValue(entry.value, ctx);
|
|
37
|
+
if (val && typeof val === 'object' && !Array.isArray(val)) {
|
|
38
|
+
Object.assign(filter, graphqlFilterToMongo(val as Record<string, unknown>));
|
|
39
|
+
}
|
|
40
|
+
continue;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (entry.children || !entry.value) continue;
|
|
44
|
+
|
|
45
|
+
const resolved = resolveMappingValue(entry.value, ctx);
|
|
46
|
+
if (resolved === undefined) continue;
|
|
47
|
+
// `_id: $args._ids` (array) => `{ _id: { $in: [...] } }`
|
|
48
|
+
if (Array.isArray(resolved)) {
|
|
49
|
+
filter[entry.key] = { $in: resolved.map(coerceFilterValue) };
|
|
50
|
+
} else {
|
|
51
|
+
filter[entry.key] = coerceFilterValue(resolved);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return filter;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** @deprecated alias */
|
|
59
|
+
export const graphqlWhereToMongo = graphqlFilterToMongo;
|
|
60
|
+
|
|
61
|
+
export function graphqlFilterToMongo(input: Record<string, unknown>): Record<string, unknown> {
|
|
62
|
+
const out: Record<string, unknown> = {};
|
|
63
|
+
|
|
64
|
+
for (const [key, value] of Object.entries(input)) {
|
|
65
|
+
if (value == null) continue;
|
|
66
|
+
if (key === 'AND' || key === 'and') {
|
|
67
|
+
out.$and = (value as Record<string, unknown>[]).map(graphqlFilterToMongo);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (key === 'OR' || key === 'or') {
|
|
71
|
+
out.$or = (value as Record<string, unknown>[]).map(graphqlFilterToMongo);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (key === 'NOT' || key === 'not') {
|
|
75
|
+
out.$nor = [graphqlFilterToMongo(value as Record<string, unknown>)];
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const mongoKey = key === 'id' ? '_id' : key;
|
|
80
|
+
if (typeof value !== 'object' || Array.isArray(value) || value instanceof Date) {
|
|
81
|
+
out[mongoKey] = coerceFilterValue(value);
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const ops = value as Record<string, unknown>;
|
|
86
|
+
const mongoOps: Record<string, unknown> = {};
|
|
87
|
+
let hasOps = false;
|
|
88
|
+
for (const [op, opVal] of Object.entries(ops)) {
|
|
89
|
+
if (opVal === undefined) continue;
|
|
90
|
+
hasOps = true;
|
|
91
|
+
switch (op) {
|
|
92
|
+
case 'eq':
|
|
93
|
+
out[mongoKey] = coerceFilterValue(opVal);
|
|
94
|
+
hasOps = false;
|
|
95
|
+
break;
|
|
96
|
+
case 'ne':
|
|
97
|
+
mongoOps.$ne = coerceFilterValue(opVal);
|
|
98
|
+
break;
|
|
99
|
+
case 'in':
|
|
100
|
+
mongoOps.$in = (opVal as unknown[]).map(coerceFilterValue);
|
|
101
|
+
break;
|
|
102
|
+
case 'nin':
|
|
103
|
+
mongoOps.$nin = (opVal as unknown[]).map(coerceFilterValue);
|
|
104
|
+
break;
|
|
105
|
+
case 'gt':
|
|
106
|
+
mongoOps.$gt = coerceFilterValue(opVal);
|
|
107
|
+
break;
|
|
108
|
+
case 'gte':
|
|
109
|
+
mongoOps.$gte = coerceFilterValue(opVal);
|
|
110
|
+
break;
|
|
111
|
+
case 'lt':
|
|
112
|
+
mongoOps.$lt = coerceFilterValue(opVal);
|
|
113
|
+
break;
|
|
114
|
+
case 'lte':
|
|
115
|
+
mongoOps.$lte = coerceFilterValue(opVal);
|
|
116
|
+
break;
|
|
117
|
+
case 'regex':
|
|
118
|
+
mongoOps.$regex = opVal;
|
|
119
|
+
break;
|
|
120
|
+
case 'exists':
|
|
121
|
+
mongoOps.$exists = opVal;
|
|
122
|
+
break;
|
|
123
|
+
default:
|
|
124
|
+
break;
|
|
125
|
+
}
|
|
126
|
+
if (!hasOps && op === 'eq') break;
|
|
127
|
+
}
|
|
128
|
+
if (hasOps && Object.keys(mongoOps).length) {
|
|
129
|
+
out[mongoKey] = mongoOps;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
export function buildMongoSort(
|
|
137
|
+
sortSource: string | null | undefined,
|
|
138
|
+
ctx: { args?: Record<string, unknown> },
|
|
139
|
+
): Record<string, 1 | -1> | undefined {
|
|
140
|
+
if (!sortSource) return undefined;
|
|
141
|
+
const parsed = parseMongoMapping(sortSource);
|
|
142
|
+
if (parsed.entries.length === 1 && parsed.entries[0].value?.kind === 'args') {
|
|
143
|
+
const val = resolveMappingValue(parsed.entries[0].value, ctx);
|
|
144
|
+
return graphqlSortToMongo(val as Record<string, unknown> | null | undefined);
|
|
145
|
+
}
|
|
146
|
+
return undefined;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function graphqlSortToMongo(
|
|
150
|
+
input: Record<string, unknown> | null | undefined,
|
|
151
|
+
): Record<string, 1 | -1> | undefined {
|
|
152
|
+
if (!input || typeof input !== 'object') return undefined;
|
|
153
|
+
const out: Record<string, 1 | -1> = {};
|
|
154
|
+
for (const [key, dir] of Object.entries(input)) {
|
|
155
|
+
if (dir == null) continue;
|
|
156
|
+
const mongoKey = key === 'id' ? '_id' : key;
|
|
157
|
+
const d = String(dir).toUpperCase();
|
|
158
|
+
out[mongoKey] = d === 'DESC' || d === '-1' ? -1 : 1;
|
|
159
|
+
}
|
|
160
|
+
return Object.keys(out).length ? out : undefined;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export function buildSetDocument(
|
|
164
|
+
setSource: string | null | undefined,
|
|
165
|
+
ctx: { args?: Record<string, unknown>; parent?: unknown },
|
|
166
|
+
): Record<string, unknown> {
|
|
167
|
+
const parsed = parseMongoMapping(setSource);
|
|
168
|
+
const doc: Record<string, unknown> = {};
|
|
169
|
+
for (const entry of parsed.entries) {
|
|
170
|
+
if (entry.key === 'records' && entry.value?.kind === 'args') {
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
if (!entry.value) continue;
|
|
174
|
+
const resolved = resolveMappingValue(entry.value, ctx);
|
|
175
|
+
if (resolved === undefined) continue;
|
|
176
|
+
doc[entry.key] = resolved;
|
|
177
|
+
}
|
|
178
|
+
return doc;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export function coerceFilterValue(value: unknown): unknown {
|
|
182
|
+
if (typeof value === 'string' && ObjectId.isValid(value) && value.length === 24) {
|
|
183
|
+
try {
|
|
184
|
+
return new ObjectId(value);
|
|
185
|
+
} catch {
|
|
186
|
+
return value;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
return value;
|
|
190
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Transport } from '@graphql-mesh/transport-common';
|
|
2
|
+
import { getMongoExecutor } from './execution.js';
|
|
3
|
+
import type { MongoTransportOptions } from './types.js';
|
|
4
|
+
|
|
5
|
+
export * from './client.js';
|
|
6
|
+
export * from './execution.js';
|
|
7
|
+
export * from './filter.js';
|
|
8
|
+
export * from './mapping.js';
|
|
9
|
+
export type * from './types.js';
|
|
10
|
+
|
|
11
|
+
export default {
|
|
12
|
+
getSubgraphExecutor({ subgraph, cwd }) {
|
|
13
|
+
return getMongoExecutor({
|
|
14
|
+
subgraph,
|
|
15
|
+
cwd,
|
|
16
|
+
});
|
|
17
|
+
},
|
|
18
|
+
} satisfies Transport<MongoTransportOptions>;
|
package/src/mapping.ts
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import type { MongoMappingEntry, MongoMappingValue, ParsedMongoMapping } from './types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parse `@mongo` `where` / `set` / `selection` block strings.
|
|
5
|
+
* Skips `# ...` comments to EOL. Supports nested `{ ... }` blocks and `$args` / `$this` / `$pseudo`.
|
|
6
|
+
*/
|
|
7
|
+
export function parseMongoMapping(source: string | null | undefined): ParsedMongoMapping {
|
|
8
|
+
if (source == null || !String(source).trim()) {
|
|
9
|
+
return { entries: [] };
|
|
10
|
+
}
|
|
11
|
+
const text = stripComments(String(source));
|
|
12
|
+
const { entries, next } = parseBlock(text, 0, false);
|
|
13
|
+
if (next < text.length && text.slice(next).trim()) {
|
|
14
|
+
throw new Error(`Unexpected trailing content in Mongo mapping: ${text.slice(next, next + 40)}`);
|
|
15
|
+
}
|
|
16
|
+
return { entries };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function resolveMappingValue(
|
|
20
|
+
value: MongoMappingValue | undefined,
|
|
21
|
+
ctx: { args?: Record<string, unknown>; parent?: unknown; literals?: Record<string, unknown> },
|
|
22
|
+
): unknown {
|
|
23
|
+
if (!value) return undefined;
|
|
24
|
+
switch (value.kind) {
|
|
25
|
+
case 'literal':
|
|
26
|
+
return value.value;
|
|
27
|
+
case 'column':
|
|
28
|
+
return value.value;
|
|
29
|
+
case 'pseudo':
|
|
30
|
+
return ctx.literals?.[value.value];
|
|
31
|
+
case 'args': {
|
|
32
|
+
return getPath(ctx.args, value.path || [value.value]);
|
|
33
|
+
}
|
|
34
|
+
case 'this': {
|
|
35
|
+
return getPath(ctx.parent as Record<string, unknown> | undefined, value.path || [value.value]);
|
|
36
|
+
}
|
|
37
|
+
default:
|
|
38
|
+
return undefined;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function columnMapFromSelection(parsed: ParsedMongoMapping): Map<string, string> {
|
|
43
|
+
const map = new Map<string, string>();
|
|
44
|
+
for (const entry of parsed.entries) {
|
|
45
|
+
if (entry.children || entry.spread) continue;
|
|
46
|
+
if (entry.value?.kind === 'column') {
|
|
47
|
+
map.set(entry.key, entry.value.value);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
return map;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** Build a Mongo projection `{ path: 1 }` from selection column mappings. */
|
|
54
|
+
export function projectionFromSelection(parsed: ParsedMongoMapping): Record<string, 0 | 1> {
|
|
55
|
+
const projection: Record<string, 0 | 1> = {};
|
|
56
|
+
collectProjection(parsed.entries, projection);
|
|
57
|
+
return projection;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function collectProjection(entries: MongoMappingEntry[], out: Record<string, 0 | 1>) {
|
|
61
|
+
for (const entry of entries) {
|
|
62
|
+
if (entry.spread) continue;
|
|
63
|
+
if (entry.children) {
|
|
64
|
+
collectProjection(entry.children, out);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (entry.value?.kind === 'column') {
|
|
68
|
+
out[entry.value.value] = 1;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Map a Mongo document to GraphQL fields via selection.
|
|
75
|
+
* Nested blocks become nested objects; lists of subdocs pass through for now.
|
|
76
|
+
*/
|
|
77
|
+
export function mapDocumentToGraphql(
|
|
78
|
+
doc: Record<string, unknown> | null | undefined,
|
|
79
|
+
selection: ParsedMongoMapping,
|
|
80
|
+
): Record<string, unknown> | null {
|
|
81
|
+
if (doc == null) return null;
|
|
82
|
+
const out: Record<string, unknown> = {};
|
|
83
|
+
for (const entry of selection.entries) {
|
|
84
|
+
if (entry.spread) continue;
|
|
85
|
+
if (entry.key === '$' || entry.value?.kind === 'pseudo') continue;
|
|
86
|
+
if (entry.children) {
|
|
87
|
+
out[entry.key] = mapDocumentToGraphql(doc, { entries: entry.children });
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (entry.value?.kind === 'column') {
|
|
91
|
+
out[entry.key] = coerceGraphqlValue(getPath(doc, entry.value.value.split('.')));
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return out;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** graphql-scalars ObjectID serialize expects a 24-char hex string, not a driver ObjectId. */
|
|
98
|
+
function coerceGraphqlValue(value: unknown): unknown {
|
|
99
|
+
if (value == null) return value;
|
|
100
|
+
if (typeof value === 'object' && value !== null && 'toHexString' in value) {
|
|
101
|
+
try {
|
|
102
|
+
return (value as { toHexString: () => string }).toHexString();
|
|
103
|
+
} catch {
|
|
104
|
+
return value;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return value;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function stripComments(source: string): string {
|
|
111
|
+
return source
|
|
112
|
+
.split('\n')
|
|
113
|
+
.map(line => {
|
|
114
|
+
let inQuote: '"' | "'" | null = null;
|
|
115
|
+
for (let i = 0; i < line.length; i++) {
|
|
116
|
+
const c = line[i];
|
|
117
|
+
if (inQuote) {
|
|
118
|
+
if (c === '\\') {
|
|
119
|
+
i++;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
if (c === inQuote) inQuote = null;
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (c === '"' || c === "'") {
|
|
126
|
+
inQuote = c;
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
if (c === '#') {
|
|
130
|
+
return line.slice(0, i);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
return line;
|
|
134
|
+
})
|
|
135
|
+
.join('\n');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function parseBlock(
|
|
139
|
+
text: string,
|
|
140
|
+
start: number,
|
|
141
|
+
expectClosing: boolean,
|
|
142
|
+
): { entries: MongoMappingEntry[]; next: number } {
|
|
143
|
+
const entries: MongoMappingEntry[] = [];
|
|
144
|
+
let i = skipWs(text, start);
|
|
145
|
+
while (i < text.length) {
|
|
146
|
+
if (text[i] === '}') {
|
|
147
|
+
if (!expectClosing) {
|
|
148
|
+
throw new Error(`Unexpected "}" in Mongo mapping at ${i}`);
|
|
149
|
+
}
|
|
150
|
+
return { entries, next: i + 1 };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
if (text.startsWith('...', i)) {
|
|
154
|
+
const afterDots = skipWs(text, i + 3);
|
|
155
|
+
if (afterDots >= text.length || text[afterDots] !== '$') {
|
|
156
|
+
throw new Error(`Expected "$" after "..." in Mongo mapping at ${i}`);
|
|
157
|
+
}
|
|
158
|
+
const rhs = readPseudoOrPath(text, afterDots);
|
|
159
|
+
if (rhs.value.kind !== 'args' && rhs.value.kind !== 'this') {
|
|
160
|
+
throw new Error(`Spread target must be $args / $this path, got ${rhs.raw}`);
|
|
161
|
+
}
|
|
162
|
+
entries.push({
|
|
163
|
+
key: `...${rhs.raw}`,
|
|
164
|
+
value: rhs.value,
|
|
165
|
+
spread: true,
|
|
166
|
+
});
|
|
167
|
+
i = skipWs(text, rhs.next);
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (text[i] === '$') {
|
|
172
|
+
const pseudo = readPseudoOrPath(text, i);
|
|
173
|
+
entries.push({
|
|
174
|
+
key: pseudo.raw,
|
|
175
|
+
value: pseudo.value,
|
|
176
|
+
});
|
|
177
|
+
i = skipWs(text, pseudo.next);
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const key = readIdent(text, i);
|
|
182
|
+
i = skipWs(text, key.next);
|
|
183
|
+
if (i < text.length && text[i] === '{') {
|
|
184
|
+
const nested = parseBlock(text, i + 1, true);
|
|
185
|
+
entries.push({ key: key.value, children: nested.entries });
|
|
186
|
+
i = skipWs(text, nested.next);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (i >= text.length || text[i] !== ':') {
|
|
190
|
+
entries.push({
|
|
191
|
+
key: key.value,
|
|
192
|
+
value: { kind: 'column', value: key.value },
|
|
193
|
+
});
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
i = skipWs(text, i + 1);
|
|
197
|
+
if (i < text.length && text[i] === '{') {
|
|
198
|
+
const nested = parseBlock(text, i + 1, true);
|
|
199
|
+
entries.push({ key: key.value, children: nested.entries });
|
|
200
|
+
i = skipWs(text, nested.next);
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
if (i < text.length && text[i] === '$') {
|
|
204
|
+
const rhs = readPseudoOrPath(text, i);
|
|
205
|
+
entries.push({ key: key.value, value: rhs.value });
|
|
206
|
+
i = skipWs(text, rhs.next);
|
|
207
|
+
continue;
|
|
208
|
+
}
|
|
209
|
+
if (i < text.length && (text[i] === '"' || text[i] === "'")) {
|
|
210
|
+
const lit = readQuoted(text, i);
|
|
211
|
+
entries.push({
|
|
212
|
+
key: key.value,
|
|
213
|
+
value: { kind: 'literal', value: lit.value },
|
|
214
|
+
});
|
|
215
|
+
i = skipWs(text, lit.next);
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
const col = readPathIdent(text, i);
|
|
219
|
+
entries.push({
|
|
220
|
+
key: key.value,
|
|
221
|
+
value: { kind: 'column', value: col.value },
|
|
222
|
+
});
|
|
223
|
+
i = skipWs(text, col.next);
|
|
224
|
+
}
|
|
225
|
+
if (expectClosing) {
|
|
226
|
+
throw new Error('Unclosed "{" in Mongo mapping');
|
|
227
|
+
}
|
|
228
|
+
return { entries, next: i };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function readPseudoOrPath(
|
|
232
|
+
text: string,
|
|
233
|
+
start: number,
|
|
234
|
+
): { raw: string; value: MongoMappingValue; next: number } {
|
|
235
|
+
if (text[start] !== '$') {
|
|
236
|
+
throw new Error(`Expected "$" at ${start}`);
|
|
237
|
+
}
|
|
238
|
+
// bare `$`
|
|
239
|
+
if (start + 1 >= text.length || !isIdentStart(text[start + 1] ?? '')) {
|
|
240
|
+
return {
|
|
241
|
+
raw: '$',
|
|
242
|
+
value: { kind: 'pseudo', value: '$' },
|
|
243
|
+
next: start + 1,
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
const path = readDottedPath(text, start + 1);
|
|
247
|
+
const raw = `$${path.value}`;
|
|
248
|
+
const segments = path.value.split('.');
|
|
249
|
+
const head = segments[0];
|
|
250
|
+
if (head === 'args') {
|
|
251
|
+
return {
|
|
252
|
+
raw,
|
|
253
|
+
value: { kind: 'args', value: segments.slice(1).join('.') || 'args', path: segments.slice(1) },
|
|
254
|
+
next: path.next,
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
if (head === 'this') {
|
|
258
|
+
return {
|
|
259
|
+
raw,
|
|
260
|
+
value: { kind: 'this', value: segments.slice(1).join('.') || 'this', path: segments.slice(1) },
|
|
261
|
+
next: path.next,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
return {
|
|
265
|
+
raw,
|
|
266
|
+
value: { kind: 'pseudo', value: path.value },
|
|
267
|
+
next: path.next,
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function readIdent(text: string, start: number): { value: string; next: number } {
|
|
272
|
+
if (!isIdentStart(text[start] ?? '')) {
|
|
273
|
+
throw new Error(`Expected identifier at ${start}`);
|
|
274
|
+
}
|
|
275
|
+
let i = start + 1;
|
|
276
|
+
while (i < text.length && isIdentPart(text[i]!)) i++;
|
|
277
|
+
return { value: text.slice(start, i), next: i };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/** Ident or dotted mongo path (`address.street`). */
|
|
281
|
+
function readPathIdent(text: string, start: number): { value: string; next: number } {
|
|
282
|
+
return readDottedPath(text, start);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function readDottedPath(text: string, start: number): { value: string; next: number } {
|
|
286
|
+
const first = readIdent(text, start);
|
|
287
|
+
let value = first.value;
|
|
288
|
+
let i = first.next;
|
|
289
|
+
while (i < text.length && text[i] === '.') {
|
|
290
|
+
i++;
|
|
291
|
+
const next = readIdent(text, i);
|
|
292
|
+
value += `.${next.value}`;
|
|
293
|
+
i = next.next;
|
|
294
|
+
}
|
|
295
|
+
return { value, next: i };
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function readQuoted(text: string, start: number): { value: string; next: number } {
|
|
299
|
+
const quote = text[start];
|
|
300
|
+
let i = start + 1;
|
|
301
|
+
let out = '';
|
|
302
|
+
while (i < text.length) {
|
|
303
|
+
const c = text[i]!;
|
|
304
|
+
if (c === '\\') {
|
|
305
|
+
out += text[i + 1] ?? '';
|
|
306
|
+
i += 2;
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
if (c === quote) {
|
|
310
|
+
return { value: out, next: i + 1 };
|
|
311
|
+
}
|
|
312
|
+
out += c;
|
|
313
|
+
i++;
|
|
314
|
+
}
|
|
315
|
+
throw new Error('Unclosed string in Mongo mapping');
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function skipWs(text: string, start: number): number {
|
|
319
|
+
let i = start;
|
|
320
|
+
while (i < text.length && /\s/.test(text[i]!)) i++;
|
|
321
|
+
return i;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
function isIdentStart(c: string): boolean {
|
|
325
|
+
const code = c.charCodeAt(0);
|
|
326
|
+
return (
|
|
327
|
+
(code >= 65 && code <= 90) || (code >= 97 && code <= 122) || code === 95 /* _ */
|
|
328
|
+
);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function isIdentPart(c: string): boolean {
|
|
332
|
+
const code = c.charCodeAt(0);
|
|
333
|
+
return (
|
|
334
|
+
(code >= 65 && code <= 90) ||
|
|
335
|
+
(code >= 97 && code <= 122) ||
|
|
336
|
+
(code >= 48 && code <= 57) ||
|
|
337
|
+
code === 95
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function getPath(obj: Record<string, unknown> | undefined, path: string[]): unknown {
|
|
342
|
+
let cur: unknown = obj;
|
|
343
|
+
for (const seg of path) {
|
|
344
|
+
if (cur == null || typeof cur !== 'object') return undefined;
|
|
345
|
+
cur = (cur as Record<string, unknown>)[seg];
|
|
346
|
+
}
|
|
347
|
+
return cur;
|
|
348
|
+
}
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
export type MongoKind =
|
|
2
|
+
| 'FIND_ONE'
|
|
3
|
+
| 'FIND_MANY'
|
|
4
|
+
| 'COUNT'
|
|
5
|
+
| 'CONNECTION'
|
|
6
|
+
| 'PAGINATION'
|
|
7
|
+
| 'AGGREGATE'
|
|
8
|
+
| 'INSERT_ONE'
|
|
9
|
+
| 'INSERT_MANY'
|
|
10
|
+
| 'UPDATE_ONE'
|
|
11
|
+
| 'UPDATE_MANY'
|
|
12
|
+
| 'DELETE_ONE'
|
|
13
|
+
| 'DELETE_MANY';
|
|
14
|
+
|
|
15
|
+
export interface MongoTransportOptions {
|
|
16
|
+
maxPoolSize?: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface MongoMappingValue {
|
|
20
|
+
kind: 'column' | 'pseudo' | 'args' | 'this' | 'literal';
|
|
21
|
+
value: string;
|
|
22
|
+
path?: string[];
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface MongoMappingEntry {
|
|
26
|
+
key: string;
|
|
27
|
+
value?: MongoMappingValue;
|
|
28
|
+
children?: MongoMappingEntry[];
|
|
29
|
+
spread?: boolean;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ParsedMongoMapping {
|
|
33
|
+
entries: MongoMappingEntry[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface MongoFindOptions {
|
|
37
|
+
projection?: Record<string, 0 | 1>;
|
|
38
|
+
sort?: Record<string, 1 | -1>;
|
|
39
|
+
limit?: number;
|
|
40
|
+
skip?: number;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface MongoClientLike {
|
|
44
|
+
findOne(
|
|
45
|
+
collection: string,
|
|
46
|
+
filter: Record<string, unknown>,
|
|
47
|
+
options?: MongoFindOptions,
|
|
48
|
+
): Promise<Record<string, unknown> | null>;
|
|
49
|
+
findMany(
|
|
50
|
+
collection: string,
|
|
51
|
+
filter: Record<string, unknown>,
|
|
52
|
+
options?: MongoFindOptions,
|
|
53
|
+
): Promise<Record<string, unknown>[]>;
|
|
54
|
+
count(collection: string, filter: Record<string, unknown>): Promise<number>;
|
|
55
|
+
insertOne(
|
|
56
|
+
collection: string,
|
|
57
|
+
doc: Record<string, unknown>,
|
|
58
|
+
): Promise<{ insertedId: unknown }>;
|
|
59
|
+
insertMany(
|
|
60
|
+
collection: string,
|
|
61
|
+
docs: Record<string, unknown>[],
|
|
62
|
+
): Promise<{ insertedIds: unknown[]; insertedCount: number }>;
|
|
63
|
+
updateOne(
|
|
64
|
+
collection: string,
|
|
65
|
+
filter: Record<string, unknown>,
|
|
66
|
+
update: Record<string, unknown>,
|
|
67
|
+
): Promise<{ matchedCount: number; modifiedCount: number; upsertedId?: unknown }>;
|
|
68
|
+
updateMany(
|
|
69
|
+
collection: string,
|
|
70
|
+
filter: Record<string, unknown>,
|
|
71
|
+
update: Record<string, unknown>,
|
|
72
|
+
): Promise<{ matchedCount: number; modifiedCount: number }>;
|
|
73
|
+
deleteOne(
|
|
74
|
+
collection: string,
|
|
75
|
+
filter: Record<string, unknown>,
|
|
76
|
+
): Promise<{ deletedCount: number }>;
|
|
77
|
+
deleteMany(
|
|
78
|
+
collection: string,
|
|
79
|
+
filter: Record<string, unknown>,
|
|
80
|
+
): Promise<{ deletedCount: number }>;
|
|
81
|
+
aggregate(
|
|
82
|
+
collection: string,
|
|
83
|
+
pipeline: Record<string, unknown>[],
|
|
84
|
+
): Promise<Record<string, unknown>[]>;
|
|
85
|
+
dispose(): void | Promise<void>;
|
|
86
|
+
}
|