Skip to content

[FDC init] Move the default value to actuate #8554

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
56 changes: 22 additions & 34 deletions src/init/features/dataconnect/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
// serviceEnvVar is used by Firebase Console to specify which service to import.
// It should be in the form <location>/<serviceId>
// It must be an existing service - if set to anything else, we'll ignore it.
const serviceEnvVar = () => envOverride("FDC_SERVICE", "");

Check warning on line 36 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function

export interface RequiredInfo {
serviceId: string;
Expand Down Expand Up @@ -76,7 +76,7 @@

// askQuestions prompts the user about the Data Connect service they want to init. Any prompting
// logic should live here, and _no_ actuation logic should live here.
export async function askQuestions(setup: Setup): Promise<void> {

Check warning on line 79 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
const hasBilling = await isBillingEnabled(setup);
if (setup.projectId) {
hasBilling ? await ensureApis(setup.projectId) : await ensureSparkApis(setup.projectId);
Expand All @@ -88,12 +88,12 @@
isNewInstance: false,
cloudSqlDatabase: "",
isNewDatabase: false,
connectors: [defaultConnector],
schemaGql: [defaultSchema],
connectors: [],
schemaGql: [],
shouldProvisionCSQL: false,
};
// Query backend and pick up any existing services quickly.
info = await promptForExistingServices(setup, info, hasBilling);
info = await promptForExistingServices(setup, info);

const requiredConfigUnset =
info.serviceId === "" ||
Expand All @@ -110,6 +110,7 @@
default: true,
}));
if (shouldConfigureBackend) {
// TODO: Prompt for app idea and use GiF backend to generate them.
info = await promptForService(info);
info = await promptForCloudSQL(setup, info);

Expand All @@ -122,14 +123,6 @@
default: true,
}))
);
} else {
// Ensure that the suggested name is DNS compatible
const defaultServiceId = toDNSCompatibleId(basename(process.cwd()));
info.serviceId = info.serviceId || defaultServiceId;
info.cloudSqlInstanceId =
info.cloudSqlInstanceId || `${info.serviceId.toLowerCase() || "app"}-fdc`;
info.locationId = info.locationId || `us-central1`;
info.cloudSqlDatabase = info.cloudSqlDatabase || `fdcdb`;
}
setup.featureInfo = setup.featureInfo || {};
setup.featureInfo.dataconnect = info;
Expand All @@ -137,16 +130,30 @@

// actuate writes product specific files and makes product specifc API calls.
// It does not handle writing firebase.json and .firebaserc
export async function actuate(setup: Setup, config: Config): Promise<void> {

Check warning on line 133 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
// Most users will want to persist data between emulator runs, so set this to a reasonable default.
const dir: string = config.get("dataconnect.source", "dataconnect");

Check warning on line 135 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
const dataDir = config.get("emulators.dataconnect.dataDir", `${dir}/.dataconnect/pgliteData`);

Check warning on line 136 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
config.set("emulators.dataconnect.dataDir", dataDir);

const info = setup.featureInfo?.dataconnect;
if (!info) {
throw new Error("Data Connect feature RequiredInfo is not provided");
}
// Populate the default values of required fields.
const defaultServiceId = toDNSCompatibleId(basename(process.cwd()));
info.serviceId = info.serviceId || defaultServiceId;
info.cloudSqlInstanceId =
info.cloudSqlInstanceId || `${info.serviceId.toLowerCase() || "app"}-fdc`;
info.locationId = info.locationId || `us-central1`;
info.cloudSqlDatabase = info.cloudSqlDatabase || `fdcdb`;
// Make sure to add some GQL files.
// Use the template if it starts from scratch or the existing service has no GQL source.
if (!info.schemaGql.length && !info.connectors.flatMap((r) => r.files).length) {
info.schemaGql = [defaultSchema];
info.connectors = [defaultConnector];
}

await writeFiles(config, info);

if (setup.projectId && info.shouldProvisionCSQL) {
Expand All @@ -161,7 +168,7 @@
}
}

export async function postSetup(setup: Setup, config: Config): Promise<void> {

Check warning on line 171 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing JSDoc comment
const cwdPlatformGuess = await getPlatformFromFolder(process.cwd());
if (cwdPlatformGuess !== Platform.NONE) {
await sdk.doSetup(setup, config);
Expand All @@ -175,22 +182,12 @@
}
}

async function writeFiles(config: Config, info: RequiredInfo) {

Check warning on line 185 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
const dir: string = config.get("dataconnect.source") || "dataconnect";

Check warning on line 186 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
const subbedDataconnectYaml = subDataconnectYamlValues({
...info,
connectorDirs: info.connectors.map((c) => c.path),
});
// If we are starting from a fresh project without data connect,
if (!config.get("dataconnect.source")) {
// Make sure to add add some GQL files.
// Use the template if the existing service is empty (no schema / connector GQL).
if (!info.schemaGql.length && !info.connectors.flatMap((r) => r.files).length) {
info.schemaGql = [defaultSchema];
info.connectors = [defaultConnector];
}
}

config.set("dataconnect", { source: dir });
await config.askWriteProjectFile(
join(dir, "dataconnect.yaml"),
Expand All @@ -215,7 +212,7 @@
}
}

async function writeConnectorFiles(

Check warning on line 215 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing return type on function
config: Config,
connectorInfo: {
id: string;
Expand All @@ -224,7 +221,7 @@
},
) {
const subbedConnectorYaml = subConnectorYamlValues({ connectorId: connectorInfo.id });
const dir: string = config.get("dataconnect.source") || "dataconnect";

Check warning on line 224 in src/init/features/dataconnect/index.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
await config.askWriteProjectFile(
join(dir, connectorInfo.path, "connector.yaml"),
subbedConnectorYaml,
Expand Down Expand Up @@ -266,24 +263,15 @@
return replaced;
}

async function promptForExistingServices(
setup: Setup,
info: RequiredInfo,
isBillingEnabled: boolean,
): Promise<RequiredInfo> {
if (!setup.projectId || !isBillingEnabled) {
// TODO(b/368609569): Don't gate this behind billing once backend billing fix is rolled out.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got rid of the isBillingEnabled check here because we no longer needs it.

FDC API can be used without billing.

async function promptForExistingServices(setup: Setup, info: RequiredInfo): Promise<RequiredInfo> {
// Check for existing Firebase Data Connect services.
if (!setup.projectId) {
return info;
}

// Check for existing Firebase Data Connect services.
const existingServices = await listAllServices(setup.projectId);
const existingServicesAndSchemas = await Promise.all(
existingServices.map(async (s) => {
return {
service: s,
schema: await getSchema(s.name),
};
return { service: s, schema: await getSchema(s.name) };
}),
);
if (existingServicesAndSchemas.length) {
Expand Down
Loading