With Velo code, is it possible to add new fields (including text) to items in a database?

Let’s say I was trying to build a peer to peer messaging system.

These would be fixed fields for each item in a “Messages” collection:

  • Initiator (email)
  • Receiver (email)
  • CreatedDate
  • etc.

Imagine a text box with the button “send message”

Imagine “onClick_submitMessage” creates a NEW field [message1] to the item that has the initiator and the receiver

Now the NEW field [message1] will be displayed on-screen (somehow, I’ll figure it out).

My problem and question:

I can’t find any velo code that will add/create a new field for an item in a database.

Can anyone help me with this? Did i miss something in the velo api references?

Cheers.

@russian-dima @anthony any thoughts?

Perhaps the solution would be to have two separate CMS collections:

A “conversations” database for what I described above
and
A “messages” database where each item is one message, and is referred to the correct “conversations” item

Could work, but normaly you need just one collection.
The DB-Fields will be generated automatically if you change the TITLE of the FIELD.
The only problem will be, to determine the type of the field.

1 Like

Cool, thanks!

So the code to add new fields to an item would be something like this? :

onMessageSubmitted → add to field_id: "message1" UNLESS "message1" already has a value, then add to field_id: "message2", UNLESS "message2" already has a value etc. etc.

Obviously the code has to be structured such that I shouldn’t have to write endless code, haha

Any ideas?

@russian-dima maybe the field_id could be = the whole textmessage itself? Then it’s easier to make sure that it’s a NEW field every time

Or is there a limit on the string length of the field_id ?

What do you think of this code? ChatGPT suggestion. Looks like it has some flaws.

import wixData from 'wix-data';

$w.onReady(function () {
    // Add an onClick event handler to the submit button
    $w("#submitButton").onClick(submitMessage);
});

async function submitMessage() {
    const messageText = $w("#messageTextBox").value.trim(); // Get the message text from the text box and trim leading/trailing spaces

    // Loop through possible message fields
    for (let i = 1; ; i++) {
        const fieldName = "message" + i;
        const existingItem = await wixData.query("YourCollectionName")
            .eq(fieldName, "") // Check if the current message field is empty
            .find();

        // If no existing item with empty field found, continue to the next field
        if (existingItem.items.length === 0) {
            continue;
        }

        // If an empty field is found, update the field with the message
        const newItem = {
            [fieldName]: messageText
        };
        await wixData.update("YourCollectionName", newItem); // Update the message field with the new text
        break; // Exit the loop
    }

    // Clear the text box after submission
    $w("#messageTextBox").value = "";
}

Before i continue to give you advices, first mention all the fields and their corresponding field-types, you want to be inside of your database ?

DATABASE = ‘conversations’

Field-1: —> user-ID ? —> Type: —> STRING
Field-2: —> message ? —> Type: —> STRING
Field-3: —> ? —> Type: —> ???
Field-4: —> ? —> Type: —> ???
Field-5: —> ? —> Type: —> ???
Field-6: —> ? —> Type: —> ???

Everything depends on the structure of your database.
What will be inside your database?

Every → MESSAGE ← should be a → ROW ← inside of your database, not a column.

1 Like

Yes, the idea is this:

Field 1, 2, 3, maybe four, will have some user id’s, maybe e-mail of the two people in the conversation. That’s not my main question, because I have ideas for that.

Main question:

The rest of the fields will be message fields.

Field 5: First message
Field 6: Second message
Field 7: Third message
Field 8: Fourth message
Field 9: Fifth message
… continuing as long as possible !

So – I’m trying to write some code where → USER submits message → Add message to [field 5] IF [field 5] is EMPTY → ELSE add the message to [field 6] IF [field 6] is EMPTY → ELSE add the message to [field 7] IF [field 7] is empty → ELSE … (you get the idea) … continuing indefinitely.

So I’m trying to create a LOOP with the code, so that I don’t have to write 10,000 lines of code mentioning 10,000 possible fields to add the message to.

Does it make sense?

Any ideas?

I’ll definitely send you a tip if this works out!

No this is surely not what you want!!!

Better way → using either…

a) ARRAY-FIELDS
…or even better, using…
b) OBJECT-FIELDS

This way you can store for each of ROW (LINE) all messages related to a USER.

You surely don’t want to create an infinite DATABASE creating infinite COLUMNS!!!

Your issue has a lot of similarity to the following…

At the end you also can create a second database → for MESSAGES <— and include them in your QUERY…

https://www.wix.com/velo/reference/wix-data/wixdataquery/include