|
| 1 | +.. Top level title in calling page, won't render in right TOC |
| 2 | +
|
| 3 | +The results of database queries cannot be passed inside the following |
| 4 | +contexts: |
| 5 | + |
| 6 | +- Class constructor functions |
| 7 | +- Non-async generator functions |
| 8 | +- Callbacks to ``.sort()`` on an array |
| 9 | + |
| 10 | +To access to the results of database calls, use `async functions |
| 11 | +<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function>`__, |
| 12 | +`async generator functions |
| 13 | +<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of>`__, |
| 14 | +or ``.map()``. |
| 15 | + |
| 16 | +Constructors |
| 17 | +~~~~~~~~~~~~ |
| 18 | + |
| 19 | +The following constructors do not work: |
| 20 | + |
| 21 | +.. code-block:: javascript |
| 22 | + :copyable: false |
| 23 | + |
| 24 | + // This code will fail |
| 25 | + class FindResults { |
| 26 | + constructor() { |
| 27 | + this.value = db.students.find(); |
| 28 | + } |
| 29 | + } |
| 30 | +
|
| 31 | + // This code will fail |
| 32 | + function listEntries() { return db.students.find(); } |
| 33 | + class FindResults { |
| 34 | + constructor() { |
| 35 | + this.value = listEntries(); |
| 36 | + } |
| 37 | + } |
| 38 | +
|
| 39 | +Use an ``async`` function instead: |
| 40 | + |
| 41 | +.. code-block:: javascript |
| 42 | +
|
| 43 | + class FindResults { |
| 44 | + constructor() { |
| 45 | + this.value = ( async() => { |
| 46 | + return db.students.find(); |
| 47 | + } )(); |
| 48 | + } |
| 49 | + } |
| 50 | +
|
| 51 | +.. note:: |
| 52 | + |
| 53 | + You can also create a method that performs a database operation |
| 54 | + inside a class as an alternative to working with asynchronous |
| 55 | + JavaScript. |
| 56 | + |
| 57 | + .. code-block:: |
| 58 | +
|
| 59 | + class FindResults { |
| 60 | + constructor() { } |
| 61 | + |
| 62 | + init() { this.value = db.students.find(); } |
| 63 | + } |
| 64 | +
|
| 65 | + To use this class, first construct a class instance then call the |
| 66 | + ``.init()`` method. |
| 67 | + |
| 68 | +Generator Functions |
| 69 | +~~~~~~~~~~~~~~~~~~~ |
| 70 | + |
| 71 | +The following generator functions do not work: |
| 72 | + |
| 73 | +.. code-block:: javascript |
| 74 | + :copyable: false |
| 75 | +
|
| 76 | + // This code will fail |
| 77 | + function* FindResults() { |
| 78 | + yield db.students.findMany(); |
| 79 | + } |
| 80 | +
|
| 81 | + // This code will fail |
| 82 | + function listEntries() { return db.students.findMany(); } |
| 83 | + function* findResults() { |
| 84 | + yield listEntries(); |
| 85 | + } |
| 86 | +
|
| 87 | +Use an ``async generator function`` instead: |
| 88 | + |
| 89 | +.. code-block:: javascript |
| 90 | +
|
| 91 | + function listEntries() { return db.students.findMany(); } |
| 92 | + async function* findResults() { |
| 93 | + yield listEntries(); |
| 94 | + } |
| 95 | +
|
| 96 | +Array Sort |
| 97 | +~~~~~~~~~~ |
| 98 | + |
| 99 | +The following array sort does not work: |
| 100 | + |
| 101 | +.. code-block:: javascript |
| 102 | + :copyable: false |
| 103 | +
|
| 104 | + // This code will fail |
| 105 | + db.getCollectionNames().sort( ( collectionOne, collectionTwo ) => { |
| 106 | + return db[ collectionOne ].estimatedDocumentCount() - db[ collectionOne ].estimatedDocumentCount() ) |
| 107 | + } ); |
| 108 | +
|
| 109 | +Use ``.map()`` instead. |
| 110 | + |
| 111 | +.. code-block:: javascript |
| 112 | +
|
| 113 | + db.getCollectionNames().map( collectionName => { |
| 114 | + return { collectionName, size: db[ collectionName ].estimatedDocumentCount() }; |
| 115 | + } ).sort( ( collectionOne, collectionTwo ) => { |
| 116 | + return collectionOne.size - collectionTwo.size; |
| 117 | + } ).map( collection => collection.collectionName); |
| 118 | +
|
| 119 | +This approach to array sort is often more performant than the |
| 120 | +equivalent unsupported code. |
| 121 | + |
0 commit comments