Skip to content

Commit

Permalink
test works +update mongo-memory-server #5
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriGor committed Oct 27, 2018
1 parent 76fd7f2 commit 010466f
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 136 deletions.
18 changes: 15 additions & 3 deletions lib/slug-generator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'use strict';
// const _ = require('lodash');
const fs = require('fs');
var getSlug = require('speakingurl'),
shortId = require('shortid');
Expand All @@ -16,8 +17,13 @@ module.exports = function(schema, options) {
for (var attrname in options) {
opts[attrname] = options[attrname];
}

// console.log(" === schema === ")
schema.eachPath(function(pathname, schemaType) {
// console.log(pathname,schemaType.instance);
if(schemaType.instance == 'Array')
{
// console.log(schemaType);
}
if (
schemaType.instance == 'String' &&
schemaType.options &&
Expand Down Expand Up @@ -78,6 +84,8 @@ module.exports = function(schema, options) {
slugs.push(slug);
}
});
// console.log("watcher",watcher);
// console.log("slugs",slugs);

/**
* Executed before update value
Expand All @@ -97,11 +105,11 @@ module.exports = function(schema, options) {

async function onUpdate(operation) {
const me = this;
// console.debug('preupdate!', me.getQuery(), me.getUpdate());
// console.log('pre-'+operation,me.getUpdate());
let upd = me.getUpdate();
// console.debug('preupdateOne before',me.getQuery(), upd);
let docs = [{ ...(upd.$set || upd) }];
// console.log("doc",doc);
// console.log("doc",docs[0]);
let updFields = Object.keys(docs[0]);

if (!updFields.filter(value => -1 !== watcher.indexOf(value)).length) {
Expand Down Expand Up @@ -187,6 +195,7 @@ module.exports = function(schema, options) {
* Executed before save value
*/
schema.pre('save', async function() {
// console.log('pre-save');
var doc = this,
reSlug = false;

Expand Down Expand Up @@ -215,6 +224,9 @@ module.exports = function(schema, options) {
if (!affectedSlugs.length) return;

doc = await setSlugs(doc, affectedSlugs, opts, query => {
if(!doc.model){
// console.log("no model",doc,schema);
}
return doc.model(doc.constructor.modelName).findOne(query);
});
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"chai": "^4.2.0",
"eslint": "*",
"mocha": "^5.2.0",
"mongodb-memory-server": "*",
"mongodb-memory-server": "^2.6.2",
"mongoose": "^5.0.0"
}
}
4 changes: 2 additions & 2 deletions tasks.todone
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- update docs to reflect permanent option
+- update docs to reflect permanent option
+- add options to turn different hooks on/off
- update docs to reflect update/One/Many option
+- update docs to reflect update/One/Many option
+- add tests for permanent flag
- notify rickogden/mongoose-slug-generator about group update bug (he will need to extend watcher list)
- when we move doc with counted slug to another group with same docs - slug not updated
Expand Down
Loading

0 comments on commit 010466f

Please sign in to comment.