New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mongoose-slug-updater

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongoose-slug-updater - npm Package Compare versions

Comparing version 2.3.0 to 2.4.0

test/model/index.js

109

lib/slug-generator.js

@@ -18,4 +18,3 @@ 'use strict';

var watcher = [],
slugs = [];
var slugs = [];

@@ -40,3 +39,3 @@ // console.log(' === schema === ');

schemaPath +
(schemaType.constructor.schemaName == 'DocumentArray' ? '.*' : ''),
(schemaType.constructor.schemaName == 'DocumentArray' ? '.$' : ''),
cb

@@ -111,25 +110,16 @@ );

}
watcher = watcher.concat(
slug.fields.filter(function(item) {
return watcher.indexOf(item) < 0;
})
);
if (slug.uniqueGroup)
watcher = watcher.concat(
slug.uniqueGroup.filter(function(item) {
return watcher.indexOf(item) < 0;
})
);
if (schemaType.options.force_id) {
slug.unique = true;
slug.isShortIdMode = true;
slug.forceShortId = true;
}
slugs.push(slug);
}
});
// console.log("watcher",watcher);
// console.log("slugs",slugs);
//
// schema.post('save', function(error, res, next) {
// console.log('post save', this);
// next();
// });
/**
* Executed before update value
*/
schema.pre('update', async function() {

@@ -156,7 +146,15 @@ return onUpdate.bind(this)('update');

// console.debug('preupdateOne before',me.getQuery(), upd);
let docs = [{ ...(upd.$set || upd) }];
// console.log("doc",docs[0]);
let doc = _.cloneDeep(upd);
if (doc.$set) {
// console.log(doc.$set);
_.forOwn(doc.$set, (v, k) => {
_.set(doc, k, v);
});
delete doc.$set;
// console.log(doc);
}
let docs = [doc];
let unwoundSlugs = unwindSlugs(
docs[0],
doc,
_.filter(slugs, `on.${operation}`),

@@ -166,10 +164,11 @@ _.get,

);
// console.log(_.filter(unwoundSlugs,['path','children.0.subChildren']));
// console.log(_.map(unwoundSlugs,'path'));
if (!unwoundSlugs.length) {
// console.log('no slugs affected', watcher);
return;
}
let updFields = _.keysDeep(docs[0]);
let updPaths= _.map(updFields,(f)=>f.replace(/\[(.+?)\]/g,'.$1'));
let updFields = _.keysDeep(doc);
let updPaths = _.map(updFields, f =>
_.trim(f.replace(/\["?(.+?)"?\]/g, '.$1'), '.')
);
// console.log(updFields);

@@ -187,3 +186,10 @@ // console.log(updPaths);

operation == 'updateOne' ? 'findOne' : 'find'
](me.getQuery(), missingFields.join(' '));
](
me.getQuery(),
_(missingFields)
.map(k => [k.replace(/\.\d+\./g, '.'), 1])
.fromPairs()
.value()
);
// console.log("missing projection",_(missingFields).map(k=>[k.replace(/\.\d+\./g,'.'),1]).fromPairs().value());
// if(!missingValuesDocs){

@@ -193,6 +199,7 @@ // console.log("cannot "+(operation == 'updateOne' ? 'findOne ' : 'find ')+' missing fields ',missingFields,me.getQuery(),missingValuesDocs);

if (missingValuesDocs) {
// console.log("missingValuesDocs",missingValuesDocs);
if (!Array.isArray(missingValuesDocs))
missingValuesDocs = [missingValuesDocs];
missingValuesDocs.forEach((missingValuesDoc, i) => {
if (i) docs[i] = { ...docs[0] };
if (i) docs[i] = { ...doc };
missingFields.forEach(function(f) {

@@ -204,6 +211,7 @@ _.set(docs[i], f, _.get(missingValuesDoc, f));

});
// console.log(docs);
}
} else {
let query = me.getQuery();
if (query._id) docs[0]._id = query._id;
if (query._id) doc._id = query._id;
else {

@@ -217,3 +225,3 @@ let missingValuesDocs = await await me.model[

missingValuesDocs.forEach((missingValuesDoc, i) => {
if (i) docs[i] = { ...docs[0] };
if (i) docs[i] = { ...doc };
if (missingValuesDoc._id) docs[i]._id = missingValuesDoc._id;

@@ -240,7 +248,18 @@ });

});
if (upd.$set) slugsUpd[i] = { $set: slugsUpd[i] };
if (upd.$set) {
// console.log(_.indexate(slugsUpd[i],{ leafsOnly: true }));
// console.log(slugsUpd[i]);
let slugPaths = _(slugsUpd[i])
.indexate({ leafsOnly: true })
// .mapValues(v=>_.isArray(v)?[]:(_.isObject(v)?{}:v))
.mapKeys((v, f) => _.trim(f.replace(/\["?(.+?)"?\]/g, '.$1'), '.'))
.value();
slugsUpd[i] = { $set: slugPaths };
// console.log('upd',slugsUpd[i]);
}
// console.log('upd',slugsUpd[i]);
if (docs.length == 1) me[operation]({}, slugsUpd[i]);
else {
if (docs.length == 1) {
me[operation]({}, slugsUpd[i]);
// console.log(me.getUpdate());
} else {
if (!docs[i]._id)

@@ -264,3 +283,2 @@ throw new Error(

}
// console.log('pre-save',watcher);
let reSlug = false;

@@ -276,3 +294,2 @@

if (!unwoundSlugs.length) {
// console.log('no slugs affected', watcher);
return;

@@ -302,3 +319,3 @@ }

// console.log(slug.path,'vs',slug.path);
if (_.endsWith(basePath, '.*')) basePath += '.';
if (_.endsWith(basePath, '.$')) basePath += '.';
slugPaths.forEach(slugPath => {

@@ -351,3 +368,3 @@ let unwoundBasePath = slugPath.replace(/\.[^\.]+$/, '');

if (restPathParts === undefined) {
restPathParts = path.split('.*.');
restPathParts = path.split('.$.');
path = restPathParts[0];

@@ -416,3 +433,4 @@ restPathParts = _.drop(restPathParts);

query,
findOne
findOne,
slug.forceShortId
)

@@ -512,3 +530,4 @@ );

query,
findOne
findOne,
forceShortId
) {

@@ -536,2 +555,6 @@ var slug = makeSlug(values, options);

} else slug += options.separator + shortId.generate();
} else {
if (forceShortId) {
slug += options.separator + shortId.generate();
}
}

@@ -538,0 +561,0 @@

{
"name": "mongoose-slug-updater",
"version": "2.3.0",
"description": "Mongoose plugin to create and update slugs and unique slugs in any field, nested too. Create, Save, Update, FindAndUpdate operations supported.",
"version": "2.4.0",
"description": "Mongoose plugin to create and update slugs and unique slugs in any field, nested too. Create, Save, Update, FindAndUpdate operations supported, including $set deep paths.",
"main": "index.js",

@@ -28,3 +28,2 @@ "directories": {

"mongoose",
"generator",
"unique",

@@ -57,3 +56,3 @@ "seo friendly url",

"dependencies": {
"deepdash": "^1.3.2",
"deepdash": "^1.5.0",
"shortid": "^2.2.14",

@@ -60,0 +59,0 @@ "speakingurl": "^7.0.0"

@@ -5,6 +5,7 @@ # mongoose-slug-updater

Operations `save`, `update`, `updateOne`, `updateMany` and `findOneAndUpdate` are supported both for creation and modifying.<br>
Nested documents and arrays supported too with relative/absolute path to any field of the document.<br>
Nested documents and arrays are supported too with relative/absolute path to any field of the document.<br>
$set deep paths to nested docs and arrays works too, but without other update operators yet.
For example you can create a slug based on a document's title and author's name: _my-post-title-slim-shady_, or unique slugs based on just the title: _my-post-title-Nyiy4wW9l_.
Update operators support and nested unique slugs coming soon.
Update operators support and unique mode for nested slugs are coming soon.

@@ -103,3 +104,3 @@ ## Installation

If _unique_ is set, the plugin searches in the mongo database, and if the slug already exists in the collection, it appends to the slug a separator (default: "-") and a random string (generated with the shortid module).
If `unique` or `unique_slug` is set, the plugin searches in the mongo database, and if the slug already exists in the collection, it appends to the slug a separator (default: "-") and a random string (generated with the shortid module).

@@ -160,2 +161,15 @@ **example random**

`force_id` option will append shortId even if no duplicates were found.<br>
This is useful for applications with high chance of concurrent modification of unique fields.<br>
Check for conflict made by plugin is not atomic with subsequent insert/update operation,<br>
so there is a possibility of external change of data in the moment between check and write.<br>
If this happened, mongo will throw unique index violation error.<br>
Chances of such case higher for counter unique mode, but with shortId this is possible too.<br>
You can just retry operation, so plugin will check collection again and regenerate correct unique slug.<br>
Or you can set `force_id` option - this will solve the problem completely, but you will pay for this by less readabilty of your slugs, because they will *always* be appended with random string.
In most cases write operations not so frequent to care about possible conflicts.
note: `force_id` option will also overwite `unique` to the `true`, and `slug_padding_size` option will be ignored.
### Unique slug within a group

@@ -436,2 +450,20 @@

```
#### Updating by deep path via $set operator
This will work too:
```js
await SimpleInline.findOneAndUpdate(
{/*some criteria*/},
{
$set: {
title: 'New root title',
'child.title': 'New nested title',
'children.2.title': 'New title for the 3d item of nested array',
},
}
);
```
All the slugs which depend on modified titles will be found and regenerated.
### Choose your own options

@@ -472,2 +504,10 @@

Nested docs and arrays support implemented for non-unique slugs.
Absolute and relative paths added.
Updating with $set operator and deep paths now works too.
All the update operators and unique nested slugs will be implemented soon.
Plugin rewritten with modern js and a lot of tests were added.

@@ -8,3 +8,3 @@ 'use strict';

const { nIterations, slug_padding_size, Counter } = require('./models');
const { nIterations, slug_padding_size, Counter } = require('./model');

@@ -31,12 +31,35 @@ const tellme = require('./tellme');

doc.should.have
.property('slug')
.and.equal(
tellme.getSlug(0,1)
);
doc.should.have
.property('uniqueSlug')
.and.equal(tellme.getSlug(0));
doc.should.have.property('slug').and.equal(tellme.getSlug(0, 1));
doc.should.have.property('uniqueSlug').and.equal(tellme.getSlug(0));
});
// it('Create several new resources concurrently', async () => {
// return Promise.all([
// Counter.create({
// title: tellme.getText(0),
// subtitle: tellme.getText(1),
// }),
// Counter.create({
// title: tellme.getText(0),
// subtitle: tellme.getText(1),
// }),
// Counter.create({
// title: tellme.getText(0),
// subtitle: tellme.getText(1),
// }),
// Counter.create({
// title: tellme.getText(0),
// subtitle: tellme.getText(1),
// }),
// ]).then((docs)=>{
// for (let i = 1; i <= 4; i++) {
// let doc = docs[i];
// doc.should.have.property('slug').and.equal(tellme.getSlug(0, 1));
// doc.should.have
// .property('uniqueSlug')
// .and.equal(tellme.getCounterSlug(0, i));
// }
// });
// });
it(

@@ -63,10 +86,4 @@ 'Create ' + nIterations + ' resources and check Slug and UniqueSlug',

});
doc.should.have
.property('slug')
.and.equal(
tellme.getSlug(2,3)
);
doc.should.have
.property('uniqueSlug')
.and.equal(tellme.getSlug(2));
doc.should.have.property('slug').and.equal(tellme.getSlug(2, 3));
doc.should.have.property('uniqueSlug').and.equal(tellme.getSlug(2));
});

@@ -78,10 +95,4 @@

doc.should.have.property('title', tellme.getText(4));
doc.should.have.property(
'subtitle',
tellme.getText(1)
);
doc.should.have.property(
'slug',
tellme.getSlug(4,1)
);
doc.should.have.property('subtitle', tellme.getText(1));
doc.should.have.property('slug', tellme.getSlug(4, 1));
doc.should.have.property('uniqueSlug', tellme.getSlug(4));

@@ -94,10 +105,4 @@ });

doc.should.have.property('title', tellme.getText(4));
doc.should.have.property(
'subtitle',
tellme.getText(1)
);
doc.should.have.property(
'slug',
tellme.getSlug(4,1)
);
doc.should.have.property('subtitle', tellme.getText(1));
doc.should.have.property('slug', tellme.getSlug(4, 1));
doc.should.have.property('uniqueSlug', tellme.getSlug(4));

@@ -110,10 +115,4 @@ });

doc.should.have.property('title', tellme.getSlug(4));
doc.should.have.property(
'subtitle',
tellme.getText(1)
);
doc.should.have.property(
'slug',
tellme.getSlug(4,1)
);
doc.should.have.property('subtitle', tellme.getText(1));
doc.should.have.property('slug', tellme.getSlug(4, 1));
doc.should.have.property('uniqueSlug', tellme.getSlug(4));

@@ -135,3 +134,3 @@ });

});
doc.should.have.property('slug').and.equal(tellme.getSlug(0,1));
doc.should.have.property('slug').and.equal(tellme.getSlug(0, 1));
doc.should.have.property('uniqueSlug').and.equal(tellme.getSlug(0));

@@ -144,5 +143,5 @@ });

});
doc.should.have.property('slug').and.equal(tellme.getSlug(2,1));
doc.should.have.property('slug').and.equal(tellme.getSlug(2, 1));
doc.should.have.property('uniqueSlug').and.equal(tellme.getSlug(2));
});
});
});

@@ -9,3 +9,3 @@ const

ShortId
} = require("./models");
} = require("./model");

@@ -37,2 +37,7 @@ const tellme = require("./tellme");

resource.should.have
.property('forcedSlug')
.match(tellme.getShortRegex(1))
.and.not.equal(tellme.getSlug(1));
assert.equal(-1, uniqueSlugs.indexOf(resource.uniqueSlug));

@@ -39,0 +44,0 @@

@@ -8,3 +8,3 @@ 'use strict';

const { ShortId, Counter } = require('./../models');
const { ShortId, Counter } = require('./../model');

@@ -11,0 +11,0 @@ const tellme = require('./../tellme');

@@ -8,3 +8,3 @@ 'use strict';

const { Hook } = require('./../models');
const { Hook } = require('./../model');

@@ -11,0 +11,0 @@ const tellme = require('./../tellme');

@@ -8,3 +8,3 @@ 'use strict';

const { nIterations, ShortId, Counter } = require('./../models');
const { nIterations, ShortId, Counter } = require('./../model');

@@ -11,0 +11,0 @@ const tellme = require('./../tellme');

@@ -11,3 +11,3 @@ 'use strict';

Counter
} = require("./../models");
} = require("./../model");

@@ -34,2 +34,6 @@ const tellme = require('./../tellme');

let { _id, slug, uniqueSlug } = res;
res.should.have
.property('forcedSlug')
.match(tellme.getShortRegex(1))
.and.not.equal(tellme.getSlug(1));
let mdf = {

@@ -137,2 +141,6 @@ otherField: tellme.getText(1),

});
res.should.have
.property('forcedSlug')
.match(tellme.getShortRegex(1))
.and.not.equal(tellme.getSlug(1));
// console.debug('created doc', res);

@@ -154,2 +162,7 @@ let { _id, slug, uniqueSlug } = res;

.and.equal(tellme.getSlug(0));
editedDoc.should.have
.property('forcedSlug')
.match(tellme.getShortRegex(3))
.and.not.equal(tellme.getSlug(3));
mdf = {

@@ -168,2 +181,6 @@ subtitle: tellme.getText(4),

.and.equal(tellme.getSlug(0));
editedDoc.should.have
.property('forcedSlug')
.match(tellme.getShortRegex(4))
.and.not.equal(tellme.getSlug(4));
});

@@ -170,0 +187,0 @@

@@ -16,7 +16,7 @@ 'use strict';

SimpleParent,
} = require('./../models');
} = require('./../model');
const tellme = require('./../tellme');
describe('Inline Docs', function() {
describe('Nested Docs', function() {
beforeEach(async () => {

@@ -54,356 +54,55 @@ await Parent.remove({});

it('Save nested doc declared in extrenal schemas', async () => {
let docCfg = getNewDoc();
let docCfg = Parent.getNewDoc();
let doc = await Parent.create(docCfg);
testNewDoc(doc);
doc = changeNewDoc(doc);
Parent.testNewDoc(doc);
doc = Parent.changeNewDoc(doc);
await doc.save();
testChangedDoc(doc);
Parent.testChangedDoc(doc);
});
it('UpdateOne nested docs declared in extrenal schemas', async () => {
await Parent.updateOne({}, getNewDoc(), { upsert: true });
await Parent.updateOne({}, Parent.getNewDoc(), { upsert: true });
let doc = await Parent.findOne({});
testNewDoc(doc);
let mdf = changeNewDoc({});
Parent.testNewDoc(doc);
let mdf = Parent.changeNewDoc({});
await Parent.updateOne({ _id: doc._id }, mdf);
let editedDoc = await Parent.findById(doc._id);
testChangedDoc(editedDoc);
Parent.testChangedDoc(editedDoc);
});
it('Update nested docs declared in extrenal schemas', async () => {
await Parent.update({}, getNewDoc(), { upsert: true });
await Parent.update({}, Parent.getNewDoc(), { upsert: true });
let doc = await Parent.findOne({});
testNewDoc(doc);
let mdf = changeNewDoc({});
Parent.testNewDoc(doc);
let mdf = Parent.changeNewDoc({});
await Parent.update({ _id: doc._id }, mdf);
let editedDoc = await Parent.findById(doc._id);
testChangedDoc(editedDoc);
Parent.testChangedDoc(editedDoc);
});
it('UpdateMany nested docs declared in extrenal schemas', async () => {
await Parent.updateMany({}, getNewDoc(), { upsert: true });
await Parent.updateMany({}, Parent.getNewDoc(), { upsert: true });
let doc = await Parent.findOne({});
testNewDoc(doc);
let mdf = changeNewDoc({});
Parent.testNewDoc(doc);
let mdf = Parent.changeNewDoc({});
await Parent.updateMany({ _id: doc._id }, mdf);
let editedDoc = await Parent.findById(doc._id);
testChangedDoc(editedDoc);
Parent.testChangedDoc(editedDoc);
});
it('findOneAndUpdate nested docs declared in extrenal schemas', async () => {
let doc = await Parent.findOneAndUpdate({}, getNewDoc(), {
let doc = await Parent.findOneAndUpdate({}, Parent.getNewDoc(), {
upsert: true,
new: true,
});
testNewDoc(doc);
let mdf = changeNewDoc({});
Parent.testNewDoc(doc);
let mdf = Parent.changeNewDoc({});
let editedDoc = await Parent.findOneAndUpdate({ _id: doc._id }, mdf, {
new: true,
});
testChangedDoc(editedDoc);
Parent.testChangedDoc(editedDoc);
});
});
function getNewDoc() {
let doc = {
title: tellme.getText(0),
child: {
title: tellme.getText(1),
subChild: {
title: tellme.getText(2),
},
subChildren: [],
},
children: [],
};
for (let i = 0; i < 9; i++) {
doc.child.subChildren.push({
title: tellme.getText(i),
});
}
for (let i = 0; i < 9; i++) {
doc.children.push({
title: tellme.getText(8 - i),
subChild: {
title: tellme.getText(i),
},
subChildren: _.cloneDeep(doc.child.subChildren),
});
}
return doc;
}
function testNewDoc(doc) {
doc.should.have.property('title').and.equal(tellme.getText(0));
doc.should.have.property('slug').and.equal(tellme.getSlug(0));
doc.should.have.property('absoluteSlug').and.equal(tellme.getSlug(0));
doc.should.have.property('childSlug').and.equal(tellme.getSlug(1));
doc.should.have.property('absoluteChildSlug').and.equal(tellme.getSlug(1));
doc.should.have.property('subChildSlug').and.equal(tellme.getSlug(2));
doc.should.have.property('childrenSlug0').and.equal(tellme.getSlug(8));
doc.should.have.property('childrenSlug4').and.equal(tellme.getSlug(4));
doc.should.have.property('subChildrenSlug3').and.equal(tellme.getSlug(3));
doc.should.have.property('subChildrenSlug7').and.equal(tellme.getSlug(7));
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('child.subChildSlug')
.and.equal(tellme.getSlug(2));
doc.should.have.nested
.property('child.absoluteSlug')
.and.equal(tellme.getSlug(1));
doc.should.have.nested
.property('child.absoluteRootSlug')
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property('child.relativeParentSlug')
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property('child.subChildrenSlug2')
.and.equal(tellme.getSlug(2));
doc.should.have.nested
.property('child.subChildrenSlug3')
.and.equal(tellme.getSlug(3));
doc.should.have.nested
.property('child.subChild.title')
.and.equal(tellme.getText(2));
doc.should.have.nested
.property('child.subChild.slug')
.and.equal(tellme.getSlug(2));
doc.should.have.nested
.property('child.subChild.absoluteRootSlug')
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property('child.subChild.absoluteChildSlug')
.and.equal(tellme.getSlug(1));
doc.should.have.nested
.property('child.subChild.relativeParentSlug')
.and.equal(tellme.getSlug(1));
doc.should.have.nested
.property('child.subChild.relativeGrandParentSlug')
.and.equal(tellme.getSlug(0));
for (let i = 0; i < 9; i++) {
doc.should.have.nested
.property(`child.subChildren.${i}.title`)
.and.equal(tellme.getText(i));
doc.should.have.nested
.property(`child.subChildren.${i}.slug`)
.and.equal(tellme.getSlug(i));
doc.should.have.nested
.property(`child.subChildren.${i}.absoluteRootSlug`)
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property(`child.subChildren.${i}.absoluteChildSlug`)
.and.equal(tellme.getSlug(1));
doc.should.have.nested
.property(`child.subChildren.${i}.relativeParentSlug`)
.and.equal(tellme.getSlug(1));
doc.should.have.nested
.property(`child.subChildren.${i}.relativeGrandParentSlug`)
.and.equal(tellme.getSlug(0));
}
for (let i = 0; i < 9; i++) {
doc.should.have.nested
.property(`children.${i}.title`)
.and.equal(tellme.getText(8 - i));
doc.should.have.nested
.property(`children.${i}.slug`)
.and.equal(tellme.getSlug(8 - i));
doc.should.have.nested
.property(`children.${i}.subChildSlug`)
.and.equal(tellme.getSlug(i));
doc.should.have.nested
.property(`children.${i}.absoluteSlug`)
.and.equal(tellme.getSlug(1));
doc.should.have.nested
.property(`children.${i}.absoluteRootSlug`)
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property(`children.${i}.relativeParentSlug`)
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property(`children.${i}.subChildrenSlug2`)
.and.equal(tellme.getSlug(2));
doc.should.have.nested
.property(`children.${i}.subChildrenSlug3`)
.and.equal(tellme.getSlug(3));
for (let j = 0; j < 9; j++) {
doc.should.have.nested
.property(`children.${i}.subChildren.${j}.title`)
.and.equal(tellme.getText(j));
doc.should.have.nested
.property(`children.${i}.subChildren.${j}.slug`)
.and.equal(tellme.getSlug(j));
doc.should.have.nested
.property(`children.${i}.subChildren.${j}.absoluteRootSlug`)
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property(`children.${i}.subChildren.${j}.absoluteChildSlug`)
.and.equal(tellme.getSlug(1));
doc.should.have.nested
.property(`children.${i}.subChildren.${j}.relativeParentSlug`)
.and.equal(tellme.getSlug(8 - i));
doc.should.have.nested
.property(`children.${i}.subChildren.${j}.relativeGrandParentSlug`)
.and.equal(tellme.getSlug(0));
}
}
}
function changeNewDoc(doc) {
let changed = {
title: tellme.getText(8),
child: {
title: tellme.getText(7),
subChild: { title: tellme.getText(6) },
subChildren: [],
},
children: [],
};
for (let i = 0; i < 9; i++) {
changed.child.subChildren[i] = { title: tellme.getText(8 - i) };
}
for (let i = 0; i < 9; i++) {
changed.children[i] = { title: tellme.getText(i), subChildren: [] };
changed.children[i].subChild = { title: tellme.getText(8 - i) };
for (let j = 0; j < 9; j++) {
changed.children[i].subChildren[j] = { title: tellme.getText(8 - j) };
}
}
return _.merge(doc, changed);
}
function testChangedDoc(doc) {
doc.should.have.property('title').and.equal(tellme.getText(8));
doc.should.have.property('slug').and.equal(tellme.getSlug(8));
doc.should.have.property('absoluteSlug').and.equal(tellme.getSlug(8));
doc.should.have.property('childSlug').and.equal(tellme.getSlug(7));
doc.should.have.property('absoluteChildSlug').and.equal(tellme.getSlug(7));
doc.should.have.property('subChildSlug').and.equal(tellme.getSlug(6));
doc.should.have.property('childrenSlug0').and.equal(tellme.getSlug(0));
doc.should.have.property('childrenSlug4').and.equal(tellme.getSlug(4));
doc.should.have.property('subChildrenSlug3').and.equal(tellme.getSlug(5));
doc.should.have.property('subChildrenSlug7').and.equal(tellme.getSlug(1));
doc.should.have.nested.property('child.title').and.equal(tellme.getText(7));
doc.should.have.nested.property('child.slug').and.equal(tellme.getSlug(7));
doc.should.have.nested
.property('child.subChildSlug')
.and.equal(tellme.getSlug(6));
doc.should.have.nested
.property('child.absoluteSlug')
.and.equal(tellme.getSlug(7));
doc.should.have.nested
.property('child.absoluteRootSlug')
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property('child.relativeParentSlug')
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property('child.subChildrenSlug2')
.and.equal(tellme.getSlug(6));
doc.should.have.nested
.property('child.subChildrenSlug3')
.and.equal(tellme.getSlug(5));
doc.should.have.nested
.property('child.subChild.title')
.and.equal(tellme.getText(6));
doc.should.have.nested
.property('child.subChild.slug')
.and.equal(tellme.getSlug(6));
doc.should.have.nested
.property('child.subChild.absoluteRootSlug')
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property('child.subChild.absoluteChildSlug')
.and.equal(tellme.getSlug(7));
doc.should.have.nested
.property('child.subChild.relativeParentSlug')
.and.equal(tellme.getSlug(7));
doc.should.have.nested
.property('child.subChild.relativeGrandParentSlug')
.and.equal(tellme.getSlug(8));
for (let i = 0; i < 9; i++) {
doc.should.have.nested
.property(`child.subChildren.${i}.title`)
.and.equal(tellme.getText(8 - i));
doc.should.have.nested
.property(`child.subChildren.${i}.slug`)
.and.equal(tellme.getSlug(8 - i));
doc.should.have.nested
.property(`child.subChildren.${i}.absoluteRootSlug`)
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property(`child.subChildren.${i}.absoluteChildSlug`)
.and.equal(tellme.getSlug(7));
doc.should.have.nested
.property(`child.subChildren.${i}.relativeParentSlug`)
.and.equal(tellme.getSlug(7));
doc.should.have.nested
.property(`child.subChildren.${i}.relativeGrandParentSlug`)
.and.equal(tellme.getSlug(8));
}
for (let i = 0; i < 9; i++) {
doc.should.have.nested
.property(`children.${i}.title`)
.and.equal(tellme.getText(i));
doc.should.have.nested
.property(`children.${i}.slug`)
.and.equal(tellme.getSlug(i));
doc.should.have.nested
.property(`children.${i}.subChildSlug`)
.and.equal(tellme.getSlug(8 - i));
doc.should.have.nested
.property(`children.${i}.absoluteSlug`)
.and.equal(tellme.getSlug(7));
doc.should.have.nested
.property(`children.${i}.absoluteRootSlug`)
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property(`children.${i}.relativeParentSlug`)
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property(`children.${i}.subChildrenSlug2`)
.and.equal(tellme.getSlug(6));
doc.should.have.nested
.property(`children.${i}.subChildrenSlug3`)
.and.equal(tellme.getSlug(5));
for (let j = 0; j < 9; j++) {
doc.should.have.nested
.property(`children.${i}.subChildren.${j}.title`)
.and.equal(tellme.getText(8 - j));
doc.should.have.nested
.property(`children.${i}.subChildren.${j}.slug`)
.and.equal(tellme.getSlug(8 - j));
doc.should.have.nested
.property(`children.${i}.subChildren.${j}.absoluteRootSlug`)
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property(`children.${i}.subChildren.${j}.absoluteChildSlug`)
.and.equal(tellme.getSlug(7));
doc.should.have.nested
.property(`children.${i}.subChildren.${j}.relativeParentSlug`)
.and.equal(tellme.getSlug(i));
doc.should.have.nested
.property(`children.${i}.subChildren.${j}.relativeGrandParentSlug`)
.and.equal(tellme.getSlug(8));
}
}
}

@@ -8,3 +8,3 @@ 'use strict';

const { nIterations, Inline, SimpleInline } = require('./../models');
const { nIterations, Inline, SimpleInline } = require('./../model');

@@ -26,7 +26,7 @@ const tellme = require('./../tellme');

it('Save nested docs declared inline', async () => {
let doc = await Inline.create(getNewDoc());
testNewDoc(doc);
changeDoc(doc);
let doc = await Inline.create(Inline.getNewDoc());
Inline.testNewDoc(doc);
Inline.changeDoc(doc);
doc = await doc.save();
testChangedDoc(doc);
Inline.testChangedDoc(doc);
});

@@ -115,459 +115,38 @@

it('UpdateOne nested docs declared inline', async () => {
await Inline.updateOne({},getNewDoc(),{upsert:true});
await Inline.updateOne({},Inline.getNewDoc(),{upsert:true});
let doc = await Inline.findOne({});
testNewDoc(doc);
let mdf = changeDoc({});
Inline.testNewDoc(doc);
let mdf = Inline.changeDoc({});
await Inline.updateOne({ _id:doc._id }, mdf);
let editedDoc = await Inline.findById(doc._id);
testChangedDoc(editedDoc);
Inline.testChangedDoc(editedDoc);
});
it('Update nested docs declared inline', async () => {
await Inline.update({},getNewDoc(),{upsert:true});
await Inline.update({},Inline.getNewDoc(),{upsert:true});
let doc = await Inline.findOne({});
testNewDoc(doc);
let mdf = changeDoc({});
Inline.testNewDoc(doc);
let mdf = Inline.changeDoc({});
await Inline.update({ _id:doc._id }, mdf);
let editedDoc = await Inline.findById(doc._id);
testChangedDoc(editedDoc);
Inline.testChangedDoc(editedDoc);
});
it('UpdateMany nested docs declared inline', async () => {
await Inline.updateMany({},getNewDoc(),{upsert:true});
await Inline.updateMany({},Inline.getNewDoc(),{upsert:true});
let doc = await Inline.findOne({});
testNewDoc(doc);
let mdf = changeDoc({});
Inline.testNewDoc(doc);
let mdf = Inline.changeDoc({});
await Inline.updateMany({ _id:doc._id }, mdf);
let editedDoc = await Inline.findById(doc._id);
testChangedDoc(editedDoc);
Inline.testChangedDoc(editedDoc);
});
it('findOneAndUpdate nested docs declared inline', async () => {
let doc = await Inline.findOneAndUpdate({},getNewDoc(),{upsert:true,new:true});
testNewDoc(doc);
let mdf = changeDoc({});
let doc = await Inline.findOneAndUpdate({},Inline.getNewDoc(),{upsert:true,new:true});
Inline.testNewDoc(doc);
let mdf = Inline.changeDoc({});
let editedDoc = await Inline.findOneAndUpdate({ _id:doc._id }, mdf,{new:true});
testChangedDoc(editedDoc);
Inline.testChangedDoc(editedDoc);
});
});
function getNewDoc() {
return {
title: tellme.getText(0),
child: {
title: tellme.getText(1),
subChild: {
title: tellme.getText(7),
},
},
children: [
{
title: tellme.getText(0),
subChild: {
title: tellme.getText(8),
},
subChildren: [
{
title: tellme.getText(0),
subChild: {
title: tellme.getText(8),
},
},
{
title: tellme.getText(1),
subChild: {
title: tellme.getText(7),
},
},
{
title: tellme.getText(2),
subChild: {
title: tellme.getText(6),
},
},
{
title: tellme.getText(3),
subChild: {
title: tellme.getText(5),
},
},
{
title: tellme.getText(4),
subChild: {
title: tellme.getText(4),
},
},
{
title: tellme.getText(5),
subChild: {
title: tellme.getText(3),
},
},
{
title: tellme.getText(6),
subChild: {
title: tellme.getText(2),
},
},
{
title: tellme.getText(7),
subChild: {
title: tellme.getText(1),
},
},
{
title: tellme.getText(8),
subChild: {
title: tellme.getText(0),
},
},
],
},
{
title: tellme.getText(1),
subChild: {
title: tellme.getText(7),
},
},
{
title: tellme.getText(2),
subChild: {
title: tellme.getText(6),
},
},
{
title: tellme.getText(3),
subChild: {
title: tellme.getText(5),
},
},
{
title: tellme.getText(4),
subChild: {
title: tellme.getText(4),
},
},
{
title: tellme.getText(5),
subChild: {
title: tellme.getText(3),
},
},
{
title: tellme.getText(6),
subChild: {
title: tellme.getText(2),
},
},
{
title: tellme.getText(7),
subChild: {
title: tellme.getText(1),
},
},
{
title: tellme.getText(8),
subChild: {
title: tellme.getText(0),
},
},
],
};
}
function testNewDoc(doc) {
doc.should.have.property('title').and.equal(tellme.getText(0));
doc.should.have.property('slug').and.equal(tellme.getSlug(0));
doc.should.have.property('absoluteSlug').and.equal(tellme.getSlug(0));
doc.should.have.property('childSlug').and.equal(tellme.getSlug(1));
doc.should.have.property('absoluteChildSlug').and.equal(tellme.getSlug(1));
doc.should.have.property('subChildSlug').and.equal(tellme.getSlug(7));
doc.should.have.property('childrenSlug0').and.equal(tellme.getSlug(0));
doc.should.have.property('childrenSlug4').and.equal(tellme.getSlug(4));
doc.should.have.property('subChildrenSlug3').and.equal(tellme.getSlug(3));
doc.should.have.property('subChildrenSlug7').and.equal(tellme.getSlug(7));
doc.should.have
.property('subChildrenSlug5SubChild')
.and.equal(tellme.getSlug(3));
doc.should.have
.property('subChildrenSlug2SubChild')
.and.equal(tellme.getSlug(6));
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('child.absoluteSlug')
.and.equal(tellme.getSlug(1));
doc.should.have.nested
.property('child.absoluteParentSlug')
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property('child.relativeParentSlug')
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property('child.subChild.title')
.and.equal(tellme.getText(7));
doc.should.have.nested
.property('child.subChild.slug')
.and.equal(tellme.getSlug(7));
doc.should.have.nested
.property('child.subChild.absoluteParentSlug')
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property('child.subChild.relativeParentSlug')
.and.equal(tellme.getSlug(1));
doc.should.have.nested
.property('child.subChild.relativeGrandParentSlug')
.and.equal(tellme.getSlug(0));
for (let i = 0; i < 9; i++) {
doc.should.have.nested
.property(`children[${i}].title`)
.and.equal(tellme.getText(i));
doc.should.have.nested
.property(`children[${i}].slug`)
.and.equal(tellme.getSlug(i));
doc.should.have.nested
.property(`children[${i}].absoluteRootSlug`)
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property(`children[${i}].absoluteChildSlug`)
.and.equal(tellme.getSlug(1));
doc.should.have.nested
.property(`children[${i}].relativeRootSlug`)
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property(`children[${i}].absoluteSiblingSlug`)
.and.equal(tellme.getSlug(3));
doc.should.have.nested
.property(`children[${i}].relativeSiblingSlug`)
.and.equal(tellme.getSlug(4));
doc.should.have.nested
.property(`children[${i}].subChild.title`)
.and.equal(tellme.getText(8 - i));
doc.should.have.nested
.property(`children[${i}].subChild.slug`)
.and.equal(tellme.getSlug(8 - i));
doc.should.have.nested
.property(`children[${i}].subChild.absoluteParentSlug`)
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property(`children[${i}].subChild.absoluteChildSlug`)
.and.equal(tellme.getSlug(1));
doc.should.have.nested
.property(`children[${i}].subChild.relativeParentSlug`)
.and.equal(tellme.getSlug(i));
doc.should.have.nested
.property(`children[${i}].subChild.relativeGrandParentSlug`)
.and.equal(tellme.getSlug(0));
}
for (let i = 0; i < 9; i++) {
doc.should.have.nested
.property(`children[0].subChildren[${i}].title`)
.and.equal(tellme.getText(i));
doc.should.have.nested
.property(`children[0].subChildren[${i}].slug`)
.and.equal(tellme.getSlug(i));
doc.should.have.nested
.property(`children[0].subChildren[${i}].absoluteRootSlug`)
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property(`children[0].subChildren[${i}].absoluteChildSlug`)
.and.equal(tellme.getSlug(1));
doc.should.have.nested
.property(`children[0].subChildren[${i}].relativeRootSlug`)
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property(`children[0].subChildren[${i}].absoluteSiblingSlug`)
.and.equal(tellme.getSlug(5));
doc.should.have.nested
.property(`children[0].subChildren[${i}].relativeSiblingSlug`)
.and.equal(tellme.getSlug(6));
doc.should.have.nested
.property(`children[0].subChildren[${i}].subChild.title`)
.and.equal(tellme.getText(8 - i));
doc.should.have.nested
.property(`children[0].subChildren[${i}].subChild.slug`)
.and.equal(tellme.getSlug(8 - i));
doc.should.have.nested
.property(`children[0].subChildren[${i}].subChild.absoluteParentSlug`)
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property(`children[0].subChildren[${i}].subChild.absoluteChildSlug`)
.and.equal(tellme.getSlug(1));
doc.should.have.nested
.property(`children[0].subChildren[${i}].subChild.relativeParentSlug`)
.and.equal(tellme.getSlug(i));
doc.should.have.nested
.property(`children[${i}].subChild.relativeGrandParentSlug`)
.and.equal(tellme.getSlug(0));
}
}
function changeDoc(doc) {
if(!doc.child)doc.child={};
if(!doc.child.subChild)doc.child.subChild={};
if(!doc.children)doc.children=[];
if(!doc.children[0])doc.children[0]={};
if(!doc.children[3])doc.children[3]={};
if(!doc.children[4])doc.children[4]={};
if(!doc.children[0].subChildren)doc.children[0].subChildren=[];
if(!doc.children[0].subChildren[2])doc.children[0].subChildren[2]={};
if(!doc.children[0].subChildren[2].subChild)doc.children[0].subChildren[2].subChild={};
if(!doc.children[0].subChildren[3])doc.children[0].subChildren[3]={};
if(!doc.children[0].subChildren[5])doc.children[0].subChildren[5]={};
if(!doc.children[0].subChildren[5].subChild)doc.children[0].subChildren[5].subChild={};
if(!doc.children[0].subChildren[6])doc.children[0].subChildren[6]={};
if(!doc.children[0].subChildren[7])doc.children[0].subChildren[7]={};
if(!doc.children[0].subChildren[8])doc.children[0].subChildren[8]={};
if(!doc.children[0].subChildren[8].subChild)doc.children[0].subChildren[8].subChild={};
if(!doc.children[7])doc.children[7]={};
if(!doc.children[7].subChildren)doc.children[7].subChildren=[];
if(!doc.children[7].subChild)doc.children[7].subChild={};
doc.title = tellme.getText(8);
doc.child.title = tellme.getText(7);
doc.child.subChild.title = tellme.getText(6);
doc.children[0].title = tellme.getText(4);
doc.children[3].title = tellme.getText(7);
doc.children[4].title = tellme.getText(0);
doc.children[0].subChildren[3].title = tellme.getText(8);
doc.children[0].subChildren[7].title = tellme.getText(2);
doc.children[0].subChildren[5].title = tellme.getText(4);
doc.children[0].subChildren[6].title = tellme.getText(1);
doc.children[0].subChildren[5].subChild.title = tellme.getText(2);
doc.children[0].subChildren[2].subChild.title = tellme.getText(4);
doc.children[7].subChild.title = tellme.getText(3);
return doc;
}
function testChangedDoc(doc) {
doc.should.have.property('title').and.equal(tellme.getText(8));
doc.should.have.property('slug').and.equal(tellme.getSlug(8));
doc.should.have.property('absoluteSlug').and.equal(tellme.getSlug(8));
doc.should.have.property('childSlug').and.equal(tellme.getSlug(7));
doc.should.have.property('absoluteChildSlug').and.equal(tellme.getSlug(7));
doc.should.have.property('subChildSlug').and.equal(tellme.getSlug(6));
doc.should.have.nested
.property(`children[0].title`)
.and.equal(tellme.getText(4));
doc.should.have.property('childrenSlug0').and.equal(tellme.getSlug(4));
doc.should.have.property('childrenSlug4').and.equal(tellme.getSlug(0));
doc.should.have.property('subChildrenSlug3').and.equal(tellme.getSlug(8));
doc.should.have.property('subChildrenSlug7').and.equal(tellme.getSlug(2));
doc.should.have
.property('subChildrenSlug5SubChild')
.and.equal(tellme.getSlug(2));
doc.should.have
.property('subChildrenSlug2SubChild')
.and.equal(tellme.getSlug(4));
doc.should.have.nested.property('child.title').and.equal(tellme.getText(7));
doc.should.have.nested.property('child.slug').and.equal(tellme.getSlug(7));
doc.should.have.nested
.property('child.absoluteSlug')
.and.equal(tellme.getSlug(7));
doc.should.have.nested
.property('child.absoluteParentSlug')
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property('child.relativeParentSlug')
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property('child.subChild.title')
.and.equal(tellme.getText(6));
doc.should.have.nested
.property('child.subChild.slug')
.and.equal(tellme.getSlug(6));
doc.should.have.nested
.property('child.subChild.absoluteParentSlug')
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property('child.subChild.relativeParentSlug')
.and.equal(tellme.getSlug(7));
doc.should.have.nested
.property('child.subChild.relativeGrandParentSlug')
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property(`children[0].slug`)
.and.equal(tellme.getSlug(4));
doc.should.have.nested
.property(`children[0].absoluteRootSlug`)
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property(`children[0].absoluteChildSlug`)
.and.equal(tellme.getSlug(7));
doc.should.have.nested
.property(`children[0].relativeRootSlug`)
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property(`children[0].absoluteSiblingSlug`)
.and.equal(tellme.getSlug(7));
doc.should.have.nested
.property(`children[0].relativeSiblingSlug`)
.and.equal(tellme.getSlug(0));
doc.should.have.nested
.property(`children[7].subChild.title`)
.and.equal(tellme.getText(3));
doc.should.have.nested
.property(`children[7].subChild.slug`)
.and.equal(tellme.getSlug(3));
doc.should.have.nested
.property(`children[0].subChild.absoluteParentSlug`)
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property(`children[0].subChild.absoluteChildSlug`)
.and.equal(tellme.getSlug(7));
doc.should.have.nested
.property(`children[0].subChild.relativeParentSlug`)
.and.equal(tellme.getSlug(4));
doc.should.have.nested
.property(`children[0].subChild.relativeGrandParentSlug`)
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property(`children[0].subChildren[3].title`)
.and.equal(tellme.getText(8));
doc.should.have.nested
.property(`children[0].subChildren[7].title`)
.and.equal(tellme.getText(2));
doc.should.have.nested
.property(`children[0].subChildren[3].slug`)
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property(`children[0].subChildren[7].slug`)
.and.equal(tellme.getSlug(2));
doc.should.have.nested
.property(`children[0].subChildren[3].absoluteRootSlug`)
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property(`children[0].subChildren[7].absoluteChildSlug`)
.and.equal(tellme.getSlug(7));
doc.should.have.nested
.property(`children[0].subChildren[3].relativeRootSlug`)
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property(`children[0].subChildren[3].absoluteSiblingSlug`)
.and.equal(tellme.getSlug(4));
doc.should.have.nested
.property(`children[0].subChildren[7].relativeSiblingSlug`)
.and.equal(tellme.getSlug(1));
doc.should.have.nested
.property(`children[0].subChildren[5].subChild.title`)
.and.equal(tellme.getText(2));
doc.should.have.nested
.property(`children[0].subChildren[5].subChild.slug`)
.and.equal(tellme.getSlug(2));
doc.should.have.nested
.property(`children[0].subChildren[2].subChild.absoluteParentSlug`)
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property(`children[0].subChildren[8].subChild.absoluteChildSlug`)
.and.equal(tellme.getSlug(7));
doc.should.have.nested
.property(`children[0].subChildren[3].subChild.relativeParentSlug`)
.and.equal(tellme.getSlug(8));
doc.should.have.nested
.property(`children[0].subChild.relativeGrandParentSlug`)
.and.equal(tellme.getSlug(8));
}
});

@@ -10,3 +10,3 @@ 'use strict';

Permanent
} = require("./models");
} = require("./model");

@@ -13,0 +13,0 @@ const tellme = require('./tellme');

'use strict';
const _ = require('lodash');
const { options, slug_padding_size } = require('./models');
const { options, slug_padding_size } = require('./options');
const slugify = require('speakingurl');

@@ -5,0 +5,0 @@

@@ -13,3 +13,3 @@ 'use strict';

GroupedUniqueShortId,
} = require("./models");
} = require("./model");

@@ -16,0 +16,0 @@ const tellme = require('./tellme');

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc