Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schemaInspector is not a function #59

Open
hermesalvesbr opened this issue Jun 14, 2021 · 5 comments
Open

schemaInspector is not a function #59

hermesalvesbr opened this issue Jun 14, 2021 · 5 comments

Comments

@hermesalvesbr
Copy link

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Author

{
  "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
Copy link
Collaborator

rijkvanzanten commented Jun 17, 2021

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
Copy link

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());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants