Skip to content

Commit dccb094

Browse files
committed
initial import 🍀
0 parents  commit dccb094

File tree

11 files changed

+2626
-0
lines changed

11 files changed

+2626
-0
lines changed

.gitignore

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
8+
# Runtime data
9+
pids
10+
*.pid
11+
*.seed
12+
*.pid.lock
13+
14+
# Directory for instrumented libs generated by jscoverage/JSCover
15+
lib-cov
16+
17+
# Coverage directory used by tools like istanbul
18+
coverage
19+
20+
# nyc test coverage
21+
.nyc_output
22+
23+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
24+
.grunt
25+
26+
# Bower dependency directory (https://bower.io/)
27+
bower_components
28+
29+
# node-waf configuration
30+
.lock-wscript
31+
32+
# Compiled binary addons (http://nodejs.org/api/addons.html)
33+
build/Release
34+
35+
# Dependency directories
36+
node_modules/
37+
jspm_packages/
38+
39+
# Typescript v1 declaration files
40+
typings/
41+
42+
# Optional npm cache directory
43+
.npm
44+
45+
# Optional eslint cache
46+
.eslintcache
47+
48+
# Optional REPL history
49+
.node_repl_history
50+
51+
# Output of 'npm pack'
52+
*.tgz
53+
54+
# Yarn Integrity file
55+
.yarn-integrity
56+
57+
# dotenv environment variables file
58+
.env
59+

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
node_js:
3+
- "6.9"
4+
- "7"
5+
script: yarn test && yarn bench
6+

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# mngs
2+
3+
<img src="https://travis-ci.org/jondot/mngs.svg?branch=master">
4+
5+
A pleasant CLI for rapid development with Mongoose.
6+
7+
![](media/mngs.gif)
8+
9+
10+
## Quickstart
11+
12+
```
13+
$ yarn add --dev mngs
14+
```
15+
16+
And add the following script to your `package.json` file:
17+
18+
```json
19+
"scripts":{
20+
"mongoose":"MNGS_MODELS=config/mngs.js mngs"
21+
}
22+
```
23+
24+
And here's how `config/mngs.js` looks like:
25+
26+
```javascript
27+
// connects mongoose. grab your own setup
28+
require('./mongoose-setup.js')
29+
30+
// exports a map, e.g. { User: <mongoose model>, Post: <mongoose model> }
31+
const models = require(path.join(__dirname, '../models'))
32+
module.exports = models
33+
```
34+
35+
# Usage
36+
37+
Use your models (they are exposed directly) to print `p`, log `l`, and display on a table `t`.
38+
Additionally you can simulate a `toJSON` with `js` to see what part of your model is exposed.
39+
40+
```
41+
>> t(User.find())
42+
>> p(User.find())
43+
>> l(User.find())
44+
>> js(User.find())
45+
```
46+
47+
48+
# Contributing
49+
50+
Fork, implement, add tests, pull request, get my everlasting thanks and a respectable place here :).
51+
52+
53+
### Thanks:
54+
55+
To all [Contributors](https://github.com/jondot/mngs/graphs/contributors) - you make this happen, thanks!
56+
57+
58+
# Copyright
59+
60+
Copyright (c) 2017 [Dotan Nahum](http://gplus.to/dotan) [@jondot](http://twitter.com/jondot). See [LICENSE](LICENSE) for further details.

examples/with-js/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const run = require('../../src')
2+
const rules = require('./rules')
3+
4+
console.log('pass?', run(rules,
5+
{uid: 'user1'}))
6+

examples/with-js/rules.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports = ['or',
2+
['or',
3+
['includes',
4+
'name',
5+
['a']],
6+
['equals',
7+
'browser',
8+
'firefox']],
9+
['includes',
10+
'uid',
11+
['user1']]
12+
]
13+

examples/with-json/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const run = require('../../src')
2+
const rules = require('./rules.json')
3+
4+
console.log('pass?', run(rules,
5+
{uid: 'user1'}))
6+

examples/with-json/rules.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
["or",
2+
["or",["includes","name",["a"]],
3+
["equals","browser","firefox"]],
4+
["includes","uid",["user1"]]]

media/mngs.gif

343 KB
Loading

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "mngs",
3+
"version": "1.0.2",
4+
"description": "A fast, sandboxed matching engine with serializable rules",
5+
"main": "src/index.js",
6+
"bin":{
7+
"mngs":"src/index.js"
8+
},
9+
"scripts": {
10+
"test": "jest",
11+
"watch": "jest --watch",
12+
"bench": "node src/__tests__/index.bench.js"
13+
},
14+
"jest": {
15+
"testMatch": [
16+
"**/?(*.)(spec|test).js?(x)"
17+
]
18+
},
19+
"author": "Dotan Nahum <jondotan@gmail.com> (http://paracode.com)",
20+
"license": "MIT",
21+
"dependencies": {
22+
"locus": "^2.0.1",
23+
"lodash": "^4.17.4",
24+
"purdy": "^2.2.1",
25+
"table": "^4.0.1"
26+
},
27+
"devDependencies": {
28+
"benchmark": "^2.1.4",
29+
"jest": "^19.0.2"
30+
}
31+
}

src/index.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env node
2+
3+
const locus = require('locus')
4+
const path = require('path')
5+
const purdy = require('purdy')
6+
const L = require('lodash')
7+
const { table } = require('table')
8+
9+
// we don't need to see this file.
10+
__locus_modules__.print = Object.assign(__locus_modules__.print, {
11+
file: () => {},
12+
})
13+
14+
const models = require(path.join(
15+
process.cwd(),
16+
process.env.MNGS_MODELS || path.join(__dirname, '../config/mngs')
17+
))
18+
19+
// load up models
20+
Object.assign(global, models)
21+
console.log('Models:\n=======')
22+
console.log(Object.keys(models).join('\n'))
23+
24+
const objectify = m => x => {
25+
if (x[m]) {
26+
return x[m]()
27+
}
28+
if (L.isArray(x)) {
29+
return L.map(x, _ => _[m]())
30+
}
31+
return x
32+
}
33+
const tablify = x => {
34+
const a = L.isArray(x) ? x : [x]
35+
const header = Object.keys(L.maxBy(a, _ => Object.keys(_).length))
36+
const rows = L.map(a, _ => L.map(header, p => _[p]))
37+
return [header, ...rows]
38+
}
39+
const unglitchTable = t => {
40+
//fix up first line
41+
console.log(
42+
'\n' +
43+
L.map(
44+
purdy.stringify(t).split('\n'),
45+
(ln, i) => (i == 0 ? ln : ' ' + ln)
46+
).join('\n')
47+
)
48+
}
49+
50+
const o = _ => _.then(objectify('toObject')).then(purdy).catch(console.log)
51+
const l = _ => _.then(console.log).catch(console.log)
52+
const js = _ => _.then(objectify('toJSON')).then(purdy).catch(console.log)
53+
const t = _ =>
54+
_.then(objectify('toObject'))
55+
.then(tablify)
56+
.then(table)
57+
.then(unglitchTable)
58+
.catch(console.log)
59+
eval(locus)

0 commit comments

Comments
 (0)