{"id":3995,"date":"2024-07-17T14:34:36","date_gmt":"2024-07-17T07:34:36","guid":{"rendered":"https:\/\/www.sqlservertutorial.net\/?page_id=3995"},"modified":"2024-07-22T07:57:11","modified_gmt":"2024-07-22T00:57:11","slug":"nodejs-sql-server-stream","status":"publish","type":"page","link":"https:\/\/www.sqlservertutorial.net\/nodejs-sql-server\/nodejs-sql-server-stream\/","title":{"rendered":"Node.js SQL Server: Stream"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the stream to process large result sets more efficiently.<\/p>\n\n\n\n<p class=\"note\">This tutorial begins where\u00a0the\u00a0C<a href=\"https:\/\/www.sqlservertutorial.net\/nodejs-sql-server\/nodejs-sql-server-calling-stored-procedures-with-output-parameters\/\">alling a stored procedure with an OUTPUT parameter in Node.js tutorial<\/a>\u00a0left off.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='how-to-use-node-js-stream-to-handle-large-result-sets'>How to use Node.js stream to handle large result sets <a href=\"#how-to-use-node-js-stream-to-handle-large-result-sets\" class=\"anchor\" id=\"how-to-use-node-js-stream-to-handle-large-result-sets\" title=\"Anchor for How to use Node.js stream to handle large result sets\">#<\/a><\/h2>\n\n\n\n<p>A stream allows you to handle a large result set efficiently by processing it row by row, rather than loading it into memory at once.<\/p>\n\n\n\n<p>The stream is useful when you handle large result sets from SQL Server without consuming much server memory.<\/p>\n\n\n\n<p>The following describes the steps for using the stream:<\/p>\n\n\n\n<p>First, <a href=\"https:\/\/www.sqlservertutorial.net\/getting-started\/connect-to-the-sql-server\/\">connect to the SQL Server<\/a>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> pool = <span class=\"hljs-keyword\">await<\/span> sql.connect(config);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, create a <code>Request<\/code> object that represents a query executed against the SQL Server and enables streaming:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> request = <span class=\"hljs-keyword\">new<\/span> sql.Request(pool);\nrequest.stream = <span class=\"hljs-literal\">true<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, handle data when it arrives. To do that, you use the <code>on()<\/code> method of the <code>Request<\/code> object. The <code>request.on()<\/code> method handles various events that occur during the execution of a query when the stream is enabled:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>recordset<\/code>: Triggered when the columns metadata is available.<\/li>\n\n\n\n<li><code>row<\/code>: Triggered for each row in the result set.<\/li>\n\n\n\n<li><code>error<\/code>: Triggered when an error occurs.<\/li>\n\n\n\n<li><code>done<\/code>: Triggered when the query execution is complete.<\/li>\n<\/ul>\n\n\n\n<p>For example, the following shows how to process each row of data as they are arrived using the <code>request.on()<\/code> with the <code>row<\/code> event:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">request.on(<span class=\"hljs-string\">'row'<\/span>, row =&gt; {\n  <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'Row:'<\/span>, row);\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='streaming-data-into-a-csv-file-example'>Streaming data into a CSV file example <a href=\"#streaming-data-into-a-csv-file-example\" class=\"anchor\" id=\"streaming-data-into-a-csv-file-example\" title=\"Anchor for Streaming data into a CSV file example\">#<\/a><\/h2>\n\n\n\n<p>We&#8217;ll show you how to query data from the <code>Authors<\/code> table and stream it directly to a file to avoid loading everything into memory:<\/p>\n\n\n\n<p><strong>Step 1.<\/strong> Create a new file named <code>stream.js<\/code> within the project directory.<\/p>\n\n\n\n<p><strong>Step 2.<\/strong> Define a function <code>exportAuthorsToCSV<\/code> that exports all rows from the <code>Authors<\/code> table to a CSV file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> fs <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'fs'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> sql <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'mssql'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { config } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/config.js'<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> exportAuthorsToCSV = <span class=\"hljs-keyword\">async<\/span> (csvFilename) =&gt; {\n  <span class=\"hljs-keyword\">try<\/span> {\n    <span class=\"hljs-comment\">\/\/ Create a write stream for the CSV file<\/span>\n    <span class=\"hljs-keyword\">const<\/span> writeStream = fs.createWriteStream(csvFilename);\n\n    <span class=\"hljs-comment\">\/\/ Connect to the database<\/span>\n    <span class=\"hljs-keyword\">const<\/span> pool = <span class=\"hljs-keyword\">await<\/span> sql.connect(config);\n\n    <span class=\"hljs-comment\">\/\/ Create a new request with streaming enabled<\/span>\n    <span class=\"hljs-keyword\">const<\/span> request = <span class=\"hljs-keyword\">new<\/span> sql.Request(pool);\n    request.stream = <span class=\"hljs-literal\">true<\/span>;\n\n    <span class=\"hljs-comment\">\/\/ Execute the query<\/span>\n    request.query(<span class=\"hljs-string\">'SELECT * FROM Authors ORDER BY FirstName'<\/span>);\n\n    <span class=\"hljs-comment\">\/\/ Event handlers for the streaming<\/span>\n    request.on(<span class=\"hljs-string\">'recordset'<\/span>, (columns) =&gt; {\n      <span class=\"hljs-keyword\">let<\/span> header = <span class=\"hljs-string\">''<\/span>;\n      <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">const<\/span> column <span class=\"hljs-keyword\">in<\/span> columns) {\n        header += <span class=\"hljs-string\">`<span class=\"hljs-subst\">${column}<\/span>,`<\/span>;\n      }\n      <span class=\"hljs-comment\">\/\/ remove the last comma (,)<\/span>\n      header = header.slice(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">-1<\/span>);\n\n      <span class=\"hljs-comment\">\/\/ write the header<\/span>\n      writeStream.write(header + <span class=\"hljs-string\">'\\n'<\/span>);\n    });\n\n    <span class=\"hljs-comment\">\/\/ Process each row when it is arrived<\/span>\n    request.on(<span class=\"hljs-string\">'row'<\/span>, ({ AuthorID, FirstName, LastName, BirthDate }) =&gt; {\n      <span class=\"hljs-keyword\">const<\/span> csvRow = <span class=\"hljs-string\">`<span class=\"hljs-subst\">${AuthorID}<\/span>,<span class=\"hljs-subst\">${FirstName}<\/span>,<span class=\"hljs-subst\">${LastName}<\/span>,<span class=\"hljs-subst\">${BirthDate.toLocaleDateString()}<\/span>`<\/span>;\n      writeStream.write(csvRow + <span class=\"hljs-string\">'\\n'<\/span>);\n    });\n\n    <span class=\"hljs-comment\">\/\/ Error handlers for the request<\/span>\n    request.on(<span class=\"hljs-string\">'error'<\/span>, (err) =&gt; {\n      <span class=\"hljs-built_in\">console<\/span>.error(<span class=\"hljs-string\">'Error:'<\/span>, err);\n      writeStream.end();\n    });\n\n    <span class=\"hljs-comment\">\/\/ Event handler for when the query is done<\/span>\n    request.on(<span class=\"hljs-string\">'done'<\/span>, (result) =&gt; {\n      <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'Query done. Rows affected:'<\/span>, result.rowsAffected);\n      writeStream.end();\n    });\n  } <span class=\"hljs-keyword\">catch<\/span> (err) {\n    <span class=\"hljs-built_in\">console<\/span>.error(err);\n  }\n};\n\n<span class=\"hljs-keyword\">export<\/span> { exportAuthorsToCSV };<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, import <code>fs<\/code>, <code>sql<\/code>, and <code>config<\/code> objects from the <code>fs<\/code>, <code>mssql<\/code>, and <code>config.js<\/code> modules respectively.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> fs <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'fs'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> sql <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'mssql'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { config } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/config.js'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, define a function <code>exportAuthorsToCSV<\/code> that accepts a CSV filename:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> exportAuthorsToCSV = <span class=\"hljs-keyword\">async<\/span> (csvFilename) =&gt; {\n    <span class=\"hljs-comment\">\/\/...<\/span>\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, create a write stream for the CSV file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> writeStream = fs.createWriteStream(csvFilename);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Fourth, connect to the SQL Server using the provided <code>config<\/code> object. <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> pool = <span class=\"hljs-keyword\">await<\/span> sql.connect(config);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that the <code>config<\/code> object contains the necessary parameters for connecting to the SQL Server, including username, password, server, and database name:<\/p>\n\n\n\n<p>Fifth, create a new <code>Request<\/code> with the streaming enabled:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">const<\/span> request = <span class=\"hljs-keyword\">new<\/span> sql.Request(pool);\nrequest.stream = <span class=\"hljs-literal\">true<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Sixth, execute a query that retrieves data from the <code>Authors<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">request.query(<span class=\"hljs-string\">'SELECT * FROM Authors ORDER BY FirstName'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Seventh, write the header of the CSV file once in the event handler of the <code>recordset<\/code> event handler:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">request.on(<span class=\"hljs-string\">'recordset'<\/span>, (columns) =&gt; {\n  <span class=\"hljs-keyword\">let<\/span> header = <span class=\"hljs-string\">''<\/span>;\n  <span class=\"hljs-keyword\">for<\/span> (<span class=\"hljs-keyword\">const<\/span> column <span class=\"hljs-keyword\">in<\/span> columns) {\n    header += <span class=\"hljs-string\">`<span class=\"hljs-subst\">${column}<\/span>,`<\/span>;\n  }\n  <span class=\"hljs-comment\">\/\/ remove the last comma (,)<\/span>\n  header = header.slice(<span class=\"hljs-number\">0<\/span>, <span class=\"hljs-number\">-1<\/span>);\n\n  <span class=\"hljs-comment\">\/\/ write the header<\/span>\n  writeStream.write(header + <span class=\"hljs-string\">'\\n'<\/span>);\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Initialize the header to an empty string.<\/li>\n\n\n\n<li>Iterate over the properties of the columns object using the <code><a href=\"https:\/\/www.javascripttutorial.net\/javascript-for-in\/\">for...in<\/a><\/code> loop and concatenate property names (<code>AuthorID<\/code>, <code>FirstName<\/code>, <code>LastName<\/code>, and <code>BirthDate<\/code>) into a single string. Note that the columns object contains all the fields in the Authors table including <code>AuthorID<\/code>, <code>FirstName<\/code>, <code>LastName<\/code>, and <code>BirthDate<\/code>.<\/li>\n\n\n\n<li>Remove the last comma (<code>,<\/code>) from the <code>header<\/code> string using the <code><a href=\"https:\/\/www.javascripttutorial.net\/javascript-string-slice\/\">slice()<\/a><\/code> method.<\/li>\n\n\n\n<li>Write the header into the CSV file by calling the <code>write()<\/code> method of the <code>writeStream<\/code> object.<\/li>\n<\/ol>\n\n\n\n<p>Eighth, write each row into the CSV file when it arrives in the row event handler:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">request.on(<span class=\"hljs-string\">'row'<\/span>, ({ AuthorID, FirstName, LastName, BirthDate }) =&gt; {\n  <span class=\"hljs-keyword\">const<\/span> csvRow = <span class=\"hljs-string\">`<span class=\"hljs-subst\">${AuthorID}<\/span>,<span class=\"hljs-subst\">${FirstName}<\/span>,<span class=\"hljs-subst\">${LastName}<\/span>,<span class=\"hljs-subst\">${BirthDate.toLocaleDateString()}<\/span>`<\/span>;\n  writeStream.write(csvRow + <span class=\"hljs-string\">'\\n'<\/span>);\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Ninth, log the error to the console if an error occurs and end the stream in the <code>error<\/code> event handler:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">request.on(<span class=\"hljs-string\">'error'<\/span>, (err) =&gt; {\n  <span class=\"hljs-built_in\">console<\/span>.error(<span class=\"hljs-string\">'Error:'<\/span>, err);\n  writeStream.end();\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Tenth, log a message to the console and end the stream in the <code>done<\/code> event handler:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">request.on(<span class=\"hljs-string\">'done'<\/span>, (result) =&gt; {\n  <span class=\"hljs-built_in\">console<\/span>.log(<span class=\"hljs-string\">'Query done. Rows affected:'<\/span>, result.rowsAffected);\n  writeStream.end();\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Eleventh, log the error message to the console in the <code>catch<\/code> block:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-built_in\">console<\/span>.error(err);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, export the <code>exportAuthorsToCSV<\/code> function.<\/p>\n\n\n\n<p>Step 3. Modify the <code>index.js<\/code> to use the <code>exportAuthorsToCSV<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> { exportAuthorsToCSV } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/stream.js'<\/span>;\n\nexportAuthorsToCSV(<span class=\"hljs-string\">'data\/output.csv'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, import <code>exportAuthorsToCSV<\/code> function from the <code>stream.js<\/code> module.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\"><span class=\"hljs-keyword\">import<\/span> { exportAuthorsToCSV } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/stream.js'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Second, call the <code>exportAuthorsToCSV()<\/code> function to export author data to the CSV file located at data\/<code>output.csv<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">exportAuthorsToCSV(<span class=\"hljs-string\">'data\/output.csv'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Step 4. Open your terminal and execute the following command to run the <code>index.js<\/code> file:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">npm start<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If the program runs successfully, you&#8217;ll see a new output file <code>output.csv<\/code> in the <code>data<\/code> directory:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">AuthorID,FirstName,LastName,BirthDate\n2,Agatha,Christie,9\/15\/1890\n9,Charles,Dickens,2\/7\/1812\n22,Emily,Bronte,7\/30\/1818\n...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use the stream to handle large result sets efficiently.<\/li>\n\n\n\n<li>Set the <code>stream<\/code> property of the <code>Request<\/code> object to true to enable streaming.<\/li>\n\n\n\n<li>Use the <code>request.on()<\/code> method to handle events that occur during the execution of a query once streaming is enabled.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"3995\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/nodejs-sql-server\/nodejs-sql-server-stream\/\"\n\t\t\t\tdata-post-title=\"Node.js SQL Server: Stream\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"3995\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/nodejs-sql-server\/nodejs-sql-server-stream\/\"\n\t\t\t\tdata-post-title=\"Node.js SQL Server: Stream\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you will learn how to use the stream to process large result sets more efficiently. This tutorial begins where\u00a0the\u00a0Calling a stored procedure with an OUTPUT parameter in Node.js tutorial\u00a0left off. How to use Node.js stream to handle large result sets # A stream allows you to handle a large result set [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3891,"menu_order":11,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3995","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Node.JS SQL Server: Streaming Data<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to use the stream to process large result sets more efficiently.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlservertutorial.net\/nodejs-sql-server\/nodejs-sql-server-stream\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Node.JS SQL Server: Streaming Data\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to use the stream to process large result sets more efficiently.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlservertutorial.net\/nodejs-sql-server\/nodejs-sql-server-stream\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Server Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-22T00:57:11+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/nodejs-sql-server\\\/nodejs-sql-server-stream\\\/\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/nodejs-sql-server\\\/nodejs-sql-server-stream\\\/\",\"name\":\"Node.JS SQL Server: Streaming Data\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\"},\"datePublished\":\"2024-07-17T07:34:36+00:00\",\"dateModified\":\"2024-07-22T00:57:11+00:00\",\"description\":\"In this tutorial, you will learn how to use the stream to process large result sets more efficiently.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/nodejs-sql-server\\\/nodejs-sql-server-stream\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlservertutorial.net\\\/nodejs-sql-server\\\/nodejs-sql-server-stream\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/nodejs-sql-server\\\/nodejs-sql-server-stream\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Node.js SQL Server\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/nodejs-sql-server\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Node.js SQL Server: Stream\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\",\"name\":\"SQL Server Tutorial\",\"description\":\"The Practical SQL Server Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Node.JS SQL Server: Streaming Data","description":"In this tutorial, you will learn how to use the stream to process large result sets more efficiently.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlservertutorial.net\/nodejs-sql-server\/nodejs-sql-server-stream\/","og_locale":"en_US","og_type":"article","og_title":"Node.JS SQL Server: Streaming Data","og_description":"In this tutorial, you will learn how to use the stream to process large result sets more efficiently.","og_url":"https:\/\/www.sqlservertutorial.net\/nodejs-sql-server\/nodejs-sql-server-stream\/","og_site_name":"SQL Server Tutorial","article_modified_time":"2024-07-22T00:57:11+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlservertutorial.net\/nodejs-sql-server\/nodejs-sql-server-stream\/","url":"https:\/\/www.sqlservertutorial.net\/nodejs-sql-server\/nodejs-sql-server-stream\/","name":"Node.JS SQL Server: Streaming Data","isPartOf":{"@id":"https:\/\/www.sqlservertutorial.net\/#website"},"datePublished":"2024-07-17T07:34:36+00:00","dateModified":"2024-07-22T00:57:11+00:00","description":"In this tutorial, you will learn how to use the stream to process large result sets more efficiently.","breadcrumb":{"@id":"https:\/\/www.sqlservertutorial.net\/nodejs-sql-server\/nodejs-sql-server-stream\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlservertutorial.net\/nodejs-sql-server\/nodejs-sql-server-stream\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlservertutorial.net\/nodejs-sql-server\/nodejs-sql-server-stream\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlservertutorial.net\/"},{"@type":"ListItem","position":2,"name":"Node.js SQL Server","item":"https:\/\/www.sqlservertutorial.net\/nodejs-sql-server\/"},{"@type":"ListItem","position":3,"name":"Node.js SQL Server: Stream"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlservertutorial.net\/#website","url":"https:\/\/www.sqlservertutorial.net\/","name":"SQL Server Tutorial","description":"The Practical SQL Server Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlservertutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/3995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/comments?post=3995"}],"version-history":[{"count":5,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/3995\/revisions"}],"predecessor-version":[{"id":4047,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/3995\/revisions\/4047"}],"up":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/3891"}],"wp:attachment":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/media?parent=3995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}