Every sample I’ve found online that uses bulk
to insert data looks something like this, which is the working code I currently have:
const table = new sql.Table(`${this.prefix}.Sections`);
table.create = false;
table.columns.add('event_id', sql.VarChar(50), { nullable: false });
table.columns.add('category_id', sql.Int);
table.columns.add('category_name', sql.VarChar(1000));
table.columns.add('category_type', sql.VarChar(1000));
table.columns.add('section_id', sql.Int);
table.columns.add('section_name', sql.VarChar(1000));
for (const s of sections) {
table.rows.add(s.event_id, s.category_id, s.category_name, s.category_type, s.section_id);
}
await tr.request().bulk(table);
But this requires me to add the columns one by one and if I ever add or update columns on the table I also need to do the corresponding changes on my node code.
So I wonder if there is a way to automatically add these columns given an existing table, which would be the target table too. I thought that’s what table.create = false
would do, but if I don’t add the columns I get a syntax error on the bulk
line. Is there any function in the mssql package that takes an existing table and returns a sql.Table
already populated with columns before I spend time trying to write such function?