John
John

Reputation: 31

appmaker data: connect to multiple Cloud SQL databases

Is it possible to connect the one App to more than one cloud SQL database?

I can connect to a single cloud SQL database within the App settings (projectName:regionName:instanceName/databaseName).

But I want to add models from more than one existing Cloud SQL database to my single App.

When Creating External Data Models, there is no option or settings to configure another/additional google cloud SQL database.

Any advise appreciated if there's a way to work around this?

Reference: Connect your app to an existing Google Cloud SQL database: https://developers.google.com/appmaker/models/cloudsql#connect_your_app_to_an_existing_google_cloud_sql_database

John

Upvotes: 1

Views: 543

Answers (1)

user8087816
user8087816

Reputation:

Just tried this out:

App Maker server scripts have access to Google Service APIs. The JDBC class allows you to connect to Cloud SQL after adding 10 whitelisted IPs.

From: https://developers.google.com/apps-script/guides/jdbc add the following IPs to your access control in Cloud console:

64.18.0.0/20
64.233.160.0/19
66.102.0.0/20
66.249.80.0/20
72.14.192.0/18
74.125.0.0/16
173.194.0.0/16
207.126.144.0/20
209.85.128.0/17
216.239.32.0/19

I put this bit in a server script and attached it to a button and checked the output log and found that it worked.

  function Connect() {
  var address = 'instance_ip'; 
  var user = 'username';
  var pw = 'password';
  var db = 'database_name'; 

  var dbUrl = 'jdbc:mysql://' + address + '/' + db; 

// read some rows
function readFrom() {
  var conn = Jdbc.getConnection(dbUrl, user, pw); 

  var start = new Date(); 
  var stmt = conn.createStatement();

  stmt.setMaxRows(100); 
  var results = stmt.executeQuery('SELECT * FROM table_name'); 
  var numCols = results.getMetaData().getColumnCount(); 

  while (results.next()) {
    var rowString = ''; 
    for(var col = 0; col < numCols; col++) {
      rowString += results.getString(col + 1) + '\t';  
    }
    console.log(rowString);
  }

  results.close();
  stmt.close();
}

readFrom();
}

Using this, you could have as many connections as you like. But this would not allow App Maker to recognize it as a datasource. You'd have to manually process the results until this feature is officially supported. (I like the idea and will make a repo library for this.)

Upvotes: 1

Related Questions