@mac777/project-pinecone-models 1.1.24 → 1.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/Event.js +14 -20
  2. package/package.json +1 -1
  3. package/src/Event.ts +14 -21
package/dist/Event.js CHANGED
@@ -288,33 +288,27 @@ eventSchema.pre('save', async function (next) {
288
288
  if (this.isModified('title') && !this.slug) {
289
289
  if (this.title) {
290
290
  // Convert to lowercase, replace spaces with hyphens, remove special characters
291
- let slug = this.title
291
+ let baseSlug = this.title
292
292
  .toLowerCase()
293
+ .trim()
293
294
  .replace(/[^\w\s-]/g, '') // Remove special characters except spaces and hyphens
294
295
  .replace(/\s+/g, '-') // Replace spaces with hyphens
295
296
  .replace(/-+/g, '-') // Replace multiple hyphens with single hyphen
296
- .trim();
297
- // Ensure slug is not empty and add random suffix if needed
298
- if (!slug)
299
- slug = 'event';
300
- // Add timestamp suffix to ensure uniqueness
301
- const timestamp = Date.now().toString(36);
302
- slug = `${slug}-${timestamp}`;
297
+ .replace(/^-+|-+$/g, ''); // Remove leading/trailing hyphens
298
+ // Ensure slug is not empty
299
+ if (!baseSlug)
300
+ baseSlug = 'event';
301
+ // Truncate to reasonable length (50 chars) to keep URLs clean
302
+ if (baseSlug.length > 50) {
303
+ baseSlug = baseSlug.substring(0, 50).replace(/-+$/, ''); // Remove trailing hyphen after truncation
304
+ }
305
+ // Use last 6 characters of event ID for uniqueness
306
+ // This ensures uniqueness without database queries
307
+ const idSuffix = this._id.toString().slice(-6);
308
+ const slug = `${baseSlug}-${idSuffix}`;
303
309
  this.slug = slug;
304
310
  }
305
311
  }
306
- // Ensure slug uniqueness
307
- if (this.slug) {
308
- const existingEvent = await mongoose_1.default.models.Event.findOne({
309
- slug: this.slug,
310
- _id: { $ne: this._id }
311
- });
312
- if (existingEvent) {
313
- // Add random suffix to make it unique
314
- const randomSuffix = Math.random().toString(36).substring(2, 8);
315
- this.slug = `${this.slug}-${randomSuffix}`;
316
- }
317
- }
318
312
  this.updatedAt = new Date();
319
313
  next();
320
314
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mac777/project-pinecone-models",
3
- "version": "1.1.24",
3
+ "version": "1.1.26",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "scripts": {
package/src/Event.ts CHANGED
@@ -378,35 +378,28 @@ eventSchema.pre('save', async function(next) {
378
378
  if (this.isModified('title') && !this.slug) {
379
379
  if (this.title) {
380
380
  // Convert to lowercase, replace spaces with hyphens, remove special characters
381
- let slug = this.title
381
+ let baseSlug = this.title
382
382
  .toLowerCase()
383
+ .trim()
383
384
  .replace(/[^\w\s-]/g, '') // Remove special characters except spaces and hyphens
384
385
  .replace(/\s+/g, '-') // Replace spaces with hyphens
385
386
  .replace(/-+/g, '-') // Replace multiple hyphens with single hyphen
386
- .trim();
387
+ .replace(/^-+|-+$/g, ''); // Remove leading/trailing hyphens
387
388
 
388
- // Ensure slug is not empty and add random suffix if needed
389
- if (!slug) slug = 'event';
389
+ // Ensure slug is not empty
390
+ if (!baseSlug) baseSlug = 'event';
390
391
 
391
- // Add timestamp suffix to ensure uniqueness
392
- const timestamp = Date.now().toString(36);
393
- slug = `${slug}-${timestamp}`;
392
+ // Truncate to reasonable length (50 chars) to keep URLs clean
393
+ if (baseSlug.length > 50) {
394
+ baseSlug = baseSlug.substring(0, 50).replace(/-+$/, ''); // Remove trailing hyphen after truncation
395
+ }
394
396
 
395
- this.slug = slug;
396
- }
397
- }
397
+ // Use last 6 characters of event ID for uniqueness
398
+ // This ensures uniqueness without database queries
399
+ const idSuffix = this._id.toString().slice(-6);
400
+ const slug = `${baseSlug}-${idSuffix}`;
398
401
 
399
- // Ensure slug uniqueness
400
- if (this.slug) {
401
- const existingEvent = await mongoose.models.Event.findOne({
402
- slug: this.slug,
403
- _id: { $ne: this._id }
404
- });
405
-
406
- if (existingEvent) {
407
- // Add random suffix to make it unique
408
- const randomSuffix = Math.random().toString(36).substring(2, 8);
409
- this.slug = `${this.slug}-${randomSuffix}`;
402
+ this.slug = slug;
410
403
  }
411
404
  }
412
405