[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,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()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user