{"_id":"tsqlstring","_rev":"2-bf6356df1a7432be940d79b1c822ece8","name":"tsqlstring","dist-tags":{"latest":"1.0.1"},"versions":{"1.0.0":{"name":"tsqlstring","description":"Simple SQL escape and format for T-SQL","version":"1.0.0","contributors":[],"license":"MIT","keywords":["sqlstring","sql","escape","sql escape","mssql escape","tsql escape"],"repository":{"type":"git","url":"git+https://github.com/kylefarris/tsqlstring.git"},"devDependencies":{"beautify-benchmark":"0.2.4","benchmark":"2.1.4","eslint":"4.18.1","eslint-plugin-markdown":"1.0.0-beta.6","nyc":"10.3.2","urun":"0.0.8","utest":"0.0.8"},"files":["lib/","HISTORY.md","LICENSE","README.md","index.js"],"engines":{"node":">= 8.0"},"scripts":{"bench":"node benchmark/index.js","lint":"eslint --plugin markdown --ext js,md .","test":"node test/run.js","test-ci":"nyc --reporter=text npm test","test-cov":"nyc --reporter=html --reporter=text npm test"},"gitHead":"26bd51c13fe2c5bc863042e2643ba43010525209","bugs":{"url":"https://github.com/kylefarris/tsqlstring/issues"},"homepage":"https://github.com/kylefarris/tsqlstring#readme","_id":"tsqlstring@1.0.0","_npmVersion":"6.1.0","_nodeVersion":"9.3.0","_npmUser":{"name":"kylefarris","email":"kyle@chomponllc.com"},"dist":{"integrity":"sha512-+j5NC+shdkRbXWU15K2AMqWZT7JvwCYwyGlUtece1TCVquBmg0aZ99dOAswWpxcWZRKzUNppM8/iDvshog1Otg==","shasum":"6867890b4f74c931983ed7c0a980a857a36227e3","tarball":"https://registry.npmjs.org/tsqlstring/-/tsqlstring-1.0.0.tgz","fileCount":6,"unpackedSize":16048,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJbTMadCRA9TVsSAnZWagAA6gEQAJ4Ic33kOqfavo3T3zon\n875ULsR0fwEfZXKO+k6czFERzfEpe4aXzqWYB3CVYVw9zmbVt0AqRs0LV7uS\n1TAaSBvkYt1/zLNvT/mrk1LPP4A/ZYtHU5UnHMPo2TeDiQdDWVpnTZSX7Ran\nBjMrlK/q1+fzxTPR8HN+lfMWpwhEBtDcfH4B5+lBIvmTW+fesYUMplTvgbhh\nZtMlsQcCi7404PpbqZF91SDG8iGEirEM0fPVeH9BjGU55ODgbDXKI/Vgbsjo\nSlCzLN6yoj170cMYHs67qUYo2TxDJfB5qx8MfL9JwaoV0MIt+8WDBh4Aa1MY\nHmenfnIX22pQmayYJLKa87EDCsXJ/fEmniQIIVX9GUCmD+3RdTKMBoAUUT+v\nPZ0k4XwpXEMlPEzZYwJuV5d5l/3F0YkTLGdphQDZMfSIGW9XRahVNlmqyEcU\nQ7D6hIxcj0R5OwN+cPmWBkEE5VXLnlaygMkwLygFJEsShd7m0ULMGmkVeXVo\nnHbkeqs/yhV+r6XHFXDnzyHMIuW1nzrJrAKIuv2HCrUOT6+xREUckcd1iCR3\nYFKOjaF8L/UaNeiJRko3RywmKhkJAPUZRz0XAwPIVJtaL8CRNYl5VeMQB4TT\ncc+MaiQRePT5lqUCFDleoCU7nzt9j+au2QsC300GwE6tQ4hm6Quwz2L/FC9l\nWCN9\r\n=A4fq\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCICsDbZXsFABvRl+C+0u01tIab3S/OhvCAP23xJJyr8T6AiB2mMQbco6TmOo8tXgL1ls2YSk14ObCCzc0+iS6o6sbuA=="}]},"maintainers":[{"name":"kylefarris","email":"kyle@chomponllc.com"}],"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/tsqlstring_1.0.0_1531758237589_0.0734173593078431"},"_hasShrinkwrap":false},"1.0.1":{"name":"tsqlstring","description":"Simple SQL escape and format for T-SQL","version":"1.0.1","contributors":[],"license":"MIT","keywords":["sqlstring","sql","escape","sql escape","mssql escape","tsql escape"],"repository":{"type":"git","url":"git+https://github.com/kylefarris/tsqlstring.git"},"devDependencies":{"beautify-benchmark":"0.2.4","benchmark":"2.1.4","eslint":"4.18.1","eslint-plugin-markdown":"1.0.0-beta.6","nyc":"10.3.2","urun":"0.0.8","utest":"0.0.8"},"engines":{"node":">= 8.0"},"scripts":{"bench":"node benchmark/index.js","lint":"eslint --plugin markdown --ext js,md .","test":"node test/run.js","test-ci":"nyc --reporter=text npm test","test-cov":"nyc --reporter=html --reporter=text npm test"},"gitHead":"504ebbe62d44c92880c3cc0bfa79a3988fc204a2","bugs":{"url":"https://github.com/kylefarris/tsqlstring/issues"},"homepage":"https://github.com/kylefarris/tsqlstring#readme","_id":"tsqlstring@1.0.1","_nodeVersion":"14.8.0","_npmVersion":"6.14.7","dist":{"integrity":"sha512-6Nzj/SrVg1SF+egwP4OMAgEa83nLKXIE3EHn+6YKinMUeMj8bGIeLuDCkDC3Cc4OIM+xhw4CD0oXKxal8J/Y6A==","shasum":"6e2e1bfb736a4a311c295c40f45572699e5302c9","tarball":"https://registry.npmjs.org/tsqlstring/-/tsqlstring-1.0.1.tgz","fileCount":6,"unpackedSize":16026,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfbLxDCRA9TVsSAnZWagAA/YIP/jjLrtdNU/wkrdHT+oHn\noh0bfXsN24IhvkjnzNQ05H+Ctvrz2LpkWPofvS73a/rfDGSLs8rS5oKbrx3e\ntcg/7ep7Fddz4yXGYmDCjcNZuZTZlEcPzY12nuIL/rwVUuydmni7tflC07A6\nftxmODmVPiWhd8VQ5x2Ow0g5u/zgOE9By76ycqG2a5C24hP5yg8kyS5r1X+B\nCO1tKkW2WwgvK8TyMQ6kY748ZzVUMnoRj3hKlfG6QYSQfwFB8V/Rd5lN6wcL\nMvVoDtn6nWjp5K29ju48ID593Lq3XoJ6R77R6XEwsOwzcHDO+31AAwG+GKJI\nJmmOVVVXrmDSafDYvvbLOZpvHZpuxg/JfSeBX4qL+60kK1JsLnuIVPM15BoX\nuxVP5C7H3dTFw3Lbe9NBfrs2n6d3thETznS75KBmDw/V8Aa0vst0avsvX7NG\nC9SweepMeTcoi/s0hX5rg72paCI4eFiTM0QzO9z/RitxnJKu+JV393C5v4EO\nJQIL1I9U7y9U4L8sL3w8lQ69SAhPXUwJLchbWOyEtZt7pvJWcvPjItOCebQC\nL2A3rQ6M66sE6BuL0krVp5KI8kngh8pAer91f4zpoYRy9ND2HCwGmruToiZI\nI/JMrKMFKYU7u0KZD/nV5PzUJWJVbdt2fDhUZ12F+++cJ8s1r90hqxYGX9h0\nlEXk\r\n=oUdO\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCspCd4G37rW5jaa6ThP9U1XYkpt8U5mZ4LfDbZaITJGQIhANF3ZiiEmp1jc9tEHyLgUaF3A1mabv5uVxEmur4agQzP"}]},"maintainers":[{"name":"kylefarris","email":"kyle@chomponllc.com"}],"_npmUser":{"name":"kylefarris","email":"kyle@chomponllc.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/tsqlstring_1.0.1_1600961603496_0.36108664586831907"},"_hasShrinkwrap":false}},"time":{"created":"2018-07-16T16:23:57.588Z","1.0.0":"2018-07-16T16:23:57.702Z","modified":"2022-05-22T08:59:46.383Z","1.0.1":"2020-09-24T15:33:23.658Z"},"maintainers":[{"name":"kylefarris","email":"kyle@chomponllc.com"}],"description":"Simple SQL escape and format for T-SQL","homepage":"https://github.com/kylefarris/tsqlstring#readme","keywords":["sqlstring","sql","escape","sql escape","mssql escape","tsql escape"],"repository":{"type":"git","url":"git+https://github.com/kylefarris/tsqlstring.git"},"contributors":[],"bugs":{"url":"https://github.com/kylefarris/tsqlstring/issues"},"license":"MIT","readme":"# sqlstring\n\n[![NPM Version][npm-version-image]][npm-url]\n[![NPM Downloads][npm-downloads-image]][npm-url]\n[![Node.js Version][node-image]][node-url]\n[![Build Status][travis-image]][travis-url]\n\nSimple SQL escape and format for T-SQL (Example: Microsoft SQL Server). This is heavily influenced (and largely copied from) the great `sqlstring` library: https://www.npmjs.com/package/sqlstring\n\n## Install\n\n```sh\n$ npm install tsqlstring\n```\n\n## Usage\n\n<!-- eslint-disable no-unused-vars -->\n\n```js\nconst SqlString = require('tsqlstring');\n```\n\n### Escaping query values\n\n**Caution**\nIn order to avoid SQL Injection attacks, you should always escape any user\nprovided data before using it inside a SQL query. You can do so using the\n`SqlString.escape()` method:\n\n```js\nconst userId = 'some user provided value';\nconst sql    = 'SELECT * FROM users WHERE id = ' + SqlString.escape(userId);\nconsole.log(sql); // SELECT * FROM users WHERE id = 'some user provided value'\n```\n\nAlternatively, you can use `?` characters as placeholders for values you would\nlike to have escaped like this:\n\n```js\nconst userId = 1;\nconst sql    = SqlString.format('SELECT * FROM users WHERE id = ?', [userId]);\nconsole.log(sql); // SELECT * FROM users WHERE id = 1\n```\n\nMultiple placeholders are mapped to values in the same order as passed. For example,\nin the following query `foo` equals `a`, `bar` equals `b`, `baz` equals `c`, and\n`id` will be `userId`:\n\n```js\nconst userId = 1;\nconst sql    = SqlString.format('UPDATE users SET foo = ?, bar = ?, baz = ? WHERE id = ?',\n  ['a', 'b', 'c', userId]);\nconsole.log(sql); // UPDATE users SET foo = 'a', bar = 'b', baz = 'c' WHERE id = 1\n```\n\nThis looks similar to prepared statements in T-SQL, however it really just uses\nthe same `SqlString.escape()` method internally.\n\n**Caution** This also differs from prepared statements in that all `?` are\nreplaced, even those contained in comments and strings.\n\nDifferent value types are escaped differently, here is how:\n\n* Numbers are left untouched\n* Booleans are converted to `true` / `false`\n* Date objects are converted to `'YYYY-mm-dd HH:ii:ss'` strings\n* Buffers are converted to hex strings, e.g. `X'0fa5'`\n* Strings are safely escaped\n* Arrays are turned into list, e.g. `['a', 'b']` turns into `'a', 'b'`\n* Nested arrays are turned into grouped lists (for bulk inserts), e.g. `[['a',\n  'b'], ['c', 'd']]` turns into `('a', 'b'), ('c', 'd')`\n* Objects that have a `toSqlString` method will have `.toSqlString()` called\n  and the returned value is used as the raw SQL.\n* Objects are turned into `key = 'val'` pairs for each enumerable property on\n  the object. If the property's value is a function, it is skipped; if the\n  property's value is an object, toString() is called on it and the returned\n  value is used.\n* `undefined` / `null` are converted to `NULL`\n* `NaN` / `Infinity` are left as-is. T-SQL does not support these, and trying\n  to insert them as values will trigger SQL Server errors until they implement\n  support.\n\nYou may have noticed that this escaping allows you to do neat things like this:\n\n```js\nconst post  = {id: 1, title: 'Hello MySQL'};\nconst sql = SqlString.format('INSERT INTO posts SET ?', post);\nconsole.log(sql); // INSERT INTO posts SET `id` = 1, `title` = 'Hello MySQL'\n```\n\nAnd the `toSqlString` method allows you to form complex queries with functions:\n\n```js\nconst GETDATE = { toSqlString: function() { return 'GETDATE()'; } };\nconst sql = SqlString.format('UPDATE posts SET modified = ? WHERE id = ?', [GETDATE, 42]);\nconsole.log(sql); // UPDATE posts SET modified = GETDATE() WHERE id = 42\n```\n\nTo generate objects with a `toSqlString` method, the `SqlString.raw()` method can\nbe used. This creates an object that will be left un-touched when using in a `?`\nplaceholder, useful for using functions as dynamic values:\n\n**Caution** The string provided to `SqlString.raw()` will skip all escaping\nfunctions when used, so be careful when passing in un-validated input.\n\n```js\nconst GETDATE = SqlString.raw('GETDATE()');\nconst sql = SqlString.format('UPDATE posts SET modified = ? WHERE id = ?', [GETDATE, 42]);\nconsole.log(sql); // UPDATE posts SET modified = GETDATE() WHERE id = 42\n```\n\nIf you feel the need to escape queries by yourself, you can also use the escaping\nfunction directly:\n\n```js\nconst sql = 'SELECT * FROM posts WHERE title=' + SqlString.escape('Hello SQL Server');\nconsole.log(sql); // SELECT * FROM posts WHERE title='Hello SQL Server'\n```\n\n### Escaping query identifiers\n\nIf you can't trust an SQL identifier (database / table / column name) because it is\nprovided by a user, you should escape it with `SqlString.escapeId(identifier)` like this:\n\n```js\nconst sorter = 'date';\nconst sql    = 'SELECT * FROM posts ORDER BY ' + SqlString.escapeId(sorter);\nconsole.log(sql); // SELECT * FROM posts ORDER BY [date]\n```\n\nIt also supports adding qualified identifiers. It will escape both parts.\n\n```js\nconst sorter = 'date';\nconst sql    = 'SELECT * FROM posts ORDER BY ' + SqlString.escapeId('posts.' + sorter);\nconsole.log(sql); // SELECT * FROM posts ORDER BY [posts].[date]\n```\n\nIf you do not want to treat `.` as qualified identifiers, you can set the second\nargument to `true` in order to keep the string as a literal identifier:\n\n```js\nconst sorter = 'date.2';\nconst sql    = 'SELECT * FROM posts ORDER BY ' + SqlString.escapeId(sorter, true);\nconsole.log(sql); // SELECT * FROM posts ORDER BY [date.2]\n```\n\nAlternatively, you can use `??` characters as placeholders for identifiers you would\nlike to have escaped like this:\n\n```js\nconst userId = 1;\nconst columns = ['username', 'email'];\nconst sql     = SqlString.format('SELECT ?? FROM ?? WHERE id = ?', [columns, 'users', userId]);\nconsole.log(sql); // SELECT [username], [email] FROM [users] WHERE id = 1\n```\n**Please note that this last character sequence is experimental and syntax might change**\n\nWhen you pass an Object to `.escape()` or `.format()`, `.escapeId()` is used to avoid SQL injection in object keys.\n\n### Formatting queries\n\nYou can use `SqlString.format` to prepare a query with multiple insertion points,\nutilizing the proper escaping for ids and values. A simple example of this follows:\n\n```js\nconst userId  = 1;\nconst inserts = ['users', 'id', userId];\nconst sql     = SqlString.format('SELECT * FROM ?? WHERE ?? = ?', inserts);\nconsole.log(sql); // SELECT * FROM [users] WHERE [id] = 1\n```\n\nFollowing this you then have a valid, escaped query that you can then send to the database safely.\nThis is useful if you are looking to prepare the query before actually sending it to the database.\nYou also have the option (but are not required) to pass in `stringifyObject` and `timeZone`,\nallowing you provide a custom means of turning objects into strings, as well as a\nlocation-specific/timezone-aware `Date`.\n\nThis can be further combined with the `SqlString.raw()` helper to generate SQL\nthat includes T-SQL functions as dynamic vales:\n\n```js\nconst userId = 1;\nconst data   = { email: 'foobar@example.com', modified: SqlString.raw('GETDATE()') };\nconst sql    = SqlString.format('UPDATE ?? SET ? WHERE `id` = ?', ['users', data, userId]);\nconsole.log(sql); // UPDATE [users] SET [email] = 'foobar@example.com', [modified] = GETDATE() WHERE [id] = 1\n```\n\n## License\n\n[MIT](LICENSE)\n\n[npm-version-image]: https://img.shields.io/npm/v/tsqlstring.svg\n[npm-downloads-image]: https://img.shields.io/npm/dm/tsqlstring.svg\n[npm-url]: https://npmjs.org/package/tsqlstring\n[travis-image]: https://img.shields.io/travis/mysqljs/tsqlstring/master.svg\n[travis-url]: https://travis-ci.org/mysqljs/tsqlstring\n[node-image]: https://img.shields.io/node/v/tsqlstring.svg\n[node-url]: https://nodejs.org/en/download\n","readmeFilename":"README.md"}