loading
open in
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
const Appbase = require("appbase-js");

// Create appbase instance to use methods
const appbaseRef = Appbase({
  url: "https://scalr.api.appbase.io",
  app: "newstreamingapp",
  credentials: "meqRf8KJC:65cc161a-22ad-40c5-aaaf-5c082d5dcfda"
});

// Array of data objects
const dataArray = [
  {
    id: "X1",
    department_name: "Books",
    department_name_analyzed: "Books",
    department_id: 1,
    name: "A Fake Book on Network Routing",
    price: 1032
  },
  {
    id: "X2",
    department_name: "Books",
    department_name_analyzed: "Books",
    department_id: 2,
    name: "A Fake Book on Computer Science",
    price: 4125
  },
  {
    id: "X3",
    department_name: "Books",
    department_name_analyzed: "Books",
    department_id: 3,
    name: "A Fake Book on Reactive Programming",
    price: 3846
  }
];

let requestBody = [];

// Creating request body
dataArray.forEach(data => {
  // Pushing the operation and _id for each request
  requestBody.push({
    index: {
      _id: data.id
    }
  });
  requestBody.push(data);
});

// Bulk request including the index method and all data objects
appbaseRef
  .bulk({
    type: "books",
    body: requestBody
  })
  .then(response => {
    console.log("Success: ", JSON.stringify(response, null, "\t"));
  })
  .catch(error => {
    console.log("Error: ", error);
  });
node v9.7.1 linux/amd64