Source: index.js

  1. if (!global._babelPolyfill) {
  2. // This should be replaced with runtime transformer when this bug is fixed:
  3. // https://phabricator.babeljs.io/T2877
  4. require('babel-polyfill');
  5. }
  6. const _ = require('lodash');
  7. // Without the --harmony and --harmony_proxies flags, options strict: false and dotNotation: true will fail with exception
  8. if (typeof(Proxy) !== 'undefined') {
  9. // Workaround for https://github.com/tvcutsem/harmony-reflect/issues/66
  10. const warn = console.warn;
  11. console.warn = function (message) {
  12. if (message !== 'getOwnPropertyNames trap is deprecated. Use ownKeys instead') {
  13. warn.apply(console, arguments);
  14. }
  15. };
  16. require('harmony-reflect');
  17. }
  18. import { Schema } from './schema'
  19. import { Model } from './model'
  20. import { ModelArray } from './modelarray'
  21. import { compile } from './compile'
  22. /**
  23. * Main Plaster class. Use this class to set default options and create schemas and models.
  24. * @class
  25. *
  26. * @example
  27. * var plaster = require('plaster');
  28. *
  29. * var schema = plaster.schema({ name: String });
  30. * var Cat = plaster.model('Cat', schema);
  31. * var kitty = new Cat({ name: 'Zildjian' });
  32. *
  33. */
  34. class Plaster {
  35. /**
  36. * By default we export an instance of Plaster object.
  37. * Clients can create a new instances using the constructor.
  38. *
  39. * @example
  40. * var plaster = require('plaster');
  41. * var p2 = new plaster.Plaster();
  42. *
  43. * @param {Object} options default schema options
  44. * @param {Boolean} options.strict - By default (<code>true</code>), allow only values in the schema to be set.
  45. * When this is <code>false</code>, setting new fields will dynamically add the field
  46. * to the schema as type "any".
  47. * @param {Boolean} options.dotNotation - Allow fields to be set via dotNotation. Default: <code>true</code>.
  48. * <code>obj['user.name'] = 'Joe'; -> obj: { user: 'Joe' }</code>
  49. *
  50. */
  51. constructor(options = {}) {
  52. this.models = {};
  53. this.options = options;
  54. /**
  55. * Expose the Schema class
  56. * @member {Schema}
  57. *
  58. * @example
  59. * var schema = new plaster.Schema({ name: String});
  60. */
  61. this.Schema = Schema;
  62. /**
  63. * Expose the Model class
  64. * @member {Model}
  65. *
  66. * @example
  67. * var schema = plaster.schema({ name: String });
  68. * var Cat = plaster.model('Cat', schema);
  69. * var kitty = new Cat({ name: 'Zildjian' });
  70. * console.log(kitty insanceof plaster.Model); // true
  71. */
  72. this.Model = Model;
  73. /**
  74. * Expose the ModelArray class
  75. * @member {ModelArray}
  76. */
  77. this.ModelArray = ModelArray;
  78. /**
  79. * Expose Plaster constructor so clients can create new instances.
  80. * @member {Plaster}
  81. */
  82. this.Plaster = Plaster;
  83. }
  84. /**
  85. * Creates a schema. Prefer to use this over Schema constructor as this will pass along Plaster config settings.
  86. *
  87. * @api public
  88. * @param {Object} descriptor the schema descriptor
  89. * @param {Object} options Schema options
  90. * @return {Object} created Schema instance
  91. */
  92. schema(descriptor, options) {
  93. let opts = _.defaults(options || {}, this.options);
  94. return new Schema(descriptor, opts);
  95. }
  96. /**
  97. * Creates a model from a schema.
  98. *
  99. * @api public
  100. * @param {String} name name of the model.
  101. * @param {Schema} schema instance
  102. * @param {Object} options - options
  103. * @param {Boolean} options.freeze - to freeze model. See <code>Object.freeze</code>. Default: <code>true</code>
  104. */
  105. model(name, schema, options = {}) {
  106. if (!(schema instanceof Schema))
  107. schema = new Schema(schema, options || this.options);
  108. if (this.models[name])
  109. return this.models[name];
  110. const M = compile(schema, options, name);
  111. this.models[name] = M;
  112. return M;
  113. }
  114. /**
  115. * Sets Plaster config options
  116. *
  117. * @api public
  118. * @param key {String} the option key
  119. * @param value {*} option value
  120. */
  121. set(key, value) {
  122. if (arguments.length === 1) {
  123. return this.options[key];
  124. }
  125. this.options[key] = value;
  126. return this;
  127. }
  128. /**
  129. * Get option.
  130. * @type {Function|*}
  131. * @return {*} Option value
  132. */
  133. get() {
  134. this.set.call(this, arguments)
  135. }
  136. /**
  137. * Returns the model given the name.
  138. * @param name
  139. * @returns {*}
  140. */
  141. getModel(name) {
  142. return this.models[name];
  143. }
  144. /**
  145. * Returns an array of model names created on this instance of Plaster.
  146. *
  147. * @api public
  148. * @return {Array} Array of model names registered.
  149. */
  150. modelNames() {
  151. return Object.keys(this.models);
  152. }
  153. }
  154. module.exports = new Plaster();