@@ -11,90 +11,140 @@ Bulk imports the given _data_ into the collection.
1111
1212** Arguments**
1313
14- * ** data** : ` Array<Array<any>> | Array<Object> `
14+ - ** data** : ` Array | Buffer | string `
1515
16- The data to import. This can be an array of documents:
16+ The data to import. Depending on the _ type_ option this can be any of the
17+ following:
1718
18- ``` js
19- [
20- {key1: value1, key2: value2}, // document 1
21- {key1: value1, key2: value2}, // document 2
22- ...
23- ]
24- ```
19+ For type ` "documents" ` or ` "auto" ` :
2520
26- Or it can be an array of value arrays following an array of keys .
21+ - an array of documents, e.g .
2722
28- ``` js
29- [
30- [' key1' , ' key2' ], // key names
31- [value1, value2], // document 1
32- [value1, value2], // document 2
33- ...
34- ]
35- ```
36-
37- * ** opts** : ` Object ` (optional) If _ opts_ is set, it must be an object with any
23+ ``` json
24+ [
25+ { "_key" : " banana" , "color" : " yellow" },
26+ { "_key" : " peach" , "color" : " pink" }
27+ ]
28+ ```
29+
30+ - a string or buffer containing one JSON document per line, e.g.
31+
32+ ```
33+ {"_key" :" banana" ,"color" :" yellow" }
34+ {"_key" :" peach" ,"color" :" pink" }
35+ ```
36+
37+ For type `"array"` or `"auto"`:
38+
39+ - a string or buffer containing a JSON array of documents, e.g.
40+
41+ ```json
42+ [
43+ { "_key" : " banana" , "color" : " yellow" },
44+ { "_key" : " peach" , "color" : " pink" }
45+ ]
46+ ```
47+
48+ For type `null`:
49+
50+ - an array containing an array of keys followed by arrays of values, e.g.
51+
52+ ```
53+ [
54+ [" _key" , " color" ],
55+ [" banana" , " yellow" ],
56+ [" peach" , " pink" ]
57+ ]
58+ ```
59+
60+ - a string or buffer containing a JSON array of keys followed by
61+ one JSON array of values per line, e.g.
62+
63+ ```
64+ [" _key" , " color" ]
65+ [" banana" , " yellow" ]
66+ [" peach" , " pink" ]
67+ ```
68+
69+ - **opts**: `Object` (optional) If _opts_ is set, it must be an object with any
3870 of the following properties:
3971
40- * ** waitForSync** : ` boolean ` (Default: ` false ` )
72+ - **type**: `string | null` (Default: `"auto"`)
73+
74+ Indicates which format the data uses.
75+ Can be `"documents"`, `"array"` or `"auto"`.
76+ Use `null` to explicitly set no type.
77+
78+ - **fromPrefix**: `string` (optional)
79+
80+ Prefix to prepend to `_from` attributes.
81+
82+ - **toPrefix**: `string` (optional)
83+
84+ Prefix to prepend to `_to` attributes.
85+
86+ - **overwrite**: `boolean` (Default: `false`)
87+
88+ If set to `true`, the collection is truncated before the data is imported.
89+
90+ - **waitForSync**: `boolean` (Default: `false`)
4191
4292 Wait until the documents have been synced to disk.
4393
44- * ** details ** : ` boolean ` (Default: ` false ` )
94+ - **onDuplicate **: `string ` (Default: `"error" `)
4595
46- Whether the response should contain additional details about documents that
47- could not be imported.false\* .
96+ Controls behavior when a unique constraint is violated.
97+ Can be `"error"`, `"update"`, `"replace"` or `"ignore"`.
98+
99+ - **complete**: `boolean` (Default: `false`)
48100
49- * ** type ** : ` string ` (Default: ` "auto" ` )
101+ If set to `true`, the import will abort if any error occurs.
50102
51- Indicates which format the data uses. Can be ` "documents" ` , ` "array" ` or
52- ` "auto" ` .
103+ - **details**: `boolean` (Default: `false`)
53104
54- If _ data_ is a JavaScript array, it will be transmitted as a line-delimited JSON
55- stream. If _ opts.type_ is set to ` "array" ` , it will be transmitted as regular
56- JSON instead. If _ data_ is a string, it will be transmitted as it is without any
57- processing.
105+ Whether the response should contain additional details about documents that
106+ could not be imported.
58107
59108For more information on the _opts_ object, see
60- [ the HTTP API documentation for bulk imports] ( https://docs.arangodb.com/latest/HTTP/BulkImports/ImportingSelfContained.html ) .
109+ [the HTTP API documentation for bulk imports ](https://docs.arangodb.com/latest/HTTP/BulkImports/).
61110
62111**Examples**
63112
64113```js
65114const db = new Database();
66- const collection = db .collection (' users' );
115+ const collection = db.collection(" users" );
67116
68- // document stream
69- const result = await collection . import ( [
70- { username: ' admin ' , password: ' hunter2 ' },
71- { username: ' jcd ' , password: ' bionicman ' },
72- { username: ' jreyes ' , password: ' amigo ' },
73- {username : ' ghermann ' , password : ' zeitgeist ' }
74- ]);
75- assert . equal ( result . created , 4 );
117+ const result = await collection.import(
118+ [
119+ { username: "jcd" , password: "bionicman" },
120+ { username: "jreyes" , password: "amigo" },
121+ { username: "ghermann" , password: "zeitgeist" }
122+ ],
123+ { type: "documents" } // optional
124+ );
76125
77126// -- or --
78127
79- // array stream with header
80- const result = await collection .import ([
81- [' username' , ' password' ], // keys
82- [' admin' , ' hunter2' ], // row 1
83- [' jcd' , ' bionicman' ], // row 2
84- [' jreyes' , ' amigo' ],
85- [' ghermann' , ' zeitgeist' ]
86- ]);
87- assert .equal (result .created , 4 );
128+ const buf = fs.readFileSync("dx_users.json");
129+ // [
130+ // {"username": "jcd", "password": "bionicman"},
131+ // {"username": "jreyes", "password": "amigo"},
132+ // {"username": "ghermann", "password": "zeitgeist"}
133+ // ]
134+ const result = await collection.import(
135+ buf,
136+ { type: "array" } // optional
137+ );
138+ ```
88139
89140// -- or --
90141
91- // raw line-delimited JSON array stream with header
92- const result = await collection .import ([
93- ' ["username", "password"]' ,
94- ' ["admin", "hunter2"]' ,
95- ' ["jcd", "bionicman"]' ,
96- ' ["jreyes", "amigo"]' ,
97- ' ["ghermann", "zeitgeist"]'
98- ].join (' \r\n ' ) + ' \r\n ' );
99- assert .equal (result .created , 4 );
100- ```
142+ const result = await collection.import(
143+ [
144+ [ "username", "password"] ,
145+ [ "jcd", "bionicman"] ,
146+ [ "jreyes", "amigo"] ,
147+ [ "ghermann", "zeitgeist"]
148+ ] ,
149+ { type: null } // required
150+ );
0 commit comments