Reference Source

js/datas/providers/sequelize_record_provider.js

  1.  
  2. // NPM IMPORTS
  3. import assert from 'assert'
  4.  
  5. // COMMON IMPORTS
  6. import T from '../../utils/types'
  7. import RecordProvider from './record_provider'
  8.  
  9. // import runtime from '../../base/runtime'
  10.  
  11.  
  12. /**
  13. * Contextual constant for this file logs.
  14. * @private
  15. */
  16. const context = 'common/datas/providers/sequelize_record_provider'
  17.  
  18.  
  19.  
  20. /**
  21. * Sequelize record provider class.
  22. * @author Luc BORIES
  23. * @license Apache-2.0
  24. */
  25. export default class SequelizeRecordProvider extends RecordProvider
  26. {
  27. /**
  28. * Create a record provider instance
  29. */
  30. constructor(arg_settings)
  31. {
  32. super(arg_settings)
  33.  
  34. assert( T.isObject(arg_settings.model), context + ':bad settings.model object')
  35. /**
  36. * Sequelize record provider model instance.
  37. * @type {object}
  38. */
  39. this.sequelize_model = arg_settings.model
  40.  
  41. /**
  42. * Sequelize record provider fields array.
  43. * @type {array}
  44. */
  45. this.fields_list = undefined
  46. if ( T.isArray(arg_settings.fields_list) )
  47. {
  48. this.fields_list = arg_settings.fields_list
  49. }
  50. }
  51. /**
  52. * Build a query to fetch datas.
  53. * @param {object|undefined} arg_query - optional query context
  54. * @returns {Promise} datas record promise
  55. */
  56. build_query(arg_query)
  57. {
  58. // logs.debug(context, 'build_query:enter')
  59.  
  60. let query = {}
  61.  
  62. // SET FIELDS LIST
  63. if ( T.isObject(arg_query) && T.isArray(arg_query.fields_list) )
  64. {
  65. query.attributes = arg_query.fields_list
  66. }
  67. else if (this.fields_list)
  68. {
  69. query.attributes = this.fields_list
  70. }
  71.  
  72. // logs.debug(context, 'build_query:leave')
  73. return query
  74. }
  75. /**
  76. * Provide all datas records
  77. * @param {object|undefined} arg_query - optional query context
  78. * @returns {Promise} datas record promise
  79. */
  80. find_all_records(arg_query)
  81. {
  82. // logs.debug(context, 'find_all_records:enter')
  83.  
  84. const query = this.build_query(arg_query)
  85. const records_promise = this.sequelize_model.findAll(query)
  86.  
  87. // logs.debug(context, 'find_all_records:leave')
  88. return records_promise
  89. }
  90. /**
  91. * Find a record by its id.
  92. * @param {string|number} arg_id - record id
  93. * @returns {Promise} - promise of found record or null
  94. */
  95. find_records_by_id(arg_id)
  96. {
  97. // logs.debug(context, 'find_record_by_id:enter')
  98. assert( T.isString(arg_id) || T.isNumber(arg_id), context + ':find_record_by_id:bad id string or number')
  99.  
  100. const records_promise = this.sequelize_model.findById()
  101.  
  102. // logs.debug(context, 'find_record_by_id:leave')
  103. return records_promise
  104. }
  105. /**
  106. * Find a record with a set of values.
  107. * @param {object} arg_values_map - values map
  108. * @param {object|undefined} arg_query - optional query context
  109. * @returns {Promise} - promise of found records or null value
  110. */
  111. find_records_by_values(arg_values_map, arg_query)
  112. {
  113. // logs.debug(context, 'find_record_by_values:enter')
  114. assert( T.isObject(arg_values_map), context + ':find_record_by_values:bad values object')
  115.  
  116. // UPDATE QUERY WITH VALUES
  117. arg_query = T.isObject(arg_query) ? arg_query : {}
  118. arg_query.where = T.isObject(arg_query.where) ? arg_query.where : {}
  119. Object.keys(arg_values_map).forEach(
  120. (value) => {
  121. arg_query.where[value] = arg_values_map[value]
  122. }
  123. )
  124. const query = this.build_query(arg_query)
  125. const records_promise = this.sequelize_model.findAll(query)
  126.  
  127. // logs.debug(context, 'find_record_by_values:leave')
  128. return records_promise
  129. }
  130. }