jsonToSqlSupabase function

String jsonToSqlSupabase({
  1. required Map jsonData,
  2. Map? originData,
  3. bool isEnableRls = true,
  4. String? tableName,
})

Implementation

String jsonToSqlSupabase({
  required Map jsonData,
  Map? originData,
  bool isEnableRls = true,
  String? tableName,
}) {
  // Map origin_data = originData ?? {};
  if (jsonData["@type"] is String == false) {
    jsonData["@type"] = "";
  }

  tableName ??= (jsonData["@type"] as String).snakeCaseClass();
  // https://supabase.com/docs/guides/database/tables?database-method=sql
  String script = """
-- 1. Create table
CREATE TABLE {table_name} (
  id bigint generated by default as identity primary key,

"""
      .trim();
  script += "\n";
  script += generateDataSqlJson(
    data: jsonData,
  );
  script += "\n";
  // script = script.replaceAll(RegExp(r"({table_name})", caseSensitive: false), table_name).trim();

  script += """
);
"""
      .trim();
  if (isEnableRls) {
    script += "\n\n";
    script += """
-- 2. Enable RLS
alter table {table_name} enable row level security;
"""
        .trim();
    script += "\n\n";
    script += """
-- 3. ADD Column in Tabble Exist (Optional)
"""
        .trim();
    script += "\n";
    script += generate_add_sql_data(
      data: jsonData,
      tableName: tableName,
    ).trim();
  }

  script += "\n\n";
  script += "-- Recommendation";

  return (script
      .trim()
      .replaceAll(RegExp(r"({table_name})", caseSensitive: false), tableName)
      .trim());
}