Data Management

Custom Slugs with Incremental Values

Summary

Hey there! In this blog post, we'll dive into the process of creating custom slugs in Xano, a no-code platform that empowers you to build and deploy backend services without writing a single line of code. Whether you're a no-code enthusiast, a citizen developer, a seasoned programmer, or part of a startup or small business, this guide will help you streamline the process of generating unique, SEO-friendly slugs for your application's records.

So, let's get started!

Step 1: Set Up the Database Table

First things first, we need to create a database table to store our records. For this example, we'll use a simple table called "stuff" with two fields: "name" and "description." However, we'll also need to add a text field called "slug" to store our custom slugs.

To ensure that each slug is unique, we'll add a unique index to the "slug" field. Here's how you can do that:

  1. In the Xano dashboard, navigate to the "stuff" table.
  2. Click on "Indexes" and then "Create Index."
  3. Select "Unique" and choose the "slug" field.

This unique index will enforce that no two slugs can be the same, which is exactly what we want.

Step 2: Create an API Endpoint

Next, we'll create an API endpoint to handle the slug generation process. This endpoint will be responsible for creating a new record and generating a unique slug for it.

  1. In the Xano dashboard, navigate to the "API" section and create a new endpoint (e.g., "Create Stuff").
  2. Hide the "slug" field from the input, as we don't want users to input the slug manually.

Step 3: Implement Slug Generation Logic

Now, it's time to implement the logic for generating custom slugs. We'll use Xano's powerful Function Stack to achieve this.

  1. In the Function Stack, start by checking if a record with the same name already exists in the database. This will determine whether we need to create a fresh slug (e.g., "baseball-1") or append an incremental value to the existing slug (e.g., "baseball-2").

// Query all records from the "stuff" table where the name matches the input name let existingRecords = await DATA.query("stuff", { where: { name: "={{inputs.name}}" }, order: [{ field: "createdAt", direction: "desc" }], limit: 1 }); // Store the most recent record (if any) in a variable let existingRecord = existingRecords[0];

  1. Next, add conditional logic to handle the two cases: creating a new slug or incrementing an existing one.

// If no existing record is found if (!existingRecord) { // Create a new slug by concatenating the name and the value "1" let slug = `${inputs.name}-1`; } else { // If an existing record is found, extract the incremental value from the slug let slugNumber = parseInt(existingRecord.slug.replace(`${inputs.name}-`, "")); // Increment the slug number by 1 slugNumber++; // Create a new slug by concatenating the name and the incremented value let slug = `${inputs.name}-${slugNumber}`; }

  1. Finally, map the generated "slug" variable to the "slug" field in the output, ensuring that the new record is created with the correct slug.

Step 4: Test and Deploy

With the slug generation logic in place, it's time to test it out!

  1. In the Xano dashboard, navigate to the "Create Stuff" endpoint you created earlier.
  2. Enter a name (e.g., "baseball") and a description (e.g., "a game you play").
  3. Click "Run" and observe the generated slug (e.g., "baseball-1").
  4. Repeat the process with the same name but a different description. You should see the incremented slug (e.g., "baseball-2").
  5. Try creating records with new names to ensure the slugs are generated correctly.

Once you're satisfied with the results, you can deploy your application and start using custom slugs in your production environment!

Wrapping Up

Congratulations! You've successfully learned how to create custom slugs in Xano, complete with incremental values for duplicate names. This functionality can be incredibly useful for generating SEO-friendly URLs, ensuring uniqueness across records, and providing a better user experience for your application.

Remember, the power of Xano lies in its ability to create custom logic without writing a single line of code. With the Function Stack, you can tackle a wide range of backend development tasks, from data manipulation to complex business logic, all through a user-friendly visual interface.

If you found this guide helpful, be sure to like and subscribe to our YouTube channel for more exciting content. Happy building!

This transcript was AI generated to allow users to quickly answer technical questions about Xano.

Was this helpful?

I found it helpful

I need more support
Sign up for XanoSign up for Xano

Build without limits on a secure, scalable backend.

Unblock your team's progress and create a backend that will scale for free.

Start building for free