[data] updated 0.0.1 -> 0.0.2
This commit is contained in:
1 parent
e9cb52d20b
commit
e9ac51d675
7 files changed
+106
-35
No files matched your search
@@ -1,18 +1,21 @@
|
|||||||
{
|
{
|
||||||
"name": "hudozka-data",
|
"name": "hudozka-data",
|
||||||
"private": true,
|
"private": true,
|
||||||
"version": "0.0.1",
|
"version": "0.0.2",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "lib/index.js",
|
"main": "lib/index.js",
|
||||||
"scripts": {
|
"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"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"babel-cli": "^6.6.5",
|
"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-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 {
|
export default class Data extends EventEmitter {
|
||||||
constructor(store) {
|
constructor(store) {
|
||||||
super();
|
super()
|
||||||
this.save = true;
|
this.save = true
|
||||||
this.store = store;
|
this.store = store
|
||||||
}
|
}
|
||||||
|
|
||||||
update(document) {
|
broadcast(data) {
|
||||||
this.emit('update', document);
|
this.emit('update', data)
|
||||||
}
|
}
|
||||||
|
|
||||||
disableSaving() {
|
disableSaving() {
|
||||||
this.save = false;
|
this.save = false
|
||||||
}
|
}
|
||||||
|
|
||||||
enableSaving() {
|
enableSaving() {
|
||||||
this.save = true;
|
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 Timeline from './Timeline'
|
||||||
import Schedule from './Schedule';
|
import Schedule from './Schedule'
|
||||||
|
import Document from './Document'
|
||||||
|
|
||||||
export default class DataManager {
|
export default class DataManager {
|
||||||
constructor(stores) {
|
constructor(stores) {
|
||||||
this.timeline = new Timeline(stores.timeline);
|
this.timeline = new Timeline(stores.timeline)
|
||||||
this.schedule = new Schedule(stores.schedules);
|
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 {
|
||||||
|
|
||||||
|
}
|
||||||
@@ -10,10 +10,6 @@ export default class Schedule extends Data {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
update(){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
delete() {
|
delete() {
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -39,7 +35,7 @@ export default class Schedule extends Data {
|
|||||||
document = await savePost(post);
|
document = await savePost(post);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.update(document);
|
this.broadcast(document);
|
||||||
return document;
|
return document;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,27 +1,51 @@
|
|||||||
import Data from './Data';
|
import Data from './Data'
|
||||||
|
|
||||||
export default class Timeline extends Data {
|
export default class Timeline extends Data {
|
||||||
add(post) {
|
async create(post) {
|
||||||
let savePost = async(post) => {
|
let savePost = async(post) => {
|
||||||
const id = post.id;
|
const id = post.id
|
||||||
const type = post.type;
|
const type = post.type
|
||||||
const query = {id: id, type: type};
|
const query = {id: id, type: type}
|
||||||
|
|
||||||
await this.store.update(query, post, {upsert: true});
|
await this.store.update(query, post, {upsert: true})
|
||||||
let document = await this.store.find(query).limit(1).toArray();
|
let document = await this.store.find(query).limit(1).toArray()
|
||||||
return document[0];
|
return document[0]
|
||||||
};
|
}
|
||||||
|
|
||||||
let add = async(post) => {
|
let add = async(post) => {
|
||||||
let document = post;
|
let document = post
|
||||||
if (this.save) {
|
if (this.save) {
|
||||||
document = await savePost(post);
|
document = await savePost(post)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.update(document);
|
this.broadcast(document)
|
||||||
return document;
|
return document
|
||||||
};
|
}
|
||||||
|
|
||||||
return add(post);
|
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