Giter Club home page Giter Club logo

planetscale-node's Introduction

planetscale-node

⚠️ Do not use this package. It is from PlanetScale's early beta and no longer necessary. ⚠️

  • To use PlanetScale with a Node.js application, follow our Node.js guide.
  • PlanetScale is MySQL compatible and you can expect it to work with any MySQL node compatible library.
  • For applications that require edge (http) connectivity, we recommend database.js

⚠️ The rest of the readme is for archival purposes only. ⚠️

Installation

$ npm install planetscale-node

Setup

This code uses the PlanetScale API to provision a TLS certificate, and then connects to the database. It uses Service Tokens for authentication, so you'll need to create one for the app:

~> pscale service-token create
  NAME           TOKEN
 -------------- ------------------------------------------
  nyprhd2z6bd3   [REDACTED]

~> pscale service-token add-access nyprhd2z6bd3 connect_production_branch --database [YOUR DB]
  DATABASE   ACCESSES
 ---------- ---------------------------
  [YOUR DB]       connect_production_branch

Usage

Set the following environment variables in your application.

export PLANETSCALE_TOKEN='[REDACTED]'
export PLANETSCALE_TOKEN_NAME='nyprhd2z6bd3'
export PLANETSCALE_ORG='[YOUR ORG]'
export PLANETSCALE_DB='[YOUR DB NAME]'
const { PSDB } = require('planetscale-node')
const conn = new PSDB('main')

async function main() {
  const [rows, fields] = await conn.query('select * from reminders')
  console.log(rows, fields)
}

main()

Using prepared statements

const { PSDB } = require('planetscale-node')
const conn = new PSDB('main')

async function main() {
  const [rows, fields] = await conn.execute('select * from reminders where id > ?', [10])
  console.log(rows, fields)
}

main()

planetscale-node's People

Contributors

dbussink avatar dgraham avatar jamesmosier avatar jtcunning avatar mscoutermarsh avatar nickvanw avatar no-itsbackpack avatar skottler avatar toby222 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

planetscale-node's Issues

URL builder

I just connected PlanetScale with Prisma and Vercel and there was a bit more friction than I think was necessary. I started with the Vercel integration, which added some env variables. It wasn't clear how I was expected to connect, but I ended up doing it initially with a single DB_URL env variable, and ultimately made a URL builder function.

It seems like this setup should be easier. I'm not sure exactly the best way, but perhaps one way could be by exposing a simple URL builder function in this package.

My implementation is as follows:

const dbUrl = () => {
  const user = process.env.PLANETSCALE_DB_USERNAME;
  const password = process.env.PLANETSCALE_DB_PASSWORD;
  const db = process.env.PLANETSCALE_DB;
  const host = process.env.PLANETSCALE_DB_HOST;
  const certPath = process.env.PLANETSCALE_SSL_CERT_PATH;

  const hasAllValues =
    [user, password, db, host, certPath].findIndex(value => !value) === -1;
  if (!hasAllValues) return null;

  return `mysql://${user}:${password}@${host}/${db}?sslmode=require&sslcert=${certPath}`;
};

v0.1.0 - Occasional SSL issue ERR_SSL_SSLV3_ALERT_BAD_RECORD_MAC

While seeding a database using a node script I'll occasionally get the following error:

C:\projects\...\node_modules\mysql2\promise.js:110
    const localErr = new Error();
                     ^

Error: 23264:error:140943FC:SSL routines:ssl3_read_bytes:sslv3 alert bad record mac:c:\ws\deps\openssl\openssl\ssl\record\rec_layer_s3.c:1544:SSL alert number 20       

    at PromiseConnection.execute (C:\projects\...\node_modules\mysql2\promise.js:110:22)
    at PSDB.execute (C:\projects\...\node_modules\planetscale-node\dist\index.js:29:43)
    at Object.module.exports.seed (C:\projects\...\seed.js:25:18)
    at processTicksAndRejections (node:internal/process/task_queues:94:5) {
  code: 'ERR_SSL_SSLV3_ALERT_BAD_RECORD_MAC',
  errno: undefined,
  sqlState: undefined,
  sqlMessage: undefined
}

I can't reliably reproduce it but it happens often enough to deserve its own issue.

Timezone issue

Seeing the same time reflected differently when I retrieve it from pscale CLI compared with the planetscale-node:
From planetscale-node: "created_ts":"2021-09-29T12:53:37.000Z","updated_ts":"2021-09-29T12:57:39.000Z
From pscale CLI: 2021-09-29 05:53:37 | 2021-09-29 05:57:39

The UTC time is actually 2021-09-29 05:57:39, so the UTC timestamp coming from planetscale-node is pretty confusing.

I suspect this is due to sidorares/node-mysql2#1337 .

Compatibility plan

What's the plan with this library in regards to using it with other libraries like Knex? Can I just use the connection object? Or is the plan to only be able to use this library?

Extremely slow connection creation time - unusable for serverless

I've been evaluating PlanetScale for use with serverless applications (specifically NextJS). I love the CLI and database offering, but I've found that planetscale-node at its current state is unusable for serverless applications.

In the serverless paradigm, each request to a cloud function will create a new connection to the database. With planetscale-node, creating a connection and executing the first query consistently takes more than three seconds, sometimes even four (!). (Subsequent queries take ~150ms).

I did some digging and found that the great majority (~96%) of that time is spent waiting for a POST request to create a TLS certificate, prior to starting the database connection itself (see postJSON).

Is it expected for certificate creation to take that long?
Are there any plans to support serverless applications?

Better errors for missing access

If you try to connect and your service token is missing the connect_branch or connect_production_branch access you simply get a generic Http 404 error in the response. Not helpful. It would be nice to have more descriptive errors for things like this.

Apologies if this is the wrong place for feedback. I know it's more of a planetscale API issue rather than the planetscale-node client.

Forbidden response for certificate request

Hi, today I was working with Planetscale and planetscale-node package. I followed all the steps in docs. When I run a basic query example on Node.js, I get a forbidden response with a 403 status code.

It happens in certificate request in postJSON function. I debugged the code and got all the header, body data from the debugger, tried to send post request on Postman with same informations and I got 200.

After a few hours, wanted to try axios for this request and it was successful again. Cannot sure the http.request implementation is wrong somehow or it is just me.

What do you think?

Code

const { PSDB } = require('./dist/index.js')
const conn = new PSDB('main')

async function main() {
  const [rows, fields] = await conn.execute('select * from likes')
  console.log(rows, fields)
}

main()

Stack Trace

undefined:1
<html>
^

SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at IncomingMessage.<anonymous> (REPO_PATH/planetscale-node/dist/index.js:102:69)
    at IncomingMessage.emit (node:events:402:35)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Packages

  • Node.js: v16.13.0
  • planetscale-node: v0.2.0

Basic example crashes

Running the basic example in the README.md crashes with:

PS C:\projects\planetscale-node-test> node .\test.js
C:\projects\planetscale-node-test\node_modules\planetscale-node\dist\index.js:38
            throw new Error(`HTTP ${status}`);
                  ^

Error: HTTP 404
    at PSDB.createConnection (C:\projects\planetscale-node-test\node_modules\planetscale-node\dist\index.js:38:19)
    at processTicksAndRejections (node:internal/process/task_queues:94:5)
    at async PSDB.query (C:\projects\planetscale-node-test\node_modules\planetscale-node\dist\index.js:21:32)
    at async main (C:\projects\planetscale-node-test\test.js:5:28)

Code, for reference:

const { PSDB } = require('planetscale-node')
const conn = new PSDB('main')

async function main() {
  const [rows, fields] = await conn.query('select * from reminders')
  console.log(rows, fields)
}

main()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.