0

Transform Table <-> Page

Leonid Semik has once again delivered a little script masterpiece that can convert a page into a table or a table into a page.
Simply create a button in the corresponding table and copy the following code into it.
You also need to insert your Ninox API key instead of "YOUR-API-KEY-FOR-THIS-ACCOUNT".

And the transformation should be successful.

Please note that when converting a table to a page, the excess records are deleted so that only one record is contained in the page.

-----------
"
//* This script is added to a butoon.
When clicked, the type of the table or page is checked and a dialog suggests to change the type
";
let me := this;
let apiKey := "YOUR-API-KEY-FOR-THIS-ACCOUNT";
"
    //* Determine if the database is located in a public cloud or in a private cloud
";
let host := first(split(urlOf(this), "#"));
let api := if host = "https://app.ninox.com/" then
        "https://api.ninox.com/v1"
    else
        host + "v1"
    end;
"
    //*Set base api url
";
api := ---
{ api }/teams/{ teamId() }/databases/{ databaseId() }
    ---;
"
//*Set headers for all API calls
";
let headers := {
        Authorization: "Bearer " + apiKey,
        'Content-type': "application/json"
    };
"
//*Check the database schema
";
let kindResponse := do as server
        http("GET", api + "/schema", headers, {})
    end;
"
//* Check if the query returns a database schema
";
if not kindResponse.result.version then
    alert(kindResponse)
else
    let myKind := item(kindResponse.result.types, tableId(this)).kind;
    let tId := tableId(me);
    "
//* Set dialog in case the properties cannot be determined
    ";
    let dialogHeader := "Table Properties";
    let dialogBody := "This cloud version does not support pages.
Please update your Cloud to the latest version.";
    let dialogButtons := ["Cancel"];
    "
//* Check the Type (page or table)
    ";
    switch myKind do
    case "page":
        (
            dialogBody := "This is a page";
            dialogButtons := ["Convert to a table", "Cancel"];
            let d := dialog(dialogHeader, dialogBody, dialogButtons);
            if d = first(dialogButtons) then
                "
    //* Prepare the body for the PATH conversion
";
                let body := {
                        types: {}
                    };
                setItem(body.types, tableId(this), {
                    kind: "table"
                });
                let patchResponse := do as server
                        http("PATCH", api + "/schema", headers, body)
                    end;
                if formatJSON(patchResponse.result) = "{}" then
                    alert("The conversion to the table was successfully performed!")
                else
                    alert(patchResponse)
                end
            end
        )
    case "table":
        (
            dialogBody := "This is a Table.
A page contains only one record.
If the table has multiple records, they will be deleted.";
            dialogButtons := ["Convert to a page", "Cancel"];
            let d := dialog(dialogHeader, dialogBody, dialogButtons);
            if d = first(dialogButtons) then
                "
            //*    Check number of Records
                ";
                let myPerPage := 1000;
                let myPage := 0;
                let myNumberOfRecords := myPerPage;
                let myRecordsArray := [0];
                myRecordsArray := null;
                while myNumberOfRecords = myPerPage do
                    let response := do as server
                            http("GET", url(api + "/tables/" + tableId(this) + "/records", {
                                perPage: myPerPage,
                                page: myPage
                            }), headers, {})
                        end;
                    myNumberOfRecords := cnt(response.result);
                    if myNumberOfRecords = myPerPage then
                        myPage := myPage + 1
                    end;
                    for i in response.result do
                        myRecordsArray := array(myRecordsArray, [number(i.id)])
                    end
                end
;
                "
//* Check if there is a record with id = 1 (is mandatory for Page)
                ";
                if not contains(myRecordsArray, 1) then
                    "
//* create a record with id=1
                    ";
                    let upsertResponse := do as server
                            http("POST", api + "/tables/" + tableId(this) + "/records", headers, {
                                _upsert: true,
                                _id: 1
                            })
                        end;
                    if upsertResponse.result.number(_id) != 1 then
                        alert(upsertResponse)
                    end
                end;
"
//* create an  with id=1
                    ";
                let delArray := for i in myRecordsArray do
                        if i != 1 then i end
                    end;
                let deleteResponse := do as server
                        http("DELETE", api + "/tables/" + tableId(this) + "/records", headers, delArray)
                    end;
                let body := {
                        types: {}
                    };
                setItem(body.types, tableId(this), {
                    kind: "page"
                });
                let patchResponse := do as server
                        http("PATCH", api + "/schema", headers, body)
                    end;
                void
            end
        )
    default:
        dialog(dialogHeader, dialogBody, dialogButtons)
    end
end
-----------------------
Beware, when you using the code inside a table included a subtable and there is no record-ID with number 1 inside!!!
Then use better the database with further information.

I have now packed Leo's script into a Ninox DB, which can be used to convert external tables/pages.
You enter the cloud name (api.ninox.com or your private cloud) and the Ninox API key and can prepare the transformation using either the respective IDs or the clear names of the team/database/table.
During this process, you are then shown whether the table in question is a page or a table.
The respective transformation is then started with the Convert button.
You can create a separate record for each table to be converted and access it at any time.

I have tried to make the DB as simple as possible.
Of course, the method described above (copy/paste) is still one way of carrying out the transformation.

4 replies

null
    • Retraité
    • Eric_Plet
    • 10 mths ago
    • Reported - view

    After using the local and iCloud version of Ninox on Mac for several years, I discovered the Public Cloud version without knowing the concept of API...

    How to obtain the Ninox API key?

    • Mel_Charles
    • 10 mths ago
    • Reported - view
      • Retraité
      • Eric_Plet
      • 10 mths ago
      • Reported - view

       Thank you so much.
      The presentation for the different accesses changed compared to the description but I ended up finding and obtaining the famous key ;-)

    • Retraité
    • Eric_Plet
    • 10 mths ago
    • Reported - view

    Thanks again everyone!
    It's great and very powerful.
    I was able to go from table to page and even do the opposite!

Content aside

  • 10 mths agoLast active
  • 4Replies
  • 243Views
  • 3 Following