From bbb957177a1be6017daf1a84792b599faf394e65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Ledentu?= Date: Thu, 17 Oct 2019 11:50:30 +0200 Subject: [PATCH 1/5] Fix hook for Sails 1 --- README.md | 6 +++--- api/models/Post.js | 4 ++-- lib/index.js | 23 +++++++++++++++-------- package.json | 6 +++--- test/.eslintrc | 3 +++ test/bootstrap.test.js | 9 +++++---- test/unit/models/Post.test.js | 30 +++++++++++++++--------------- 7 files changed, 46 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 102f7e8..5158e1a 100644 --- a/README.md +++ b/README.md @@ -74,10 +74,10 @@ Post.create({ }); ``` -Like any other attribute, you can use dynamic finders: +Like any other attribute, you can use it as criteria to find a given resource: ```js -Post.findOneBySlug('this-is-a-new-post') +Post.findOne({ slug: 'this-is-a-new-post' }) .then(function(post) { // Use the post }) @@ -98,4 +98,4 @@ separator | `string` | Separator to use in slugs. Defaults to `-`. ## License -MIT © 2017 Jérémie Ledentu +MIT © 2019 Jérémie Ledentu diff --git a/api/models/Post.js b/api/models/Post.js index 59c9210..0ddf75c 100644 --- a/api/models/Post.js +++ b/api/models/Post.js @@ -6,8 +6,8 @@ */ module.exports = { - attributes: { + id: { type: 'number', autoIncrement: true}, title: { type: 'string', required: true, @@ -17,7 +17,7 @@ module.exports = { type: 'string' }, content: { - type: 'text' + type: 'string' }, slug: { type: 'slug', diff --git a/lib/index.js b/lib/index.js index 2c6a81f..a39ef27 100644 --- a/lib/index.js +++ b/lib/index.js @@ -34,9 +34,9 @@ module.exports = function(sails) { }); } - function patchModels() { - for (let modelName of Object.keys(sails.models)) { - let model = sails.models[modelName]; + function patchModels(models) { + for (let modelName of Object.keys(models)) { + let model = models[modelName]; for (let name of Object.keys(model.attributes)) { let attr = model.attributes[name]; @@ -65,6 +65,7 @@ module.exports = function(sails) { })(model.beforeCreate, attr.from, attr.blacklist); delete attr.from; + delete attr.blacklist; } } } @@ -79,11 +80,17 @@ module.exports = function(sails) { }, }, - initialize: function(next) { - sails.after(['hook:moduleloader:loaded'], function() { - patchModels(); - - return next(); + initialize(done) { + sails.after('hook:moduleloader:loaded', () => { + const originalLoadModels = sails.modules.loadModels; + sails.modules.loadModels = function(cb) { + originalLoadModels((error, models) => { + patchModels(models); + console.log(models); + cb(error, models); + done(); + }); + }; }); }, }; diff --git a/package.json b/package.json index 8f9dc1a..8ada4fa 100644 --- a/package.json +++ b/package.json @@ -34,12 +34,12 @@ "eslint": "^4.8.0", "mocha": "^5.0.0", "nyc": "^11.2.1", - "sails": "^0.12.14", - "sails-disk": "^0.10.8", + "sails": "^1.2.3", + "sails-hook-orm": "^2.1.1", "should": "^13.1.1" }, "dependencies": { "slugg": "^1.2.1", "uuid": "^3.0.1" } -} \ No newline at end of file +} diff --git a/test/.eslintrc b/test/.eslintrc index 4668ae7..7a3fd4b 100644 --- a/test/.eslintrc +++ b/test/.eslintrc @@ -1,5 +1,8 @@ { "env": { "mocha": true + }, + "globals": { + "sails": true } } diff --git a/test/bootstrap.test.js b/test/bootstrap.test.js index ae38ad7..34ddbd5 100644 --- a/test/bootstrap.test.js +++ b/test/bootstrap.test.js @@ -1,12 +1,12 @@ require('should'); -var Sails = require('sails'); +var sails = require('sails'); before(function(done) { this.timeout(50000); let config = { log: { - level: 'info' + level: 'verbose' }, hooks: { slugs: require('../lib'), @@ -18,14 +18,15 @@ before(function(done) { } }; - Sails.lift(config, function (err, sails) { + sails.lift(config, function (err) { if (err) { return done(err); } + global.sails = sails; return done(); }); }); after(function(done) { - Sails.lower(done); + sails.lower(done); }); diff --git a/test/unit/models/Post.test.js b/test/unit/models/Post.test.js index 2c615e4..4e61165 100644 --- a/test/unit/models/Post.test.js +++ b/test/unit/models/Post.test.js @@ -4,11 +4,12 @@ describe('PostModel', function() { describe('#create()', function() { it('should create a new post', function(done) { - Post.create({ + sails.models.post.create({ title: 'This is a new post!!!', content: 'Post content', author: 'Jérémie Ledentu' }) + .fetch() .then(function(post) { post.should.have.property('title'); post.title.should.eql('This is a new post!!!'); @@ -27,11 +28,12 @@ describe('PostModel', function() { }); it('should resolve slug conflicts', function(done) { - Post.create({ + sails.models.post.create({ title: 'This is a new post!!', content: 'Post content 2', author: 'Jérémie Ledentu' }) + .fetch() .then(function(post) { post.should.have.property('title'); post.title.should.eql('This is a new post!!'); @@ -51,10 +53,11 @@ describe('PostModel', function() { it('should not use a slug from global blacklist', (done) => { sails.config.slugs.blacklist = ['new']; - Post.create({ + sails.models.post.create({ title: 'New', author: 'Jérémie Ledentu' }) + .fetch() .then((post) => { post.should.have.property('slug'); post.slug.should.match(/^new-/); @@ -67,10 +70,11 @@ describe('PostModel', function() { }); it('should not use a slug from local blacklist', (done) => { - Post.create({ + sails.models.post.create({ title: 'My new post', author: 'Profile' }) + .fetch() .then((post) => { post.should.have.property('author'); post.slugAuthor.should.match(/^profile-/); @@ -82,15 +86,16 @@ describe('PostModel', function() { it('should not use lowercase if lowercase === false in the config', done => { sails.config.slugs.lowercase = false; - Post.create({ + sails.models.post.create({ title: 'THIS IS A TITLE IN UPPERCASE', content: 'Post content', author: 'Jérémie Ledentu' }) + .fetch() .then(post => { post.should.have.property('slug'); post.slug.should.eql('THIS-IS-A-TITLE-IN-UPPERCASE'); - post.slugAuthor.should.match(/^Jeremie-Ledentu-/); + post.slugAuthor.should.eql('Jeremie-Ledentu'); sails.config.slugs.lowercase = true; @@ -102,11 +107,12 @@ describe('PostModel', function() { it('should use separator defined in config', done => { sails.config.slugs.separator = '_'; - Post.create({ + sails.models.post.create({ title: 'This is underscore', content: 'Post content', author: 'Jérémie Ledentu' }) + .fetch() .then(post => { post.should.have.property('slug'); post.slug.should.eql('this_is_underscore'); @@ -120,15 +126,9 @@ describe('PostModel', function() { }); }); - describe('#findOneBySlug()', function() { - it('should exist in the model', function(done) { - Post.should.have.property('findBySlug'); - Post.findOneBySlug.should.be.a.Function(); - done(); - }); - + describe('#findOne({slug})', function() { it('should return the entity with given slug', function(done) { - Post.findOneBySlug('this-is-a-new-post') + sails.models.post.findOne({ slug: 'this-is-a-new-post' }) .then(function(post) { post.should.have.property('title'); post.title.should.eql('This is a new post!!!'); From 82d486044c619840fa368e897d9f76759c0749d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Ledentu?= Date: Thu, 17 Oct 2019 12:10:37 +0200 Subject: [PATCH 2/5] Upgrade major version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8ada4fa..aae0c81 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sails-hook-slugs", - "version": "3.0.0", + "version": "4.0.0", "description": "A hook for Sails to handle slugs in your models.", "main": "dist/lib/index.js", "scripts": { From 458e3e9165a73ec2c6218a7d0f665c4960b2606f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Ledentu?= Date: Thu, 17 Oct 2019 12:12:54 +0200 Subject: [PATCH 3/5] Remove console.log --- lib/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/index.js b/lib/index.js index a39ef27..a98f85b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -86,7 +86,6 @@ module.exports = function(sails) { sails.modules.loadModels = function(cb) { originalLoadModels((error, models) => { patchModels(models); - console.log(models); cb(error, models); done(); }); From fe77db4109e00809f3889e9421eecd85ebc59da2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Ledentu?= Date: Thu, 17 Oct 2019 12:14:03 +0200 Subject: [PATCH 4/5] Remove Node < 8 from supported versions --- .babelrc | 2 +- .travis.yml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.babelrc b/.babelrc index 8e9ebcc..a897811 100644 --- a/.babelrc +++ b/.babelrc @@ -4,7 +4,7 @@ "env", { "targets": { - "node": 6 + "node": 8 } } ] diff --git a/.travis.yml b/.travis.yml index b78d362..5f96e49 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,9 +1,9 @@ language: node_js node_js: -- '6' -- '7' - '8' - '9' +- '10' +- '12' env: - CXX=g++-4.8 addons: From 1517d3dde3d5ef6e6d7a6b453533aa57077cd5ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Ledentu?= Date: Thu, 17 Oct 2019 12:18:16 +0200 Subject: [PATCH 5/5] Remove Node 9 from tested versions --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 5f96e49..0e795eb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: node_js node_js: - '8' -- '9' - '10' - '12' env: