[data] updated 0.0.1 -> 0.0.2
This commit is contained in:
1 parent
e9cb52d20b
commit
e9ac51d675
7 files changed
+152
-81
No files matched your search
@@ -1,18 +1,21 @@
|
||||
{
|
||||
"name": "hudozka-data",
|
||||
"private": true,
|
||||
"version": "0.0.1",
|
||||
"version": "0.0.2",
|
||||
"description": "",
|
||||
"main": "lib/index.js",
|
||||
"scripts": {
|
||||
"compile": "babel --presets es2015,stage-0 src/ -d lib/",
|
||||
"compile": "babel --plugins async-to-promises --presets es2015,stage-0 src/ -d lib/",
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"babel-cli": "^6.6.5",
|
||||
"babel-plugin-async-to-promises": "^1.0.5",
|
||||
"babel-polyfill": "^6.16.0",
|
||||
"babel-preset-es2015": "^6.6.0",
|
||||
"babel-preset-stage-0": "^6.5.0"
|
||||
"babel-preset-stage-0": "^6.5.0",
|
||||
"mongodb": "^2.2.12"
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,44 @@
|
||||
import {EventEmitter} from 'events';
|
||||
import {EventEmitter} from 'events'
|
||||
|
||||
export default class Data extends EventEmitter {
|
||||
constructor(store) {
|
||||
super();
|
||||
this.save = true;
|
||||
this.store = store;
|
||||
}
|
||||
constructor(store) {
|
||||
super()
|
||||
this.save = true
|
||||
this.store = store
|
||||
}
|
||||
|
||||
update(document) {
|
||||
this.emit('update', document);
|
||||
}
|
||||
broadcast(data) {
|
||||
this.emit('update', data)
|
||||
}
|
||||
|
||||
disableSaving() {
|
||||
this.save = false;
|
||||
}
|
||||
disableSaving() {
|
||||
this.save = false
|
||||
}
|
||||
|
||||
enableSaving() {
|
||||
this.save = true;
|
||||
}
|
||||
enableSaving() {
|
||||
this.save = true
|
||||
}
|
||||
|
||||
async create(data) {
|
||||
const id = data.id
|
||||
if (!id) throw new Error('Field `id` not found in Data object')
|
||||
const query = {id: id}
|
||||
|
||||
await this.store.update(query, data, {upsert: true})
|
||||
const result = await this.store.findOne(query)
|
||||
this.emit('update', result)
|
||||
return result
|
||||
}
|
||||
|
||||
async read(id) {
|
||||
return this.store.findOne({id: id})
|
||||
}
|
||||
|
||||
async find(query, skip = 0, limit = 0) {
|
||||
const cursor = this.store
|
||||
.find(query)
|
||||
.skip(skip)
|
||||
if (limit) cursor.limit(limit)
|
||||
return await cursor.toArray()
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
import Timeline from './Timeline';
|
||||
import Schedule from './Schedule';
|
||||
import Timeline from './Timeline'
|
||||
import Schedule from './Schedule'
|
||||
import Document from './Document'
|
||||
|
||||
export default class DataManager {
|
||||
constructor(stores) {
|
||||
this.timeline = new Timeline(stores.timeline);
|
||||
this.schedule = new Schedule(stores.schedules);
|
||||
}
|
||||
constructor(stores) {
|
||||
this.timeline = new Timeline(stores.timeline)
|
||||
this.schedule = new Schedule(stores.schedules)
|
||||
this.document = new Document(stores.documents)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
import Data from './Data'
|
||||
|
||||
export default class Document extends Data {
|
||||
|
||||
}
|
||||
@@ -2,47 +2,43 @@ import Data from './Data';
|
||||
|
||||
export default class Schedule extends Data {
|
||||
|
||||
create(){
|
||||
create() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
read(){
|
||||
read() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
update(){
|
||||
delete() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
delete(){
|
||||
find(query) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
find(query){
|
||||
add(post) {
|
||||
let savePost = async(post) => {
|
||||
const id = post.id;
|
||||
const type = post.type;
|
||||
const query = {id: id, type: type};
|
||||
|
||||
}
|
||||
await this.store.update(query, post, {upsert: true});
|
||||
let document = await this.store.find(query).limit(1).toArray();
|
||||
return document[0];
|
||||
};
|
||||
|
||||
add(post) {
|
||||
let savePost = async(post) => {
|
||||
const id = post.id;
|
||||
const type = post.type;
|
||||
const query = {id: id, type: type};
|
||||
|
||||
await this.store.update(query, post, {upsert: true});
|
||||
let document = await this.store.find(query).limit(1).toArray();
|
||||
return document[0];
|
||||
};
|
||||
|
||||
let add = async(post) => {
|
||||
let document = post;
|
||||
if(this.save){
|
||||
document = await savePost(post);
|
||||
}
|
||||
|
||||
this.update(document);
|
||||
return document;
|
||||
};
|
||||
|
||||
return add(post);
|
||||
}
|
||||
let add = async(post) => {
|
||||
let document = post;
|
||||
if (this.save) {
|
||||
document = await savePost(post);
|
||||
}
|
||||
|
||||
this.broadcast(document);
|
||||
return document;
|
||||
};
|
||||
|
||||
return add(post);
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,51 @@
|
||||
import Data from './Data';
|
||||
import Data from './Data'
|
||||
|
||||
export default class Timeline extends Data {
|
||||
add(post) {
|
||||
let savePost = async(post) => {
|
||||
const id = post.id;
|
||||
const type = post.type;
|
||||
const query = {id: id, type: type};
|
||||
|
||||
await this.store.update(query, post, {upsert: true});
|
||||
let document = await this.store.find(query).limit(1).toArray();
|
||||
return document[0];
|
||||
};
|
||||
|
||||
let add = async(post) => {
|
||||
let document = post;
|
||||
if(this.save){
|
||||
document = await savePost(post);
|
||||
}
|
||||
|
||||
this.update(document);
|
||||
return document;
|
||||
};
|
||||
|
||||
return add(post);
|
||||
}
|
||||
async create(post) {
|
||||
let savePost = async(post) => {
|
||||
const id = post.id
|
||||
const type = post.type
|
||||
const query = {id: id, type: type}
|
||||
|
||||
await this.store.update(query, post, {upsert: true})
|
||||
let document = await this.store.find(query).limit(1).toArray()
|
||||
return document[0]
|
||||
}
|
||||
|
||||
let add = async(post) => {
|
||||
let document = post
|
||||
if (this.save) {
|
||||
document = await savePost(post)
|
||||
}
|
||||
|
||||
this.broadcast(document)
|
||||
return document
|
||||
}
|
||||
|
||||
return add(post)
|
||||
}
|
||||
|
||||
add(post) {
|
||||
let savePost = async(post) => {
|
||||
const id = post.id
|
||||
const type = post.type
|
||||
const query = {id: id, type: type}
|
||||
|
||||
await this.store.update(query, post, {upsert: true})
|
||||
let document = await this.store.find(query).limit(1).toArray()
|
||||
return document[0]
|
||||
}
|
||||
|
||||
let add = async(post) => {
|
||||
let document = post
|
||||
if (this.save) {
|
||||
document = await savePost(post)
|
||||
}
|
||||
|
||||
this.broadcast(document)
|
||||
return document
|
||||
}
|
||||
|
||||
return add(post)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
require('babel-polyfill')
|
||||
|
||||
const Data = require('../lib/Data').default
|
||||
//const Data = require('../src/Data')
|
||||
const MongoClient = require('mongodb').MongoClient
|
||||
|
||||
MongoClient
|
||||
.connect('mongodb://localhost:27017/hudozka')
|
||||
.then(function (db) {
|
||||
const store = db.collection('documents')
|
||||
const data = new Data(store)
|
||||
|
||||
data.find({})
|
||||
.then(function(data){
|
||||
console.log('data >')
|
||||
console.log(data)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user