@tryghost/bookshelf-has-posts 0.1.6
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.
- package/LICENSE +21 -0
- package/README.md +39 -0
- package/index.js +1 -0
- package/lib/bookshelf-has-posts.js +63 -0
- package/package.json +32 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2013-2021 Ghost Foundation
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Bookshelf Has Posts
|
|
2
|
+
|
|
3
|
+
## Install
|
|
4
|
+
|
|
5
|
+
`npm install @tryghost/bookshelf-has-posts --save`
|
|
6
|
+
|
|
7
|
+
or
|
|
8
|
+
|
|
9
|
+
`yarn add @tryghost/bookshelf-has-posts`
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## Usage
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## Develop
|
|
16
|
+
|
|
17
|
+
This is a mono repository, managed with [lerna](https://lernajs.io/).
|
|
18
|
+
|
|
19
|
+
Follow the instructions for the top-level repo.
|
|
20
|
+
1. `git clone` this repo & `cd` into it as usual
|
|
21
|
+
2. Run `yarn` to install top-level dependencies.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
## Run
|
|
25
|
+
|
|
26
|
+
- `yarn dev`
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
## Test
|
|
30
|
+
|
|
31
|
+
- `yarn lint` run just eslint
|
|
32
|
+
- `yarn test` run lint and tests
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# Copyright & License
|
|
38
|
+
|
|
39
|
+
Copyright (c) 2013-2021 Ghost Foundation - Released under the [MIT license](LICENSE).
|
package/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require('./lib/bookshelf-has-posts');
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
const _ = require('lodash');
|
|
2
|
+
const _debug = require('@tryghost/debug')._base;
|
|
3
|
+
const debug = _debug('ghost-query');
|
|
4
|
+
|
|
5
|
+
const addHasPostsWhere = (tableName, config) => {
|
|
6
|
+
const comparisonField = `${tableName}.id`;
|
|
7
|
+
|
|
8
|
+
return function (qb) {
|
|
9
|
+
return qb.whereIn(comparisonField, function () {
|
|
10
|
+
const innerQb = this
|
|
11
|
+
.distinct(`${config.joinTable}.${config.joinTo}`)
|
|
12
|
+
.select()
|
|
13
|
+
.from(config.joinTable)
|
|
14
|
+
.whereRaw(`${config.joinTable}.${config.joinTo} = ${comparisonField}`)
|
|
15
|
+
.join('posts', 'posts.id', `${config.joinTable}.post_id`)
|
|
16
|
+
.andWhere('posts.status', '=', 'published');
|
|
17
|
+
|
|
18
|
+
debug(`QUERY has posts: ${innerQb.toSQL().sql}`);
|
|
19
|
+
|
|
20
|
+
return innerQb;
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @param {import('bookshelf')} Bookshelf
|
|
27
|
+
*/
|
|
28
|
+
const hasPosts = function hasPosts(Bookshelf) {
|
|
29
|
+
const modelPrototype = Bookshelf.Model.prototype;
|
|
30
|
+
|
|
31
|
+
Bookshelf.Model = Bookshelf.Model.extend({
|
|
32
|
+
initialize: function () {
|
|
33
|
+
return modelPrototype.initialize.apply(this, arguments);
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
fetch: function () {
|
|
37
|
+
if (this.shouldHavePosts) {
|
|
38
|
+
this.query(addHasPostsWhere(_.result(this, 'tableName'), this.shouldHavePosts));
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if (_debug.enabled('ghost-query')) {
|
|
42
|
+
debug('QUERY', this.query().toQuery());
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return modelPrototype.fetch.apply(this, arguments);
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
fetchAll: function () {
|
|
49
|
+
if (this.shouldHavePosts) {
|
|
50
|
+
this.query(addHasPostsWhere(_.result(this, 'tableName'), this.shouldHavePosts));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (_debug.enabled('ghost-query')) {
|
|
54
|
+
debug('QUERY', this.query().toQuery());
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return modelPrototype.fetchAll.apply(this, arguments);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
module.exports = hasPosts;
|
|
63
|
+
module.exports.addHasPostsWhere = addHasPostsWhere;
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tryghost/bookshelf-has-posts",
|
|
3
|
+
"version": "0.1.6",
|
|
4
|
+
"repository": "https://github.com/TryGhost/framework/tree/main/packages/bookshelf-has-posts",
|
|
5
|
+
"author": "Ghost Foundation",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "index.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"dev": "echo \"Implement me!\"",
|
|
10
|
+
"test": "NODE_ENV=testing c8 mocha './test/**/*.test.js'",
|
|
11
|
+
"lint": "eslint . --ext .js --cache",
|
|
12
|
+
"posttest": "yarn lint"
|
|
13
|
+
},
|
|
14
|
+
"files": [
|
|
15
|
+
"index.js",
|
|
16
|
+
"lib"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"c8": "7.9.0",
|
|
23
|
+
"mocha": "9.0.2",
|
|
24
|
+
"should": "13.2.3",
|
|
25
|
+
"sinon": "11.1.1"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@tryghost/debug": "^0.1.5",
|
|
29
|
+
"lodash": "^4.17.21"
|
|
30
|
+
},
|
|
31
|
+
"gitHead": "1279f0d8f3636161e6fa6cd49ea44079e5070321"
|
|
32
|
+
}
|