Skip to content
This repository was archived by the owner on Jul 5, 2019. It is now read-only.

Commit eac10df

Browse files
committed
Initial release
0 parents  commit eac10df

11 files changed

+2905
-0
lines changed

.editorconfig

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
root = true
2+
3+
[*]
4+
end_of_line = lf
5+
insert_final_newline = true
6+
indent_style = space
7+
indent_size = 2

__tests__/_database.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { MongoClient } from 'mongodb'
2+
3+
const state = {
4+
db: null,
5+
mode: null,
6+
}
7+
8+
const DATABASE_URI = 'mongodb://127.0.0.1:27017/test'
9+
10+
exports.connect = (mode, done) => {
11+
if (state.db) {
12+
return done()
13+
}
14+
15+
MongoClient.connect(DATABASE_URI, (err, db) => {
16+
if (err) {
17+
return done(err)
18+
}
19+
20+
state.db = db
21+
state.mode = mode
22+
23+
done()
24+
})
25+
}
26+
27+
exports.getDB = () => {
28+
return state.db
29+
}
30+
31+
exports.drop = (done) => {
32+
if (!state.db) {
33+
return done()
34+
}
35+
36+
// This is faster then dropping the database
37+
state.db.collections(async (err, collections) => {
38+
39+
collections.forEach((collection) => {
40+
await collection.remove(cb)
41+
})
42+
43+
done()
44+
})
45+
}
46+
47+
exports.fixtures = function (data, done) {
48+
var db = state.db
49+
50+
if (!db) {
51+
return done(new Error('Missing database connection.'))
52+
}
53+
54+
const names = Object.keys(data.collections)
55+
56+
names.forEach(async (name) => {
57+
const collection = await db.createCollection(name, function (err, collection) {
58+
await collection.insert(data.collections[name], resolve)
59+
})
60+
61+
done()
62+
}

__tests__/fixtures/database.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"collections": {
3+
"comments": [
4+
{
5+
"user": "Peter Parker",
6+
"text": "I like ice cream."
7+
},
8+
{
9+
"user": "John Doe",
10+
"text": "Nodejs is the best."
11+
},
12+
{
13+
"user": "Peter Jones",
14+
"text": "Keep your keyboard clean."
15+
}
16+
]
17+
}
18+
}

__tests__/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import Mongo from '../dist/'
2+
3+
describe('Init Mongo', () => {
4+
it('finds single', () => {
5+
6+
})
7+
})

dist/index.js

+56
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.ts

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { MongoClient } from 'mongodb'
2+
3+
interface Config {
4+
url: string
5+
}
6+
7+
class InitMongo {
8+
private client: MongoClient
9+
private collection
10+
private config: Config
11+
12+
constructor(config: Config) {
13+
this.client = MongoClient
14+
this.config = config
15+
}
16+
17+
private caller(collectionName: string, callee: Function) {
18+
return new Promise((resolve, reject) => {
19+
this.client.connect(this.config.url, (err, db) => {
20+
if (err) {
21+
return reject(err)
22+
}
23+
24+
this.collection = db.collection(collectionName)
25+
26+
return callee()
27+
.then((data) => {
28+
db.close()
29+
resolve(data)
30+
})
31+
.catch(console.error)
32+
})
33+
})
34+
}
35+
36+
private insertCallee(elements: object[] | object) {
37+
return new Promise((resolve, reject) => {
38+
this.collection.insertMany(elements, (error, result) => {
39+
if (error) {
40+
return reject(error)
41+
}
42+
43+
return resolve(result)
44+
})
45+
})
46+
}
47+
48+
public insert(collectionName: string, elements: object[] | object) {
49+
if (elements.constructor !== Array) {
50+
elements = [elements]
51+
}
52+
53+
return this.caller(collectionName, () => this.insertCallee(elements))
54+
}
55+
56+
private findCallee(query: object) {
57+
return new Promise((resolve, reject) => {
58+
this.collection.find(query).toArray((error, result) => {
59+
if (error) {
60+
return reject(error)
61+
}
62+
63+
return resolve(result)
64+
});
65+
})
66+
}
67+
68+
public find(collectionName: string, query: object) {
69+
return this.caller(collectionName, () => this.findCallee(query))
70+
}
71+
72+
}
73+
74+
export default InitMongo

package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "init-mongo",
3+
"version": "0.1.0",
4+
"main": "dist/index.js",
5+
"author": "Hans Christian Reinl <[email protected]>",
6+
"license": "MIT",
7+
"dependencies": {
8+
"@types/jest": "^21.1.6",
9+
"@types/node": "^8.0.53",
10+
"jest": "^21.2.1",
11+
"mongodb": "^2.2.33",
12+
"tslint": "^5.8.0",
13+
"typescript": "^2.6.1"
14+
},
15+
"scripts": {
16+
"compile": "tsc"
17+
}
18+
}

tsconfig.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"moduleResolution": "node",
5+
"module": "commonjs",
6+
"outDir": "dist",
7+
"sourceMap": true,
8+
"types": [
9+
"node",
10+
"jest"
11+
]
12+
},
13+
"include": [
14+
"**/*.ts"
15+
],
16+
"exclude": [
17+
"node_modules",
18+
"__tests__"
19+
]
20+
}

tslint.json

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"rules": {
3+
"max-line-length": {
4+
"options": [
5+
120
6+
]
7+
},
8+
"new-parens": true,
9+
"no-arg": true,
10+
"no-bitwise": true,
11+
"no-conditional-assignment": true,
12+
"no-consecutive-blank-lines": false,
13+
"no-console": {
14+
"options": [
15+
"debug",
16+
"info",
17+
"log",
18+
"time",
19+
"timeEnd",
20+
"trace"
21+
]
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)