Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

castBulkWrite.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use strict';
  2. const applyTimestampsToChildren = require('../update/applyTimestampsToChildren');
  3. const applyTimestampsToUpdate = require('../update/applyTimestampsToUpdate');
  4. const cast = require('../../cast');
  5. const castUpdate = require('../query/castUpdate');
  6. const setDefaultsOnInsert = require('../setDefaultsOnInsert');
  7. /*!
  8. * Given a model and a bulkWrite op, return a thunk that handles casting and
  9. * validating the individual op.
  10. */
  11. module.exports = function castBulkWrite(model, op, options) {
  12. const now = model.base.now();
  13. if (op['insertOne']) {
  14. return (callback) => {
  15. const doc = new model(op['insertOne']['document']);
  16. if (model.schema.options.timestamps != null) {
  17. doc.initializeTimestamps();
  18. }
  19. if (options.session != null) {
  20. doc.$session(options.session);
  21. }
  22. op['insertOne']['document'] = doc;
  23. op['insertOne']['document'].validate({ __noPromise: true }, function(error) {
  24. if (error) {
  25. return callback(error, null);
  26. }
  27. callback(null);
  28. });
  29. };
  30. } else if (op['updateOne']) {
  31. op = op['updateOne'];
  32. return (callback) => {
  33. try {
  34. op['filter'] = cast(model.schema, op['filter']);
  35. op['update'] = castUpdate(model.schema, op['update'], {
  36. strict: model.schema.options.strict,
  37. overwrite: false
  38. });
  39. if (op.setDefaultsOnInsert) {
  40. setDefaultsOnInsert(op['filter'], model.schema, op['update'], {
  41. setDefaultsOnInsert: true,
  42. upsert: op.upsert
  43. });
  44. }
  45. if (model.schema.$timestamps != null) {
  46. const createdAt = model.schema.$timestamps.createdAt;
  47. const updatedAt = model.schema.$timestamps.updatedAt;
  48. applyTimestampsToUpdate(now, createdAt, updatedAt, op['update'], {});
  49. }
  50. applyTimestampsToChildren(now, op['update'], model.schema);
  51. } catch (error) {
  52. return callback(error, null);
  53. }
  54. callback(null);
  55. };
  56. } else if (op['updateMany']) {
  57. op = op['updateMany'];
  58. return (callback) => {
  59. try {
  60. op['filter'] = cast(model.schema, op['filter']);
  61. op['update'] = castUpdate(model.schema, op['update'], {
  62. strict: model.schema.options.strict,
  63. overwrite: false
  64. });
  65. if (op.setDefaultsOnInsert) {
  66. setDefaultsOnInsert(op['filter'], model.schema, op['update'], {
  67. setDefaultsOnInsert: true,
  68. upsert: op.upsert
  69. });
  70. }
  71. if (model.schema.$timestamps != null) {
  72. const createdAt = model.schema.$timestamps.createdAt;
  73. const updatedAt = model.schema.$timestamps.updatedAt;
  74. applyTimestampsToUpdate(now, createdAt, updatedAt, op['update'], {});
  75. }
  76. applyTimestampsToChildren(now, op['update'], model.schema);
  77. } catch (error) {
  78. return callback(error, null);
  79. }
  80. callback(null);
  81. };
  82. } else if (op['replaceOne']) {
  83. return (callback) => {
  84. try {
  85. op['replaceOne']['filter'] = cast(model.schema,
  86. op['replaceOne']['filter']);
  87. } catch (error) {
  88. return callback(error, null);
  89. }
  90. // set `skipId`, otherwise we get "_id field cannot be changed"
  91. const doc = new model(op['replaceOne']['replacement'], null, true);
  92. if (model.schema.options.timestamps != null) {
  93. doc.initializeTimestamps();
  94. }
  95. if (options.session != null) {
  96. doc.$session(options.session);
  97. }
  98. op['replaceOne']['replacement'] = doc;
  99. op['replaceOne']['replacement'].validate({ __noPromise: true }, function(error) {
  100. if (error) {
  101. return callback(error, null);
  102. }
  103. callback(null);
  104. });
  105. };
  106. } else if (op['deleteOne']) {
  107. return (callback) => {
  108. try {
  109. op['deleteOne']['filter'] = cast(model.schema,
  110. op['deleteOne']['filter']);
  111. } catch (error) {
  112. return callback(error, null);
  113. }
  114. callback(null);
  115. };
  116. } else if (op['deleteMany']) {
  117. return (callback) => {
  118. try {
  119. op['deleteMany']['filter'] = cast(model.schema,
  120. op['deleteMany']['filter']);
  121. } catch (error) {
  122. return callback(error, null);
  123. }
  124. callback(null);
  125. };
  126. } else {
  127. return (callback) => {
  128. callback(new Error('Invalid op passed to `bulkWrite()`'), null);
  129. };
  130. }
  131. };