Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. var ezdata = require('./index.js');
  2.  
  3. //our test table object
  4. var testdb;
  5. var SQLopts = {
  6. host: '',
  7. user: '',
  8. password: '',
  9. database: '',
  10. connectionLimit : 10
  11. }
  12.  
  13. ezdata.ez_Init(SQLopts, function() {
  14. console.log("SQL Connected");
  15. console.log("Creating table...");
  16.  
  17. testdb = ezdata.ez_CreateTable('test', {
  18. //columns definition
  19. val1: {default: 'Hello world', type:'varchar(64)'},
  20. val2: {default: 'Nice to see you', type:'varchar(64)'},
  21. valInt: {default: 2, type:'int(11)'},
  22. valObj: {
  23. default: {
  24. subitem1: 'object branch 1',
  25. subitem2: 'obj branch 2'
  26. },
  27. type: 'object(128)'
  28. }
  29. }, {}, () => {
  30.  
  31. console.log('\nPost creation state:');
  32. console.log(testdb)
  33.  
  34. // Async row add example, function returns after added to memory, callback called after SQL query completes
  35. console.log('\nStarting Async AddRow...')
  36. testdb.ez_AddRow(() => {
  37. console.log('Async AddRow complete!')
  38. if (testdb.MyKey.val1 === 'Hello world'){
  39. console.log('Updating specified key, val1');
  40. testdb.MyKey.val1 = "Im special";
  41. testdb.MyKey.ez_Save();
  42. }
  43. return
  44. }, "MyKey")//Custom, specific key lookup
  45.  
  46. console.log('\nStarting load all...');
  47. testdb.ez_LoadAllRows(function(){
  48. //after load completes
  49. console.log('LoadAllRows complete...')
  50.  
  51. console.log('\nAdding row...');
  52. var key = testdb.ez_AddRow(() => {
  53. console.log("Added row " + key + " to SQL...\n");
  54.  
  55. console.log("testdb[key].val2 equals " + testdb[key].val2)
  56. testdb[key].val2 = 'A whole new value';
  57. console.log('Changed val2 to ' + testdb[key].val2);
  58.  
  59. console.log("Saving...");
  60. testdb[key].ez_Save((err, result) => {
  61. console.log('Save completed!')
  62.  
  63. console.log('\nUnloading key from memory...');
  64. testdb.ez_UnloadKey(key);
  65. console.log('\nCurrent memory key values:');
  66. console.log(testdb[key]);
  67. console.log('\nLoading key from SQL...');
  68. testdb.ez_LoadKey(key, () => {
  69. //key in memory guarenteed to match SQL values here.
  70. console.log('\nFinal dump:')
  71. console.log(testdb)
  72. process.exit(0);
  73. });
  74. return
  75. });
  76. return
  77. });
  78. console.log('Added row ' + key + ' to memory...')
  79. return;
  80.  
  81. });
  82. console.log('LoadAll SQL statement awayyyy...');
  83. return;
  84. });
  85.  
  86. });
  87.  
  88. -----------------------------------------------------------------
  89. Console output after a few preliminary runs to fill the database:
  90. -----------------------------------------------------------------
  91.  
  92. SQL Connected
  93. Creating table...
  94.  
  95. Post creation state:
  96. { ez_Get: [Function: ez_Get],
  97. ez_Iterate: [Function: ez_Iterate],
  98. ez_AddRow: [Function: ez_AddRow],
  99. ez_UnloadKey: [Function: ez_UnloadKey],
  100. ez_LoadKey: [Function: ez_LoadKey],
  101. ez_LoadAllRows: [Function: ez_LoadAllRows],
  102. ez_TableName: 'test',
  103. ez_Columns:
  104. { val1: { default: 'Hello world', type: 'varchar(64)' },
  105. val2: { default: 'Nice to see you', type: 'varchar(64)' },
  106. valInt: { default: 2, type: 'int(11)' },
  107. valObj: { default: [Object], type: 'object(128)' } },
  108. MyKey: {},
  109. HyUFxDmHG: {},
  110. rkcN8vmrM: {},
  111. r1T8vDmBf: {} }
  112.  
  113. Starting Async AddRow...
  114. WARNING --- tried to create row with existing key. Loading from SQL instead
  115.  
  116. Starting load all...
  117. LoadAll SQL statement awayyyy...
  118. Async AddRow complete!
  119. LoadAllRows complete...
  120.  
  121. Adding row...
  122. Added row r1PunD7SG to memory...
  123. Added row r1PunD7SG to SQL...
  124.  
  125. testdb[key].val2 equals Nice to see you
  126. Changed val2 to A whole new value
  127. Saving...
  128. Save completed!
  129.  
  130. Unloading key from memory...
  131.  
  132. Current memory key values:
  133. { ez_TableName: 'test' }
  134.  
  135. Loading key from SQL...
  136.  
  137. Final dump:
  138. { ez_Get: [Function: ez_Get],
  139. ez_Iterate: [Function: ez_Iterate],
  140. ez_AddRow: [Function: ez_AddRow],
  141. ez_UnloadKey: [Function: ez_UnloadKey],
  142. ez_LoadKey: [Function: ez_LoadKey],
  143. ez_LoadAllRows: [Function: ez_LoadAllRows],
  144. ez_TableName: 'test',
  145. ez_Columns:
  146. { val1: { default: 'Hello world', type: 'varchar(64)' },
  147. val2: { default: 'Nice to see you', type: 'varchar(64)' },
  148. valInt: { default: 2, type: 'int(11)' },
  149. valObj: { default: [Object], type: 'object(128)' } },
  150. MyKey:
  151. { ez_Save: [Function: ez_Save],
  152. ez_RowID: 'MyKey',
  153. ez_TableName: 'test',
  154. val1: 'Im special',
  155. val2: 'Nice to see you',
  156. valInt: 2,
  157. valObj: { subitem1: 'object branch 1', subitem2: 'obj branch 2' } },
  158. HyUFxDmHG:
  159. { ez_RowID: 'HyUFxDmHG',
  160. val1: 'Hello world',
  161. val2: 'A whole new value',
  162. valInt: 2,
  163. valObj: { subitem1: 'object branch 1', subitem2: 'obj branch 2' } },
  164. rkcN8vmrM:
  165. { ez_RowID: 'rkcN8vmrM',
  166. val1: 'Hello world',
  167. val2: 'A whole new value',
  168. valInt: 2,
  169. valObj: { subitem1: 'object branch 1', subitem2: 'obj branch 2' } },
  170. r1T8vDmBf:
  171. { ez_RowID: 'r1T8vDmBf',
  172. val1: 'Hello world',
  173. val2: 'A whole new value',
  174. valInt: 2,
  175. valObj: { subitem1: 'object branch 1', subitem2: 'obj branch 2' } },
  176. r1PunD7SG:
  177. { ez_Save: [Function: ez_Save],
  178. ez_RowID: 'r1PunD7SG',
  179. ez_TableName: 'test',
  180. val1: 'Hello world',
  181. val2: 'A whole new value',
  182. valInt: 2,
  183. valObj: { subitem1: 'object branch 1', subitem2: 'obj branch 2' } } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement