Show EchoJS: SQL Server orchestration with Node.js

栏目: IT技术 · 发布时间: 4年前

内容简介:This package makes it easier to executeAs the

ms-sqlcmd

This package makes it easier to execute sqlcmd scripts from node.js. It will use an mssql url string as a connection string. The path should be /INSTANCENAME/DATABASENAME or simply /DATABASENAME . The fields should be encoded via encodeURIComponent in order to facilitate special characters, especially in passphrases that might otherwise interfere or have special characters.

As the mssql and underlying tedious packages favor TCP connections, this library will default to matching constraints. You should of course make certain that your SQL Server installation is setup to listen to TCP connection requests.

Script file(s) should be encoded in UTF-8, they will be written in a temporary location for execution, and cleaned up (via UTF-16LE / Unicode in sqlcmd).

Usage

const { sqlcmd } = require('ms-sqlcmd');
...
// mssql url formatted connection string - for now, no querystring options will be parsed
// for connection/execution options, see Query String section below.
const connectionString = 'mssql://sa:password@server:port/database';

// path to the script to run, should prefer absolute paths, or relative to the current working directory.
// see Connection String below
const scripts = [
  path.join(__dirname, '../sql-scripts/somescript.sql'),
];

// Optional, script variables, for scripts using `:setvar` and `$(VarName)`
const scriptVars = {
  "DatabaseName": "foo"
};

// Optional, additional options, defaults below
const options = {
  echo: true, // echo sqlcmd output to stdout/stderr while running
};

// call sqlcmd with the parameters, if you want to pass options, without scriptVars, use null for scriptVars.
// returns a promise, you can await on it directly, or listen for specific events.
try {
  // if sqlcmd returns with a non-zero exit code, an error will be thrown
  const output = await sqlcmd(connectionString, scripts, scriptVars, options);
} catch(error) {
  // standard properties set on error object other error properties may also be set
  //   if a code of "INVALID_CONNECTION_STRING" is used, there will be an innerError property
  const { message, code, output, stdout, stderr } = error;
}

Errors

The error should have a code property. This will be a string you can match on, or the exit code from sqlcmd directly.

  • ### - the exit code from sqlcmd executable.
  • SQLCMD_NOT_FOUND - the sqlcmd executable could not be found, either in the PATH environment, or in known install locations.
    PATH
    sqlcmd
    
  • INVALID_CONNECTION_STRING - the connectionString argument is not a valid URL, or does not contain all the necessary parameters.
  • UNEXPECTED_ERROR - Any other errors will be wrapped in this, with an innerError property containing the original error.

Connection String

The main portions of the connectionString are as follows: (See Query String section below)

Protocol://Username:Passphrase@ServerName:Port/InstanceName/DatabaseName?Query

Each section should be URI Component Path encoded (encodeURIComponent).

  • Protocol: The connection protocol to use
    • mssql: will use TCP for local sqlcmd or default for docker runs.
    • mssql+tcp - TCP, default for non-docker requests
    • msssql+lpc: - shared memory
    • mssql+np: - named pipes
    • mssql+docker - will run a default connection INSIDE a named container
  • Username: Required for TCP connections, if unspecified will use a Trusted Connection option.
  • Passphrase: Required if Username is specified.
  • ServerName: The name of the server to connect to.
    • localhost for local connections (lpc, np, tcp)
      • For docker users, always use localhost
    • The dns or ip address for the server for remote connections (tcp)
    • docker for execution inside a docker sql server instance.
  • Port: (optional) the port to connect to the server on, TCP Only
    • Docker users, use the listen port, this will allow the command to attempt match a running container.
  • InstanceName: (optional)
    • Named Instance (tcp, lcp, np)
    • Container ID or Name (docker)
    • If unspecifed, will use the default instance
  • DatabaseName: The name of the database to connect to.
  • Query: See below

Docker

If the Protocol is "mssql+docker" , then a locally installed Docker server is expected with the INSTANCE specified to either the Container ID or Name. The script will be copied into the container as /tmp/sqlscript.sql . Inside the container, /opt/mssql-tools/bin/sqlcmd will be executed against the script.

For Docker runs, the ServerName and Port will be ignored as the default instance inside the container will be used. In practice, you should use either localhost or docker . The Username and Passphrase fields are also optional for Docker runs.

Query String

The following query string parameters may also be specified as additional parameters passed to sqlcmd . Boolean values may be a literal true , t , y or 1 as a boolean or string for a true value, false , f , n , or 1 for a false value, all other values will use the default/unset value.

ReadOnly

Other Details

Install SQL Server command line tools on Linux

While the Microsoft instructions should work, I noticed on at least Pop!_OS/Ubuntu/Debian in 19.10+ that the instructions fail in practice. For ubuntu, you should first isntall unixodbc first, then install mssql-tools .

Setup:

sudo apt update
sudo apt install unixodbc
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list
sudo apt update
sudo apt install mssql-tools
echo 'export PATH="$PATH:/opt/mssql-tools/bin"' >> ~/.bash_profile

Related Projects

Some knowledge for this project was learned from reading the source for @quorum/sqlcmd-runner .

License

MIT License


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法设计与分析

算法设计与分析

张德富 / 2009-8 / 36.00元

《算法设计与分析》主要取材于算法设计与分析领域的经典内容,并介绍了算法设计的发展趋势。内容主要包括非常经典的算法设计技术,例如递归与分治、动态规划、贪心、回溯、分支限界、图算法,也包括了一些高级的算法设计主题,例如网络流和匹配、启发式搜索、线性规划、数论以及计算几何。在算法分析方面,介绍了概率分析以及最新的分摊分析和实验分析方法。在算法的理论方面,介绍了问题的下界、算法的正确性证明以及NP完全理论......一起来看看 《算法设计与分析》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具