porsager/postgres

TypeScript introspection, add more custom types?

Open

#661 创建于 2023年8月17日

在 GitHub 查看
 (0 评论) (4 反应) (0 负责人)JavaScript (6,221 star) (230 fork)batch import
Typescripthelp wanted

描述

I'm not sure if this is something that is lacking in the typescript definition. When I create a postgresql instance in typescript such as:

import postgres from 'postgres';

const sql = postgres({
  host                 : awsSecret.host,
  port                 : awsSecret.port,
  database             : awsSecret.dbname,
  username             : awsSecret.username,
  password             : awsSecret.password
});

If I console.log the sql object I get a lot of property of the sql such as

  console.log
    [Function: sql] {
      types: [Function: typed],
      typed: [Function: typed],
      unsafe: [Function: unsafe],
      array: [Function: array],
      json: [Function: json],
      file: [Function: file],
      parameters: {},
      largeObject: [Function: bound largeObject],
      PostgresError: [class PostgresError extends Error],
      options: {
        host: [ 'xxxxxxxxxx' ],
        port: [ xxxxxx ],
        path: false,
        database: 'xxxxx',
        user: 'xxxxxxx',
        pass: 'xxxxxxxxxx',
        max: 10,
        ssl: false,
        idle_timeout: null,
        connect_timeout: 30,
        max_lifetime: [Function: max_lifetime],
        max_pipeline: 100,
        backoff: [Function: backoff],
        keep_alive: 60,
        prepare: true,
        debug: false,
        fetch_types: true,
        publications: 'alltables',
        target_session_attrs: undefined,
        connection: { application_name: 'postgres.js' },
        types: {},
        onnotice: undefined,
        onnotify: undefined,
        onclose: undefined,
        onparameter: undefined,
        socket: undefined,
        transform: {
          undefined: undefined,
          column: [Object],
          value: [Object],
          row: [Object]
        },
        parameters: {},
        shared: { retries: 0, typeArrayMap: {} },
        serializers: {
          '0': [Function: serialize],
          '16': [Function: serialize],
          '17': [Function: serialize],
          '21': [Function: serialize],
          '23': [Function: serialize],
          '25': [Function: serialize],
          '26': [Function: serialize],
          '114': [Function: serialize],
          '700': [Function: serialize],
          '701': [Function: serialize],
          '1082': [Function: serialize],
          '1114': [Function: serialize],
          '1184': [Function: serialize],
          '3802': [Function: serialize]
        },
        parsers: {
          '16': [Function: parse],
          '17': [Function: parse],
          '21': [Function: parse],
          '23': [Function: parse],
          '26': [Function: parse],
          '114': [Function: parse],
          '700': [Function: parse],
          '701': [Function: parse],
          '1082': [Function: parse],
          '1114': [Function: parse],
          '1184': [Function: parse],
          '3802': [Function: parse]
        }
      },
      listen: [AsyncFunction: listen],
      notify: [AsyncFunction: notify],
      begin: [AsyncFunction: begin],
      close: [AsyncFunction: close],
      end: [AsyncFunction: end]
    }

But not all these properties are available in typescript. Using keys of the sql instance only get the following properties:

 'length',
      'name',
      'arguments',
      'caller',
      'constructor',
      'apply',
      'bind',
      'call',
      'toString',
      Symbol(Symbol.hasInstance),
      '__defineGetter__',
      '__defineSetter__',
      'hasOwnProperty',
      '__lookupGetter__',
      '__lookupSetter__',
      'isPrototypeOf',
      'propertyIsEnumerable',
      'valueOf',
      '__proto__',
      'toLocaleString'

And VS Code also complains when I try to access sql.options. Is this because not all properties are exported in the type definition?

Is there a way in typescript to verify the integrity of the sql instance? Or is there a recommended way to do unit test in typescript for this lib?

贡献者指南