Skip to content

Commit 25b8f82

Browse files
author
Ryan Haskell-Glatz
committed
add documentation
0 parents  commit 25b8f82

File tree

3 files changed

+138
-0
lines changed

3 files changed

+138
-0
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# KeystoneJS | Singleton Plugin
2+
> A plugin that allows the creation of "singleton" KeystoneJS lists.
3+
4+
### Description
5+
6+
This plugin prevents users from creating multiple items, and guarantees that an item is created automatically.
7+
8+
So if you want to make "Homepage Settings", we gotchu <3
9+
10+
11+
### Example Usage
12+
13+
14+
__`HomepageSettings.js`__
15+
16+
```js
17+
const keystone = require('keystone')
18+
19+
// 1. Include the `singleton` option
20+
const List = keystone.List('HomepageSettings', {
21+
singleton: true
22+
})
23+
24+
// 2. Add in your fields.
25+
List.add( /* ... */ )
26+
27+
// 3. Register your list.
28+
List.register()
29+
```
30+
31+
32+
__`keystone.js`__
33+
34+
```js
35+
const keystone = require('keystone')
36+
const singleton = require('keystone-singleton')
37+
38+
// 1. Initialize KeystoneJS
39+
keystone.init( /* ... */ )
40+
41+
// 2. Initialize this plugin
42+
singleton.init({ keystone })
43+
44+
// 3. Include your Models
45+
keystone.import('models')
46+
47+
// 4. Start KeystoneJS
48+
keystone.start()
49+
```
50+
51+
Your KeystoneJS list will automatically be created on startup.

index.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
module.exports = {
2+
init: ({ keystone }) => {
3+
const register = keystone.List.prototype.register
4+
5+
keystone.List.prototype.register = function () {
6+
const List = this
7+
const isSingleton = List.options.singleton
8+
9+
// Only do this if the `singleton` option is set:
10+
if (isSingleton) {
11+
// Get the list name, and a human-readable name
12+
const listName = List.key
13+
const prettyName = listName
14+
.split('')
15+
.map(letter =>
16+
(letter.toUpperCase() === letter)
17+
? ' ' + letter
18+
: letter
19+
)
20+
.join('')
21+
.trim()
22+
23+
// Automatically name the item
24+
List.add({
25+
singleton__name: {
26+
type: String,
27+
required: true,
28+
default: prettyName,
29+
hidden: true,
30+
select: false
31+
}
32+
})
33+
34+
// Map `singleton__name` to KeystoneJS name
35+
List.options.map = List.options.map || {}
36+
List.options.map.name = List.options.map.name || 'singleton__name'
37+
38+
// Disallow creation and removal
39+
List.options.nocreate = true
40+
List.options.nodelete = true
41+
42+
// Keep label singular by default
43+
List.options.label = List.options.label || prettyName
44+
45+
// Register the singleton
46+
register.apply(this)
47+
48+
// Attempt to create item, if none already exist
49+
const Model = keystone.list(listName).model
50+
51+
Model.count()
52+
.lean()
53+
.exec()
54+
.then(count => {
55+
if (count === 0) {
56+
console.info(`SINGLETON: Creating '${listName}'...`)
57+
return Model.create({})
58+
} else {
59+
return Promise.resolve(undefined)
60+
}
61+
})
62+
.catch(reason => {
63+
console.error(`SINGLETON: Could not create '${listName}'.`)
64+
})
65+
} else {
66+
// Register the list
67+
register.apply(this)
68+
}
69+
}
70+
}
71+
}

package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "keystone-singleton",
3+
"version": "1.0.0",
4+
"description": "A plugin that allows the creation of \"singleton\" KeystoneJS lists.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://bitbucket.org/onenorth/keystone-singleton.git"
12+
},
13+
"author": "Ryan Haskell-Glatz",
14+
"license": "ISC",
15+
"homepage": "https://bitbucket.org/onenorth/keystone-singleton#readme"
16+
}

0 commit comments

Comments
 (0)