async initializeSchemaViewResources()

in firestore-bigquery-export/scripts/gen-schema-view/src/schema.ts [105:193]


  async initializeSchemaViewResources(
    datasetId: string,
    tableNamePrefix: string,
    schemaName: string,
    firestoreSchema: FirestoreSchema
  ): Promise<void> {
    const rawChangeLogTableName = changeLog(raw(tableNamePrefix));
    const latestRawViewName = latest(raw(tableNamePrefix));
    const changeLogSchemaViewName = changeLog(
      schema(tableNamePrefix, schemaName)
    );
    const latestSchemaViewName = latest(schema(tableNamePrefix, schemaName));
    const dataset = this.bq.dataset(datasetId);

    const udfNames = Object.keys(udfs);

    for (let i = 0; i < udfNames.length; i++) {
      const functionName = udfNames[i];
      const udf = udfs[functionName](datasetId);
      await this.bq.query({
        query: udf.query,
      });
    }

    if (firestoreSchema.fields) {
      firestoreSchema.fields = updateFirestoreSchemaFields(
        firestoreSchema.fields
      );
    }

    let changeLogSchemaView = dataset.table(changeLogSchemaViewName);
    const [changeLogSchemaViewExists] = await changeLogSchemaView.exists();

    let latestSchemaView = dataset.table(latestSchemaViewName);
    const [latestSchemaViewExists] = await latestSchemaView.exists();

    let result = userSchemaView(
      datasetId,
      rawChangeLogTableName,
      firestoreSchema
    );
    let bigQueryFields = result.fields;

    const changelogOptions = {
      friendlyName: changeLogSchemaViewName,
      view: result.viewInfo,
    };
    if (!changeLogSchemaViewExists) {
      logs.bigQuerySchemaViewCreating(
        changeLogSchemaViewName,
        firestoreSchema,
        result.viewInfo.query
      );
      await changeLogSchemaView.create(changelogOptions);
      logs.bigQuerySchemaViewCreated(changeLogSchemaViewName);
    }

    await changeLogSchemaView.setMetadata({
      schema: decorateSchemaWithChangelogFields({
        fields: bigQueryFields,
      }),
    });

    result = latestConsistentSnapshotSchemaView(
      datasetId,
      latestRawViewName,
      firestoreSchema
    );

    bigQueryFields = result.fields;
    const latestOptions = {
      friendlyName: latestSchemaViewName,
      view: result.viewInfo,
    };
    if (!latestSchemaViewExists) {
      logs.bigQuerySchemaViewCreating(
        latestSchemaViewName,
        firestoreSchema,
        result.viewInfo.query
      );
      await latestSchemaView.create(latestOptions);
      logs.bigQueryViewCreated(latestSchemaViewName);
    }
    await latestSchemaView.setMetadata({
      schema: decorateSchemaWithChangelogFields({
        fields: bigQueryFields,
      }),
    });
  }