Advertisement
Guest User

Untitled

a guest
Apr 28th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const _ = require('lodash');
  4. const objection = require('objection');
  5. const Model = objection.Model;
  6.  
  7. const knex = require('knex')({
  8. debug: false,
  9. client: 'pg',
  10. connection: 'INSERT CONNECTION STRING HERE',
  11. pool: {
  12. min: 2,
  13. max: 2
  14. }
  15. });
  16.  
  17. Model.knex(knex);
  18.  
  19. const createTables = knex.schema.createTableIfNotExists('performance_test', table => {
  20.  
  21. table.string('object_id', 4).notNullable();
  22. table.integer('frequency').notNullable();
  23. table.timestamp('time').defaultTo(knex.fn.now());
  24.  
  25. table.primary(['object_id', 'frequency', 'time']);
  26. });
  27.  
  28. const dropTables = knex.schema.dropTableIfExists('performance_test');
  29.  
  30.  
  31. const FREQ_LONG_TO_SHORT = {
  32. 'minutely': 1,
  33. 'hourly': 2,
  34. 'daily': 3
  35. };
  36.  
  37. const FREQ_SHORT_TO_LONG = _.invert(FREQ_LONG_TO_SHORT);
  38.  
  39.  
  40. class Performance extends Model {
  41.  
  42. static get tableName() {
  43. return 'performance_test';
  44. }
  45.  
  46. static get idColumn() {
  47. return ['object_id', 'time', 'frequency'];
  48. }
  49.  
  50. static get jsonSchema() {
  51. return {
  52.  
  53. type: 'object',
  54.  
  55. required: ['object_id', 'frequency', 'time'],
  56.  
  57. properties: {
  58.  
  59. object_id: {
  60. type: 'string',
  61. maxLength: 4
  62. },
  63.  
  64. frequency: {
  65. type: 'string',
  66. enum: _.keys(FREQ_LONG_TO_SHORT)
  67. },
  68.  
  69. time: {
  70. type: 'string',
  71. format: 'date-time'
  72. }
  73. }
  74. };
  75. }
  76.  
  77. // CALLED TO SERIALIZE OBJECT ON ITS WAY TO THE DATABASE
  78. $formatDatabaseJson(json) {
  79. json = super.$formatDatabaseJson(json);
  80.  
  81. // CONVERT LONG FREQ NAME TO SHORT FREQ NAME
  82. if('frequency' in json) {
  83. json.frequency = FREQ_LONG_TO_SHORT[json.frequency];
  84. }
  85.  
  86. return json;
  87. }
  88.  
  89. // CALLED WHEN OBJECT IS READ FROM THE DB FOR USE IN MODEL
  90. $parseDatabaseJson(json) {
  91.  
  92. // CONVERT SHORT FREQ NAME TO LONG FREQ NAME
  93. json.frequency = FREQ_SHORT_TO_LONG[json.frequency];
  94.  
  95. return super.$parseDatabaseJson(json);
  96. }
  97. }
  98.  
  99. // GO FOR IT
  100. dropTables
  101. .then(() => {
  102. return createTables;
  103. })
  104. .then(() => {
  105. console.log('CREATING INSTANCE');
  106.  
  107. return Performance
  108. .query()
  109. // .debug()
  110. .insert({
  111. object_id: '1234',
  112. time: new Date().toISOString(),
  113. frequency: 'daily'
  114. })
  115. .first()
  116. .returning('*');
  117. })
  118. .then(instance => {
  119. console.log('INSTANCE CREATED');
  120. console.log(instance);
  121.  
  122. console.log('DELETING INSTANCE???');
  123. return instance
  124. .$query()
  125. .debug()
  126. .delete();
  127. })
  128. .then(() => {
  129. console.log('YOU ARE THE GREATEST OF ALL TIME!');
  130.  
  131. return dropTables;
  132. })
  133. .catch(err => {
  134. console.warn(err);
  135. return dropTables
  136. })
  137. .then(() => {
  138. process.exit();
  139. })
  140. .catch(() => {
  141. process.exit();
  142. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement