Auto-generate, auto-increment primary key

I wrote the following hook (the code should be placed to data.js):

export async function MyTable_beforeInsert(item, context) {  
       var max = await getMax('MyTable', 'myField');
       if(max > 0){
              item.myField = max + 1;       
       }else{
              var count = await getCount('MyTable');
              item.myField = count + 1;
       }
       return item;
}

async function getCount(tableName){
       return await wixData.query(tableName).count();
}

async function getMax(tableName, fieldName) {
	var max = 0;
	var res = await wixData.aggregate(tableName).max(fieldName, 'fieldMax').run();
	if(res.items.length === 1){ max = res.items[0].fieldMax; }
	return max;	
}