knex / knex-schema-inspector

Utility for extracting information about existing DB schema
MIT License
99 stars 43 forks source link

schemaInspector is not a function #59

Open hermesalvesbr opened 3 years ago

hermesalvesbr commented 3 years ago

Version 1.5.7 show me : schemaInspector is not a function.

import Knex from 'knex';
import schemaInspector from 'knex-schema-inspector';

const database = Knex({
    client: 'postgres',
    connection: {
        host: 'localhost',
        user: 'postgres',
        password: 'postgres',
        database: 'test',
        charset: 'utf8',
    },
});

database
    .select('*')
    .from('directus_users')
    .where('id', '093d8085-cfcf-4f88-b11c-ef3445838101')
    .then(function (meals) {
        console.log(meals);
        // [ { id: 1, description: 'Burrito', ... } ]
    });

const inspector = schemaInspector(database);
console.log(inspector);

The select works fine. But the schemaInpsector didn´t work. What´s wrong?

"dependencies": {
    "@directus/schema": "^9.0.0-rc.75",
    "axios": "^0.21.1",
    "command-line-args": "^5.1.1",
    "knex": "0.95",
    "knex-schema-inspector": "1.5",
    "listr": "^0.14.3",
    "pg": "^8.6.0"
  },
rijkvanzanten commented 3 years ago

What is this file we're looking at? Is this bundled using typescript/another tool? Is this Node native ESM?

The schema inspector package exports as commonJS currently

hermesalvesbr commented 3 years ago

Native ESM: node --version v14.17.0 I´m using in just one file for testing.:

import Knex from 'knex';
import schemaInspector from 'knex-schema-inspector';

const database = Knex({
    client: 'postgres',
    connection: {
        host: 'localhost',
        user: 'postgres',
        password: 'postgres',
        database: 'test',
        charset: 'utf8',
    },
});

database
    .select('*')
    .from('directus_users')
    .where('id', '093d8085-cfcf-4f88-b11c-ef3445838101')
    .then(function (meals) {
        console.log(meals);
        // [ { id: 1, description: 'Burrito', ... } ]
    });

const inspector = schemaInspector(database);
console.log(inspector);

I'm kind of dumb about it, I don't know if I answer your question correctly.

hermesalvesbr commented 3 years ago
{
  "name": "testemigration",
  "version": "1.0.0",
  "description": "Migrando banco de dados",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
    "@directus/schema": "^9.0.0-rc.75",
    "axios": "^0.21.1",
    "command-line-args": "^5.1.1",
    "knex": "0.95",
    "knex-schema-inspector": "1.5",
    "listr": "^0.14.3",
    "pg": "^8.6.0"
  },
  "author": "Hermes Alves",
  "license": "ISC"
}
rijkvanzanten commented 3 years ago

Schema inspector is still built to CJS and using exports.default = schemainspector. I thought that was compatible with ESM, but could you try using

import * as schemaInspector from 'knex-schema-inspector';

and if that doesn't work, try

import { default as schemaInspector } from 'knex-schema-inspector';

This big Node-wide shift from CJS to ESM is really exciting, but also a little confusing 😅

thebrownfox commented 2 years ago

To anyone wondering what's wrong you need to import it as

import { default as schemaInspectorImport } from "knex-schema-inspector";
const schemaInspector = schemaInspectorImport.default;

// Export inspector with knex instance from Objection's model
export const inspector = schemaInspector(Model.knex());