Advertisement
Guest User

Untitled

a guest
Feb 20th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. const
  2. fs = require('fs'),
  3. mkdirp = require('mkdirp'),
  4. {
  5. Kuzzle,
  6. WebSocket
  7. } = require('kuzzle-sdk');
  8.  
  9. const kuzzle = new Kuzzle(
  10. new WebSocket('localhost', {
  11. port: 7512
  12. }));
  13.  
  14. async function dump_collection(index, collection, filename, directoryName) {
  15. console.log(`Dumping collection: ${collection}`);
  16. const
  17. documents = [],
  18. searchOptions = {
  19. scroll: '1m',
  20. size: 100
  21. };
  22.  
  23. let searchResult = await kuzzle.document.search(index, collection, {}, searchOptions);
  24. while (searchResult) {
  25. documents.push(...searchResult.hits)
  26. try {
  27. searchResult = await searchResult.next();
  28. } catch (e) {
  29. searchResult = undefined;
  30. }
  31. }
  32.  
  33. const bulkDocs = [];
  34.  
  35. documents.forEach(doc => {
  36. bulkDocs.push({
  37. create: {
  38. _id: doc._id,
  39. _index: doc._index,
  40. _type: doc._type
  41. }
  42. })
  43.  
  44. delete doc._source._kuzzle_info;
  45.  
  46. bulkDocs.push(doc._source)
  47. });
  48.  
  49. mkdirp.sync(directoryName);
  50. fs.writeFileSync(`${directoryName}/${filename}`, JSON.stringify(bulkDocs, undefined, 2))
  51. }
  52.  
  53.  
  54.  
  55. async function dump_index(index, directoryName) {
  56. console.log(`Dumping index: ${index}`);
  57.  
  58. const collections = await kuzzle.collection.list(index);
  59. console.log('Collections to dump: ', collections);
  60.  
  61. await Promise.all(
  62. collections.collections.map(collection => {
  63. return dump_collection(index, collection.name, `${index}-${collection.name}-dump.json`, directoryName);
  64. })
  65. );
  66. }
  67.  
  68. async function main(index, collection, directoryName) {
  69. try {
  70. await kuzzle.connect();
  71. // await kuzzle.auth.login('local', { username: 'admin-kuzzle-team', password: 'password'});
  72.  
  73. if (collection !== undefined) {
  74. await dump_collection(index, collection, `${index}-${collection}-dump.json`, directoryName);
  75. } else {
  76. await dump_index(index, directoryName);
  77. }
  78. } catch (e) {
  79. console.log(e);
  80. }
  81.  
  82. kuzzle.disconnect();
  83. }
  84.  
  85. try {
  86. const index = process.argv[2];
  87. const collection = process.argv[3];
  88.  
  89. if (!index) {
  90. console.log('Usage: node dump-kuzzle-index.js <indexName> [collectionName]');
  91. process.exit(1);
  92. }
  93.  
  94. const directoryName = `./dump/${index}`;
  95.  
  96. main(index, collection, directoryName);
  97.  
  98. console.log(`Dump files are available in directory ${directoryName}/.`);
  99. } catch (e) {
  100. console.log(e);
  101. process.exit(1);
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement