async function qualifiedSave() {
  let slug = this;
  slug.baseName = slug.name; // Store base name for later reference
  if (reservedSlugs.indexOf(slug.name.toLowerCase()) !== -1)
    return await _resolveConflicts(); // saves new slug if needed

  try {
    return await slug.save();
  } catch (error) {
    if (error.name == 'DuplicatePrimaryKeyError')
      return await _resolveConflicts(); // saves new slug if needed
    else
      throw error;
  }

  /**
   * Resolves naming conflicts by creating a new slug with a numeric qualifier
   * if needed.
   *
   * @memberof ThingSlug
   * @returns {ThingSlug}
   *  the best available slug to use
   * @inner
   * @protected
   */
  async function _resolveConflicts() {
    // Check first if we've used this base name before for the same target
    let slugs = await ThingSlug
      .filter({
        baseName: slug.name,
        thingID: slug.thingID
      })
      .orderBy(r.desc('createdOn'))
      .limit(1);

    if (slugs.length)
      return slugs[0]; // Got a match, no need to save -- just re-use :)

    // Widen search for most recent use of this base name
    slugs = await ThingSlug
      .filter({
        baseName: slug.name
      })
      .orderBy(r.desc('createdOn'))
      .limit(1);

    let latestQualifierStr;
    if (slugs.length && !isNaN(+slugs[0].qualifierPart))
      latestQualifierStr = String(+slugs[0].qualifierPart + 1);
    else
      latestQualifierStr = '2';
    slug.name = `${slug.name}-${latestQualifierStr}`;
    slug.qualifierPart = latestQualifierStr;
    return await slug.save();
  }
}
/**
 * Save a slug update, adding a numeric qualifier if necessary because we
 * already have a slug pointing to a different thing.
 *
 * @returns {ThingSlug}
 *  the slug that should be associated with the {@link Thing} object.
 * @memberof ThingSlug
 * @instance
 */
Save a slug update, adding a numeric qualifier if necessary because we
already have a slug pointing to a different thing.

@returns {ThingSlug}
the slug that should be associated with the  Thing object.
Save a slug update adding a numeric qualifier if necessary because we already have a slug pointing to a different thing . @returns { ThingSlug } the slug that should be associated with the Thing object .
async function qualifiedSave() {
  let slug = this;
  slug.baseName = slug.name; 
  if (reservedSlugs.indexOf(slug.name.toLowerCase()) !== -1)
    return await _resolveConflicts(); 
  try {
    return await slug.save();
  } catch (error) {
    if (error.name == 'DuplicatePrimaryKeyError')
      return await _resolveConflicts(); 
    else
      throw error;
  }
  async function _resolveConflicts() {
    let slugs = await ThingSlug
      .filter({
        baseName: slug.name,
        thingID: slug.thingID
      })
      .orderBy(r.desc('createdOn'))
      .limit(1);
    if (slugs.length)
      return slugs[0]; 
    slugs = await ThingSlug
      .filter({
        baseName: slug.name
      })
      .orderBy(r.desc('createdOn'))
      .limit(1);
    let latestQualifierStr;
    if (slugs.length && !isNaN(+slugs[0].qualifierPart))
      latestQualifierStr = String(+slugs[0].qualifierPart + 1);
    else
      latestQualifierStr = '2';
    slug.name = `${slug.name}-${latestQualifierStr}`;
    slug.qualifierPart = latestQualifierStr;
    return await slug.save();
  }
}
async function qualifiedSave ( ) { let slug = this ; slug . baseName = slug . name ; if ( reservedSlugs . indexOf ( slug . name . toLowerCase ( ) ) !== - 1 ) return await _resolveConflicts ( ) ; try { return await slug . save ( ) ; } catch ( error ) { if ( error . name == 'DuplicatePrimaryKeyError' ) return await _resolveConflicts ( ) ; else throw error ; } async function _resolveConflicts ( ) { let slugs = await ThingSlug . filter ( { baseName : slug . name , thingID : slug . thingID } ) . orderBy ( r . desc ( 'createdOn' ) ) . limit ( 1 ) ; if ( slugs . length ) return slugs [ 0 ] ; slugs = await ThingSlug . filter ( { baseName : slug . name } ) . orderBy ( r . desc ( 'createdOn' ) ) . limit ( 1 ) ; let latestQualifierStr ; if ( slugs . length && ! isNaN ( + slugs [ 0 ] . qualifierPart ) ) latestQualifierStr = String ( + slugs [ 0 ] . qualifierPart + 1 ) ; else latestQualifierStr = '2' ; slug . name = ` ${ slug . name } ${ latestQualifierStr } ` ; slug . qualifierPart = latestQualifierStr ; return await slug . save ( ) ; } }
Save a slug update, adding a numeric qualifier if necessary because we
 already have a slug pointing to a different thing.
Save a slug update adding a numeric qualifier if necessary because we already have a slug pointing to a different thing .
// Store base name for later reference // saves new slug if needed // saves new slug if needed /**
   * Resolves naming conflicts by creating a new slug with a numeric qualifier
   * if needed.
   *
   * @memberof ThingSlug
   * @returns {ThingSlug}
   *  the best available slug to use
   * @inner
   * @protected
   */ // Check first if we've used this base name before for the same target // Got a match, no need to save -- just re-use :) // Widen search for most recent use of this base name