diff --git a/src/DB.js b/src/DB.js index 2bc2928..de560d8 100644 --- a/src/DB.js +++ b/src/DB.js @@ -3,40 +3,9 @@ import { struct } from 'superstruct'; import Immutable from 'immutable'; import { File } from './adapters/File'; import { LocalStorage } from './adapters/LocalStorage'; +import { IMMUTABLE_READ, IMMUTABLE_TYPE, IMMUTABLE_WRITE, JAYSN_PATH } from './constants'; -export const JAYSN_PATH = '.jaysn'; - -const IMMUTABLE_TYPE = [ - 'List', - 'Map', - 'OrderedMap', - 'Set', - 'OrderedSet', - 'Stack', - 'Range', - 'Repeat', - 'Record', - 'Seq', - 'Collection', -]; - -const IMMUTABLE_READ = ['get', 'getIn']; - -const IMMUTABLE_WRITE = [ - 'delete', - 'deleteIn', - 'merge', - 'mergeDeep', - 'mergeDeepIn', - 'mergeIn', - 'remove', - 'removeIn', - 'set', - 'setIn', - 'update', - 'updateIn', -]; - +export { JAYSN_PATH } from './constants'; export class DB { constructor(schema, options = {}) { const defaultOpts = { @@ -54,7 +23,6 @@ export class DB { // Merge options with defaultOptions const opts = Immutable.fromJS(defaultOpts).merge(Immutable.fromJS(options)).toJS(); - let adapter; // Let's pick the specific adapter switch (opts.use) { @@ -62,16 +30,16 @@ export class DB { if (typeof localStorage === 'undefined' || localStorage === null) { throw new TypeError('LocalStorage adapter only available on browser!'); } - adapter = new LocalStorage(opts.source); + this.adapter = new LocalStorage(opts.source); if (!localStorage.getItem(opts.source)) { - adapter.write(defaultData); + this.adapter.write(defaultData); } break; default: - adapter = new File(opts.source); + this.adapter = new File(opts.source); if (!existsSync(opts.source)) { - adapter.write(defaultData); + this.adapter.write(defaultData); } break; } @@ -94,7 +62,7 @@ export class DB { Immutable[K].prototype.write = () => { const data = this.getState().toJS(); - this._state.push(Immutable.fromJS(adapter.write(data))); + this._state.push(Immutable.fromJS(this.adapter.write(data))); this._stateId += 1; return this.getState(); }; @@ -103,22 +71,29 @@ export class DB { }, {}); // Init state from the db - const initState = Immutable.fromJS(adapter.read()); + const initState = Immutable.fromJS(this.adapter.read()); this._state = [initState]; this._stateId = 0; + this.initiateImmutableRead(); + this.initiateImmutableWrite(); + } + + initiateImmutableRead() { // Return Latest State when using an Immutable Read Functions IMMUTABLE_READ.forEach((method) => { this[method] = (...args) => { const state = this.getState(); - this._state.push(Immutable.fromJS(adapter.read())); + this._state.push(Immutable.fromJS(this.adapter.read())); this._stateId += 1; return state[method](...args); }; }); + } + initiateImmutableWrite() { // Return and push newState, then increase stateId when using an Immutable Write Functions IMMUTABLE_WRITE.forEach((method) => { this[method] = (...args) => { @@ -140,7 +115,7 @@ export class DB { typeof L === 'number' && L > -1 && L % 1 === 0 && - L <= 9007199254740991 + L <= Number.MAX_SAFE_INTEGER + 1 ) { V.forEach((NV) => { struct(this.schema[O])(NV); diff --git a/src/constants.js b/src/constants.js new file mode 100644 index 0000000..7e132ed --- /dev/null +++ b/src/constants.js @@ -0,0 +1,39 @@ +export const JAYSN_PATH = '.jaysn'; + +export const IMMUTABLE_TYPE = [ + 'List', + 'Map', + 'OrderedMap', + 'Set', + 'OrderedSet', + 'Stack', + 'Range', + 'Repeat', + 'Record', + 'Seq', + 'Collection', +]; + +export const IMMUTABLE_READ = ['get', 'getIn']; + +export const IMMUTABLE_WRITE = [ + 'delete', + 'deleteIn', + 'merge', + 'mergeDeep', + 'mergeDeepIn', + 'mergeIn', + 'remove', + 'removeIn', + 'set', + 'setIn', + 'update', + 'updateIn', +]; + +export default { + JAYSN_PATH, + IMMUTABLE_TYPE, + IMMUTABLE_READ, + IMMUTABLE_WRITE, +};