Show EchoJS: SQL Server orchestration with Node.js

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

内容简介: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


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

查看所有标签

猜你喜欢:

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

算法分析-有效的学习方法(影印版)

算法分析-有效的学习方法(影印版)

Jeffrey J.McConnell / 高等教育出版社 / 2003-03-01 / 28.0

本书主要目标是提高读者关于算法对程序效率的影响等问题的认知水平,并培养读者分析程序中的算法所必需的技巧。各章材料以激发读者有效的、协同的学习方法的形式讲述。通过全面的论述和完整的数学推导,本书帮助读者最大限度地理解基本概念。 本书内容包括促使学生参与其中的大量程序设计课题。书中所有算法以伪码形式给出,使得具备条件表达式、循环与递归方面知识的读者均易于理解。本书以简洁的写作风格向读者介绍了兼具......一起来看看 《算法分析-有效的学习方法(影印版)》 这本书的介绍吧!

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

在线压缩/解压 CSS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

SHA 加密
SHA 加密

SHA 加密工具