From e9ac51d675953c0390b3401bda10c2ba57dc01af Mon Sep 17 00:00:00 2001 From: Roman Timashev Date: Tue, 3 Jan 2017 06:30:03 +0300 Subject: [PATCH] [data] updated 0.0.1 -> 0.0.2 --- modules/hudozka-data/package.json | 9 ++-- modules/hudozka-data/src/Data.js | 53 ++++++++++++------ modules/hudozka-data/src/DataManager.js | 14 ++--- modules/hudozka-data/src/Document.js | 5 ++ modules/hudozka-data/src/Schedule.js | 62 ++++++++++----------- modules/hudozka-data/src/Timeline.js | 72 ++++++++++++++++--------- modules/hudozka-data/test/Data.js | 18 +++++++ 7 files changed, 152 insertions(+), 81 deletions(-) create mode 100644 modules/hudozka-data/src/Document.js create mode 100644 modules/hudozka-data/test/Data.js diff --git a/modules/hudozka-data/package.json b/modules/hudozka-data/package.json index e16e97cf..34c83670 100644 --- a/modules/hudozka-data/package.json +++ b/modules/hudozka-data/package.json @@ -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" } } diff --git a/modules/hudozka-data/src/Data.js b/modules/hudozka-data/src/Data.js index 1fb6ee14..991c2062 100644 --- a/modules/hudozka-data/src/Data.js +++ b/modules/hudozka-data/src/Data.js @@ -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() + } } diff --git a/modules/hudozka-data/src/DataManager.js b/modules/hudozka-data/src/DataManager.js index 43f9eb06..79fe7f28 100644 --- a/modules/hudozka-data/src/DataManager.js +++ b/modules/hudozka-data/src/DataManager.js @@ -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) + } } diff --git a/modules/hudozka-data/src/Document.js b/modules/hudozka-data/src/Document.js new file mode 100644 index 00000000..fa7eb0e5 --- /dev/null +++ b/modules/hudozka-data/src/Document.js @@ -0,0 +1,5 @@ +import Data from './Data' + +export default class Document extends Data { + +} diff --git a/modules/hudozka-data/src/Schedule.js b/modules/hudozka-data/src/Schedule.js index 03dfd2a0..d9118eac 100644 --- a/modules/hudozka-data/src/Schedule.js +++ b/modules/hudozka-data/src/Schedule.js @@ -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); + } } diff --git a/modules/hudozka-data/src/Timeline.js b/modules/hudozka-data/src/Timeline.js index 21ab1de0..054a5219 100644 --- a/modules/hudozka-data/src/Timeline.js +++ b/modules/hudozka-data/src/Timeline.js @@ -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) + } } diff --git a/modules/hudozka-data/test/Data.js b/modules/hudozka-data/test/Data.js new file mode 100644 index 00000000..4bcff7b0 --- /dev/null +++ b/modules/hudozka-data/test/Data.js @@ -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) + }) + })