data api with io

This commit is contained in:
tmshv committed 2016-03-23 19:38:59 +03:00
1 parent 0ffa403b60
commit aea2b8ff25
18 files changed
+162 -94

No files matched your search

+1 -1
View File
@@ -1,7 +1,7 @@
import {c} from '../core/db';
export async function getCollective(query={}, show={}) {
let data = await c('collective').find(query, show);
let data = await c('collective').find(query, show).toArray();
if (!data) {
return null;
}
+19 -41
View File
@@ -1,46 +1,24 @@
/**
* Created by tmshv on 22/11/14.
*/
import {MongoClient, ObjectId} from 'mongodb';
import co from 'co';
var mongo = require("promised-mongo");
var ObjectId = mongo.ObjectId;
export var db;
var client = null;
export function connect(uri){
return co(function*() {
db = yield MongoClient.connect(uri);
return db;
})
}
module.exports = {
init: function (uri) {
//noinspection JSUnresolvedFunction
return new Promise(function (resolve) {
client = mongo(uri);
resolve();
});
},
export function collection(name){
return db.collection(name);
}
close: function () {
client.close();
},
export const c = collection;
get db() {
return client;
},
c: function(name){
return client.collection(name);
},
id: function(i) {
if(typeof i === "string"){
return ObjectId(i);
}
else if(i instanceof ObjectId) {
return i;
}
else if(i instanceof Object) {
return "_id" in i ? ObjectId(i._id) : null;
}
return null;
}
};
export function id(i){
if(typeof i === 'string')return ObjectId(i);
else if(i instanceof ObjectId) return i;
else if(i instanceof Object) return '_id' in i ? ObjectId(i._id) : null;
return null;
}