forked from medikoo/dbjs-persistence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.js
171 lines (163 loc) · 6.5 KB
/
driver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
'use strict';
var aFrom = require('es5-ext/array/from')
, customError = require('es5-ext/error/custom')
, ensureIterable = require('es5-ext/iterable/validate-object')
, assign = require('es5-ext/object/assign')
, copy = require('es5-ext/object/copy')
, ensureCallable = require('es5-ext/object/valid-callable')
, ensureString = require('es5-ext/object/validate-stringifiable-value')
, capitalize = require('es5-ext/string/#/capitalize')
, d = require('d')
, lazy = require('d/lazy')
, autoBind = require('d/auto-bind')
, ee = require('event-emitter')
, emitError = require('event-emitter/emit-error')
, deferred = require('deferred')
, ensureDatabase = require('dbjs/valid-dbjs')
, Event = require('dbjs/_setup/event')
, unserializeValue = require('dbjs/_setup/unserialize/value')
, Storage = require('./storage')
, ReducedStorage = require('./reduced-storage')
, ensureDriver = require('./ensure-driver')
, create = Object.create, defineProperty = Object.defineProperty, keys = Object.keys
, stringify = JSON.stringify
, resolved = deferred(null)
, isIdent = RegExp.prototype.test.bind(/^[a-z][a-z0-9A-Z]*$/);
var notImplemented = function () { throw customError("Not implemented", 'NOT_IMPLEMENTED'); };
var Driver = module.exports = Object.defineProperties(function (/*options*/) {
var options;
if (!(this instanceof Driver)) return new Driver(arguments[0]);
options = Object(arguments[0]);
if (options.database != null) this.database = ensureDatabase(options.database);
if (options.name != null) this.name = ensureString(options.name);
if (options.resolveAutoSaveFilter != null) {
defineProperty(this, '_resolveAutoSaveFilter',
d(ensureCallable(options.resolveAutoSaveFilter)));
}
if (options.storageNames != null) {
aFrom(ensureIterable(options.storageNames), this.getStorage, this);
this._isStoragesCreationLocked = true;
}
}, {
storageClass: d(Storage),
reducedStorageClass: d(ReducedStorage)
});
ee(Object.defineProperties(Driver.prototype, assign({
name: d(null),
getStorage: d(function (name) {
var storageOptions;
name = ensureString(name);
if (!isIdent(name)) throw new TypeError(stringify(name) + " is an invalid storage name");
if (this._storages[name]) return this._storages[name];
if (this._isStoragesCreationLocked && (this._storages[name] == null)) {
throw new Error("Storage name " + stringify(name) + " is not recognized, and " +
"generation of new storages is not allowed at this point");
}
if (this.database && (name !== 'base')) {
storageOptions = { autoSaveFilter: this._resolveAutoSaveFilter(name) };
}
defineProperty(this._storages, name, d('cew',
new this.constructor.storageClass(this, name, storageOptions)));
return this._storages[name];
}),
hasStorage: d(function (name) {
name = ensureString(name);
return this._resolveAllStorages()(function () {
return Boolean(this._storages[name]);
}.bind(this));
}),
getStorages: d(function () {
return this._resolveAllStorages()(function () { return copy(this._storages); }.bind(this));
}),
getReducedStorage: d(function () { return this._reducedStorage; }),
loadAll: d(function () {
if (!this.database) throw new Error("No database registered to load data in");
return this._resolveAllStorages()(function () {
return deferred.map(keys(this._storages),
function (name) { return this[name].loadAll(); }, this._storages);
}.bind(this));
}),
export: d(function (externalDriver) {
ensureDriver(externalDriver);
return deferred(
this._resolveAllStorages()(function () {
return deferred.map(keys(this._storages), function (name) {
return this[name].export(externalDriver.getStorage(name));
}, this._storages);
}.bind(this)),
this._reducedStorage.export(externalDriver._reducedStorage)
)(Function.prototype);
}),
clear: d(function () {
return deferred(
this._resolveAllStorages()(function () {
return deferred.map(keys(this._storages),
function (name) {
return this[name].drop().aside(function () {
defineProperty(this, name, d(false));
}.bind(this));
}, this._storages);
}.bind(this)),
this._reducedStorage.drop().aside(function () {
delete this._reducedStorage;
}.bind(this))
)(Function.prototype);
}),
close: d(function () {
return deferred(
deferred.map(keys(this._storages), function (name) {
return this[name].close();
}, this._storages)(function () {
return this.__close();
}.bind(this)),
this.hasOwnProperty('_reducedStorage') && this._reducedStorage.close()
);
}),
onDrain: d.gs(function () {
return deferred(
deferred.map(keys(this._storages), function (name) {
return this[name].onDrain;
}, this._storages),
this.hasOwnProperty('_reducedStorage') && this._reducedStorage.onDrain
);
}),
recalculateAllSizes: d(function () {
return this._resolveAllStorages()(function () {
return deferred.map(keys(this._storages),
function (name) { return this[name].recalculateAllSizes(); }, this._storages);
}.bind(this));
}),
toString: d(function () {
return '[dbjs-driver' + (this.name ? (' ' + this.name) : '') + ']';
}),
_load: d(function (id, value, stamp) {
var proto;
if (this._loadedEventsMap[id + '.' + stamp]) return;
this._loadedEventsMap[id + '.' + stamp] = true;
value = unserializeValue(value, this.database.objects);
if (value && value.__id__ && (value.constructor.prototype === value)) proto = value.constructor;
return new Event(this.database.objects.unserialize(id, proto), value, stamp, 'persistentLayer');
}),
_resolveAutoSaveFilter: d(function (name) {
var className = capitalize.call(name);
return function (event, previous) {
if (event.object.master.constructor.__id__ === className) return true;
if (event.value) return false;
if (!previous || !previous.value) return false;
if (event.object !== event.object.master) return false;
if (!previous.value.constructor) return false;
return (previous.value.constructor.__id__ === className);
};
}),
__resolveAllStorages: d(notImplemented),
__close: d(notImplemented)
}, autoBind({
emitError: d(emitError)
}), lazy({
_loadedEventsMap: d(function () { return create(null); }),
_storages: d(function () { return create(null); }),
_resolveAllStorages: d(function () {
return this._isStoragesCreationLocked ? resolved : this.__resolveAllStorages();
}),
_reducedStorage: d(function () { return new this.constructor.reducedStorageClass(this); })
}))));