-
Notifications
You must be signed in to change notification settings - Fork 1
/
IndexedDB.js
169 lines (138 loc) · 3.15 KB
/
IndexedDB.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
// IndexedDB module for Node.js
//
// Jesús Leganés Combarro "Piranna" <[email protected]>
//
// This module implements the IndexedDB specification using the LevelDB module
// as underlying database engine. It's mainly intention is to port client-side
// IndexedDB intensive applications to Node.js.
//
// It is based on IndexedDB-javascript.js polyfill from ShareIt! project
// https://github.com/piranna/ShareIt
var leveldb = require('leveldb');
function IDBRequest()
{
this.target = {};
};
IDBRequest.prototype =
{
set onsuccess(func)
{
this._onsuccess = func;
var event = {target: this.target};
func.call(this, event);
}
};
function IDBOpenDBRequest()
{
IDBRequest.call(this);
// this.prototype.__defineSetter__("onupgradeneeded", function(func)
// {
// var event = {target: this.target}
// func.call(this, event)
// })
};
IDBOpenDBRequest.prototype = new IDBRequest();
function IDBCursor()
{
this._objects = [];
this._index = 0;
this.continue = function()
{
this._index += 1;
var event = {target: {}};
if(this.value)
event.target.result = this;
this._request._onsuccess(event);
};
}
IDBCursor.prototype =
{
get value()
{
return this._objects[this._index];
}
};
function IDBObjectStore()
{
var objects = {}
this.add = function(value, key)
{
return this.put(value, key);
}
this.get = function(key)
{
var request = new IDBRequest();
request.result = objects[key];
return request;
}
this.openCursor = function(range)
{
var request = new IDBRequest();
if(Object.keys(objects).length)
{
// Fill the cursor with the objectstore objects
var cursor = new IDBCursor();
for(var key in objects)
cursor._objects.push(objects[key]);
// Link the request and the cursor between them
request.target.result = cursor;
cursor._request = request;
}
return request;
}
this.put = function(value, key)
{
if(this.keyPath)
{
if(key)
throw DOMException;
key = value[this.keyPath];
}
if(!key)
throw DOMException;
objects[key] = value;
var request = new IDBRequest();
request.result = objects[key];
return request;
};
}
function IDBTransaction()
{
this.objectStore = function(name)
{
return this.db._stores[name];
};
}
function IDBDatabase()
{
this._stores = {};
this.createObjectStore = function(name, optionalParameters)
{
var objectStore = new IDBObjectStore();
if(optionalParameters)
objectStore.keyPath = optionalParameters.keyPath;
this._stores[name] = objectStore;
}
this.setVersion = function(version)
{
this.version = version;
return new IDBRequest();
}
this.transaction = function(storeNames, mode)
{
var result = new IDBTransaction();
result.db = this;
return result;
};
}
exports.open = function(name, version)
{
var request = new IDBOpenDBRequest();
leveldb.open(name, {create_if_missing: true}, function(error, handle)
{
var db = new IDBDatabase();
db._handle = handle;
request.result = db;
});
return request;
};