PageSourceSearch

https://www.happycampus.com/js/IDB.js?jsVer=20260928.1

js happycampus.com collected 2026-10-01 10:43:12 UTC 6,957 bytes, 313 lines download raw bytes

1
2/**
3 * IndexedDB 관리 클래스
4 * *
5 * @param {string} dbName 데이터베이스명
6 * @param {string} version 버전
7 * @param {string} tableName 테이블 명
8 * @param {function} [openCallback]
9 * @param {Object} [initParams
10 * @constructor
11 */
12function IDB(dbName, version, tableName, openCallback, initParams) {
13	// // IndexedDb 객체
14	this.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB || window.shimIndexedDB;
15
16	this.db = null;
17
18	try {
19		if(!this.indexedDB) {
20			throw  {
21				'code' : -1,
22				'message' : '해당 브라우저에서는 IndexedDB를 지원하지 않습니다.'
23			}
24		}
25
26		// 데이터 베이스를 열어 준다.
27		this.open(dbName, version, tableName, openCallback, initParams);
28	} catch(e) {
29
30	}
31}
32
33/**
34 * 데이터 베이스를 시작(열기)한다.
35 * 기본적으로 Autoincrement 를 사용한다.
36 *
37 * @param {string} dbName 데이터 베이스 명
38 * @param {number} version 버전 (integer 형)
39 * @param {string} tableName
40 * @param {function} openCallback
41 * @param {Object} initParams
42 */
43IDB.prototype.open = function (dbName, version, tableName, openCallback, initParams) {
44	this.dbName = dbName;
45	this.tableName = tableName;
46	this.version = version;
47	if(typeof initParams != 'object') initParams = {};
48
49	var request = this.indexedDB.open(this.dbName , this.version);
50	request.onerror = function(event) {
51		throw {
52			'code' : event.target.errorCode,
53			'message' : '데이터베이스를 열지 못했습니다.'
54		};
55	};
56
57	request.onsuccess = function(event) {
58		this.db = event.target.result;
59
60		if(typeof openCallback == 'function') {
61			openCallback();
62		}
63	}.bind(this);
64
65	request.onupgradeneeded = function(event) {
66		this.db = request.result;
67
68		var objectStore = this.db.createObjectStore(
69			this.tableName,
70			event.hasOwnProperty('params') ? event.params : null
71		);
72
73		if(event.hasOwnProperty('indices') && event.indices.length > 0) {
74			for(var i in event.indices) {
75				if(event.indices.hasOwnProperty(i)) {
76					objectStore.createIndex(
77						event.indices[i].indexName,
78						event.indices[i].keyPath,
79						event.indices[i].params
80					);
81				}
82			}
83		}
84	}.bind(this, initParams);
85};
86
87/**
88 * 데이터 삽입시 호출할 콜백 함수
89 * @type {null}
90 */
91IDB.prototype.addCallback = null;
92
93/**
94 * 데이터 불러 오기시 호출할 콜백
95 * @type {null}
96 */
97IDB.prototype.getCallback = null;
98
99/**
100 * 데이터 베이스를 삭제 한다.
101 * @param dbName
102 */
103IDB.prototype.deleteDatabase = function(dbName) {
104	window.indexedDB.deleteDatabase(dbName);
105};
106
107/**
108 * 트랜잭션을 생성한다.
109 */
110IDB.prototype.setTransaction = function(tableName, mode) {
111	try{
112		var transaction = this.db.transaction(tableName, mode);
113	} catch(e) {
114	}
115
116	/*
117	if(typeof this.tnxComplete == 'function') {
118		transaction.oncomplete = this.tnxComplete;
119	}
120
121	if(typeof this.tnxError == 'function') {
122		transaction.oncomplete = this.tnxError;
123	}
124	*/
125
126	return transaction;
127};
128
129/**
130 * Object store 를 가져온다.
131 *
132 * @param transaction
133 * @param tableName
134 * @returns {IDBObjectStore|*}
135 */
136IDB.prototype.objectStore = function(transaction, tableName) {
137	var objectStore = transaction.objectStore(tableName);
138	objectStore.onsuccess = function(event) {
139
140	};
141
142	return objectStore;
143};
144
145/**
146 * DB에 데이터 넣기
147 *
148 * @param data 삽입할 데이터
149 * @param callback 콜백 (옵션)
150 * @param tableName 테이블명 (옵션)
151 */
152IDB.prototype.add = function(data, callback , tableName) {
153	if(tableName) {
154		this.tableName = tableName;
155	}
156
157	var transaction;
158	try {
159		transaction = this.setTransaction(this.tableName, "readwrite");
160	} catch(e) {
161	}
162
163	var objectStore = this.objectStore(transaction, this.tableName);
164
165	var request = objectStore.add(data);
166
167	request.onsuccess = function(event) {
168		if(typeof callback == 'function') {
169			callback(event.target.result);
170		}
171	};
172};
173
174
175/**
176 * 데이터 베이스에서 값을 가지고 온다.
177 *
178 * @param key 가져올 키
179 * @param callback 콜백 함수 (옵션)
180 * @param tableName 테이블명 (옵션)
181 */
182IDB.prototype.get = function(key, callback, tableName) {
183	if(tableName) {
184		this.tableName = tableName;
185	}
186
187	var transaction = this.setTransaction(this.tableName, "readwrite");
188
189	var objectStore = this.objectStore(transaction, this.tableName);
190	var request = objectStore.get(key);
191
192	request.onsuccess = function(event) {
193		if(typeof callback == 'function') {
194			callback(event.target.result);
195		}
196	};
197
198	request.onerror = function(event) {
199	};
200};
201
202/**
203 * 데이터 베이스에서 값을 삭제 한다.
204 * @param {string} key
205 * @param {function} callback
206 * @param {string} tableName
207 */
208IDB.prototype.del = function(key, callback, tableName) {
209	if(tableName) {
210		this.tableName = tableName;
211	}
212	var transaction = this.setTransaction(this.tableName, "readwrite");
213
214	var objectStore = this.objectStore(transaction, this.tableName);
215
216	var request = objectStore.delete(key);
217
218	request.onsuccess = function(event) {
219		if(typeof callback == 'function') {
220			callback(key);
221		}
222	};
223
224	request.onerror = function(event) {
225	};
226};
227
228/**
229 * 테이블을 비워준다.
230 *
231 * @param tableName
232 */
233IDB.prototype.clear = function(tableName) {
234	if(tableName) {
235		this.tableName = tableName;
236	}
237	var transaction = this.setTransaction(this.tableName, "readwrite");
238
239	var objectStore = this.objectStore(transaction, this.tableName);
240	var request = objectStore.clear();
241	request.onsuccess = function(event) {
242	};
243
244	request.onerror = function(event) {
245	};
246};
247
248IDB.prototype.search = function(search, callback, tableName) {
249	if(tableName) {
250		this.tableName = tableName;
251	}
252
253	var transaction = this.setTransaction(this.tableName, "readwrite");
254
255	var objectStore = this.objectStore(transaction, this.tableName);
256
257	var keyRange = window.IDBKeyRange.only(search);
258
259	var request = objectStore.index('keyword').openCursor(keyRange);
260
261	var result = {};
262
263	request.onsuccess = function(event) {
264		var cursor = event.target.result;
265		if(cursor) {
266			result[cursor.key] = cursor.value;
267			cursor.continue();
268		} else {
269			if(typeof callback == 'function') {
270				callback(result);
271			}
272		}
273	};
274};
275
276/**
277 * 모든 데이터를 가지고 온다.
278 *
279 * @param callback 데이터를 가지고 온 후 실행할 콜백
280 * @param tableName 테이블 명
281 */
282IDB.prototype.getAll = function(callback, tableName) {
283	if(tableName) {
284		this.tableName = tableName;
285	}
286
287	var transaction = this.setTransaction(this.tableName, "readwrite");
288
289	var objectStore = this.objectStore(transaction, this.tableName);
290
291	var request = objectStore.openCursor();
292
293	var result = [];
294
295	request.onsuccess = function(event) {
296		var data, x, cursor = event.target.result;
297		if(cursor) {
298			// result[cursor.key] = cursor.value;
299			var data = {
300				'key' : cursor.key
301			};
302			for(x in cursor.value) {
303				data[x] = cursor.value[x];
304			}
305			result.push(data);
306			cursor.continue();
307		} else {
308			if(typeof callback == 'function') {
309				callback(result);
310			}
311		}
312	};
313};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.