1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import { openDB } from 'idb';
|
9 | import '../_version.js';
|
10 | const DB_VERSION = 3;
|
11 | const DB_NAME = 'workbox-background-sync';
|
12 | const REQUEST_OBJECT_STORE_NAME = 'requests';
|
13 | const QUEUE_NAME_INDEX = 'queueName';
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | export class QueueDb {
|
22 | constructor() {
|
23 | this._db = null;
|
24 | }
|
25 | |
26 |
|
27 |
|
28 |
|
29 |
|
30 | async addEntry(entry) {
|
31 | const db = await this.getDb();
|
32 | const tx = db.transaction(REQUEST_OBJECT_STORE_NAME, 'readwrite', {
|
33 | durability: 'relaxed',
|
34 | });
|
35 | await tx.store.add(entry);
|
36 | await tx.done;
|
37 | }
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 | async getFirstEntryId() {
|
44 | const db = await this.getDb();
|
45 | const cursor = await db
|
46 | .transaction(REQUEST_OBJECT_STORE_NAME)
|
47 | .store.openCursor();
|
48 | return cursor === null || cursor === void 0 ? void 0 : cursor.value.id;
|
49 | }
|
50 | |
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | async getAllEntriesByQueueName(queueName) {
|
57 | const db = await this.getDb();
|
58 | const results = await db.getAllFromIndex(REQUEST_OBJECT_STORE_NAME, QUEUE_NAME_INDEX, IDBKeyRange.only(queueName));
|
59 | return results ? results : new Array();
|
60 | }
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | async getEntryCountByQueueName(queueName) {
|
68 | const db = await this.getDb();
|
69 | return db.countFromIndex(REQUEST_OBJECT_STORE_NAME, QUEUE_NAME_INDEX, IDBKeyRange.only(queueName));
|
70 | }
|
71 | |
72 |
|
73 |
|
74 |
|
75 |
|
76 | async deleteEntry(id) {
|
77 | const db = await this.getDb();
|
78 | await db.delete(REQUEST_OBJECT_STORE_NAME, id);
|
79 | }
|
80 | |
81 |
|
82 |
|
83 |
|
84 |
|
85 | async getFirstEntryByQueueName(queueName) {
|
86 | return await this.getEndEntryFromIndex(IDBKeyRange.only(queueName), 'next');
|
87 | }
|
88 | |
89 |
|
90 |
|
91 |
|
92 |
|
93 | async getLastEntryByQueueName(queueName) {
|
94 | return await this.getEndEntryFromIndex(IDBKeyRange.only(queueName), 'prev');
|
95 | }
|
96 | |
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | async getEndEntryFromIndex(query, direction) {
|
106 | const db = await this.getDb();
|
107 | const cursor = await db
|
108 | .transaction(REQUEST_OBJECT_STORE_NAME)
|
109 | .store.index(QUEUE_NAME_INDEX)
|
110 | .openCursor(query, direction);
|
111 | return cursor === null || cursor === void 0 ? void 0 : cursor.value;
|
112 | }
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
118 | async getDb() {
|
119 | if (!this._db) {
|
120 | this._db = await openDB(DB_NAME, DB_VERSION, {
|
121 | upgrade: this._upgradeDb,
|
122 | });
|
123 | }
|
124 | return this._db;
|
125 | }
|
126 | |
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 | _upgradeDb(db, oldVersion) {
|
134 | if (oldVersion > 0 && oldVersion < DB_VERSION) {
|
135 | if (db.objectStoreNames.contains(REQUEST_OBJECT_STORE_NAME)) {
|
136 | db.deleteObjectStore(REQUEST_OBJECT_STORE_NAME);
|
137 | }
|
138 | }
|
139 | const objStore = db.createObjectStore(REQUEST_OBJECT_STORE_NAME, {
|
140 | autoIncrement: true,
|
141 | keyPath: 'id',
|
142 | });
|
143 | objStore.createIndex(QUEUE_NAME_INDEX, QUEUE_NAME_INDEX, { unique: false });
|
144 | }
|
145 | }
|