Back

[SOLVED] creating unique attribute values by range-integer

  • 0
  • Databases
  • Functions
ireneus
9 Mar, 2023, 15:57

i dont have a specific reference to this, for example i have a unique attribute in a collection of type integer and i want to create that attribute based on a range lets say 250-500 if was to do query.lessthanequal 500 query.greaterthanequal 250 i would have to list all the documents just to check if that integer is occupied so i can create one in that range am i right? i think thats a heavy approach i may be missing something here

TL;DR
The user wants to create unique attribute values within a specific range in their database. Another user suggests creating a separate collection called Flags to store the last inserted ID. The code is shown to fetch the last ID, check if it exceeds the maximum ID, and create a new user. The user is confused about how the flag collection works and how to query for ascending documents. The suggestion is made to create and get an error if the ID is already occupied. However, the user finds this approach heavy and wants a more efficient solution. In conclusion, the user is looking for a more efficient way to create unique attribute values within a range without having
Binyamin
9 Mar, 2023, 16:02

You can just create and get error if the ID is occupied

ireneus
9 Mar, 2023, 16:07

ive thought of thise but then i would iterate until i find one document that doesnt have that int occupied

Binyamin
9 Mar, 2023, 16:08

Yes that sounds as a bummer I don't think you have a way to do it in AppWrite database engine What I would suggest is that you create a flag collection with the last ID as integer and go from there

ireneus
9 Mar, 2023, 16:11

can you explain how a flag collection works? is it just that integer as doc id and then how would i query for the ascending doc in a collection?

Binyamin
9 Mar, 2023, 17:28

It's just a logic offer What I mean Let's say you have Database Website Collection Users when each user id can be only between 250-500 Then you create a second collection named Flags with the attributes name, value and you add a document with the balues name last_inserted_id value 250

Then in your code you first fetch this data like so

TypeScript
const MAX_USERS_ID = env['MAX_USERS_ID'] ?? 500;

const lastID = getLastID() // In this function you will connect to database and get the value 

if(lastID >= MAX_USERS_ID){
  throw "We don't accept any new users"
}

if(createNewUser(++lastID)) {
  updateLastID(lastID);
}
ireneus
9 Mar, 2023, 17:35

i think this is a really good approach thank you for sharing

ireneus
9 Mar, 2023, 17:36

[SOLVED] creating unique attribute values by range-integer

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more