@webda/mongo 4.0.0-beta.2 → 4.0.0-beta.4

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/README.md ADDED
@@ -0,0 +1,118 @@
1
+ # @webda/mongo module
2
+
3
+ This module is part of Webda Application Framework that allows you to quickly develop applications with all modern prerequisites: Security, Extensibility, GraphQL, REST, CloudNative [https://webda.io](https://webda.io)
4
+
5
+ <img src="https://webda.io/images/webda.svg" width="128" />
6
+
7
+ ![CI](https://github.com/loopingz/webda.io/workflows/CI/badge.svg)
8
+
9
+ [![Join the chat at https://gitter.im/loopingz/webda](https://badges.gitter.im/loopingz/webda.svg)](https://gitter.im/loopingz/webda?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
10
+ [![codecov](https://codecov.io/gh/loopingz/webda.io/branch/main/graph/badge.svg?token=8N9DNM3K3O)](https://codecov.io/gh/loopingz/webda.io)
11
+ [![SonarCloud.io](https://sonarcloud.io/api/project_badges/measure?project=loopingz_webda.io&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=loopingz_webda.io)
12
+ ![CodeQL](https://github.com/loopingz/webda.io/workflows/CodeQL/badge.svg)
13
+
14
+ <!-- README_HEADER -->
15
+
16
+ # @webda/mongodb
17
+
18
+ > MongoDB-backed Store for Webda — persists any model to MongoDB using the JSONB document model, with full query, pagination, and update-condition support.
19
+
20
+ ## When to use it
21
+
22
+ - You want a flexible document store for Webda models without a fixed schema (MongoDB's schema-less documents match Webda's JSON models naturally).
23
+ - You are deploying on infrastructure that already runs MongoDB (Atlas, self-hosted, DocumentDB).
24
+ - You need free-text indexes, geospatial queries, or aggregation pipelines alongside your Webda models.
25
+
26
+ ## Install
27
+
28
+ ```bash
29
+ pnpm add @webda/mongodb
30
+ ```
31
+
32
+ ## Configuration
33
+
34
+ ```json
35
+ {
36
+ "services": {
37
+ "userStore": {
38
+ "type": "MongoStore",
39
+ "model": "MyApp/User",
40
+ "collection": "users",
41
+ "database": "my-app"
42
+ }
43
+ }
44
+ }
45
+ ```
46
+
47
+ Set the connection URL via the `WEBDA_MONGO_URL` environment variable (recommended) or inline:
48
+
49
+ ```json
50
+ {
51
+ "services": {
52
+ "userStore": {
53
+ "type": "MongoStore",
54
+ "mongoUrl": "mongodb+srv://user:pass@cluster.mongodb.net",
55
+ "collection": "users",
56
+ "database": "my-app"
57
+ }
58
+ }
59
+ }
60
+ ```
61
+
62
+ | Parameter | Type | Default | Required | Description |
63
+ |---|---|---|---|---|
64
+ | `mongoUrl` | string | `$WEBDA_MONGO_URL` | Yes | MongoDB connection string (falls back to `WEBDA_MONGO_URL` env variable) |
65
+ | `collection` | string | — | Yes | MongoDB collection name to store model documents in |
66
+ | `database` | string | — | No | MongoDB database name (uses default from connection string if omitted) |
67
+ | `options` | object | `{}` | No | Additional `MongoClientOptions` passed to the driver |
68
+ | `databaseOptions` | object | — | No | `DbOptions` passed to `client.db()` |
69
+
70
+ ## Usage
71
+
72
+ ```typescript
73
+ import { CoreModel, Model, Expose } from "@webda/core";
74
+ import { Bean, Inject } from "@webda/core";
75
+ import { Store } from "@webda/core";
76
+
77
+ @Model()
78
+ export class User extends CoreModel {
79
+ @Expose() name: string;
80
+ @Expose() email: string;
81
+ }
82
+
83
+ // With MongoStore configured, all CoreModel CRUD is automatically backed by MongoDB:
84
+ const user = new User();
85
+ user.name = "Alice";
86
+ user.email = "alice@example.com";
87
+ await user.save(); // inserts into MongoDB
88
+
89
+ const found = await User.ref(user.uuid).get(); // fetch by UUID
90
+ const results = await User.query("name = 'Alice'"); // filtered query
91
+ await user.patch({ name: "Alice Smith" }); // partial update
92
+ await user.delete(); // hard delete
93
+ ```
94
+
95
+ ## Reference
96
+
97
+ - API reference: see the auto-generated typedoc at `docs/pages/Modules/mongodb/`.
98
+ - Source: [`packages/mongodb`](https://github.com/loopingz/webda.io/tree/main/packages/mongodb)
99
+ - Related: [`@webda/postgres`](../postgres) for a SQL-backed alternative; [`@webda/aws`](../aws) for `DynamoStore`; [`@webda/core`](../core) for the `Store` base class and model CRUD API.
100
+
101
+ <!-- README_FOOTER -->
102
+ ## Sponsors
103
+
104
+ <!--
105
+ Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor](mailto:sponsor@webda.io)
106
+ -->
107
+
108
+ Arize AI is a machine learning observability and model monitoring platform. It helps you visualize, monitor, and explain your machine learning models. [Learn more](https://arize.com)
109
+
110
+ [<img src="https://arize.com/hubfs/arize/brand/arize-logomark-1.png" width="200">](https://arize.com)
111
+
112
+ Loopingz is a software development company that provides consulting and development services. [Learn more](https://loopingz.com)
113
+
114
+ [<img src="https://loopingz.com/images/logo.png" width="200">](https://loopingz.com)
115
+
116
+ Tellae is an innovative consulting firm specialized in cities transportation issues. We provide our clients, both public and private, with solutions to support your strategic and operational decisions. [Learn more](https://tellae.fr)
117
+
118
+ [<img src="https://tellae.fr/" width="200">](https://tellae.fr)
package/lib/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- export * from "./mongodb.js";
1
+ export * from "./mongodb.service.js";
2
2
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC"}
package/lib/index.js CHANGED
@@ -1,2 +1,2 @@
1
- export * from "./mongodb.js";
1
+ export * from "./mongodb.service.js";
2
2
  //# sourceMappingURL=index.js.map
package/lib/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,sBAAsB,CAAC"}
@@ -0,0 +1,175 @@
1
+ import { MemoryRepository, Store, StoreParameters } from "@webda/core";
2
+ import type { ModelClass, Repository } from "@webda/core";
3
+ import * as WebdaQL from "@webda/ql";
4
+ import { Collection, Db, DbOptions, Document, MongoClient } from "mongodb";
5
+ /**
6
+ * MongoDB store parameters
7
+ */
8
+ export declare class MongoParameters extends StoreParameters {
9
+ /**
10
+ * Contains the URL to Mongo Server
11
+ *
12
+ * Will try to use WEBDA_MONGO_URL environment variable if not defined
13
+ */
14
+ mongoUrl?: string;
15
+ /**
16
+ * Additional options for Mongo connetion
17
+ *
18
+ * Should be typed with MongoClientOptions but not available due to bug in ts-json-schema-generator
19
+ * https://docs.mongodb.com/manual/reference/connection-string
20
+ */
21
+ options?: any;
22
+ /**
23
+ * Which collection to use
24
+ *
25
+ * All the models managed by the store share this collection, the `__type`
26
+ * field is used to differentiate them
27
+ */
28
+ collection: string;
29
+ /**
30
+ * Database to use, default to the one defined in the URL
31
+ */
32
+ database?: string;
33
+ /**
34
+ * Mongo Database Options
35
+ *
36
+ * @see https://www.npmjs.com/package/mongodb
37
+ */
38
+ databaseOptions?: DbOptions;
39
+ /**
40
+ * @override
41
+ * @param params - raw parameters
42
+ * @returns this
43
+ */
44
+ load(params?: any): this;
45
+ }
46
+ /**
47
+ * Get a mongodb query object from WebdaQL
48
+ * @param expression - the WebdaQL expression
49
+ * @returns the MongoDB filter
50
+ */
51
+ export declare function mapExpression(expression: WebdaQL.Expression): any;
52
+ /**
53
+ * MongoDB-backed repository for a single model class
54
+ *
55
+ * Every object is stored as a plain document with `_id` set to its primary key
56
+ * and `__type` set to its model identifier, so several models can share one
57
+ * collection. The inherited MemoryRepository helpers are only used for primary
58
+ * key and class filter computation.
59
+ */
60
+ export declare class MongoRepository<T extends ModelClass> extends MemoryRepository<T> {
61
+ protected readonly getCollection: () => Promise<Collection<Document>>;
62
+ /**
63
+ * @param model - the model class
64
+ * @param pks - primary key field names
65
+ * @param getCollection - return the collection, connecting if needed
66
+ * @param separator - composite key separator
67
+ */
68
+ constructor(model: T, pks: string[], getCollection: () => Promise<Collection<Document>>, separator?: string);
69
+ /**
70
+ * Convert an object to the document stored in MongoDB
71
+ * @param item - the model instance or raw data
72
+ * @param key - the primary key
73
+ * @returns the document
74
+ */
75
+ protected toDocument(item: any, key: string): any;
76
+ /**
77
+ * Convert a stored document into a model instance
78
+ * @param doc - the MongoDB document
79
+ * @returns the model instance
80
+ */
81
+ protected fromDocument(doc: any): InstanceType<T>;
82
+ /**
83
+ * Return a filter for Mongo command
84
+ * @param key - the primary key
85
+ * @param conditionField - field to check
86
+ * @param condition - expected value
87
+ * @returns the filter
88
+ */
89
+ protected getFilter(key: string, conditionField?: any, condition?: any): any;
90
+ /** @override */
91
+ get(primaryKey: any): Promise<any>;
92
+ /** @override */
93
+ create(data: any, save?: boolean): Promise<any>;
94
+ /** @override */
95
+ update(data: any, conditionField?: any, condition?: any): Promise<void>;
96
+ /** @override */
97
+ patch(primaryKey: any, data: any, conditionField?: any, condition?: any): Promise<void>;
98
+ /** @override */
99
+ delete(primaryKey: any, conditionField?: any, condition?: any): Promise<void>;
100
+ /** @override */
101
+ exists(primaryKey: any): Promise<boolean>;
102
+ /** @override */
103
+ removeAttribute(primaryKey: any, attribute: any, conditionField?: any, condition?: any): Promise<void>;
104
+ /** @override */
105
+ incrementAttributes(primaryKey: any, info: any, conditionField?: any, condition?: any): Promise<void>;
106
+ /** @override */
107
+ upsertItemToCollection(primaryKey: any, collection: any, item: any, index?: number, itemWriteConditionField?: any, itemWriteCondition?: any): Promise<void>;
108
+ /** @override */
109
+ deleteItemFromCollection(primaryKey: any, collection: any, index: number, itemWriteConditionField?: any, itemWriteCondition?: any): Promise<void>;
110
+ /**
111
+ * Build the MongoDB filter restricting results to this model and its subclasses
112
+ *
113
+ * Documents without `__type` are considered as instance of the repository model
114
+ * @returns the class filter or undefined if the model has no metadata
115
+ */
116
+ protected getClassFilter(): any;
117
+ /** @override — translate the WebdaQL query to a MongoDB find */
118
+ query(query: string | any): Promise<{
119
+ results: InstanceType<T>[];
120
+ continuationToken?: string;
121
+ }>;
122
+ /**
123
+ * Name of the underlying collection, used in errors
124
+ * @returns the collection name
125
+ */
126
+ protected getCollectionName(): string;
127
+ }
128
+ /**
129
+ * Store Objects in MongoDB
130
+ *
131
+ * Parameters:
132
+ * mongoUrl: 'mongodb://127.0.0.1:27017' // If not found try to read WEBDA_MONGO_URL env variable
133
+ *
134
+ * @WebdaModda
135
+ */
136
+ export declare class MongoStore<K extends MongoParameters = MongoParameters> extends Store<K> {
137
+ /**
138
+ * Connect promise
139
+ */
140
+ protected _connectPromise: Promise<Collection<Document>>;
141
+ /**
142
+ * Client
143
+ */
144
+ _client: MongoClient;
145
+ _db: Db;
146
+ _collection: Collection<Document>;
147
+ /**
148
+ * Connect to MongoDB if not already connected
149
+ * @returns the collection
150
+ */
151
+ _connect(): Promise<Collection<Document>>;
152
+ /**
153
+ * @override
154
+ */
155
+ init(): Promise<this>;
156
+ /**
157
+ * @override
158
+ */
159
+ stop(): Promise<void>;
160
+ /**
161
+ * Build and return a MongoRepository for the given model
162
+ *
163
+ * The collection is resolved lazily so the repository can be registered
164
+ * before the store is connected.
165
+ * @param model - the model class
166
+ * @returns a repository backed by the store collection
167
+ */
168
+ getRepository<T extends ModelClass>(model: T): Repository<T>;
169
+ /**
170
+ * Remove every document from the collection (used in tests)
171
+ */
172
+ __clean(): Promise<void>;
173
+ }
174
+ export default MongoStore;
175
+ //# sourceMappingURL=mongodb.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mongodb.service.d.ts","sourceRoot":"","sources":["../src/mongodb.service.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,gBAAgB,EAChB,KAAK,EAEL,eAAe,EAGhB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAC1D,OAAO,KAAK,OAAO,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,UAAU,EAAE,EAAE,EAAE,SAAS,EAAE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