I am new to base row and I would want to set up a system such that a record added to one table is automatically copied to another table.
I can't really go into too much detail but for example, once someone pays for the service I would want the user data to be copied to another table where the payment ID of the user is concatenated with the last 3 characters of their name and that is saved as the user ID.
Likewise, I had an idea to use a lambda script, but that logic makes no sense.
I have tried writing a python script that accesses the base row API to copy data from table 1, but the script does not access the other table.
Here is some code:
import os
import json
import requests
def lambda_handler(event, context):
# Fetch data from the database
data = fetch_data_from_database()
# Write the data to Baserow
write_to_baserow(data)
return {
'statusCode': 200,
'body': json.dumps(data)
}
def fetch_data_from_database():
url = "https://base.example.com/api/database/rows/table/666/?user_field_names=true"
headers = {
"Authorization": f"Token {os.environ['DATABASE_TOKEN']}"
}
try:
response = requests.request("GET", url, headers=headers)
response.raise_for_status()
data = response.json()
except requests.exceptions.RequestException as e:
print(f"Error fetching data from the database: {e}")
raise
# Extract the relevant fields
results = []
for item in data['results']:
results.append({
# Sensitive data
})
return results
def write_to_baserow(data):
baserow_url = os.environ['BASEROW_URL']
baserow_api_key = os.environ['BASEROW_API_KEY']
destination_table_id = os.environ['DESTINATION_TABLE_ID']
headers = {
"Authorization": f"Token {baserow_api_key}",
"Content-Type": "application/json",
}
url = f"{baserow_url}/api/database/rows/table/{destination_table_id}/"
for row in data:
response = requests.post(url, headers=headers, json=row)
response.raise_for_status()