{"name":"@planetscale/database","version":"0.6.1","keywords":["planetscale","database","mysql","vitess","serverless","vercel","lambda"],"author":{"name":"PlanetScale"},"license":"Apache-2.0","_id":"@planetscale/database@0.6.1","maintainers":[{"name":"iheanyi","email":"iekechukwu@gmail.com"},{"name":"dgraham","email":"david.malcom.graham@gmail.com"},{"name":"iamjasonlong","email":"jason@jasonlong.me"},{"name":"planetscale-npm","email":"npm@planetscale.com"}],"homepage":"https://github.com/planetscale/database-js#readme","bugs":{"url":"https://github.com/planetscale/database-js/issues"},"dist":{"shasum":"52f7ec1d337dcabeb3f56c29e8f71a764352b895","tarball":"http://registry-npm.ywwl.com/@planetscale/database/download/@planetscale/database-0.6.1.tgz","fileCount":11,"integrity":"sha512-tOEhEob+oU7vjS9SSy/7Xu7kyPMdQRGAWy5ElakIWpih3jtlqHye5DbqxXOFP7AW3Eg/ML5fx3W1O30iSiO35Q==","signatures":[{"sig":"MEUCIHx2fwoLodpQ0L0SSQGMc+ePFnS8OSIci4P0vynHeSAeAiEAgsteNhkcd5M+aKsUgR093pqvMkKJ+1GzBYzMGW8FIBA=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":26944,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJi/BxkACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmpAGw/+Nvenh4e5yWv7BFgYaRPbMfsYJSvylVy8AgpZriHSMC75mBWw\r\nN2IFOpBYCok8r8ach/vTemnr2DN93m6YcXN+7En/nmTl5+/uBDek19Gsqo3Q\r\n+krahsueT2WtpzO/HZWH/T6bqsidHKYcllRVSj/MfsBb0dQCWctTg4dE3/iE\r\nJpT7k7oOGc5cgXhx5o8mkdCWT8JtWBqXDnVkChLjYDSAsQBXqQdx4HwvJc79\r\npa4SzHJO9eVnLrsqhtXZfCHho94uGuS3GfMRvSUm2NPI0OFxE9Es9pM6zuzp\r\nxsrJnQSWGDc7FrX3+rXuFuRed9p4P8zJODK+4YiEFlC0zieVvYNyt3OlmWro\r\nsu58uhaz4Z7vTBpd+K8GHv+pxs8OFa93u5Db6IHlvNE4V9krd0YxNdm8dMI7\r\nCXeSGx+prA7Nj23QYacbbgpGrGgEF9aAlDCnh3+feWvX4WwFYBeOF8MLIBxD\r\nBEggcgoE1xHRMvB6E6fu3GB/2yTFNppTml2L1v3c/E75AwFmGetI+NopIW8m\r\nIrQ2uzF0bTysDi3NdImnifvoz94nQLS85h3TUHv1l/sp4zI3JEGDaQfU9sCG\r\nYHnl551UH7fn0rP6WQ0PaKmcmDxAG8BBtzh7GDDZ0F6aygW9UKMcApGcVrCh\r\nQKtPXo2K/XC5npnMt5Lxn+O7BLoZZflV3r8=\r\n=P0Lv\r\n-----END PGP SIGNATURE-----\r\n","size":8956,"noattachment":false,"key":"/@planetscale/database/-/@planetscale/database-0.6.1.tgz"},"main":"dist/index.js","type":"module","types":"dist/index.d.js","engines":{"node":">=16"},"gitHead":"bd09cbf362c9d6a5f27cb8c14f0e9f41aff0bde0","scripts":{"lint":"eslint src/ __tests__/","test":"jest","build":"tsc","clean":"rm -rf dist/","pretest":"npm run build","version":"echo \"export const Version = '$npm_package_version'\" > src/version.ts && git add src/version.ts && npm run build","prebuild":"npm run clean","postpublish":"npm publish --ignore-scripts --@planetscale:registry='https://npm.pkg.github.com'","prepublishOnly":"npm run build"},"_npmUser":{"name":"dgraham","email":"david.malcom.graham@gmail.com"},"prettier":{"semi":false,"useTabs":false,"tabWidth":2,"printWidth":120,"singleQuote":true,"trailingComma":"none","bracketSpacing":true},"repository":{"url":"git+https://github.com/planetscale/database-js.git","type":"git"},"_npmVersion":"8.16.0","description":"A JavaScript client for PlanetScale databases.","directories":{},"_nodeVersion":"18.7.0","eslintConfig":{"env":{"node":true,"browser":true},"root":true,"rules":{"no-control-regex":"off","prettier/prettier":["error",{},{"usePrettierrc":true}],"@typescript-eslint/ban-ts-comment":"off","@typescript-eslint/no-explicit-any":"off","@typescript-eslint/no-empty-function":"off"},"parser":"@typescript-eslint/parser","extends":["eslint:recommended","plugin:@typescript-eslint/recommended","plugin:prettier/recommended"],"plugins":["@typescript-eslint"]},"_hasShrinkwrap":false,"devDependencies":{"jest":"^28.1.3","eslint":"^8.20.0","undici":"^5.8.0","ts-jest":"^28.0.7","ts-node":"^10.9.1","prettier":"^2.7.1","sqlstring":"^2.3.3","typescript":"^4.7.4","@types/jest":"^28.1.6","@types/sqlstring":"^2.3.0","eslint-config-prettier":"^8.5.0","eslint-plugin-prettier":"^4.2.1","@typescript-eslint/parser":"^5.31.0","@typescript-eslint/eslint-plugin":"^5.31.0"},"_npmOperationalInternal":{"tmp":"tmp/database_0.6.1_1660689507929_0.7485129444372303","host":"s3://npm-registry-packages"},"readme":"![PlanetScale serverless driver for JavaScript](https://github.com/planetscale/database-js/assets/440926/0dfab33f-b01f-4814-ae40-c5fe5cbe94e3)\n\n# PlanetScale serverless JavaScript driver for Vitess/MySQL\n\nA Fetch API-compatible PlanetScale Vitess/MySQL database driver for serverless and edge compute platforms that require HTTP external connections, such as Cloudflare Workers or Vercel Edge Functions\n\n> [!TIP]\n> Connecting to a PlanetScale Postgres database? We support the Neon serverless driver, [read the documentation](https://planetscale.com/docs/postgres/connecting/neon-serverless-driver) to connect.\n\n## Installation\n\n```sh\nnpm install @planetscale/database\n```\n\n## Usage\n\n```ts\nimport { connect } from '@planetscale/database'\n\nconst config = {\n  host: '<host>',\n  username: '<user>',\n  password: '<password>'\n}\n\nconst conn = connect(config)\nconst results = await conn.execute('select 1 from dual')\nconsole.log(results)\n```\n\n### Database URL\n\nA single database URL value can be used to configure the `host`, `username`, and `password` values.\n\n```ts\nimport { connect } from '@planetscale/database'\n\nconst config = {\n  url: process.env['DATABASE_URL'] || 'mysql://user:pass@host'\n}\n\nconst conn = connect(config)\n```\n\n### Connection factory\n\nUse the `Client` connection factory class to create fresh connections for each transaction or web request handler.\n\n```ts\nimport { Client } from '@planetscale/database'\n\nconst client = new Client({\n  host: '<host>',\n  username: '<user>',\n  password: '<password>'\n})\n\nconst conn = client.connection()\nconst results = await conn.execute('select 1 from dual')\nconsole.log(results)\n```\n\n### Transactions\n\nUse the `transaction` function to safely perform database transactions. If any unhandled errors are thrown during execution of the transaction, the transaction will be rolled back.\n\nThe following example is based on [the Slotted Counter Pattern](https://planetscale.com/blog/the-slotted-counter-pattern).\n\n```ts\nimport { connect } from '@planetscale/database'\n\nconst config = {\n  host: '<host>',\n  username: '<user>',\n  password: '<password>'\n}\n\nconst conn = connect(config)\nconst results = await conn.transaction(async (tx) => {\n  const whenBranch = await tx.execute(`INSERT INTO branches (database_id, name) VALUES (42, 'planetscale')`)\n  const whenCounter = await tx.execute(`INSERT INTO slotted_counters(record_type, record_id, slot, count) VALUES ('branch_count', 42, RAND() * 100, 1) ON DUPLICATE KEY UPDATE count = count + 1\"`\n  return [whenBranch, whenCounter]\n})\nconsole.log(results)\n```\n\n### Custom fetch function\n\nThe driver uses the global `fetch` function by default. You can provide a custom fetch function implementation, for example to configure proxies or connection pooling. We recommend the [`undici`][1] package on which Node's built-in fetch is based.\n\n[1]: https://github.com/nodejs/undici\n\n```ts\nimport { connect } from '@planetscale/database'\nimport { fetch } from 'undici'\n\nconst config = {\n  fetch,\n  host: '<host>',\n  username: '<user>',\n  password: '<password>'\n}\n\nconst conn = connect(config)\nconst results = await conn.execute('select 1 from dual')\nconsole.log(results)\n```\n\nTo leverage HTTP/2, you can use the [`fetch-h2`][2] shim.\n\n[2]: https://www.npmjs.com/package/fetch-h2\n\n```ts\nimport { connect } from '@planetscale/database'\nimport { context } from 'fetch-h2'\nconst { fetch, disconnectAll } = context()\n\nconst config = {\n  fetch,\n  host: '<host>',\n  username: '<user>',\n  password: '<password>'\n}\n\nconst conn = connect(config)\nconst results = await conn.execute('select 1 from dual')\nconsole.log(results)\nawait disconnectAll()\n```\n\n### Query parameters\n\n`execute` does not interpolate query parameters. Pass it a complete SQL string. Calling `execute` with an array or a named-parameter object throws an error.\n\nAny values you include in a query must be escaped first, for example with [`sql-escaper`](https://github.com/mysqljs/sql-escaper):\n\n```ts\nimport { format } from 'sql-escaper'\n\nconst results = await conn.execute(format('select 1 from dual where 1=?', [42]))\n```\n\n### Custom type casting function\n\nColumn values are converted to their corresponding JavaScript data types. This can be customized by providing a `cast` function.\n\n```ts\nimport { connect, cast } from '@planetscale/database'\n\nfunction inflate(field, value) {\n  if (field.type === 'INT64' || field.type === 'UINT64') {\n    return BigInt(value)\n  }\n  return cast(field, value)\n}\n\nconst config = {\n  cast: inflate,\n  host: '<host>',\n  username: '<user>',\n  password: '<password>'\n}\n\nconst conn = connect(config)\n```\n\nYou can also pass a custom `cast` function to `execute`. If present, this will override the `cast` function set by the connection:\n\n```ts\nconst result = await conn.execute(\n  'SELECT userId, SUM(balance) AS balance FROM UserBalanceItem GROUP BY userId',\n  {\n    cast: (field, value) => {\n      if (field.name === 'balance') {\n        return BigInt(value)\n      }\n      return cast(field, value)\n    }\n  }\n)\n```\n\n### Row return values\n\nRows can be returned as an object or an array of column values by passing an `as` option to `execute`.\n\n```ts\nconst query = 'select 1 as one, 2 as two'\nconst objects = conn.execute(query, { as: 'object' })\n// objects.rows => [{one: '1', two: '2'}]\n\nconst arrays = conn.execute(query, { as: 'array' })\n// arrays.rows => [['1', '2']]\n```\n\n## Development\n\n```sh\nnpm install\nnpm test\n```\n\n## Need help?\n\nGet help from [the PlanetScale support team](https://support.planetscale.com/), or [join our community on Discord](https://discord.gg/EqrcEf2dGv) or [GitHub discussion board](https://github.com/planetscale/discussion/discussions) to see how others are using PlanetScale.\n\n## License\n\nDistributed under the Apache 2.0 license. See LICENSE for details.\n","publish_time":1660689508130,"_cnpm_publish_time":1660689508130,"dist-tags":{"latest":"2.0.0"}}