mongoose-slug-updater
Advanced tools
Comparing version 3.0.1 to 3.0.2
@@ -8,3 +8,3 @@ 'use strict'; | ||
module.exports = function(schema, options) { | ||
module.exports = function plugin(schema, options) { | ||
options = _.merge( | ||
@@ -107,2 +107,3 @@ { | ||
} else { | ||
console.warn(`slug option expected to be string or array. ${typeof opts.slug} found and will be ignored.`); | ||
return; | ||
@@ -130,5 +131,2 @@ } | ||
} | ||
if (opts.uniqueNestedSlug) { | ||
slug.uniqueNested = opts.uniqueNestedSlug; | ||
} | ||
if (opts.forceIdSlug) { | ||
@@ -178,5 +176,5 @@ slug.unique = true; | ||
let updFields = _.keysDeep(doc); | ||
let updPaths = _.map(updFields, f => | ||
_.trim(f.replace(/\["?(.+?)"?\]/g, '.$1'), '.') | ||
); | ||
// let updPaths = _.map(updFields, f => | ||
// _.trim(f.replace(/\["?(.+?)"?\]/g, '.$1'), '.') | ||
// ); | ||
let involvedPaths = _.uniq( | ||
@@ -281,2 +279,3 @@ unwoundSlugs.reduce( | ||
.mapKeys((v, f) => _.trim(f.replace(/\["?(.+?)"?\]/g, '.$1'), '.')) | ||
.omitBy(v=>v===undefined) | ||
.value(); | ||
@@ -289,6 +288,7 @@ doUpd = { $set: slugPaths }; | ||
} else { | ||
if (!docs[i]._id) | ||
throw new Error( | ||
'Cannot update slug massively, because _id not found for each doc.' | ||
); | ||
// Mongoose doesn't support docs without _id | ||
// if (!docs[i]._id) | ||
// throw new Error( | ||
// 'Cannot update slug massively, because _id not found for each doc.' | ||
// ); | ||
if (doUpd) { | ||
@@ -299,2 +299,3 @@ await me.model.updateOne({ _id: docs[i]._id }, doUpd); | ||
} | ||
// console.log(me.getUpdate()); | ||
} | ||
@@ -350,3 +351,3 @@ schema.pre('save', async function() { | ||
findOne, | ||
slugsMdfPaths | ||
//slugsMdfPaths | ||
) { | ||
@@ -370,3 +371,3 @@ // console.log(`findSame#${doc.n} ${path}: ${slug}`); | ||
let key = localKey + '-' + JSON.stringify(group); | ||
let candidates = []; | ||
// let candidates = []; | ||
if (isArrayPath) { | ||
@@ -395,21 +396,21 @@ // console.log('array? maskedPath', maskedPath); | ||
} | ||
if (cache.unwind[maskedPath] === undefined) { | ||
cache.unwind[maskedPath] = { | ||
list: _(unwindPath(doc, maskedPath)) | ||
.map(p => [p, docGet(doc, p)]) | ||
.filter(([p, v]) => v !== undefined && slugsMdfPaths[p] === undefined) | ||
.sortBy('[1]') | ||
.value(), | ||
}; | ||
} | ||
let top = _(cache.unwind[maskedPath].list) | ||
.filter(([p, v]) => p != path && v.match(slugRx)) | ||
.last(); | ||
if (top) { | ||
// console.log(`unwind cache for ${path}: ${top[1]}`); | ||
candidates.push(top[1]); | ||
} else { | ||
// console.log(`no unwind cache for ${path}: ${cache.unwind[maskedPath].list}`); | ||
} | ||
// if (cache.unwind[maskedPath] === undefined) { | ||
// cache.unwind[maskedPath] = { | ||
// list: _(unwindPath(doc, maskedPath)) | ||
// .map(p => [p, docGet(doc, p)]) | ||
// .filter(([p, v]) => v !== undefined && slugsMdfPaths[p] === undefined) | ||
// .sortBy('[1]') | ||
// .value(), | ||
// }; | ||
// } | ||
// | ||
// let top = _(cache.unwind[maskedPath].list) | ||
// .filter(([p, v]) => p != path && v.match(slugRx)) | ||
// .last(); | ||
// if (top) { | ||
// // console.log(`unwind cache for ${path}: ${top[1]}`); | ||
// candidates.push(top[1]); | ||
// } else { | ||
// console.log(`no unwind cache for ${path}: `,cache.unwind[maskedPath].list); | ||
// } | ||
} | ||
@@ -449,4 +450,5 @@ let query = _.cloneDeep(group); | ||
.last(); | ||
if (foundDoc) cache.db[key] = foundDoc[1]; | ||
else cache.db[key] = null; | ||
cache.db[key] = foundDoc[1]; | ||
// if (foundDoc) cache.db[key] = foundDoc[1]; | ||
// else cache.db[key] = null; | ||
// console.log('db cache set', path, cache.db[key], query, sort, group); | ||
@@ -456,12 +458,14 @@ } | ||
} | ||
if (cache.db[key] !== undefined) { | ||
return cache.db[key]; | ||
// if (cache.db[key] !== undefined) { | ||
// console.log(`db cache for ${path}: ${cache.db[key]}`); | ||
candidates.push(cache.db[key]); | ||
} | ||
// candidates.push(cache.db[key]); | ||
// } | ||
let res = _(candidates.sort()) | ||
.filter(c => c !== null) | ||
.last(); | ||
// console.log(`${path}: ${res} chosen from `,candidates); | ||
return res; | ||
// let res = _(candidates.sort()) | ||
// .filter(c => c !== null) | ||
// .last(); | ||
// // console.log(`${path}: ${res} chosen from `,candidates); | ||
// return res; | ||
// return null; | ||
} | ||
@@ -565,6 +569,6 @@ let docGet = (doc, path) => | ||
async function setSlugs(doc, slugs, options, findOne, cache) { | ||
let slugsMdfPaths = _(slugs) | ||
.map('path') | ||
.zipObject(_.fill(Array(_.size(slugs)), '')) | ||
.value(); | ||
// let slugsMdfPaths = _(slugs) | ||
// .map('path') | ||
// .zipObject(_.fill(Array(_.size(slugs)), '')) | ||
// .value(); | ||
// console.log("slugsMdfPaths",slugsMdfPaths); | ||
@@ -595,3 +599,3 @@ await Promise.all( | ||
} | ||
if (!doc[slug.path] || !(slug.permanent && doc[slug.path])) { | ||
if (!docGet(doc,slug.path) || !slug.permanent) { | ||
if (slug.isShortIdMode) { | ||
@@ -610,3 +614,3 @@ docSet( | ||
cache, | ||
slugsMdfPaths | ||
//slugsMdfPaths | ||
) | ||
@@ -627,3 +631,3 @@ ); | ||
cache, | ||
slugsMdfPaths | ||
//slugsMdfPaths | ||
) | ||
@@ -664,3 +668,3 @@ ); | ||
cache, | ||
slugsMdfPaths | ||
//slugsMdfPaths | ||
) { | ||
@@ -680,3 +684,3 @@ let slug = makeSlug(values, options); | ||
findOne, | ||
slugsMdfPaths | ||
//slugsMdfPaths | ||
); | ||
@@ -701,3 +705,3 @@ if (result) { | ||
cache, | ||
slugsMdfPaths | ||
//slugsMdfPaths | ||
) { | ||
@@ -716,3 +720,3 @@ let slug = makeSlug(values, options); | ||
findOne, | ||
slugsMdfPaths | ||
//slugsMdfPaths | ||
); | ||
@@ -719,0 +723,0 @@ if (result) { |
{ | ||
"name": "mongoose-slug-updater", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "Schema-based slug plugin for Mongoose - single/compound - unique over collection/group - nested docs/arrays - relative/abs paths - sync on change: create/save/update/updateOne/updateMany/findOneAndUpdate tracked - $set operator - counter/shortId", | ||
@@ -12,2 +12,3 @@ "main": "index.js", | ||
"test": "nyc mocha --reporter spec", | ||
"report": "nyc --reporter=html --reporter=text mocha", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls", | ||
@@ -14,0 +15,0 @@ "dev": "nodemon ./node_modules/.bin/mocha --reporter min --no-deprecation", |
@@ -45,2 +45,5 @@ 'use strict'; | ||
}, | ||
{ | ||
title: tellme.getText(3), | ||
}, | ||
], | ||
@@ -60,2 +63,51 @@ }, | ||
.and.equal(tellme.getSlug(2)); | ||
doc.should.have.nested | ||
.property(`children[1].title`) | ||
.and.equal(tellme.getText(3)); | ||
doc.should.have.nested | ||
.property(`children[1].slug`) | ||
.and.equal(tellme.getSlug(3)); | ||
await SimpleInline.updateOne( | ||
{_id:doc._id}, | ||
{ | ||
$set:{ | ||
'children.1.title': tellme.getText(4), | ||
} | ||
} | ||
); | ||
doc = await SimpleInline.findById(doc._id); | ||
// console.log(doc); | ||
doc.should.have.property('slug').and.equal(tellme.getSlug(0)); | ||
doc.should.have.nested.property('child.title').and.equal(tellme.getText(1)); | ||
doc.should.have.nested.property('child.slug').and.equal(tellme.getSlug(1)); | ||
doc.should.have.nested | ||
.property(`children[0].title`) | ||
.and.equal(tellme.getText(2)); | ||
doc.should.have.nested | ||
.property(`children[0].slug`) | ||
.and.equal(tellme.getSlug(2)); | ||
doc.should.have.nested | ||
.property(`children[1].title`) | ||
.and.equal(tellme.getText(4)); | ||
doc.should.have.nested | ||
.property(`children[1].slug`) | ||
.and.equal(tellme.getSlug(4)); | ||
doc.children[1].title=tellme.getText(5); | ||
await doc.save(); | ||
doc.should.have.property('slug').and.equal(tellme.getSlug(0)); | ||
doc.should.have.nested.property('child.title').and.equal(tellme.getText(1)); | ||
doc.should.have.nested.property('child.slug').and.equal(tellme.getSlug(1)); | ||
doc.should.have.nested | ||
.property(`children[0].title`) | ||
.and.equal(tellme.getText(2)); | ||
doc.should.have.nested | ||
.property(`children[0].slug`) | ||
.and.equal(tellme.getSlug(2)); | ||
doc.should.have.nested | ||
.property(`children[1].title`) | ||
.and.equal(tellme.getText(5)); | ||
doc.should.have.nested | ||
.property(`children[1].slug`) | ||
.and.equal(tellme.getSlug(5)); | ||
}); | ||
@@ -62,0 +114,0 @@ |
@@ -7,2 +7,3 @@ 'use strict'; | ||
truncate: 120, | ||
backwardCompatible:false | ||
}, | ||
@@ -9,0 +10,0 @@ nIterations: 15, |
436307
51
5842