@openfn/language-mongodb 1.0.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/dist/index.cjs ADDED
@@ -0,0 +1,219 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
21
+ mod
22
+ ));
23
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
24
+
25
+ // src/index.js
26
+ var src_exports = {};
27
+ __export(src_exports, {
28
+ alterState: () => import_language_common2.alterState,
29
+ dataPath: () => import_language_common2.dataPath,
30
+ dataValue: () => import_language_common2.dataValue,
31
+ default: () => src_default,
32
+ each: () => import_language_common2.each,
33
+ execute: () => execute,
34
+ field: () => import_language_common2.field,
35
+ fields: () => import_language_common2.fields,
36
+ findDocuments: () => findDocuments,
37
+ fn: () => import_language_common2.fn,
38
+ insertDocuments: () => insertDocuments,
39
+ lastReferenceValue: () => import_language_common2.lastReferenceValue,
40
+ merge: () => import_language_common2.merge,
41
+ sourceValue: () => import_language_common2.sourceValue,
42
+ updateDocument: () => updateDocument
43
+ });
44
+ module.exports = __toCommonJS(src_exports);
45
+
46
+ // src/Adaptor.js
47
+ var Adaptor_exports = {};
48
+ __export(Adaptor_exports, {
49
+ alterState: () => import_language_common2.alterState,
50
+ dataPath: () => import_language_common2.dataPath,
51
+ dataValue: () => import_language_common2.dataValue,
52
+ each: () => import_language_common2.each,
53
+ execute: () => execute,
54
+ field: () => import_language_common2.field,
55
+ fields: () => import_language_common2.fields,
56
+ findDocuments: () => findDocuments,
57
+ fn: () => import_language_common2.fn,
58
+ insertDocuments: () => insertDocuments,
59
+ lastReferenceValue: () => import_language_common2.lastReferenceValue,
60
+ merge: () => import_language_common2.merge,
61
+ sourceValue: () => import_language_common2.sourceValue,
62
+ updateDocument: () => updateDocument
63
+ });
64
+ var import_language_common = require("@openfn/language-common");
65
+ var import_mongodb = __toESM(require("mongodb"), 1);
66
+ var import_language_common2 = require("@openfn/language-common");
67
+ var { MongoClient } = import_mongodb.default;
68
+ function execute(...operations) {
69
+ const initialState = {
70
+ references: [],
71
+ data: null
72
+ };
73
+ return (state) => {
74
+ return (0, import_language_common.execute)(
75
+ connect,
76
+ ...operations,
77
+ disconnect
78
+ )({ ...initialState, ...state });
79
+ };
80
+ }
81
+ function connect(state) {
82
+ const { clusterUrl, username, password } = state.configuration;
83
+ const uri = `mongodb+srv://${encodeURIComponent(
84
+ username
85
+ )}:${encodeURIComponent(
86
+ password
87
+ )}@${clusterUrl}/test?retryWrites=true&w=majority`;
88
+ const client = new MongoClient(uri, { useNewUrlParser: true });
89
+ return new Promise((resolve, reject) => {
90
+ client.connect((err) => {
91
+ if (err) {
92
+ reject(err);
93
+ } else {
94
+ console.log("Connected successfully to server");
95
+ resolve({ ...state, client });
96
+ }
97
+ });
98
+ });
99
+ }
100
+ function disconnect(state) {
101
+ state.client.close();
102
+ delete state.client;
103
+ return state;
104
+ }
105
+ function insertDocuments(params) {
106
+ return (state) => {
107
+ const { client } = state;
108
+ try {
109
+ const { database, collection, documents, callback } = (0, import_language_common.expandReferences)(params)(state);
110
+ const db = client.db(database);
111
+ const mCollection = db.collection(collection);
112
+ return new Promise((resolve, reject) => {
113
+ mCollection.insertMany(documents, (err, result2) => {
114
+ if (err) {
115
+ reject(err);
116
+ state.client.close();
117
+ } else {
118
+ console.log(
119
+ `Inserted ${documents.length} documents into the collection`
120
+ );
121
+ console.log(JSON.stringify(result2, null, 2));
122
+ const nextState = (0, import_language_common.composeNextState)(state, result2);
123
+ if (callback)
124
+ resolve(callback(nextState));
125
+ resolve(nextState);
126
+ }
127
+ });
128
+ });
129
+ } catch (error) {
130
+ state.client.close();
131
+ throw error;
132
+ }
133
+ };
134
+ }
135
+ function findDocuments(params) {
136
+ return (state) => {
137
+ const { client } = state;
138
+ try {
139
+ const { database, collection, query, callback } = (0, import_language_common.expandReferences)(params)(state);
140
+ const db = client.db(database);
141
+ const mCollection = db.collection(collection);
142
+ return new Promise((resolve, reject) => {
143
+ mCollection.find(query).toArray((err, docs) => {
144
+ if (err) {
145
+ reject(err);
146
+ state.client.close();
147
+ } else {
148
+ console.log(`Found ${docs.length} documents in the collection`);
149
+ console.log(JSON.stringify(result, null, 2));
150
+ const nextState = (0, import_language_common.composeNextState)(state, docs);
151
+ if (callback)
152
+ resolve(callback(nextState));
153
+ resolve(nextState);
154
+ }
155
+ });
156
+ });
157
+ } catch (error) {
158
+ state.client.close();
159
+ throw error;
160
+ }
161
+ };
162
+ }
163
+ function updateDocument(params) {
164
+ return (state) => {
165
+ const { client } = state;
166
+ try {
167
+ const { database, collection, filter, changes, options, callback } = (0, import_language_common.expandReferences)(params)(state);
168
+ const db = client.db(database);
169
+ const mCollection = db.collection(collection);
170
+ return new Promise((resolve, reject) => {
171
+ mCollection.updateMany(
172
+ filter,
173
+ { $set: changes },
174
+ options,
175
+ (err, result2) => {
176
+ if (err) {
177
+ reject(err);
178
+ state.client.close();
179
+ } else {
180
+ console.log(
181
+ `Updated a document matching ${JSON.stringify(
182
+ filter
183
+ )} in the collection.`
184
+ );
185
+ console.log(JSON.stringify(result2, null, 2));
186
+ const nextState = (0, import_language_common.composeNextState)(state, result2);
187
+ if (callback)
188
+ resolve(callback(nextState));
189
+ resolve(nextState);
190
+ }
191
+ }
192
+ );
193
+ });
194
+ } catch (error) {
195
+ state.client.close();
196
+ throw error;
197
+ }
198
+ };
199
+ }
200
+
201
+ // src/index.js
202
+ var src_default = Adaptor_exports;
203
+ // Annotate the CommonJS export names for ESM import in node:
204
+ 0 && (module.exports = {
205
+ alterState,
206
+ dataPath,
207
+ dataValue,
208
+ each,
209
+ execute,
210
+ field,
211
+ fields,
212
+ findDocuments,
213
+ fn,
214
+ insertDocuments,
215
+ lastReferenceValue,
216
+ merge,
217
+ sourceValue,
218
+ updateDocument
219
+ });
package/dist/index.js ADDED
@@ -0,0 +1,195 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ // src/Adaptor.js
8
+ var Adaptor_exports = {};
9
+ __export(Adaptor_exports, {
10
+ alterState: () => alterState,
11
+ dataPath: () => dataPath,
12
+ dataValue: () => dataValue,
13
+ each: () => each,
14
+ execute: () => execute,
15
+ field: () => field,
16
+ fields: () => fields,
17
+ findDocuments: () => findDocuments,
18
+ fn: () => fn,
19
+ insertDocuments: () => insertDocuments,
20
+ lastReferenceValue: () => lastReferenceValue,
21
+ merge: () => merge,
22
+ sourceValue: () => sourceValue,
23
+ updateDocument: () => updateDocument
24
+ });
25
+ import {
26
+ execute as commonExecute,
27
+ expandReferences,
28
+ composeNextState
29
+ } from "@openfn/language-common";
30
+ import pkg from "mongodb";
31
+ import {
32
+ field,
33
+ fields,
34
+ sourceValue,
35
+ fn,
36
+ alterState,
37
+ each,
38
+ merge,
39
+ dataPath,
40
+ dataValue,
41
+ lastReferenceValue
42
+ } from "@openfn/language-common";
43
+ var { MongoClient } = pkg;
44
+ function execute(...operations) {
45
+ const initialState = {
46
+ references: [],
47
+ data: null
48
+ };
49
+ return (state) => {
50
+ return commonExecute(
51
+ connect,
52
+ ...operations,
53
+ disconnect
54
+ )({ ...initialState, ...state });
55
+ };
56
+ }
57
+ function connect(state) {
58
+ const { clusterUrl, username, password } = state.configuration;
59
+ const uri = `mongodb+srv://${encodeURIComponent(
60
+ username
61
+ )}:${encodeURIComponent(
62
+ password
63
+ )}@${clusterUrl}/test?retryWrites=true&w=majority`;
64
+ const client = new MongoClient(uri, { useNewUrlParser: true });
65
+ return new Promise((resolve, reject) => {
66
+ client.connect((err) => {
67
+ if (err) {
68
+ reject(err);
69
+ } else {
70
+ console.log("Connected successfully to server");
71
+ resolve({ ...state, client });
72
+ }
73
+ });
74
+ });
75
+ }
76
+ function disconnect(state) {
77
+ state.client.close();
78
+ delete state.client;
79
+ return state;
80
+ }
81
+ function insertDocuments(params) {
82
+ return (state) => {
83
+ const { client } = state;
84
+ try {
85
+ const { database, collection, documents, callback } = expandReferences(params)(state);
86
+ const db = client.db(database);
87
+ const mCollection = db.collection(collection);
88
+ return new Promise((resolve, reject) => {
89
+ mCollection.insertMany(documents, (err, result2) => {
90
+ if (err) {
91
+ reject(err);
92
+ state.client.close();
93
+ } else {
94
+ console.log(
95
+ `Inserted ${documents.length} documents into the collection`
96
+ );
97
+ console.log(JSON.stringify(result2, null, 2));
98
+ const nextState = composeNextState(state, result2);
99
+ if (callback)
100
+ resolve(callback(nextState));
101
+ resolve(nextState);
102
+ }
103
+ });
104
+ });
105
+ } catch (error) {
106
+ state.client.close();
107
+ throw error;
108
+ }
109
+ };
110
+ }
111
+ function findDocuments(params) {
112
+ return (state) => {
113
+ const { client } = state;
114
+ try {
115
+ const { database, collection, query, callback } = expandReferences(params)(state);
116
+ const db = client.db(database);
117
+ const mCollection = db.collection(collection);
118
+ return new Promise((resolve, reject) => {
119
+ mCollection.find(query).toArray((err, docs) => {
120
+ if (err) {
121
+ reject(err);
122
+ state.client.close();
123
+ } else {
124
+ console.log(`Found ${docs.length} documents in the collection`);
125
+ console.log(JSON.stringify(result, null, 2));
126
+ const nextState = composeNextState(state, docs);
127
+ if (callback)
128
+ resolve(callback(nextState));
129
+ resolve(nextState);
130
+ }
131
+ });
132
+ });
133
+ } catch (error) {
134
+ state.client.close();
135
+ throw error;
136
+ }
137
+ };
138
+ }
139
+ function updateDocument(params) {
140
+ return (state) => {
141
+ const { client } = state;
142
+ try {
143
+ const { database, collection, filter, changes, options, callback } = expandReferences(params)(state);
144
+ const db = client.db(database);
145
+ const mCollection = db.collection(collection);
146
+ return new Promise((resolve, reject) => {
147
+ mCollection.updateMany(
148
+ filter,
149
+ { $set: changes },
150
+ options,
151
+ (err, result2) => {
152
+ if (err) {
153
+ reject(err);
154
+ state.client.close();
155
+ } else {
156
+ console.log(
157
+ `Updated a document matching ${JSON.stringify(
158
+ filter
159
+ )} in the collection.`
160
+ );
161
+ console.log(JSON.stringify(result2, null, 2));
162
+ const nextState = composeNextState(state, result2);
163
+ if (callback)
164
+ resolve(callback(nextState));
165
+ resolve(nextState);
166
+ }
167
+ }
168
+ );
169
+ });
170
+ } catch (error) {
171
+ state.client.close();
172
+ throw error;
173
+ }
174
+ };
175
+ }
176
+
177
+ // src/index.js
178
+ var src_default = Adaptor_exports;
179
+ export {
180
+ alterState,
181
+ dataPath,
182
+ dataValue,
183
+ src_default as default,
184
+ each,
185
+ execute,
186
+ field,
187
+ fields,
188
+ findDocuments,
189
+ fn,
190
+ insertDocuments,
191
+ lastReferenceValue,
192
+ merge,
193
+ sourceValue,
194
+ updateDocument
195
+ };
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@openfn/language-mongodb",
3
+ "version": "1.0.4",
4
+ "description": "A language package for working with MongoDb",
5
+ "main": "dist/index.cjs",
6
+ "author": "Open Function Group",
7
+ "license": "LGPLv3",
8
+ "files": [
9
+ "dist/",
10
+ "types/",
11
+ "ast.json",
12
+ "configuration-schema.json"
13
+ ],
14
+ "dependencies": {
15
+ "@openfn/language-common": "^1.7.5",
16
+ "mongodb": "^3.7.3"
17
+ },
18
+ "devDependencies": {
19
+ "@openfn/buildtools": "^1.0.2",
20
+ "@openfn/simple-ast": "0.4.1",
21
+ "assertion-error": "^1.1.0",
22
+ "chai": "^3.5.0",
23
+ "deep-eql": "^0.1.3",
24
+ "esno": "^0.16.3",
25
+ "mocha": "^10.1.0",
26
+ "nock": "^12.0.3",
27
+ "rimraf": "^3.0.2"
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/openfn/adaptors.git"
32
+ },
33
+ "type": "module",
34
+ "exports": {
35
+ ".": {
36
+ "import": "./dist/index.js",
37
+ "require": "./dist/index.cjs"
38
+ },
39
+ "./package.json": "./package.json"
40
+ },
41
+ "types": "types/index.d.ts",
42
+ "scripts": {
43
+ "build": "pnpm clean && build-adaptor mongodb",
44
+ "test": "mocha --experimental-specifier-resolution=node --no-warnings",
45
+ "test:watch": "mocha -w --experimental-specifier-resolution=node --no-warnings",
46
+ "clean": "rimraf dist types docs",
47
+ "pack": "pnpm pack --pack-destination ../../dist"
48
+ }
49
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Execute a sequence of operations.
3
+ * Wraps `@openfn/language-common/execute`, and prepends initial state for http.
4
+ * @example
5
+ * execute(
6
+ * insertDocuments(params),
7
+ * findDocuments(params)
8
+ * )(state)
9
+ * @function
10
+ * @param {Operations} operations - Operations to be performed.
11
+ * @returns {Operation}
12
+ */
13
+ export function execute(...operations: Operations): Operation;
14
+ /**
15
+ * Inserts documents into a mongoDb collection
16
+ * @example
17
+ * insertDocuments({
18
+ * database: 'str',
19
+ * collection: 'kids',
20
+ * documents: [1,2,3]
21
+ * });
22
+ * @function
23
+ * @param {object} params - Configuration for mongo
24
+ * @returns {State}
25
+ */
26
+ export function insertDocuments(params: object): State;
27
+ /**
28
+ * Find documents in a mongoDb collection
29
+ * @example
30
+ * findDocuments({
31
+ * database: 'str',
32
+ * collection: 'cases',
33
+ * query: {a:3}
34
+ * });
35
+ * @function
36
+ * @param {object} params - Configuration for mongo
37
+ * @returns {State}
38
+ */
39
+ export function findDocuments(params: object): State;
40
+ /**
41
+ * Updates document (optionally upserting) into a mongoDb collection
42
+ * @example
43
+ * updateDocuments({
44
+ * database: 'str',
45
+ * collection: 'animals',
46
+ * filter: { type: 'fuzzy' },
47
+ * changes: { kind: 'soft' },
48
+ * options: { upsert: true }
49
+ * });
50
+ * @function
51
+ * @param {object} params - Configuration for mongo
52
+ * @returns {State}
53
+ */
54
+ export function updateDocument(params: object): State;
55
+ export { field, fields, sourceValue, fn, alterState, each, merge, dataPath, dataValue, lastReferenceValue } from "@openfn/language-common";
@@ -0,0 +1,3 @@
1
+ export default Adaptor;
2
+ export * from "./Adaptor";
3
+ import * as Adaptor from "./Adaptor";