Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 40 additions & 18 deletions firestore-bigquery-export/scripts/gen-schema-view/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,52 @@
import firebase = require("firebase-admin");
import { FirestoreBigQuerySchemaViewFactory, FirestoreSchema } from "./schema";
import { readSchemas } from "./schema-loader-utils";
import { parseConfig } from "./config";
import { parseConfig, CliConfig } from "./config";
import { generateSchemaFilesWithGemini } from "./schema/genkit";

export async function run(): Promise<number> {
const config = await parseConfig();
export async function run(config?: CliConfig): Promise<number> {
const cliConfig = config ?? (await parseConfig());

process.env.PROJECT_ID = config.projectId;
process.env.GOOGLE_CLOUD_PROJECT = config.bigQueryProjectId;
process.env.PROJECT_ID = cliConfig.projectId;
process.env.GOOGLE_CLOUD_PROJECT = cliConfig.bigQueryProjectId;

if (!firebase.apps.length) {
firebase.initializeApp({
credential: firebase.credential.applicationDefault(),
databaseURL: `https://${config.projectId}.firebaseio.com`,
databaseURL: `https://${cliConfig.projectId}.firebaseio.com`,
});
}

const viewFactory = new FirestoreBigQuerySchemaViewFactory(
config.bigQueryProjectId
cliConfig.bigQueryProjectId
);

// Generate schema files using Gemini if enabled
// Otherwise, read schema files from the filesystem
let schemas = config.schemas;
if (config.useGemini) {
let schemas = cliConfig.schemas;
if (cliConfig.useGemini) {
try {
await generateSchemaFilesWithGemini(config);
schemas = readSchemas([`./schemas/${config.geminiSchemaFileName}.json`]);
await generateSchemaFilesWithGemini(cliConfig);
schemas = readSchemas([
`./schemas/${cliConfig.geminiSchemaFileName}.json`,
]);

console.log("Schema file generated successfully.");
} catch (error) {
console.error("Error during schema generation:", error);
throw error;
}
} else {
if (Object.keys(config.schemas).length === 0) {
if (Object.keys(cliConfig.schemas).length === 0) {
console.log(`No schema files found!`);
}
}

// Initialize schema views
for (const name in schemas) {
await viewFactory.initializeSchemaViewResources(
config.datasetId,
config.tableNamePrefix,
cliConfig.datasetId,
cliConfig.tableNamePrefix,
name,
schemas[name]
);
Expand All @@ -72,14 +74,34 @@ export async function run(): Promise<number> {
}

if (process.env.NODE_ENV !== "test") {
run()
.then((result) => {
let config: CliConfig | null = null;

parseConfig()
.then((parsedConfig) => {
config = parsedConfig;
return run(parsedConfig);
})
Comment thread
IzaakGough marked this conversation as resolved.
.then(() => {
console.log("done.");
process.exit();
process.exit(0);
})
.catch((error) => {
if (config) {
const errorMessage = error.message || error.errors?.[0]?.message;
if (
errorMessage?.includes("ProjectId must be non-empty") ||
errorMessage?.includes("Cannot parse as CloudRegion")
) {
const improvedMessage = `The BigQuery Project ID '${config.bigQueryProjectId}' is not valid. Please verify that the project ID is correct and that you have access to it.`;
error.message = improvedMessage;
if (error.errors?.[0]) {
error.errors[0].message = improvedMessage;
}
}
}

console.log(JSON.stringify(error));
Comment thread
IzaakGough marked this conversation as resolved.
console.error(error.message);
process.exit();
process.exit(1);
});
}
Loading