Devin
Devin

Reputation: 323

How to import and use javascript constants in config block of sqlx, when js file not in includes folder

The google docs outline how to define field definitions globaly in the includes folder. e.g. you could have includes/docs.js

This might look like:

const myField = 'A definition for myField';

module.exports = {
 myField
};

Then in a definitions subfolder, say definitions/myData/source/mySourceData.sqlx you could have a config block like:

config {
  type: "view",
  schema: "mySchema",
  columns: {
    myField: docs.myField
  }

...some SQL

However, my dataform repo is large with numerous modules. For ease of organization, I want to have my docs.js file in the appropriate module (really folder) for the specified integration.

e.g. I want to include my docs.js file in definitions/myData/shared/myDataDocs.js

I attempted this. The myDataDocs.js file is the same as docs.js example specified above, just located at definitions/myData/shared/myDataDocs.js

In the mySourceData.sqlx I adjusted the file to include this:

js {
  const myDataDocs = require('definitions/myData/shared/myDataDocs.js');

}
config {
  type: "view",
  schema: "mySchema",
  columns: {
    myField: myDataDocs.myField
  }

...some SQL

I am getting 'myDataDocs' is not defined. How do I reference a JS file and use it in the config block for column definitions, when the JS file is not located in the includes folder?

Upvotes: 0

Views: 335

Answers (3)

Tiberiu
Tiberiu

Reputation: 548

You can not access code from js{} block in config{} block. Just use it like this:

config {
      type: "table",
      name: "table_name",
      columns: {
         myField: require('definitions/myData/shared/myDataDocs.js').myField
      }
    }

Upvotes: 0

Mohammad Usman Sajid
Mohammad Usman Sajid

Reputation: 114

I think the issue you're encountering is likely due to how Dataform resolves file paths when using require(). Why don't you try to use a relative path from the current file to the JavaScript file you want to import?

By looking at your question I'm guessing that your SQLX file is in

definitions/myData/source/

and your JS file is in

definitions/myData/shared/

And if these assumptions are correct all you need to do is simply go up one directory level and then into the shared directory.

Other then that you should also try to remove the ".js" extension from the require statement.

Node.js (which Dataform uses) will automatically append the ".js" extension.

After all of this, your SQLX file should look something like this:

js {
  const myDataDocs = require("../shared/myDataDocs");
}

config {
  type: "view",
  schema: "mySchema",
  columns: {
    myField: myDataDocs.myField
  }
}

// ... your SQL code here

On a separate not, if you are using a package.json file, you should also have a look if it is set up correctly to recognize your project structure.

Hoping that your problem gets solved, good luck. 👍😇

Upvotes: 0

bhautik
bhautik

Reputation: 7

for imporing and use the Javascript constants in config block of sqlx , if jsx is not include in folder : so there is simple method we can use for this like :

  1. creating a Javascript file which include all the constant like i.e

     export const DB_HOST = 'locahost:3001';
     export const DB_USER = 'user';
     export const DB_PASSWORD = '12345678';
    

after creating a file that include all the constant we can import all the const SQLX configuration which result like this name of file is : sqlx-config.js

import { DB_HOST, DB_USER, DB_PASSWORD } from './constants.js';

const sqlxConfig = {
  host: DB_HOST,
  user: DB_USER,
  password: DB_PASSWORD,
  database: 'database_name',
};

export default sqlxConfig;
  1. now use the config in your main.js file which is simple and easy
import sqlxConfig from './sqlx-config.js';
import sqlx from 'sqlx';

const db = sqlx(sqlxConfig);

db.connect()
  .then(() => {
    //success statement or console
  })
  .catch((error) => {
    //error statement or console
  });

Upvotes: 0

Related Questions