{"name":"@planetscale/database","version":"1.4.1","keywords":["planetscale","database","mysql","vitess","serverless","vercel","lambda"],"author":{"name":"PlanetScale"},"license":"Apache-2.0","_id":"@planetscale/database@1.4.1","maintainers":[{"name":"planetfrances","email":"frances@planetscale.com"},{"name":"pixeljanitor","email":"derek.briggs@me.com"},{"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":"068d4730778451e2dc6598a075210d43a5cc19eb","tarball":"http://registry-npm.ywwl.com/@planetscale/database/download/@planetscale/database-1.4.1.tgz","fileCount":11,"integrity":"sha512-ERWUqRnG0z29RYeoqSN3LxQlGumvnRSaEIpiCCvUVT50V5Xbg/pJtMBuPhTUu69C+x5mV5KCImGEdK8umVHwuw==","signatures":[{"sig":"MEUCIG7lSnCII7UcXrkolUwknAx4LPjNmonaQ71vDdgxNklMAiEAojDxwpjG2Ry2t1zbKNw9Yz+YDkkzzrl6TFk3wWIGMpw=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":30142,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjhRELACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmrSDA/9GjCwv2YEzGWBrwvji51aC7WKZUPfHfnQyLtCAcHe0WlU1BOC\r\n9/si73usvs6g02caMkR84HZivpKTTk8wiAPPhOfNqK1mp2AsU8u5XkwXjGIa\r\nm+//Y6Gud39hxac+9z4dDOz9enYwu6DrSsCyV+tZfRv+3/pG6tFTmHzXbvEb\r\nCRsadDIeOnwl2NKtNKqz5VMtyIrYdG7vGAge8icIvo4HhlpKygcz242Y3xTG\r\nA3PrZIHpmRnOVGZsUPuIM3eQ6090XUap2AjJ7rZ2AIb2D6gvVTNN/Waw0imE\r\ni9Ewf0W6fGnKmpBO4wTeChUpYwwGdNxpHTun+YDMz49m2GjuM0t0FS76BaqJ\r\nj3VJT3bQAz0NCac2uEO9mQPgDUXZ9H8L8R3LJCAt2KmydeRlpwNdFWnXqexa\r\n/dEZ94fgUXYTPgCxYyMVwksko3nYfdRPz+pbtL+lAgp8Sn8pvBqbRzx46Lr0\r\nOWVSkgPj46dnZkxHN92QeJh63QSdUGpiOSBZvbjbXqZXoddaR/BLPQisGHlG\r\n3mDwJck9/U4HfhlXD1R2LV5b+RY8fjh7cY4R0v3hAW5hAr+hpouLHjah+vUu\r\nq6tvWDfe1Ll4xNWQ+0iccNRq5DoEST6rrARkGD917Z4rYhFeGW9TI2p67cPl\r\nkS6+8TG+PcW0Ed/l+uZsOQAEXz2AeT/HPpY=\r\n=LYJB\r\n-----END PGP SIGNATURE-----\r\n","size":9696,"noattachment":false,"key":"/@planetscale/database/-/@planetscale/database-1.4.1.tgz"},"main":"dist/index.js","type":"module","types":"dist/index.d.js","engines":{"node":">=16"},"gitHead":"615b706dd7d61b91f7c90c88075706ba4f1651f3","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":"iheanyi","email":"iekechukwu@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":"9.1.1","description":"A Fetch API-compatible PlanetScale database driver","directories":{},"_nodeVersion":"18.10.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_1.4.1_1669665035043_0.8834974058054679","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":1669665035178,"_cnpm_publish_time":1669665035178,"dist-tags":{"latest":"2.0.0"}}