21
21
'use strict' ;
22
22
23
23
var arrify = require ( 'arrify' ) ;
24
- var crypto = require ( 'crypto' ) ;
25
24
var duplexify = require ( 'duplexify' ) ;
26
25
var extend = require ( 'extend' ) ;
27
26
var format = require ( 'string-format-obj' ) ;
@@ -849,6 +848,21 @@ Table.prototype.import = function(source, metadata, callback) {
849
848
* @resource [Tabledata: insertAll API Documentation]{@link https://cloud.google.com/bigquery/docs/reference/v2/tabledata/insertAll}
850
849
*
851
850
* @param {object|object[] } rows - The rows to insert into the table.
851
+ * @param {object= } options - Configuration object.
852
+ * @param {boolean } options.ignoreUnknownValues - Accept rows that contain
853
+ * values that do not match the schema. The unknown values are ignored.
854
+ * Default: `false`.
855
+ * @param {boolean } options.raw - If `true`, the `rows` argument is expected to
856
+ * be formatted as according to the
857
+ * [specification](https://cloud.google.com/bigquery/docs/reference/v2/tabledata/insertAll).
858
+ * @param {boolean } options.skipInvalidRows - Insert all valid rows of a
859
+ * request, even if invalid rows exist. Default: `false`.
860
+ * @param {string } options.templateSuffix - Treat the destination table as a
861
+ * base template, and insert the rows into an instance table named
862
+ * "{destination}{templateSuffix}". BigQuery will manage creation of
863
+ * the instance table, using the schema of the base template table. See
864
+ * [Automatic table creation using template tables](https://cloud.google.com/bigquery/streaming-data-into-bigquery#template-tables)
865
+ * for considerations when working with templates tables.
852
866
* @param {function } callback - The callback function.
853
867
* @param {?error } callback.err - An error returned while making this request.
854
868
* @param {array } callback.insertErrors - A list of errors for insert failures.
@@ -879,6 +893,25 @@ Table.prototype.import = function(source, metadata, callback) {
879
893
* table.insert(rows, insertHandler);
880
894
*
881
895
* //-
896
+ * // Insert a row as according to the <a href="https://cloud.google.com/bigquery/docs/reference/v2/tabledata/insertAll">
897
+ * // specification</a>.
898
+ * //-
899
+ * var row = {
900
+ * insertId: '1',
901
+ * json: {
902
+ * INSTNM: 'Motion Picture Institute of Michigan',
903
+ * CITY: 'Troy',
904
+ * STABBR: 'MI'
905
+ * }
906
+ * };
907
+ *
908
+ * var options = {
909
+ * raw: true
910
+ * };
911
+ *
912
+ * table.insert(row, options, insertHandler);
913
+ *
914
+ * //-
882
915
* // Handling the response.
883
916
* //-
884
917
* function insertHandler(err, insertErrors, apiResponse) {
@@ -897,23 +930,30 @@ Table.prototype.import = function(source, metadata, callback) {
897
930
* // recommendations on handling errors.
898
931
* }
899
932
*/
900
- Table . prototype . insert = function ( rows , callback ) {
901
- var body = {
902
- rows : arrify ( rows ) . map ( function ( row ) {
903
- var rowObject = { } ;
904
- // Use the stringified contents of the row as a unique insert ID.
905
- var md5 = crypto . createHash ( 'md5' ) ;
906
- md5 . update ( JSON . stringify ( row ) ) ;
907
- rowObject . insertId = md5 . digest ( 'hex' ) ;
908
- rowObject . json = row ;
909
- return rowObject ;
910
- } )
911
- } ;
933
+ Table . prototype . insert = function ( rows , options , callback ) {
934
+ if ( is . fn ( options ) ) {
935
+ callback = options ;
936
+ options = { } ;
937
+ }
938
+
939
+ var json = extend ( true , options , {
940
+ rows : arrify ( rows )
941
+ } ) ;
942
+
943
+ if ( ! options . raw ) {
944
+ json . rows = arrify ( rows ) . map ( function ( row ) {
945
+ return {
946
+ json : row
947
+ } ;
948
+ } ) ;
949
+ }
950
+
951
+ delete options . raw ;
912
952
913
953
this . request ( {
914
954
method : 'POST' ,
915
955
uri : '/insertAll' ,
916
- json : body
956
+ json : json
917
957
} , function ( err , resp ) {
918
958
if ( err ) {
919
959
callback ( err , null , resp ) ;
@@ -928,7 +968,7 @@ Table.prototype.insert = function(rows, callback) {
928
968
reason : error . reason
929
969
} ;
930
970
} ) ,
931
- row : body . rows [ insertError . index ] . json
971
+ row : json . rows [ insertError . index ] . json
932
972
} ;
933
973
} ) ;
934
974
0 commit comments