cube-js / cube

📊 Cube — Universal semantic layer platform for AI, BI, spreadsheets, and embedded analytics
https://cube.dev
Other
17.96k stars 1.78k forks source link

Can driver_factory return Custom Driver instances and Driver Config Objects? #8962

Open matheusb-comp opened 6 days ago

matheusb-comp commented 6 days ago

Problem

From the driver_factory documentation, when using JavaScript, there are examples for returning a driver config object ({ type: "duckdb" }) and an instance of a Custom Driver (class that extends BaseDriver).

However, I'm getting an error with the option configured to return both formats:

driverFactory: ({ dataSource }) => {
  if (dataSource === "mySource") {
    return new MyCustomDriver();
  } else {
    return { type: "duckdb" };
  }
}

From the TypeScript definition this should be OK, but this is the error that happens during schema compilation:

Compiling schema error: e25ceb53-6fa6-4128-bdc9-a0cc1d92728c-span-1 (289ms)
{
  "version": "default_schema_version_69b10a3ae4ba76ea6144b864e4d001d1"
} 
Error: CreateOptions.driverFactory function must return either BaseDriver or DriverConfig.
    at OptsHandler.assertDriverFactoryResult (/cube/node_modules/@cubejs-backend/server-core/src/core/OptsHandler.ts:146:15)
    at /cube/node_modules/@cubejs-backend/server-core/src/core/OptsHandler.ts:225:21
    at CubejsServerCore.contextToDbType (/cube/node_modules/@cubejs-backend/server-core/src/core/OptsHandler.ts:246:17)
    at CompilerApi.dbType (/cube/node_modules/@cubejs-backend/server-core/src/core/server.ts:499:28)

If instead I return a driver instance, no error is shown:

// const CubejsServerCore = require('@cubejs-backend/server-core');
driverFactory: ({ dataSource }) => {
  if (dataSource === "mySource") {
    return new MyCustomDriver();
  } else {
    return CubejsServerCore.createDriver("duckdb");
  }
}
igorlukanin commented 3 days ago

Hi @matheusb-comp 👋

Thanks for posting! It looks like the error message has pointed you in the right direction and you were able to configure what you want. What is the question here?

matheusb-comp commented 3 days ago

Well, from the from typescript I understood that driver_factory could return class instances and config objects. However, from the tests it seems it can only return one or the other, not both formats.

So the question is basically if this is the intended behaviour. And if so, I'm not sure if CubejsServerCore.createDriver is the correct way to create the non-custom driver instances (to be equivalent to the other format, returning the config objects).