-
Notifications
You must be signed in to change notification settings - Fork 3
/
localstorage-schema.js
147 lines (131 loc) · 4.95 KB
/
localstorage-schema.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
(function() {
function genId() {
return Math.floor((1 + Math.random()) * 0x1000000)
.toString(16)
.substring(1);
}
// Accessors.
var RawStorageAccessor = function(storage) {
this.storage = storage;
};
RawStorageAccessor.prototype.get = function(key) {
return this.storage.getItem(key);
};
RawStorageAccessor.prototype.set = function(key, value) {
return this.storage.setItem(key, value);
};
RawStorageAccessor.prototype.remove = function(key) {
return this.storage.removeItem(key);
};
var JSONStorageAccessor = function(storage) {
this.storage = storage || localStorage;
};
JSONStorageAccessor.prototype.get = function(key) {
return JSON.parse(this.storage.getItem(key));
};
JSONStorageAccessor.prototype.set = function(key, value) {
return this.storage.setItem(key, JSON.stringify(value));
};
JSONStorageAccessor.prototype.remove = function(key) {
return this.storage.removeItem(key);
};
// -Accessors
var localStorageSchema = window.localStorageSchema = function(options) {
options = options || {};
var rootPrefix = options.schemaPrefix || '_lsscm',
storage = options.storage || localStorage,
schemaDelimiter = options.schemaDelimiter || ':',
schemaAccessor = new (options.schemaAccessor || JSONStorageAccessor)(storage);
var DaoMixin = function(schemaPrefix) {
this.schemaPrefix = schemaPrefix;
};
DaoMixin.prototype.createKey = function(key) {
return this.schemaPrefix + schemaDelimiter + key;
};
DaoMixin.prototype.get = function(id) {
return schemaAccessor.get(this.createKey(id));
};
DaoMixin.prototype.set = function(id, mixed) {
return schemaAccessor.set(this.createKey(id), mixed);
};
DaoMixin.prototype.del = function(id) {
return schemaAccessor.remove(this.createKey(id));
};
DaoMixin.prototype.read = function() {
return schemaAccessor.get(this.schemaPrefix);
};
DaoMixin.prototype.persist = function(mixed) {
return schemaAccessor.set(this.schemaPrefix, mixed);
};
var NodeAccessor = function(schemaPrefix) {
DaoMixin.call(this, schemaPrefix);
};
NodeAccessor.prototype = new DaoMixin();
NodeAccessor.prototype.collection = function(collectionName) {
return new CollectionDao(this.createKey(collectionName));
};
NodeAccessor.prototype.object = function(name) {
return new ObjectDao(this.createKey(name));
};
NodeAccessor.prototype.node = function(name) {
return new NodeAccessor(this.createKey(name));
};
var ObjectDao = function(schemaPrefix) {
NodeAccessor.call(this, schemaPrefix);
};
ObjectDao.prototype = new NodeAccessor();
var CollectionDao = function(schemaPrefix) {
DaoMixin.call(this, schemaPrefix);
};
CollectionDao.prototype = new DaoMixin();
CollectionDao.prototype.keys = function() {
return this.read() || [];
};
CollectionDao.prototype.all = function() {
return this.keys().map(this.get.bind(this));
};
CollectionDao.prototype.allWithKeys = function() {
};
CollectionDao.prototype.allObjects = function() {
return this.keys().map(function(key) {
return new ObjectDao(this.createKey(key));
}, this);
};
CollectionDao.prototype.object = function(name) {
return new ObjectDao(this.createKey(name));
};
CollectionDao.prototype.insert = function(mixed, id) {
var keys = this.keys() || [],
newId;
if (id) {
if (keys.indexOf(id) != -1) {
throw Error('Id ' + id + ' already exist.');
} else {
newId = id;
}
} else {
newId = genId();
while (keys.indexOf(newId) != -1) newId = genId();
}
keys.push(newId);
this.set(newId, mixed);
this.persist(keys);
return newId;
};
CollectionDao.prototype.select = function(filter, condition) {
return this.all().filter(condition);
};
CollectionDao.prototype.del = function(id) {
var keys = this.keys(),
index = keys.indexOf(id);
keys = keys.splice(index, index + 1);
this.persist(keys);
return DaoMixin.prototype.del.call(this, id);
};
return new NodeAccessor(rootPrefix);
};
localStorageSchema.accessors = {
JSONStorageAccessor: JSONStorageAccessor,
RawStorageAccessor: RawStorageAccessor
};
})();