@ebarahona/loopback-connector-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.
- package/LICENSE +21 -0
- package/README.md +526 -0
- package/dist/connector/coercion.d.ts +30 -0
- package/dist/connector/coercion.js +75 -0
- package/dist/connector/coercion.js.map +1 -0
- package/dist/connector/errors.d.ts +13 -0
- package/dist/connector/errors.js +20 -0
- package/dist/connector/errors.js.map +1 -0
- package/dist/connector/index.d.ts +6 -0
- package/dist/connector/index.js +24 -0
- package/dist/connector/index.js.map +1 -0
- package/dist/connector/mongo.connector.d.ts +171 -0
- package/dist/connector/mongo.connector.js +567 -0
- package/dist/connector/mongo.connector.js.map +1 -0
- package/dist/connector/property-mapping.d.ts +64 -0
- package/dist/connector/property-mapping.js +105 -0
- package/dist/connector/property-mapping.js.map +1 -0
- package/dist/connector/query-builder.d.ts +42 -0
- package/dist/connector/query-builder.js +204 -0
- package/dist/connector/query-builder.js.map +1 -0
- package/dist/datasource/index.d.ts +3 -0
- package/dist/datasource/index.js +10 -0
- package/dist/datasource/index.js.map +1 -0
- package/dist/datasource/mongo.datasource.d.ts +17 -0
- package/dist/datasource/mongo.datasource.factory.d.ts +30 -0
- package/dist/datasource/mongo.datasource.factory.js +44 -0
- package/dist/datasource/mongo.datasource.factory.js.map +1 -0
- package/dist/datasource/mongo.datasource.js +40 -0
- package/dist/datasource/mongo.datasource.js.map +1 -0
- package/dist/datasource/mongo.datasource.provider.d.ts +17 -0
- package/dist/datasource/mongo.datasource.provider.js +42 -0
- package/dist/datasource/mongo.datasource.provider.js.map +1 -0
- package/dist/helpers/config-validator.d.ts +34 -0
- package/dist/helpers/config-validator.js +79 -0
- package/dist/helpers/config-validator.js.map +1 -0
- package/dist/helpers/connection-manager.d.ts +78 -0
- package/dist/helpers/connection-manager.js +212 -0
- package/dist/helpers/connection-manager.js.map +1 -0
- package/dist/helpers/index.d.ts +5 -0
- package/dist/helpers/index.js +15 -0
- package/dist/helpers/index.js.map +1 -0
- package/dist/helpers/topology.d.ts +23 -0
- package/dist/helpers/topology.js +27 -0
- package/dist/helpers/topology.js.map +1 -0
- package/dist/helpers/url-builder.d.ts +7 -0
- package/dist/helpers/url-builder.js +30 -0
- package/dist/helpers/url-builder.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.js +37 -0
- package/dist/index.js.map +1 -0
- package/dist/keys.d.ts +38 -0
- package/dist/keys.js +38 -0
- package/dist/keys.js.map +1 -0
- package/dist/mongo.component.d.ts +59 -0
- package/dist/mongo.component.js +138 -0
- package/dist/mongo.component.js.map +1 -0
- package/dist/providers/index.d.ts +0 -0
- package/dist/providers/index.js +4 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/services/index.d.ts +2 -0
- package/dist/services/index.js +7 -0
- package/dist/services/index.js.map +1 -0
- package/dist/services/mongo.service.d.ts +61 -0
- package/dist/services/mongo.service.impl.d.ts +58 -0
- package/dist/services/mongo.service.impl.js +211 -0
- package/dist/services/mongo.service.impl.js.map +1 -0
- package/dist/services/mongo.service.js +3 -0
- package/dist/services/mongo.service.js.map +1 -0
- package/dist/types.d.ts +85 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/package.json +109 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/connector/errors.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;GAQG;AACH,MAAa,mBAAoB,SAAQ,KAAK;IAE5C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QAFC,SAAI,GAAG,qBAAqB,CAAC;IAG/C,CAAC;CACF;AALD,kDAKC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { MongoConnector, initialize } from './mongo.connector';
|
|
2
|
+
export { MongoConnectorError } from './errors';
|
|
3
|
+
export { buildWhere, buildSort, buildFields } from './query-builder';
|
|
4
|
+
export { toObjectId, isObjectIdString, toDecimal128, binaryToBuffer, coerceId, } from './coercion';
|
|
5
|
+
export { toDatabase, fromDatabase, getDatabaseColumnName, getIdPropertyName, } from './property-mapping';
|
|
6
|
+
export type { ModelDefinition, PropertyDefinition } from './property-mapping';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getIdPropertyName = exports.getDatabaseColumnName = exports.fromDatabase = exports.toDatabase = exports.coerceId = exports.binaryToBuffer = exports.toDecimal128 = exports.isObjectIdString = exports.toObjectId = exports.buildFields = exports.buildSort = exports.buildWhere = exports.MongoConnectorError = exports.initialize = exports.MongoConnector = void 0;
|
|
4
|
+
var mongo_connector_1 = require("./mongo.connector");
|
|
5
|
+
Object.defineProperty(exports, "MongoConnector", { enumerable: true, get: function () { return mongo_connector_1.MongoConnector; } });
|
|
6
|
+
Object.defineProperty(exports, "initialize", { enumerable: true, get: function () { return mongo_connector_1.initialize; } });
|
|
7
|
+
var errors_1 = require("./errors");
|
|
8
|
+
Object.defineProperty(exports, "MongoConnectorError", { enumerable: true, get: function () { return errors_1.MongoConnectorError; } });
|
|
9
|
+
var query_builder_1 = require("./query-builder");
|
|
10
|
+
Object.defineProperty(exports, "buildWhere", { enumerable: true, get: function () { return query_builder_1.buildWhere; } });
|
|
11
|
+
Object.defineProperty(exports, "buildSort", { enumerable: true, get: function () { return query_builder_1.buildSort; } });
|
|
12
|
+
Object.defineProperty(exports, "buildFields", { enumerable: true, get: function () { return query_builder_1.buildFields; } });
|
|
13
|
+
var coercion_1 = require("./coercion");
|
|
14
|
+
Object.defineProperty(exports, "toObjectId", { enumerable: true, get: function () { return coercion_1.toObjectId; } });
|
|
15
|
+
Object.defineProperty(exports, "isObjectIdString", { enumerable: true, get: function () { return coercion_1.isObjectIdString; } });
|
|
16
|
+
Object.defineProperty(exports, "toDecimal128", { enumerable: true, get: function () { return coercion_1.toDecimal128; } });
|
|
17
|
+
Object.defineProperty(exports, "binaryToBuffer", { enumerable: true, get: function () { return coercion_1.binaryToBuffer; } });
|
|
18
|
+
Object.defineProperty(exports, "coerceId", { enumerable: true, get: function () { return coercion_1.coerceId; } });
|
|
19
|
+
var property_mapping_1 = require("./property-mapping");
|
|
20
|
+
Object.defineProperty(exports, "toDatabase", { enumerable: true, get: function () { return property_mapping_1.toDatabase; } });
|
|
21
|
+
Object.defineProperty(exports, "fromDatabase", { enumerable: true, get: function () { return property_mapping_1.fromDatabase; } });
|
|
22
|
+
Object.defineProperty(exports, "getDatabaseColumnName", { enumerable: true, get: function () { return property_mapping_1.getDatabaseColumnName; } });
|
|
23
|
+
Object.defineProperty(exports, "getIdPropertyName", { enumerable: true, get: function () { return property_mapping_1.getIdPropertyName; } });
|
|
24
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/connector/index.ts"],"names":[],"mappings":";;;AAAA,qDAA6D;AAArD,iHAAA,cAAc,OAAA;AAAE,6GAAA,UAAU,OAAA;AAClC,mCAA6C;AAArC,6GAAA,mBAAmB,OAAA;AAC3B,iDAAmE;AAA3D,2GAAA,UAAU,OAAA;AAAE,0GAAA,SAAS,OAAA;AAAE,4GAAA,WAAW,OAAA;AAC1C,uCAMoB;AALlB,sGAAA,UAAU,OAAA;AACV,4GAAA,gBAAgB,OAAA;AAChB,wGAAA,YAAY,OAAA;AACZ,0GAAA,cAAc,OAAA;AACd,oGAAA,QAAQ,OAAA;AAEV,uDAK4B;AAJ1B,8GAAA,UAAU,OAAA;AACV,gHAAA,YAAY,OAAA;AACZ,yHAAA,qBAAqB,OAAA;AACrB,qHAAA,iBAAiB,OAAA"}
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import type { MongoClient, Db, Collection, ClientSession, TransactionOptions } from 'mongodb';
|
|
2
|
+
import type { MongoConnectorConfig } from '../types';
|
|
3
|
+
import type { ModelDefinition } from './property-mapping';
|
|
4
|
+
import { MongoConnectionManager } from '../helpers/connection-manager';
|
|
5
|
+
import { MongoConnectorError } from './errors';
|
|
6
|
+
export { MongoConnectorError };
|
|
7
|
+
/**
|
|
8
|
+
* MongoDB connector for LoopBack 4.
|
|
9
|
+
*
|
|
10
|
+
* Implements the loopback-connector Connector interface using the
|
|
11
|
+
* native MongoDB Node.js driver 7.x.
|
|
12
|
+
*
|
|
13
|
+
* Uses a shared MongoConnectionManager for connection lifecycle.
|
|
14
|
+
* When used via MongoComponent, the same manager is shared with
|
|
15
|
+
* MongoService. When used standalone via juggler DataSource, the
|
|
16
|
+
* connector creates its own manager.
|
|
17
|
+
*
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
export declare class MongoConnector {
|
|
21
|
+
name: string;
|
|
22
|
+
settings: MongoConnectorConfig;
|
|
23
|
+
dataSource?: Record<string, unknown>;
|
|
24
|
+
private connectionManager;
|
|
25
|
+
private readonly ownsConnectionManager;
|
|
26
|
+
private _models;
|
|
27
|
+
/**
|
|
28
|
+
* @param settings - Connector configuration
|
|
29
|
+
* @param connectionManager - Optional shared connection manager.
|
|
30
|
+
* When provided (e.g. by MongoComponent), the connector uses
|
|
31
|
+
* the shared MongoClient and does NOT own disconnect.
|
|
32
|
+
* When omitted (standalone juggler use), the connector creates
|
|
33
|
+
* and owns its own manager.
|
|
34
|
+
*/
|
|
35
|
+
constructor(settings: MongoConnectorConfig, connectionManager?: MongoConnectionManager);
|
|
36
|
+
/**
|
|
37
|
+
* Install per-instance shims for the juggler 6.x DAO bridge.
|
|
38
|
+
*
|
|
39
|
+
* Juggler 6.x's `invokeConnectorMethod` invokes connector methods
|
|
40
|
+
* with `connector[method](modelName, ...args, opts?, cb)`, ignores
|
|
41
|
+
* any returned promise, and waits for `cb(err, result)` to fire.
|
|
42
|
+
* The class methods themselves stay strictly promise-returning;
|
|
43
|
+
* the shim simply extracts a trailing callback (if present) and
|
|
44
|
+
* relays settlement to it without altering the call's promise
|
|
45
|
+
* return value.
|
|
46
|
+
*
|
|
47
|
+
* This is the only place in the connector that inspects an
|
|
48
|
+
* argument to see if it's a function.
|
|
49
|
+
*/
|
|
50
|
+
private installJugglerBridge;
|
|
51
|
+
/**
|
|
52
|
+
* Connect to MongoDB. Delegates to the connection manager.
|
|
53
|
+
* Idempotent and concurrency-safe.
|
|
54
|
+
*/
|
|
55
|
+
connect(): Promise<Db>;
|
|
56
|
+
/**
|
|
57
|
+
* Disconnect from MongoDB.
|
|
58
|
+
*
|
|
59
|
+
* Only disconnects if this connector owns its connection manager
|
|
60
|
+
* (standalone juggler DataSource use). When the manager is shared
|
|
61
|
+
* via MongoComponent, the lifecycle observer owns disconnect --
|
|
62
|
+
* calling this is a no-op.
|
|
63
|
+
*/
|
|
64
|
+
disconnect(): Promise<void>;
|
|
65
|
+
ping(): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* Get the shared connection manager.
|
|
68
|
+
*/
|
|
69
|
+
getConnectionManager(): MongoConnectionManager;
|
|
70
|
+
/**
|
|
71
|
+
* Get the native MongoClient instance.
|
|
72
|
+
*/
|
|
73
|
+
getClient(): MongoClient | undefined;
|
|
74
|
+
/**
|
|
75
|
+
* Get the native Db instance.
|
|
76
|
+
*/
|
|
77
|
+
getDb(): Db | undefined;
|
|
78
|
+
define(modelDefinition: ModelDefinition): void;
|
|
79
|
+
getModelDefinition(modelName: string): ModelDefinition | undefined;
|
|
80
|
+
create(modelName: string, data: Record<string, unknown>, options?: Record<string, unknown>): Promise<unknown>;
|
|
81
|
+
find(modelName: string, id: unknown, options?: Record<string, unknown>): Promise<Record<string, unknown> | null>;
|
|
82
|
+
all(modelName: string, filter?: Record<string, unknown>, options?: Record<string, unknown>): Promise<Record<string, unknown>[]>;
|
|
83
|
+
updateAll(modelName: string, where: Record<string, unknown> | undefined, data: Record<string, unknown>, options?: Record<string, unknown>): Promise<{
|
|
84
|
+
count: number;
|
|
85
|
+
}>;
|
|
86
|
+
deleteAll(modelName: string, where: Record<string, unknown> | undefined, options?: Record<string, unknown>): Promise<{
|
|
87
|
+
count: number;
|
|
88
|
+
}>;
|
|
89
|
+
count(modelName: string, where?: Record<string, unknown>, options?: Record<string, unknown>): Promise<number>;
|
|
90
|
+
replaceById(modelName: string, id: unknown, data: Record<string, unknown>, options?: Record<string, unknown>): Promise<{
|
|
91
|
+
count: number;
|
|
92
|
+
}>;
|
|
93
|
+
updateOrCreate(modelName: string, data: Record<string, unknown>, options?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
94
|
+
/**
|
|
95
|
+
* Find a document matching `filter` or create it from `data`.
|
|
96
|
+
*
|
|
97
|
+
* @experimental
|
|
98
|
+
*
|
|
99
|
+
* KNOWN LIMITATION: on a duplicate-key conflict (error 11000), the
|
|
100
|
+
* follow-up lookup re-uses the caller's `filter`. If the conflicting
|
|
101
|
+
* unique index covers a field NOT in `filter`, the returned document
|
|
102
|
+
* may be unrelated to the duplicate. Use `upsert` via
|
|
103
|
+
* `updateOrCreate()` or the connector's raw `replaceOne` for stricter
|
|
104
|
+
* semantics until this is addressed in a future release.
|
|
105
|
+
*/
|
|
106
|
+
findOrCreate(modelName: string, filter: Record<string, unknown>, data: Record<string, unknown>, options?: Record<string, unknown>): Promise<[Record<string, unknown>, boolean]>;
|
|
107
|
+
exists(modelName: string, id: unknown, options?: Record<string, unknown>): Promise<boolean>;
|
|
108
|
+
/**
|
|
109
|
+
* Juggler alias for updateAll.
|
|
110
|
+
*/
|
|
111
|
+
update(modelName: string, where: Record<string, unknown> | undefined, data: Record<string, unknown>, options?: Record<string, unknown>): Promise<{
|
|
112
|
+
count: number;
|
|
113
|
+
}>;
|
|
114
|
+
/**
|
|
115
|
+
* Juggler alias for deleteAll.
|
|
116
|
+
*/
|
|
117
|
+
destroyAll(modelName: string, where: Record<string, unknown> | undefined, options?: Record<string, unknown>): Promise<{
|
|
118
|
+
count: number;
|
|
119
|
+
}>;
|
|
120
|
+
/**
|
|
121
|
+
* Juggler: update specific attributes on a single document by id.
|
|
122
|
+
*/
|
|
123
|
+
updateAttributes(modelName: string, id: unknown, data: Record<string, unknown>, options?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
124
|
+
/**
|
|
125
|
+
* Juggler: save (upsert) a document.
|
|
126
|
+
*/
|
|
127
|
+
save(modelName: string, data: Record<string, unknown>, options?: Record<string, unknown>): Promise<Record<string, unknown>>;
|
|
128
|
+
private static readonly SAFE_COMMANDS;
|
|
129
|
+
/**
|
|
130
|
+
* Raw-driver escape hatch. Calls the named MongoDB Collection method
|
|
131
|
+
* directly with the provided args.
|
|
132
|
+
*
|
|
133
|
+
* The `SAFE_COMMANDS` allowlist controls which methods may be called;
|
|
134
|
+
* argument shape is NOT validated. Malformed args can cause data
|
|
135
|
+
* loss (e.g. an empty filter on `deleteMany`). Prefer the typed
|
|
136
|
+
* helpers on `MongoService` or the connector's CRUD methods.
|
|
137
|
+
*
|
|
138
|
+
* @experimental
|
|
139
|
+
* @throws MongoConnectorError if the command is not in the allowlist
|
|
140
|
+
* or is not a function on the Collection.
|
|
141
|
+
*/
|
|
142
|
+
execute(modelName: string, command: string, ...args: unknown[]): Promise<unknown>;
|
|
143
|
+
/**
|
|
144
|
+
* Begin a MongoDB transaction and return the underlying session.
|
|
145
|
+
*
|
|
146
|
+
* NOTE: this method does NOT auto-retry on `TransientTransactionError`
|
|
147
|
+
* or `UnknownTransactionCommitResult`. The juggler shape mandates
|
|
148
|
+
* separate begin/commit/rollback. For new code, prefer
|
|
149
|
+
* `MongoService.withTransaction()`, which wraps the retry loop
|
|
150
|
+
* automatically.
|
|
151
|
+
*/
|
|
152
|
+
beginTransaction(options?: TransactionOptions): Promise<ClientSession>;
|
|
153
|
+
commit(session: ClientSession): Promise<void>;
|
|
154
|
+
rollback(session: ClientSession): Promise<void>;
|
|
155
|
+
collectionForModel(modelName: string): Collection;
|
|
156
|
+
private getCollection;
|
|
157
|
+
private buildUpdate;
|
|
158
|
+
private fromDb;
|
|
159
|
+
private extractSessionOptions;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Initialize function for loopback-datasource-juggler.
|
|
163
|
+
* Creates a standalone connector with its own connection manager.
|
|
164
|
+
*
|
|
165
|
+
* Juggler invokes this with `(dataSource, callback)` and expects
|
|
166
|
+
* the callback to signal "setup done." This is the one place the
|
|
167
|
+
* connector still exposes a callback contract.
|
|
168
|
+
*
|
|
169
|
+
* @public
|
|
170
|
+
*/
|
|
171
|
+
export declare function initialize(dataSource: Record<string, unknown>, callback?: (err: Error | null) => void): void;
|