How to create Unique field on a database?

Hey there!
I’ve been asking about this question for days yet i couldn’t get any answer.
On one of the previous forum posts ( this post exactly) I saw this answer for creating a unique field.

 import wixData from 'wix-data'; 
 export async function test_beforeInsert(item, context) { 
        const {items} = await wixData.query(context.collectionName).eq("uniqueField", item.uniqueField).find() 
        
        return !items.length ? item : Promise.reject() 
 } 

yet i couldn’t get it work for my setup… how should i be writing this down?
Database: droptest
Dataset: dynamicDataset
input: “#input1
Field: username

I want my “username” field to be unique and it’s being filled by “#input1


I tried over and over and over again
yet i couldn’t find any way to
put this down. I obviously can’t do it
on my own… Need help here :slight_smile:

Could someone please show me how?
Thank you!

let me explain this code snippet a bit.

import wixData from 'wix-data'; 

//this is a backend data hook.
//it is called before an item is inserted to a collection.
//read about data hooks here: 
//https://support.wix.com/en/article/how-to-use-data-hooks

//this hook is called *before* an *insert* to the *test* collection.
//hence its name is: "test_beforeInsert"
export async function test_beforeInsert(item, context) 
{ 
    //build a query that looks for items with
    //the same value in the field "uniqueField" 
    //as the item we want to insert: 
    const {items} = await wixData.query(context.collectionName)
        .eq("uniqueField", item.uniqueField).find() 
    
    //if the result has non-zero length, just return the item as is.
    //this will make the insert operation continue and insert the item to the collection,
    //otherwise, reject the promise, thus the insert will fail
    return !items.length ? item : Promise.reject()
}

note that the same logic can be implemented like this, a bit clearer:

  //note you can write the logic as follows, a bit clearer: 
  export async function test_beforeInsert(item, context) 
  { 
    //build a query that counts items with the same value in the field 
    //"uniqueField" as the item we want to insert:
    let valueToTest = item.uniqueField;
    const numOfItems = await wixData.query("test")
      .eq("uniqueField", valueToTest).count();
    
   //if no items found, we can continue on with the insert operation 
   if (numOfItems === 0) return item; 
   
   //otherwise, reject the promise     
   Promise.reject("an item with value " + item.uniqueField          
   + " in the field 'uniqueField' already found in the collection" ); 
 } 

hope this helps!

Hey Ziv i’m still in the middle of nowhere. Still don’t know how and where to put this :slight_smile:

Mert, have you managed to make it work?

yes i have. thank you

I have created/enabled afterSave hook in my database collection. I added only console.log to see if the function is being called or not. But the hooks are not calling at all.

I didnt do any imports in data.js and I set the mode to “write-only” in the form page dataset

Mert how did you fix this, I am in exactly the same position and going crazy! thanks in advance