model.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. "use strict";
  2. /**
  3. * @fileOverview A model is the first level if usable data-bearing entity in the system. It does NOT include any verbs for saving or anything like
  4. * that, it is a pure, in memory data container
  5. * @module documents/model
  6. * @require base
  7. * @require documents/probe
  8. * @require lodash
  9. */
  10. var Base = require( "../base" );
  11. var probe = require( "./probe" );
  12. var sys = require( "lodash" );
  13. /**
  14. * A model is the first level if usable data-bearing entity in the system. It does NOT include any verbs for saving or anything like
  15. * that, it is a pure, in memory data container
  16. * @exports documents/model
  17. * @constructor
  18. * @borrows module:documents/probe.get as get
  19. * @borrows module:documents/probe.set as set
  20. * @borrows module:documents/probe.any as any
  21. * @borrows module:documents/probe.all as all
  22. * @borrows module:documents/probe.remove as remove
  23. * @borrows module:documents/probe.seekKey as seekKey
  24. * @borrows module:documents/probe.seek as seek
  25. * @borrows module:documents/probe.findOne as findOne
  26. * @borrows module:documents/probe.findOneKey as findOneKey
  27. * @borrows module:documents/probe.findKeys as findKeys
  28. * @borrows module:documents/probe.find as find
  29. * @borrows module:documents/probe.update as update
  30. * @borrows module:documents/probe.some as some
  31. * @borrows module:documents/probe.every as every
  32. */
  33. var Model = Base.compose( [Base], /** @lends documents/model# */{
  34. constructor : function () {
  35. var that = this;
  36. probe.mixin( this );
  37. var idField = "_id";
  38. /**
  39. * The name of the field that uniquely identifies a record. When provided, some operations will take advantage of it
  40. *
  41. * @name _idField
  42. * @memberOf documents/model#
  43. * @type {string}
  44. * @private
  45. */
  46. Object.defineProperty( this, "_idField", {
  47. get : function () {
  48. return idField;
  49. },
  50. set : function ( val ) {
  51. idField = val;
  52. },
  53. configurable : false,
  54. enumerable : true,
  55. writable : true
  56. } );
  57. /**
  58. * The value of the primary key if {@link documents/model#_idField} is filled in. It will be null if none found
  59. *
  60. * @name _pkey
  61. * @memberOf documents/model#
  62. * @type {*}
  63. * @private
  64. */
  65. Object.defineProperty( this, "_pkey", {
  66. get : function () {
  67. var val;
  68. if ( !sys.isEmpty( that._idField ) ) {
  69. val = that[that._idField];
  70. }
  71. return val;
  72. },
  73. set : function ( val ) {
  74. if ( !sys.isEmpty( that._idField ) ) {
  75. that[that._idField] = val;
  76. }
  77. },
  78. configurable : false,
  79. enumerable : true,
  80. writable : true
  81. } );
  82. /**
  83. * If {@link documents/model#_idField} is filled in and it's value is empty this will be true.
  84. * @type {boolean}
  85. * @name isNew
  86. * @memberOf documents/model#
  87. */
  88. Object.defineProperty( this, "isNew", {
  89. get : function () {
  90. return !sys.isEmpty( that._idField ) && !sys.isEmpty( that[that._idField] )
  91. },
  92. configurable : false,
  93. enumerable : true,
  94. writable : false
  95. } );
  96. /**
  97. * Returns true if this instance is empty
  98. * @type {boolean}
  99. * @name isEmpty
  100. * @memberOf documents/model#
  101. */
  102. Object.defineProperty( this, "isEmpty", {
  103. get : function () {
  104. return sys.isEmpty( that );
  105. },
  106. configurable : false,
  107. enumerable : true,
  108. writable : false
  109. } );
  110. }
  111. } );
  112. module.exports = Model;