modelSet.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const { isObject } = require('../utils/objectUtils');
  2. const { splitQueryProps } = require('./modelQueryProps');
  3. const { parseRelationsIntoModelInstances } = require('./modelParseRelations');
  4. function setJson(model, json, options) {
  5. json = json || {};
  6. options = options || {};
  7. if (Object.prototype.toString.call(json) !== '[object Object]') {
  8. throw new Error(
  9. 'You should only pass objects to $setJson method. ' +
  10. '$setJson method was given an invalid value ' +
  11. json
  12. );
  13. }
  14. if (!json.$isObjectionModel) {
  15. // json can contain "query properties" like `raw` instances, query builders etc.
  16. // We take them out of `json` and store them to a hidden property $$queryProps
  17. // in the model instance for later use.
  18. json = splitQueryProps(model, json);
  19. }
  20. json = model.$parseJson(json, options);
  21. json = model.$validate(json, options);
  22. model.$set(json);
  23. if (!options.skipParseRelations) {
  24. parseRelationsIntoModelInstances(model, json, options);
  25. }
  26. return model;
  27. }
  28. function setDatabaseJson(model, json) {
  29. json = model.$parseDatabaseJson(json);
  30. if (json) {
  31. const keys = Object.keys(json);
  32. for (let i = 0, l = keys.length; i < l; ++i) {
  33. const key = keys[i];
  34. model[key] = json[key];
  35. }
  36. }
  37. return model;
  38. }
  39. function setFast(model, obj) {
  40. if (obj) {
  41. // Don't try to set read-only virtual properties. They can easily get here
  42. // through `fromJson` when parsing an object that was previously serialized
  43. // from a model instance.
  44. const readOnlyVirtuals = model.constructor.getReadOnlyVirtualAttributes();
  45. const keys = Object.keys(obj);
  46. for (let i = 0, l = keys.length; i < l; ++i) {
  47. const key = keys[i];
  48. const value = obj[key];
  49. if (
  50. key.charAt(0) !== '$' &&
  51. typeof value !== 'function' &&
  52. (readOnlyVirtuals === null || readOnlyVirtuals.indexOf(key) === -1)
  53. ) {
  54. model[key] = value;
  55. }
  56. }
  57. }
  58. return model;
  59. }
  60. function setRelated(model, relation, models) {
  61. relation = ensureRelation(model, relation);
  62. if (relation.isOneToOne()) {
  63. if (Array.isArray(models)) {
  64. if (models.length === 0) {
  65. model[relation.name] = null;
  66. } else {
  67. model[relation.name] = models[0] || null;
  68. }
  69. } else {
  70. model[relation.name] = models || null;
  71. }
  72. } else {
  73. if (!models) {
  74. model[relation.name] = [];
  75. } else if (Array.isArray(models)) {
  76. model[relation.name] = models;
  77. } else {
  78. model[relation.name] = [models];
  79. }
  80. }
  81. return this;
  82. }
  83. function appendRelated(model, relation, models) {
  84. relation = ensureRelation(model, relation);
  85. if (!model[relation.name] || relation.isOneToOne()) {
  86. return model.$setRelated(relation, models);
  87. } else {
  88. if (Array.isArray(models)) {
  89. models.forEach(it => model[relation.name].push(it));
  90. } else if (models) {
  91. model[relation.name].push(models);
  92. }
  93. }
  94. return this;
  95. }
  96. function ensureRelation(model, relation) {
  97. if (typeof relation === 'string') {
  98. return model.constructor.getRelation(relation);
  99. } else {
  100. return relation;
  101. }
  102. }
  103. module.exports = {
  104. setFast,
  105. setJson,
  106. setDatabaseJson,
  107. setRelated,
  108. appendRelated
  109. };