@insertish/mutable
Advanced tools
Comparing version 1.0.6 to 1.1.0
@@ -1,3 +0,3 @@ | ||
import { Collection } from '@insertish/zangodb'; | ||
import EventEmitter from 'eventemitter3'; | ||
import { IDBPDatabase } from 'idb'; | ||
export declare type ObjectWithId = { | ||
@@ -11,4 +11,5 @@ _id: string; | ||
private proxyHandler; | ||
private collection?; | ||
constructor(collection?: Collection); | ||
private db?; | ||
private name; | ||
constructor(name: string, db?: IDBPDatabase); | ||
private $create; | ||
@@ -15,0 +16,0 @@ private mutation; |
@@ -19,3 +19,3 @@ "use strict"; | ||
class MutableMap extends eventemitter3_1.default { | ||
constructor(collection) { | ||
constructor(name, db) { | ||
super(); | ||
@@ -26,3 +26,4 @@ this.map = {}; | ||
}; | ||
this.collection = collection; | ||
this.name = name; | ||
this.db = db; | ||
} | ||
@@ -45,3 +46,3 @@ $create(state) { | ||
this.emit('mutation', target, property); | ||
(_a = this.collection) === null || _a === void 0 ? void 0 : _a.update({ _id: target._id }, { $set: { [property]: value } }); | ||
(_a = this.db) === null || _a === void 0 ? void 0 : _a.put(this.name, target); | ||
return true; | ||
@@ -53,3 +54,3 @@ } | ||
this.emit('clear'); | ||
(_a = this.collection) === null || _a === void 0 ? void 0 : _a.remove({}); | ||
(_a = this.db) === null || _a === void 0 ? void 0 : _a.clear(this.name); | ||
} | ||
@@ -59,3 +60,3 @@ create(state) { | ||
this.$create(state); | ||
(_a = this.collection) === null || _a === void 0 ? void 0 : _a.insert(state); | ||
(_a = this.db) === null || _a === void 0 ? void 0 : _a.put(this.name, state); | ||
} | ||
@@ -82,6 +83,3 @@ get(id) { | ||
this.emit('update', state._id); | ||
(_a = this.collection) === null || _a === void 0 ? void 0 : _a.update({ _id: state._id }, { | ||
$unset: Object.keys(existing).filter(x => x !== '_id'), | ||
$set: Object.assign({}, state) | ||
}); | ||
(_a = this.db) === null || _a === void 0 ? void 0 : _a.put(this.name, state); | ||
} | ||
@@ -93,2 +91,3 @@ else { | ||
removeField(id, ...path) { | ||
var _a; | ||
let existing = this.getMutable(id), i = 0; | ||
@@ -102,2 +101,3 @@ while (existing && i < path.length - 1) { | ||
} | ||
(_a = this.db) === null || _a === void 0 ? void 0 : _a.put(this.name, existing); | ||
this.emit('update', id); | ||
@@ -116,3 +116,3 @@ } | ||
delete this.map[_id]; | ||
(_a = this.collection) === null || _a === void 0 ? void 0 : _a.remove({ _id }); | ||
(_a = this.db) === null || _a === void 0 ? void 0 : _a.delete(this.name, _id); | ||
this.emit('update', _id); | ||
@@ -123,5 +123,5 @@ this.emit('delete', _id); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (typeof this.collection === 'undefined') | ||
throw "No collection provided."; | ||
yield this.collection.find({}) | ||
if (typeof this.db === 'undefined') | ||
throw "No db provided."; | ||
(yield this.db.getAll(this.name)) | ||
.forEach(doc => this.$create(transform ? transform(doc) : doc)); | ||
@@ -128,0 +128,0 @@ }); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
global.indexedDB = require('fake-indexeddb'); | ||
global.IDBKeyRange = require('fake-indexeddb/lib/FDBKeyRange'); | ||
const zangodb_1 = require("@insertish/zangodb"); | ||
let db = new zangodb_1.Db('state', 1, ['users']); | ||
require("fake-indexeddb/auto"); | ||
const idb_1 = require("idb"); | ||
const _1 = require("."); | ||
class Users extends _1.MutableMap { | ||
updateUsername(id, username) { | ||
// we would call an API here to verify we can do this | ||
// and then use Reflect to set the correct field | ||
this.map[id].username = username; | ||
idb_1.openDB('Test', 1, { | ||
upgrade(db) { | ||
db.createObjectStore('users', { | ||
keyPath: '_id' | ||
}); | ||
}, | ||
}) | ||
.then(db => { | ||
class Users extends _1.MutableMap { | ||
updateUsername(id, username) { | ||
// we would call an API here to verify we can do this | ||
// and then use Reflect to set the correct field | ||
this.map[id].username = username; | ||
} | ||
} | ||
} | ||
let map = new Users(db.collection('users')); | ||
map.on('create', item => console.log('Created user', item)); | ||
map.on('delete', id => console.log('Deleted user with id', id)); | ||
map.on('mutation', (user, property) => { | ||
console.log('User', user._id, 'has had property', property, 'changed!'); | ||
let map = new Users('users', db); | ||
map.on('create', item => console.log('Created user', item)); | ||
map.on('delete', id => console.log('Deleted user with id', id)); | ||
map.on('mutation', (user, property) => { | ||
console.log('User', user._id, 'has had property', property, 'changed!'); | ||
}); | ||
map.create({ _id: '123', username: 'aaa', online: true, flag: 0 }); | ||
let user = map.get('123'); | ||
map.updateUsername('123', 'bbb'); | ||
map.updateUsername('123', 'bbb'); | ||
console.log(JSON.stringify(user)); | ||
map.set({ _id: '456', username: 'bbb', online: false, flag: 0 }); | ||
map.set({ _id: '456', username: 'bbb', online: false, flag: 8 }); | ||
map.patch('456', { online: true }); | ||
map.patch('123', { flag: 1 }); | ||
map.delete('456'); | ||
let map2 = new Users('users', db); | ||
map2.restore(doc => { return Object.assign(Object.assign({}, doc), { online: false }); }) | ||
.then(() => console.log(map2.mapKeys(['123', '456']))); | ||
}); | ||
map.create({ _id: '123', username: 'aaa', online: true, flag: 0 }); | ||
let user = map.get('123'); | ||
map.updateUsername('123', 'bbb'); | ||
map.updateUsername('123', 'bbb'); | ||
console.log(JSON.stringify(user)); | ||
map.set({ _id: '456', username: 'bbb', online: false, flag: 0 }); | ||
map.set({ _id: '456', username: 'bbb', online: false, flag: 8 }); | ||
map.patch('456', { online: true }); | ||
map.patch('123', { flag: 1 }); | ||
map.delete('456'); | ||
let map2 = new Users(db.collection('users')); | ||
map2.restore(doc => { return Object.assign(Object.assign({}, doc), { online: false }); }) | ||
.then(() => console.log(map2.mapKeys(['123', '456']))); | ||
//# sourceMappingURL=tester.js.map |
{ | ||
"name": "@insertish/mutable", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "Mutable object utilities.", | ||
@@ -13,2 +13,3 @@ "main": "dist/index.js", | ||
"fake-indexeddb": "^3.1.2", | ||
"idb": "^6.1.2", | ||
"in-publish": "^2.0.1", | ||
@@ -29,3 +30,2 @@ "rimraf": "^3.0.2", | ||
"dependencies": { | ||
"@insertish/zangodb": "^1.0.12", | ||
"eventemitter3": "^4.0.7", | ||
@@ -32,0 +32,0 @@ "lodash.isequal": "^4.5.0" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2
204
18187
8
- Removed@insertish/zangodb@^1.0.12
- Removed@insertish/zangodb@1.0.12(transitive)
- Removedclone@2.1.2(transitive)
- Removedd@1.0.2(transitive)
- Removeddeepmerge@4.3.1(transitive)
- Removedes5-ext@0.10.64(transitive)
- Removedes6-iterator@2.0.3(transitive)
- Removedes6-symbol@3.1.4(transitive)
- Removedes6-weak-map@2.0.3(transitive)
- Removedesniff@2.0.1(transitive)
- Removedevent-emitter@0.3.5(transitive)
- Removedext@1.7.0(transitive)
- Removedis-promise@2.2.2(transitive)
- Removedlru-queue@0.1.0(transitive)
- Removedmemoizee@0.4.17(transitive)
- Removednext-tick@1.1.0(transitive)
- Removedobject-hash@2.2.0(transitive)
- Removedq@1.5.1(transitive)
- Removedtimers-ext@0.1.8(transitive)
- Removedtype@2.7.3(transitive)