@objectstack/driver-mongodb 1.0.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.
@@ -0,0 +1,201 @@
1
+ import { DriverOptions, QueryAST } from '@objectstack/spec/data';
2
+ import { IDataDriver } from '@objectstack/spec/contracts';
3
+ import { MongoClientOptions, ClientSession, Db, MongoClient, Filter, Document } from 'mongodb';
4
+
5
+ /**
6
+ * MongoDB Driver for ObjectStack
7
+ *
8
+ * Implements the IDataDriver contract using the official MongoDB Node.js driver.
9
+ * Provides native document database operations with full support for
10
+ * ObjectStack's query protocol, aggregations, transactions, and streaming.
11
+ */
12
+
13
+ /**
14
+ * MongoDB driver configuration.
15
+ */
16
+ interface MongoDBDriverConfig {
17
+ /** MongoDB connection URI (e.g., 'mongodb://localhost:27017/mydb') */
18
+ url: string;
19
+ /** Database name (overrides the database in the URI) */
20
+ database?: string;
21
+ /** Maximum connection pool size */
22
+ maxPoolSize?: number;
23
+ /** Minimum connection pool size */
24
+ minPoolSize?: number;
25
+ /** Connection timeout in milliseconds */
26
+ connectTimeoutMS?: number;
27
+ /** Server selection timeout in milliseconds */
28
+ serverSelectionTimeoutMS?: number;
29
+ /** Additional MongoClient options */
30
+ options?: MongoClientOptions;
31
+ }
32
+ /**
33
+ * MongoDB Driver for ObjectStack.
34
+ *
35
+ * Implements the IDataDriver contract via the official MongoDB driver.
36
+ * Uses native MongoDB queries, aggregation pipelines, and transactions.
37
+ */
38
+ declare class MongoDBDriver implements IDataDriver {
39
+ readonly name: string;
40
+ readonly version: string;
41
+ readonly supports: {
42
+ create: boolean;
43
+ read: boolean;
44
+ update: boolean;
45
+ delete: boolean;
46
+ bulkCreate: boolean;
47
+ bulkUpdate: boolean;
48
+ bulkDelete: boolean;
49
+ transactions: boolean;
50
+ savepoints: boolean;
51
+ queryFilters: boolean;
52
+ queryAggregations: boolean;
53
+ querySorting: boolean;
54
+ queryPagination: boolean;
55
+ queryWindowFunctions: boolean;
56
+ querySubqueries: boolean;
57
+ queryCTE: boolean;
58
+ joins: boolean;
59
+ fullTextSearch: boolean;
60
+ jsonQuery: boolean;
61
+ geospatialQuery: boolean;
62
+ streaming: boolean;
63
+ jsonFields: boolean;
64
+ arrayFields: boolean;
65
+ vectorSearch: boolean;
66
+ schemaSync: boolean;
67
+ batchSchemaSync: boolean;
68
+ migrations: boolean;
69
+ indexes: boolean;
70
+ connectionPooling: boolean;
71
+ preparedStatements: boolean;
72
+ queryCache: boolean;
73
+ };
74
+ private client;
75
+ private db;
76
+ private config;
77
+ constructor(config: MongoDBDriverConfig);
78
+ connect(): Promise<void>;
79
+ disconnect(): Promise<void>;
80
+ checkHealth(): Promise<boolean>;
81
+ getPoolStats(): undefined;
82
+ execute(command: unknown, _parameters?: unknown[], options?: DriverOptions): Promise<unknown>;
83
+ find(object: string, query: QueryAST, options?: DriverOptions): Promise<Record<string, unknown>[]>;
84
+ findOne(object: string, query: QueryAST, options?: DriverOptions): Promise<Record<string, unknown> | null>;
85
+ findStream(object: string, query: QueryAST, options?: DriverOptions): AsyncGenerator<Record<string, unknown>>;
86
+ private _findStream;
87
+ create(object: string, data: Record<string, unknown>, options?: DriverOptions): Promise<Record<string, unknown>>;
88
+ update(object: string, id: string | number, data: Record<string, unknown>, options?: DriverOptions): Promise<Record<string, unknown>>;
89
+ upsert(object: string, data: Record<string, unknown>, conflictKeys?: string[], options?: DriverOptions): Promise<Record<string, unknown>>;
90
+ delete(object: string, id: string | number, options?: DriverOptions): Promise<boolean>;
91
+ count(object: string, query?: QueryAST, options?: DriverOptions): Promise<number>;
92
+ bulkCreate(object: string, dataArray: Record<string, unknown>[], options?: DriverOptions): Promise<Record<string, unknown>[]>;
93
+ bulkUpdate(object: string, updates: Array<{
94
+ id: string | number;
95
+ data: Record<string, unknown>;
96
+ }>, options?: DriverOptions): Promise<Record<string, unknown>[]>;
97
+ bulkDelete(object: string, ids: Array<string | number>, options?: DriverOptions): Promise<void>;
98
+ updateMany(object: string, query: QueryAST, data: Record<string, unknown>, options?: DriverOptions): Promise<number>;
99
+ deleteMany(object: string, query: QueryAST, options?: DriverOptions): Promise<number>;
100
+ aggregate(object: string, query: QueryAST, options?: DriverOptions): Promise<Record<string, unknown>[]>;
101
+ beginTransaction(_options?: {
102
+ isolationLevel?: string;
103
+ }): Promise<ClientSession>;
104
+ commit(transaction: unknown): Promise<void>;
105
+ rollback(transaction: unknown): Promise<void>;
106
+ syncSchema(object: string, schema: unknown, _options?: DriverOptions): Promise<void>;
107
+ syncSchemasBatch(schemas: Array<{
108
+ object: string;
109
+ schema: unknown;
110
+ }>, options?: DriverOptions): Promise<void>;
111
+ dropTable(object: string, _options?: DriverOptions): Promise<void>;
112
+ explain(object: string, query: QueryAST, _options?: DriverOptions): Promise<unknown>;
113
+ /** Get the underlying Db instance for advanced usage. */
114
+ getDb(): Db;
115
+ /** Get the underlying MongoClient for advanced usage. */
116
+ getClient(): MongoClient;
117
+ private getCollection;
118
+ private getSession;
119
+ private mapFieldName;
120
+ private extractDatabaseName;
121
+ }
122
+
123
+ /**
124
+ * MongoDB Filter Translator
125
+ *
126
+ * Converts ObjectStack FilterCondition / AST-style `where` clauses into
127
+ * native MongoDB filter documents.
128
+ *
129
+ * Supports:
130
+ * - Implicit equality: `{ field: value }`
131
+ * - Explicit operators: `{ field: { $eq, $ne, $gt, $gte, $lt, $lte, $in, $nin } }`
132
+ * - String operators: `{ field: { $contains, $startsWith, $endsWith, $notContains } }`
133
+ * - Special: `{ field: { $null, $exists } }`
134
+ * - Logical: `{ $and, $or, $not }`
135
+ * - Range: `{ field: { $between: [min, max] } }`
136
+ * - Legacy array-style: `[field, op, value]`
137
+ */
138
+
139
+ /**
140
+ * Translate an ObjectStack `where` clause into a MongoDB filter document.
141
+ *
142
+ * The `where` clause can be:
143
+ * 1. A FilterCondition object (MongoDB-style with `$` operators)
144
+ * 2. A legacy array-style filter `[[field, op, value], 'or', [field, op, value]]`
145
+ * 3. A plain key-value object for implicit equality
146
+ */
147
+ declare function translateFilter(where: unknown): Filter<any>;
148
+
149
+ /**
150
+ * MongoDB Aggregation Pipeline Builder
151
+ *
152
+ * Translates ObjectStack QueryAST aggregations + groupBy into
153
+ * MongoDB aggregation pipeline stages ($match, $group, $sort, $project).
154
+ */
155
+
156
+ /**
157
+ * Aggregation function descriptor from QueryAST.
158
+ */
159
+ interface AggregationInput {
160
+ function: string;
161
+ field?: string;
162
+ alias: string;
163
+ distinct?: boolean;
164
+ filter?: unknown;
165
+ }
166
+ /**
167
+ * Build a MongoDB aggregation pipeline from QueryAST components.
168
+ *
169
+ * @param where - Filter condition (translated to $match)
170
+ * @param aggregations - Array of aggregation descriptors
171
+ * @param groupBy - Fields to group by
172
+ * @param orderBy - Sort specification
173
+ * @param limit - Max results
174
+ * @param offset - Skip results
175
+ */
176
+ declare function buildAggregationPipeline(opts: {
177
+ where?: unknown;
178
+ aggregations?: AggregationInput[];
179
+ groupBy?: string[];
180
+ orderBy?: Array<{
181
+ field: string;
182
+ order?: string;
183
+ }>;
184
+ limit?: number;
185
+ offset?: number;
186
+ }): Document[];
187
+ /**
188
+ * Post-process aggregation results.
189
+ *
190
+ * Handles count_distinct conversion ($addToSet → count) and
191
+ * string_agg conversion ($push → joined string).
192
+ */
193
+ declare function postProcessAggregation(results: Document[], aggregations: AggregationInput[]): Document[];
194
+
195
+ declare const _default: {
196
+ id: string;
197
+ version: string;
198
+ onEnable: (context: any) => Promise<void>;
199
+ };
200
+
201
+ export { type AggregationInput, MongoDBDriver, type MongoDBDriverConfig, buildAggregationPipeline, _default as default, postProcessAggregation, translateFilter };