I am attempting to store some JSON data to a SQL Server table using Node.js and the "mssql" package. However, I need to dynamically generate the table and populate it.
I have the table defined based on whatever JSON data is coming in, now I need to populate it with the JSON data. My problem is I cannot figure out how to dynamically create mssql.Table rows. Doing the following
var sql = require('mssql');
var table = new sql.Table('table');
table.columns.add('one', sql.VarChar(2));
table.columns.add('two', sql.VarChar(2));
table.columns.add('three', sql.VarChar(2));
table.rows.add('1', '2', '3');
table.rows.add('4', '5', '6');
table.rows.add('7', '8', '9');
table.rows.add('10', '11', '12');
table.rows.add(row);
results in
Table {
name: 'table',
schema: null,
database: null,
path: '[table]',
temporary: false,
columns:
[ { type: [sql.VarChar],
length: 2,
name: 'one',
nullable: undefined,
primary: undefined },
{ type: [sql.VarChar],
length: 2,
name: 'two',
nullable: undefined,
primary: undefined },
{ type: [sql.VarChar],
length: 2,
name: 'three',
nullable: undefined,
primary: undefined } ],
rows:
[ [ '1', '2', '3' ],
[ '4', '5', '6' ],
[ '7', '8', '9' ],
[ '10', '11', '12' ] ] }
When adding
var row = {one: '13', two: '14', three: '15'};
table.rows.add(row);
row = ['16', '17', '18']
table.rows.add(row);
I get
Table {
name: 'table',
schema: null,
database: null,
path: '[table]',
temporary: false,
columns:
[ { type: [sql.VarChar],
length: 2,
name: 'one',
nullable: undefined,
primary: undefined },
{ type: [sql.VarChar],
length: 2,
name: 'two',
nullable: undefined,
primary: undefined },
{ type: [sql.VarChar],
length: 2,
name: 'three',
nullable: undefined,
primary: undefined } ],
rows:
[ [ '1', '2', '3' ],
[ '4', '5', '6' ],
[ '7', '8', '9' ],
[ '10', '11', '12' ],
[ [Object] ],
[ [Array] ] ] }
which clearly is not what I want.
Doing something like
var row = new sql.Table.row;
row.item.add('13')
row.item.add('14')
row.item.add('15')
table.rows.add(row);
is what I am trying to do, which would allow me to add whatever values I want to the table without hard coding them. Looking through the mssql package docs, I cannot find anything that mentions anything of the sort.
Any help would be much appreciated! Thanks!