cosa 7.1.3 → 9.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  The MIT License (MIT)
2
2
 
3
- Copyright (c) 2023 Losant IoT, Inc.
3
+ Copyright (c) 2024 Losant IoT, Inc.
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Cosa
2
2
 
3
- [![Build Status](https://travis-ci.com/Losant/cosa.svg?branch=master)](https://travis-ci.com/Losant/cosa) [![npm version](https://badge.fury.io/js/cosa.svg)](https://badge.fury.io/js/cosa)
3
+ ![Build Status](https://github.com/Losant/cosa/actions/workflows/test.yml/badge.svg?branch=master) [![npm version](https://badge.fury.io/js/cosa.svg)](https://badge.fury.io/js/cosa)
4
4
 
5
5
  Simplified object modeling for MongoDB
6
6
 
@@ -17,7 +17,7 @@ yarn add cosa
17
17
  First define a model:
18
18
 
19
19
  ```javascript
20
- const { Model } = require('cosa');
20
+ import { Model } from 'cosa';
21
21
 
22
22
  const UserModel = Model.define({
23
23
  name: 'UserModel',
package/lib/array.js CHANGED
@@ -1,5 +1,5 @@
1
- const clone = require('clone');
2
- const Immutable = require('./immutable');
1
+ import clone from 'clone';
2
+ import Immutable from './immutable.js';
3
3
 
4
4
  /**
5
5
  * Immutable handler used to wrap a javascript array.
@@ -9,7 +9,7 @@ const Immutable = require('./immutable');
9
9
  * @param {object} options - Optional settings
10
10
  * @returns {undefined} no return value
11
11
  */
12
- module.exports = function(data, builder, options) {
12
+ export default function(data, builder, options) {
13
13
  builder.type = 'Array';
14
14
 
15
15
  builder.defineProperty('length', data.length);
@@ -73,4 +73,4 @@ module.exports = function(data, builder, options) {
73
73
  const newArr = clone(data);
74
74
  return Immutable.create(newArr.reverse(), options);
75
75
  });
76
- };
76
+ }
@@ -1,12 +1,13 @@
1
1
  /**
2
- * Immutable handler used to wrap bson ObjectID.
3
- * @name ImmutableBSONObjectID
2
+ * Immutable handler used to wrap bson ObjectId.
3
+ * @name ImmutableBSONObjectId
4
4
  * @param {object} data - the object id
5
5
  * @param {object} builder - the immutable handler to wrapper the object id.
6
6
  * @returns {undefined} - this adds methods on to the builder.
7
7
  */
8
- module.exports = function(data, builder) {
9
- builder.type = 'bson.ObjectID';
8
+
9
+ export default function(data, builder) {
10
+ builder.type = 'bson.ObjectId';
10
11
 
11
12
  builder.defineMethod('toString', function() {
12
13
  return data.toString();
@@ -31,4 +32,4 @@ module.exports = function(data, builder) {
31
32
  builder.defineProperty('id', function() {
32
33
  return data.id;
33
34
  });
34
- };
35
+ }
package/lib/cursor.js CHANGED
@@ -1,4 +1,4 @@
1
- const { times } = require('omnibelt');
1
+ import { times } from 'omnibelt';
2
2
  /**
3
3
  * Wrapper around MongoDB Cursor to convert results to Models.
4
4
  * @class
@@ -15,16 +15,11 @@ class Cursor {
15
15
  * Iterates over cursor and executes the iterator for each model returned.
16
16
  * @param {function} iterator - a function that will be called for each doc
17
17
  * @returns {Promise} - resolves when list is exhausted.
18
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#foreach}
18
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/FindCursor.html#forEach}
19
19
  */
20
20
  async forEach(iterator) {
21
- return new Promise((resolve, reject) => {
22
- this._cursor.forEach(
23
- (doc) => { return iterator(this._factory(doc)); },
24
- (err) => {
25
- if (err) { return reject(err); }
26
- return resolve();
27
- });
21
+ return this._cursor.forEach((doc) => {
22
+ return iterator(this._factory(doc));
28
23
  });
29
24
  }
30
25
 
@@ -64,7 +59,7 @@ class Cursor {
64
59
  * @param {boolean} applySkipLimit - the number of docs to skip over in the count
65
60
  * @param {object} options - mongo cursor count options
66
61
  * @returns {number} the count of items in the list
67
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#count}
62
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/FindCursor.html#count}
68
63
  */
69
64
  count(applySkipLimit, options) {
70
65
  return this._cursor.count(applySkipLimit, options);
@@ -73,7 +68,7 @@ class Cursor {
73
68
  /**
74
69
  * Close the underlying MongoDB cursor.
75
70
  * @returns {boolean} `true` if cursor is successfully closed
76
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#close}
71
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/FindCursor.html#close-1}
77
72
  */
78
73
  close() {
79
74
  return this._cursor.close();
@@ -82,7 +77,7 @@ class Cursor {
82
77
  /**
83
78
  * Returns `true` if the cursor is closed.
84
79
  * @returns {boolean} `true` if the cursor is closed.
85
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#close}
80
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/FindCursor.html#closed}
86
81
  */
87
82
  isClosed() {
88
83
  return this._cursor.closed;
@@ -92,7 +87,7 @@ class Cursor {
92
87
  * Sets the cursor query filter.
93
88
  * @param {object} filter - The filter object used for the cursor.
94
89
  * @returns {Cursor} the cursor instance
95
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#filter}
90
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/FindCursor.html#filter}
96
91
  */
97
92
  filter(filter) {
98
93
  this._cursor.filter(filter);
@@ -103,7 +98,7 @@ class Cursor {
103
98
  * Sets the cursor limit.
104
99
  * @param {number} limit - A number to limit the cursor by.
105
100
  * @returns {Cursor} the cursor instance
106
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#limit}
101
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/FindCursor.html#limit}
107
102
  */
108
103
  limit(limit) {
109
104
  this._cursor.limit(limit);
@@ -114,7 +109,7 @@ class Cursor {
114
109
  * Maps cursor results using the provided function.
115
110
  * @param {function} transform - A function that will be called by each document in the cursor.
116
111
  * @returns {Cursor} the cursor instance
117
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#map}
112
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/FindCursor.html#map}
118
113
  */
119
114
  map(transform) {
120
115
  return this._cursor.map((doc) => {
@@ -126,7 +121,7 @@ class Cursor {
126
121
  * Sets the cursor max.
127
122
  * @param {number} max - Specify a $max value to specify the exclusive upper bound for a specific index in order to constrain the results of find().
128
123
  * @returns {Cursor} the cursor instance
129
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#max}
124
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/FindCursor.html#max}
130
125
  */
131
126
  max(max) {
132
127
  this._cursor.max(max);
@@ -137,7 +132,7 @@ class Cursor {
137
132
  * Sets the cursor min.
138
133
  * @param {number} min - Specify a $min value to specify the inclusive lower bound for a specific index in order to constrain the results of find().
139
134
  * @returns {Cursor} the cursor instance
140
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#min}
135
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/FindCursor.html#min}
141
136
  */
142
137
  min(min) {
143
138
  this._cursor.min(min);
@@ -147,7 +142,7 @@ class Cursor {
147
142
  /**
148
143
  * Get the next available document from the cursor.
149
144
  * @returns {Promise} - resolves with the next document from the cursor.
150
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#next}
145
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/FindCursor.html#next}
151
146
  */
152
147
  async next() {
153
148
  const doc = await this._cursor.next();
@@ -158,7 +153,7 @@ class Cursor {
158
153
  * Like `Cursor.forEach` but guarantees non-parallel execution of iterator.
159
154
  * @param {function} iterator - the function that will be called by each document serially.
160
155
  * @returns {Promise} - resolves when each item in the cursor has been passed to the iterator function
161
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#forEach}
156
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/FindCursor.html#forEach}
162
157
  */
163
158
  serialForEach(iterator) {
164
159
  const itemWrapper = async () => {
@@ -174,7 +169,7 @@ class Cursor {
174
169
  * Sets the cursor skip.
175
170
  * @param {number} value - The skip for the cursor query.
176
171
  * @returns {Cursor} the cursor instance
177
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#skip}
172
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/FindCursor.html#skip}
178
173
  */
179
174
  skip(value) {
180
175
  this._cursor.skip(value);
@@ -186,7 +181,7 @@ class Cursor {
186
181
  * @param {string|Array} sortOrList The key or keys set for the sort.
187
182
  * @param {number} direction - `1` or `-1` for ascending or descending
188
183
  * @returns {Cursor} the cursor instance
189
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#sort}
184
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/FindCursor.html#sort}
190
185
  */
191
186
  sort(sortOrList, direction) {
192
187
  this._cursor.sort(sortOrList, direction);
@@ -196,11 +191,11 @@ class Cursor {
196
191
  /**
197
192
  * Returns the cursor results as an array.
198
193
  * @returns {Promise} that resolves into an array from the cursor
199
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#toArray}
194
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/FindCursor.html#toArray}
200
195
  */
201
196
  async toArray() {
202
197
  return this._cursor.map((doc) => { return this._factory(doc); }).toArray();
203
198
  }
204
199
  }
205
200
 
206
- module.exports = Cursor;
201
+ export default Cursor;
package/lib/date.js CHANGED
@@ -1,4 +1,4 @@
1
- const Immutable = require('./immutable');
1
+ import Immutable from './immutable.js';
2
2
 
3
3
  /**
4
4
  * Immutable handler used to wrap javascript Dates.
@@ -8,7 +8,7 @@ const Immutable = require('./immutable');
8
8
  * @param {object} options - Optional settings
9
9
  * @returns {undefined} no return value
10
10
  */
11
- module.exports = function(data, builder, options) {
11
+ export default function(data, builder, options) {
12
12
  builder.type = 'Date';
13
13
 
14
14
  builder.defineMethod('mutate', function(cb) {
@@ -40,4 +40,4 @@ module.exports = function(data, builder, options) {
40
40
  });
41
41
  });
42
42
 
43
- };
43
+ }
package/lib/db.js CHANGED
@@ -1,11 +1,10 @@
1
- const debug = require('debug')('cosa:db');
2
- const { ObjectId } = require('bson');
3
- const { EventEmitter } = require('events');
4
- const { MongoClient } = require('mongodb');
5
- const {
1
+ import _debug from 'debug';
2
+ import { EventEmitter } from 'events';
3
+ import { MongoClient, ObjectId } from 'mongodb';
4
+ import {
6
5
  curry, split, last, pipe, clamp, isPlainObject, isNilOrEmpty
7
- } = require('omnibelt');
8
- const Immutable = require('./immutable');
6
+ } from 'omnibelt';
7
+ import Immutable from './immutable.js';
9
8
  const RESULT_FIELDS = [ 'insertedId', 'insertedCount', 'insertedIds', 'matchedCount', 'modifiedCount', 'upsertedCount', 'upsertedId', 'deletedCount', 'upsertedIds' ];
10
9
  const getDbName = (uri) => {
11
10
  let name = pipe(split('/'), last)(uri);
@@ -14,6 +13,7 @@ const getDbName = (uri) => {
14
13
  }
15
14
  return name;
16
15
  };
16
+ const debug = _debug('cosa:db');
17
17
 
18
18
  const normalizeResult = (r) => {
19
19
  const result = {
@@ -141,7 +141,7 @@ class Database extends EventEmitter {
141
141
  * @param {string} name - Collection name.
142
142
  * @param {object} [options] - Optional collection settings.
143
143
  * @returns {Promise} resolves with the connection
144
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/db.html#collection}
144
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/Db.html#collection}
145
145
  */
146
146
  async collection(name, options) {
147
147
  if (this._connectionStatus === 'connected') {
@@ -184,14 +184,14 @@ class Database extends EventEmitter {
184
184
  * @param {number} [options.maxTimeMS] - maximum amount of time (in ms) this cursor is allowed to live
185
185
  * @param {object} [options.session] - Mongo session or Cosa Mongo session wrapper
186
186
  * @returns {Cursor} returns Cursor object
187
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html}
188
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#find}
189
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#findOne}
190
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#count}
191
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/interfaces/findoptions.html#readpreference}
192
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/interfaces/findoptions.html#batchsize}
193
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/interfaces/findoptions.html#nocursortimeout}
194
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/interfaces/findoptions.html#maxtimems}
187
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html}
188
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#find}
189
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#findOne}
190
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#count}
191
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/interfaces/FindOptions.html#readPreference}
192
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/interfaces/FindOptions.html#batchSize}
193
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/interfaces/FindOptions.html#noCursorTimeout}
194
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/interfaces/FindOptions.html#maxTimeMS}
195
195
  */
196
196
  async find(collectionName, query, options = {}) {
197
197
  query = deserialize(query);
@@ -254,9 +254,9 @@ class Database extends EventEmitter {
254
254
  * @param {object} [options.writeConcern] - the write concern
255
255
  * @param {object} [options.session] - Mongo session or Cosa Mongo session wrapper
256
256
  * @returns {Promise} resolves with an object with results, and ops as keys
257
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#insertmany}
258
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#insertOne}
259
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/interfaces/insertoneoptions.html#writeconcern}
257
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#insertMany}
258
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#insertOne}
259
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/interfaces/BulkWriteOptions.html#writeConcern}
260
260
  */
261
261
  async insert(collectionName, docs, { writeConcern, session } = {}) {
262
262
  docs = deserialize(docs);
@@ -265,7 +265,6 @@ class Database extends EventEmitter {
265
265
  writeConcern,
266
266
  session: session?.mongoSession || session
267
267
  });
268
-
269
268
  if (Array.isArray(docs)) {
270
269
  docs.forEach((doc, i) => {
271
270
  doc._id = results.insertedIds[`${i}`];
@@ -289,9 +288,9 @@ class Database extends EventEmitter {
289
288
  * @param {object} [options.writeConcern] - the write concern options
290
289
  * @param {object} [options.session] - Mongo session or Cosa Mongo session wrapper
291
290
  * @returns {Promise} resolves with an object with results, and ops as keys
292
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#updateMany}
293
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#updateOne}
294
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/interfaces/updateoptions.html#writeconcern}
291
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#updateMany}
292
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#updateOne}
293
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/interfaces/BulkWriteOptions.html#writeConcern}
295
294
  */
296
295
  async update(collectionName, query, update, options = {}) {
297
296
  query = deserialize(query);
@@ -313,9 +312,9 @@ class Database extends EventEmitter {
313
312
  * @param {object} [options.writeConcern] - the write concern options
314
313
  * @param {object} [options.session] - Mongo session or Cosa Mongo session wrapper
315
314
  * @returns {Promise} resolves with an object with results, and ops as keys
316
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#deleteMany}
317
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#deleteOne}
318
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/interfaces/deleteoptions.html#writeconcern}
315
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#deleteMany}
316
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#deleteOne}
317
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/interfaces/DeleteOptions.html#writeConcern}
319
318
  */
320
319
  async remove(collectionName, query, options = {}) {
321
320
  query = deserialize(query);
@@ -336,10 +335,10 @@ class Database extends EventEmitter {
336
335
  * @param {number} [options.maxTimeMS] - maximum amount of time (in ms) this cursor is allowed to live
337
336
  * @param {object} [options.session] - Mongo session or Cosa Mongo session wrapper
338
337
  * @returns {Promise} resolves with the result of the aggregation from mongo
339
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#aggregate}
340
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/interfaces/aggregateoptions.html#readpreference}
341
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/interfaces/aggregateoptions.html#batchsize}
342
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/interfaces/aggregateoptions.html#maxtimems}
338
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#aggregate}
339
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/interfaces/AggregateOptions.html#readPreference}
340
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/interfaces/AggregateOptions.html#batchSize}
341
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/interfaces/AggregateOptions.html#maxTimeMS}
343
342
  */
344
343
  async aggregate(collectionName, pipeline, options = {}) {
345
344
  pipeline = deserialize(pipeline);
@@ -362,9 +361,9 @@ class Database extends EventEmitter {
362
361
  * @param {number} [options.maxTimeMS] - maximum amount of time (in ms) this cursor is allowed to live
363
362
  * @param {object} [options.session] - Mongo session or Cosa Mongo session wrapper
364
363
  * @returns {Promise} resolves with the result of the distinct query from mongo
365
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#distinct}
366
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/interfaces/commandoperationoptions.html#readpreference}
367
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/interfaces/commandoperationoptions.html#maxtimems}
364
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#distinct}
365
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/interfaces/CommandOperationOptions.html#readPreference}
366
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/interfaces/CommandOperationOptions.html#maxTimeMS}
368
367
  */
369
368
  async distinct(collectionName, key, query, options = {}) {
370
369
  query = deserialize(query);
@@ -385,8 +384,8 @@ class Database extends EventEmitter {
385
384
  * @param {object} [options.writeConcern] - the write concern options
386
385
  * @param {object} [options.session] - Mongo session or Cosa Mongo session wrapper
387
386
  * @returns {Promise} resolves with the result of the distinct query from mongo
388
- * @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#replace}
389
- * @see {@link https://mongodb.github.io/node-mongodb-native/4.0/interfaces/replaceoptions.html#writeconcern}
387
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/classes/Collection.html#replaceOne}
388
+ * @see {@link https://mongodb.github.io/node-mongodb-native/6.3/interfaces/ReplaceOptions.html#writeConcern}
390
389
  */
391
390
  async replace(collectionName, query, replace, { writeConcern, session } = {}) {
392
391
  const collection = await this.collection(collectionName);
@@ -403,4 +402,4 @@ class Database extends EventEmitter {
403
402
 
404
403
  }
405
404
 
406
- module.exports = new Database();
405
+ export default new Database();
@@ -1,4 +1,4 @@
1
- const Immutable = require('./immutable');
1
+ import Immutable from './immutable.js';
2
2
 
3
3
  /**
4
4
  * Immutable handler used to create an immutable object based on definition
@@ -13,7 +13,7 @@ const Immutable = require('./immutable');
13
13
  * @param {object} [options.definition.methods] - Describes the methods of the immutable object.
14
14
  * @returns {undefined} no return value
15
15
  */
16
- module.exports = function(data, builder, options) {
16
+ export default function(data, builder, options) {
17
17
 
18
18
  if (options.definition) {
19
19
  const definition = options.definition;
@@ -55,4 +55,4 @@ module.exports = function(data, builder, options) {
55
55
  }
56
56
  }
57
57
 
58
- };
58
+ }
package/lib/errors.js CHANGED
@@ -1,5 +1,5 @@
1
- const TypedError = require('error/typed');
2
- const WrappedError = require('error/wrapped');
1
+ import TypedError from 'error/typed.js';
2
+ import WrappedError from 'error/wrapped.js';
3
3
 
4
4
  // http response code reference:
5
5
  // 400 BAD REQUEST 401 UNAUTHORIZED 402 PAYMENT REQUIRED
@@ -16,7 +16,7 @@ const WrappedError = require('error/wrapped');
16
16
  * Custom errors.
17
17
  * @namespace errors
18
18
  */
19
- module.exports = {
19
+ export default {
20
20
 
21
21
  /**
22
22
  * Error used when an action results in a conflict between documents.
package/lib/immutable.js CHANGED
@@ -1,4 +1,4 @@
1
- const clone = require('clone');
1
+ import clone from 'clone';
2
2
 
3
3
  const IMMUTABLE_TYPES = [ 'function', 'string', 'boolean', 'number', 'undefined' ];
4
4
 
@@ -107,9 +107,9 @@ Immutable = {
107
107
  if (typeHandler.type === '*' || typeof data === typeHandler.type) {
108
108
  return typeHandler.handler(data, builder, options);
109
109
  }
110
- if (typeHandler.type.toLowerCase() === 'array' && Array.isArray(data)) {
110
+ if (typeHandler.type === 'array' && Array.isArray(data)) {
111
111
  return typeHandler.handler(data, builder, options);
112
- } else if (typeHandler.type.toLowerCase() === 'objectid' && data._bsontype === 'ObjectID') {
112
+ } else if (typeHandler.type === 'objectId' && data._bsontype === 'ObjectId') {
113
113
  return typeHandler.handler(data, builder, options);
114
114
  }
115
115
  } else if ('function' === typeof typeHandler.type && data instanceof typeHandler.type) {
@@ -125,4 +125,4 @@ Immutable = {
125
125
  }
126
126
  };
127
127
 
128
- module.exports = Immutable;
128
+ export default Immutable;
package/lib/index.js CHANGED
@@ -1,34 +1,29 @@
1
- const debug = require('debug')('cosa');
2
- const Model = require('./model');
3
- const Immutable = require('./immutable');
1
+ import _debug from 'debug';
2
+ import _db from './db.js';
3
+
4
+ const debug = _debug('cosa');
4
5
 
5
6
  /**
6
7
  * Main module that provides access to library classes and functions.
7
8
  * @module cosa
8
9
  */
9
- module.exports = {
10
-
11
- Model: Model,
12
-
13
- Immutable: Immutable,
14
-
15
- /**
16
- * Database connection instance.
17
- * @see {@link Database}
18
- */
19
- db: require('./db'),
20
-
21
- /**
22
- * Initialize cosa and connect to the database. Explicitly calling the
23
- * function is not needed if `process.env.COSA_DB_URI` is properly set.
24
- * @param {string} [uri] - URI to the database. If no URI is provided, the value
25
- * of `process.env.COSA_DB_URI` is used. See
26
- * https://docs.mongodb.com/manual/reference/connection-string/ for
27
- * information on the URI format.
28
- * @returns {Promise} db.init promise
29
- */
30
- init: function(uri) {
31
- debug('initializing cosa');
32
- module.exports.db.init(uri || process.env.COSA_DB_URI);
33
- }
10
+ export { default as Model } from './model.js';
11
+ export { default as Immutable } from './immutable.js';
12
+ /**
13
+ * Database connection instance.
14
+ * @see {@link Database}
15
+ */
16
+ export const db = _db;
17
+ /**
18
+ * Initialize cosa and connect to the database. Explicitly calling the
19
+ * function is not needed if `process.env.COSA_DB_URI` is properly set.
20
+ * @param {string} [uri] - URI to the database. If no URI is provided, the value
21
+ * of `process.env.COSA_DB_URI` is used. See
22
+ * https://docs.mongodb.com/manual/reference/connection-string/ for
23
+ * information on the URI format.
24
+ * @returns {Promise} db.init promise
25
+ */
26
+ export const init = function(uri) {
27
+ debug('initializing cosa');
28
+ return db.init(uri || process.env.COSA_DB_URI);
34
29
  };
package/lib/model.js CHANGED
@@ -1,27 +1,34 @@
1
- const joi = require('joi');
2
- const etag = require('etag');
3
- const debug = require('debug')('cosa:model');
4
- const objectPath = require('object-path');
5
- const clone = require('clone');
6
- const { EJSON } = require('bson');
7
- const Cursor = require('./cursor');
8
- const errors = require('./errors');
9
- const { ObjectId } = require('bson');
10
- const {
1
+ import joi from 'joi';
2
+ import etag from 'etag';
3
+ import _debug from 'debug';
4
+ const debug = _debug('cosa:model');
5
+ import objectPath from 'object-path';
6
+ import clone from 'clone';
7
+ import { ObjectId, BSON } from 'mongodb';
8
+ import Cursor from './cursor.js';
9
+ import errors from './errors.js';
10
+ import {
11
11
  pathEq, complement, pick, omit, has, isEmpty, isPlainObject
12
- } = require('omnibelt');
13
- const { buildPropertySchema } = require('./utils');
14
- const Immutable = require('./immutable');
15
- Immutable.use(require('./defined-object'));
16
- Immutable.use('array', require('./array'));
17
- Immutable.use(Date, require('./date'));
18
- Immutable.use('objectid', require('./bson-objectId'));
19
- Immutable.use(require('./object'));
12
+ } from 'omnibelt';
13
+ import { buildPropertySchema } from './utils.js';
14
+ import Immutable from './immutable.js';
15
+ import definedObject from './defined-object.js';
16
+ import immutableArray from './array.js';
17
+ import immutableDate from './date.js';
18
+ import immutableBSONObjectId from './bson-objectId.js';
19
+ import immutableObject from './object.js';
20
+
21
+ const { EJSON } = BSON;
22
+ Immutable.use(definedObject);
23
+ Immutable.use('array', immutableArray);
24
+ Immutable.use(Date, immutableDate);
25
+ Immutable.use('objectId', immutableBSONObjectId);
26
+ Immutable.use(immutableObject);
20
27
 
21
28
  const isNotVirtual = complement(pathEq('virtual', [ 'type' ]));
22
29
  const removeMeta = omit(['__modified', '__original']);
23
30
 
24
- const db = require('./db');
31
+ import db from './db.js';
25
32
  // functions that are defined later on.
26
33
  let _serialize, _extend;
27
34
 
@@ -368,7 +375,6 @@ const _create = (data, definition) => {
368
375
  const result = await db.find(collection, query, options);
369
376
  return _create(result, definition);
370
377
  };
371
-
372
378
  return Immutable.create(data, { definition });
373
379
  };
374
380
 
@@ -397,7 +403,7 @@ const define = (definition) => {
397
403
  }
398
404
 
399
405
  definition.properties._id = {
400
- type: 'objectid'
406
+ type: 'objectId'
401
407
  };
402
408
 
403
409
  definition.properties._etag = {
@@ -547,6 +553,6 @@ _extend = (definition, superDef) => {
547
553
  return define(newDef);
548
554
  };
549
555
 
550
- module.exports = {
556
+ export default {
551
557
  define
552
558
  };
package/lib/object.js CHANGED
@@ -1,4 +1,4 @@
1
- const Immutable = require('./immutable');
1
+ import Immutable from './immutable.js';
2
2
 
3
3
  const addProp = function(name, builder, data) {
4
4
  if (builder.props[name]) { return; }
@@ -17,10 +17,10 @@ const addProp = function(name, builder, data) {
17
17
  * @param {object} builder - Builder instance
18
18
  * @returns {undefined} no return value
19
19
  */
20
- module.exports = function(data, builder) {
20
+ export default function(data, builder) {
21
21
  for (const p in data) {
22
22
  if (Object.prototype.hasOwnProperty.call(data, p)) {
23
23
  addProp(p, builder, data);
24
24
  }
25
25
  }
26
- };
26
+ }
package/lib/session.js CHANGED
@@ -1,7 +1,7 @@
1
- const db = require('./db');
2
- const { forEachSerialP } = require('omnibelt');
1
+ import db from './db.js';
2
+ import { forEachSerialP } from 'omnibelt';
3
3
 
4
- const createSession = async () => {
4
+ export const createSession = async () => {
5
5
  const afterCommits = [];
6
6
  const afterAborts = [];
7
7
  if (!db._client) {
@@ -48,7 +48,3 @@ const createSession = async () => {
48
48
  afterAborts
49
49
  };
50
50
  };
51
-
52
- module.exports = {
53
- createSession
54
- };
package/lib/utils.js CHANGED
@@ -1,8 +1,9 @@
1
- const joi = require('joi');
2
- const { ObjectId } = require('bson');
3
- const buildPropertySchema = (name, propertyDef) => { // eslint-disable-line complexity
1
+ import joi from 'joi';
2
+ import { ObjectId } from 'mongodb';
3
+
4
+ export const buildPropertySchema = (name, propertyDef) => { // eslint-disable-line complexity
4
5
  let schema = null;
5
- switch ((propertyDef.type || 'any').trim().toLowerCase()) {
6
+ switch ((propertyDef.type || 'any').trim()) {
6
7
  case 'array':
7
8
  schema = joi.array();
8
9
  if ('undefined' !== typeof propertyDef.items) {
@@ -65,7 +66,7 @@ const buildPropertySchema = (name, propertyDef) => { // eslint-disable-line comp
65
66
  if (propertyDef.uppercase) { schema = schema.uppercase(); }
66
67
  if (propertyDef.trim) { schema = schema.trim(); }
67
68
  break;
68
- case 'objectid':
69
+ case 'objectId':
69
70
  schema = joi.object().instance(ObjectId);
70
71
  break;
71
72
  case 'any':
@@ -105,8 +106,3 @@ const buildPropertySchema = (name, propertyDef) => { // eslint-disable-line comp
105
106
  if ('undefined' !== typeof propertyDef.default) { schema = schema.default(propertyDef.default); }
106
107
  return schema;
107
108
  };
108
-
109
- module.exports = {
110
- buildPropertySchema
111
- };
112
-
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "cosa",
3
- "version": "7.1.3",
3
+ "version": "9.0.0",
4
4
  "description": "Cosa Models for MongoDB",
5
5
  "main": "lib/index.js",
6
+ "type": "module",
6
7
  "engines": {
7
- "node": ">=14",
8
+ "node": ">=18",
8
9
  "yarn": ">=1.2.1"
9
10
  },
10
11
  "scripts": {
@@ -15,8 +16,7 @@
15
16
  "reinstall": "rm -rf node_modules && yarn install",
16
17
  "test": "NODE_ENV='test' COSA_DB_URI='mongodb://localhost:27017/test' mocha",
17
18
  "test-debug": "yarn test --debug-brk",
18
- "doc": "documentation build lib/index.js -f md --github > API.md",
19
- "prepare": "husky install"
19
+ "doc": "documentation build lib/index.js -f md --github > API.md"
20
20
  },
21
21
  "author": "Losant <hello@losant.com>",
22
22
  "license": "MIT",
@@ -35,29 +35,28 @@
35
35
  "lib"
36
36
  ],
37
37
  "dependencies": {
38
- "bson": "~4.7.2",
39
38
  "clone": "^2.1.2",
40
- "debug": "^4.3.4",
39
+ "debug": "^4.3.6",
41
40
  "error": "^7.0.2",
42
41
  "etag": "^1.8.1",
43
- "joi": "^17.9.2",
44
- "mongodb": "~4.17.1",
42
+ "joi": "^17.13.3",
43
+ "mongodb": "~6.8.0",
45
44
  "object-path": "^0.11.8",
46
- "omnibelt": "^3.1.2"
45
+ "omnibelt": "^4.0.0"
47
46
  },
48
47
  "devDependencies": {
49
48
  "@losant/eslint-config-losant": "^1.6.1",
50
- "husky": "^8.0.3",
51
- "lint-staged": "~15.0.2",
52
- "chai": "^4.3.10",
53
- "chai-as-promised": "^7.1.1",
49
+ "husky": "^9.1.5",
50
+ "lint-staged": "~15.2.9",
51
+ "chai": "^5.1.1",
52
+ "chai-as-promised": "^8.0.0",
54
53
  "chai-datetime": "^1.8.0",
55
54
  "documentation": "^14.0.2",
56
- "mocha": "^10.2.0",
55
+ "mocha": "^10.7.3",
57
56
  "string-template": "^1.0.0"
58
57
  },
59
58
  "eslintConfig": {
60
- "extends": "@losant/eslint-config-losant/env/node"
59
+ "extends": "@losant/eslint-config-losant/env/esm"
61
60
  },
62
61
  "mocha": {
63
62
  "require": "chai",