Skip to content

Simple show tables statement not cast correctly by default #165

Closed
@cdcarson

Description

@cdcarson

I believe this is related to PR #164.

Given this...

import { DATABASE_URL } from '$env/static/private';
import { connect } from '@planetscale/database';
export const load = async () => {
  const conn = connect({ url: DATABASE_URL });
  const result = await conn.execute('SHOW TABLES;');
  console.dir(result, { depth: null });
  return { result: 'broken' };
};

... I get this....

{
  headers: [ 'Tables_in_mydbname' ],
  types: { Tables_in_mydbname: 'VARBINARY' },
  fields: [
    {
      name: 'Tables_in_mydbname',
      type: 'VARBINARY',
      table: 'TABLES',
      orgTable: 'tables',
      orgName: 'Tables_in_mydbname',
      columnLength: 256,
      charset: 255,
      flags: 4225
    }
  ],
  rows: [
    {
      Tables_in_mydbname: Uint8Array(7) [
         65, 114, 116,
        105,  99, 108,
        101
      ]
    },
  // etc.
  ]
}

Doing this...

import { DATABASE_URL } from '$env/static/private';
import { connect, cast } from '@planetscale/database';
export const load = async () => {
  const conn = connect({ url: DATABASE_URL, cast: (field, value) => {
    if (field.type === 'VARBINARY' && (value as unknown) instanceof Uint8Array) {
      return new TextDecoder().decode(value as unknown as Uint8Array);
    }
    return cast(field, value);
  }});
  const result = await conn.execute('SHOW TABLES;');
  console.dir(result, { depth: null });
  return { result: 'not broken' };
};

...works, but (a) it's mighty inconvenient typescript (as unknown etc,) (b) it seems like it'd likely produce similar unwanted behavior in other off-the-cuff queries, and (c) it at least requires the shape of cast to be changed to string|null|Uint8Array.

Not sure if this is possible, but it'd be simpler to go back to string|null, and leave it up to userland or ORMs (Prisma filed the original issue) to deal with. I'm aware that it might be more complicated than this, but one can turn a string into a UintArray ...

const arr = new TextEncoder().encode('Hello, I am a string from planetscale');

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions