Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add table.rows.clear() method for chunking table row updates #1094

Merged
merged 3 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
v8.0.0 (2021-??-??)
-------------------
[new] Add table.rows.clear() method to allow for chunking updates ([#1094](https://github.com/tediousjs/node-mssql/pull/1094))
[change] msnodesqlv8 driver detects os platform and attempts to pick correct connections string for it ((#1318)[https://github.com/tediousjs/node-mssql/pull/1318])
[change] Updated to latest Tedious 14 ((#1312)[https://github.com/tediousjs/node-mssql/pull/1312])
[change] Errors for bad bulk load parameters have slightly different error messages ((#1318)[https://github.com/tediousjs/node-mssql/pull/1318])
Expand Down Expand Up @@ -255,7 +256,7 @@ v4.0.4 (2017-04-25)

v4.0.3 (2017-04-25)
-------------------
[fix] Fixed broken CLI & debugging
[fix] Fixed broken CLI & debugging

v4.0.2 (2017-04-19)
-------------------
Expand All @@ -276,7 +277,7 @@ v4.0.0 (2017-04-01)
[change] Removed verbose and debug mode
[change] Removed 'driver' from options
[change] Removed Transaction and Prepared Statement queues
[change] Removed 'multiple' directive
[change] Removed 'multiple' directive
[change] Connection renamed to ConnectionPool
[change] Updated to latest Tedious 2.0.0

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,20 @@ request.execute('MyCustomStoredProcedure', (err, result) => {

**TIP**: You can also create Table variable from any recordset with `recordset.toTable()`. You can optionally specify table type name in the first argument.

You can clear the table rows for easier batching by using `table.rows.clear()`

```js
const tvp = new sql.Table() // You can optionally specify table type name in the first argument.

// Columns must correspond with type we have created in database.
tvp.columns.add('a', sql.VarChar(50))
tvp.columns.add('b', sql.Int)

// Add rows
tvp.rows.add('hello tvp', 777) // Values are in same order as columns.
tvp.rows.clear()
```

## Response Schema

An object returned from a `sucessful` basic query would look like the following.
Expand Down
7 changes: 7 additions & 0 deletions lib/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ function Table (name) {
}
}
)

Object.defineProperty(this.rows, 'clear', {
value () {
return this.splice(0, this.length)
}
}
)
}

/*
Expand Down
3 changes: 3 additions & 0 deletions test/common/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ describe('Unit', () => {
assert.deepStrictEqual(t.rows[3], [12, 'XCXCDCDSCDSC'])
assert.strictEqual(t.temporary, false)

t.rows.clear()
assert.strictEqual(t.rows.length, 0)

t = new sql.Table('schm.MyTable')

assert.strictEqual(t.name, 'MyTable')
Expand Down