Skip to content

Commit 08147fb

Browse files
Merge pull request #2 from angular-package/develop
1.0.0-alpha
2 parents 07a89d9 + a92644e commit 08147fb

File tree

15 files changed

+375
-42
lines changed

15 files changed

+375
-42
lines changed

README.md

Lines changed: 225 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Package is **free** to use. If you enjoy it, please consider donating via [fiat]
4040
* [Demonstration](#demonstration)
4141
* [Skeleton](#skeleton)
4242
* [Code scaffolding](#code-scaffolding)
43+
* [Example usage](#example-usage)
4344
* [Documentation](#documentation)
4445
* [IDBConnection](#idbconnection) Class to open connection and create object store.
4546
* [IDBData](#idbdata) Class with opened connection (IDBConnection), to handle transaction and store.
@@ -94,6 +95,125 @@ To get more help on the Angular CLI use `ng help` or go check out the [Angular C
9495

9596
<br>
9697

98+
## Example usage
99+
100+
Prepare configuration.
101+
102+
```typescript
103+
// config.ts
104+
import { IndexedDB } from "@angular-package/indexeddb";
105+
106+
// Config.
107+
export const IDB_CONFIG = IndexedDB.config({
108+
name: 'databasename',
109+
storeNames: ['storename1', 'storename2'],
110+
store: IndexedDB.store({
111+
'storename1': {
112+
keyPath: 'id',
113+
autoIncrement: false,
114+
index: [
115+
{ name: "name", keyPath: "name", options: { unique: false } },
116+
]
117+
},
118+
'storename2': {
119+
keyPath: "id",
120+
autoIncrement: true,
121+
index: [
122+
{ name: "name", keyPath: "name", options: { unique: false } },
123+
{ name: "position", keyPath: "position", options: { unique: false } },
124+
{ name: "weight", keyPath: "weight", options: { unique: false } },
125+
{ name: "symbol", keyPath: "symbol", options: { unique: false } },
126+
],
127+
},
128+
}),
129+
version: 1
130+
});
131+
```
132+
133+
Use configuration and initialize database.
134+
135+
```typescript
136+
// example.ts
137+
import { IndexedDB } from "@angular-package/indexeddb";
138+
import { IDB_CONFIG } from './config';
139+
140+
// Initialize.
141+
const indexeddb = new IndexedDB<
142+
// Create store interface.
143+
{
144+
storename1: {
145+
id: number,
146+
name: string
147+
},
148+
storename2: {
149+
id: number,
150+
name: string,
151+
position: number,
152+
weight: number,
153+
symbol: string
154+
}
155+
}
156+
>(
157+
IDB_CONFIG.name,
158+
IDB_CONFIG.storeNames,
159+
IDB_CONFIG.store,
160+
IDB_CONFIG.version
161+
);
162+
163+
// Add by method
164+
indexeddb.query.method({
165+
'add': {
166+
'storename2': {
167+
value: {
168+
'id': 1,
169+
'name': 'name',
170+
'position': 1,
171+
'symbol': 'N',
172+
'weight': 100
173+
},
174+
'onsuccess': (result) => console.log(result),
175+
'onerror': (ev) => console.log(`error`, ev),
176+
}
177+
}
178+
})
179+
180+
// Get
181+
indexeddb.query.method({
182+
'get': {
183+
'storename2': {
184+
'query': 1,
185+
'onsuccess': (result => console.log(result)),
186+
'onerror': () => console.log(`error`)
187+
},
188+
}
189+
});
190+
191+
// Add by store
192+
indexeddb.query.store({
193+
'storename1': {
194+
'add': {
195+
'value': {
196+
'id': 2,
197+
'name': 'item'
198+
},
199+
'onsuccess': (result) => console.log(result)
200+
}
201+
}
202+
})
203+
204+
// Get
205+
indexeddb.query.store({
206+
'storename1': {
207+
'get': {
208+
'query': 2,
209+
'onsuccess': result => console.log(result)
210+
}
211+
}
212+
})
213+
```
214+
215+
<br>
216+
97217
## Documentation
98218

99219
The documentation is in construction and it's available at [https://angular-package.gitbook.io/indexedb](https://angular-package.gitbook.io/indexedb)
@@ -102,49 +222,150 @@ The documentation is in construction and it's available at [https://angular-pack
102222

103223
Class to open connection and create object store.
104224

225+
```typescript
226+
export class IDBConnection<
227+
Name extends string = string,
228+
StoreNames extends string | number | symbol = string,
229+
Version extends number = number
230+
> { ... }
231+
```
232+
105233
[GitHub](https://github.com/angular-package/indexeddb/blob/main/src/lib/idb-connection.class.ts)
106234

107235
### IDBData
108236

109237
Class with opened connection (IDBConnection), to handle transaction and store.
110238

239+
```typescript
240+
export class IDBData<
241+
Name extends string = string,
242+
StoreNames extends string | number | symbol = string,
243+
Version extends number = number,
244+
> { ... }
245+
```
246+
111247
[GitHub](https://github.com/angular-package/indexeddb/blob/main/src/lib/idb-data.class.ts)
112248

113249
### IDBQuery
114250

115251
Query store with JSON, by method-store or store-method.
116252

253+
```typescript
254+
export class IDBQuery<
255+
StoreSchema extends object,
256+
Name extends string = string,
257+
StoreNames extends keyof StoreSchema = keyof StoreSchema,
258+
Version extends number = number
259+
> { ... }
260+
```
261+
117262
[GitHub](https://github.com/angular-package/indexeddb/blob/main/src/lib/idb-query.class.ts)
118263

119264
### IDBStore
120265

121266
Store methods with database connection (IDBData).
122267

268+
```typescript
269+
export class IDBStore<
270+
StoreSchema extends object,
271+
Name extends string = string,
272+
StoreNames extends keyof StoreSchema = keyof StoreSchema,
273+
Version extends number = number,
274+
> implements IDBStoreInterface<StoreSchema, StoreNames> {
275+
...
276+
}
277+
```
278+
123279
[GitHub](https://github.com/angular-package/indexeddb/blob/main/src/lib/idb-store.class.ts)
124280

125281
### IDBConfig
126282

127283
IDB configuration used in Angular IDBModule.
128284

129-
[GitHub](https://github.com/angular-package/indexeddb/blob/main/src/lib/idb-config.class.ts)
285+
```typescript
286+
export class IDBConfig<
287+
Name extends string = string,
288+
StoreNames extends string | number | symbol = string,
289+
Version extends number = number
290+
> {
291+
name?: Name;
292+
storeNames?: StoreNames | StoreNames[];
293+
store?: IDBStoreParameters<StoreNames>;
294+
version?: Version;
295+
};
296+
```
297+
298+
[GitHub](https://github.com/angular-package/indexeddb/blob/main/src/lib/idb.config.ts)
130299

131300
### IDBModule
132301

133302
Angular Module with indexeddb service.
134303

135-
[GitHub](https://github.com/angular-package/indexeddb/blob/main/src/lib/idb-module.class.ts)
304+
```typescript
305+
@NgModule({
306+
declarations: [],
307+
imports: [
308+
CommonModule
309+
]
310+
})
311+
export class IDBModule {
312+
static forRoot(@Optional() @Inject(IDBConfig) config?: IDBConfig): ModuleWithProviders<IDBModule> {
313+
return {
314+
ngModule: IDBModule,
315+
providers: [
316+
IDBService,
317+
{provide: IDBConfig, useValue: config, multi: false}
318+
]
319+
}
320+
}
321+
static forChild(@Optional() @Inject(IDBConfig) config: IDBConfig): ModuleWithProviders<IDBModule> {
322+
return {
323+
ngModule: IDBModule,
324+
providers: [
325+
IDBService,
326+
{provide: IDBConfig, useValue: config, multi: false}
327+
]
328+
};
329+
}
330+
}
331+
```
332+
333+
[GitHub](https://github.com/angular-package/indexeddb/blob/main/src/lib/idb.module.ts)
136334

137335
### IDBService
138336

139337
Angular Service with IndexedDB class.
140338

141-
[GitHub](https://github.com/angular-package/indexeddb/blob/main/src/lib/idb-service.class.ts)
339+
```typescript
340+
@Injectable({
341+
providedIn: "root",
342+
})
343+
export class IDBService<
344+
StoreSchema extends object,
345+
Name extends string = string,
346+
StoreNames extends keyof StoreSchema = keyof StoreSchema,
347+
Version extends number = number
348+
> {
349+
...
350+
}
351+
```
352+
353+
[GitHub](https://github.com/angular-package/indexeddb/blob/main/src/lib/idb.service.ts)
142354

143355
### IndexedDB
144356

145357
Store and query for IndexedDB client-side storage.
146358

147-
[GitHub](https://github.com/angular-package/indexeddb/blob/main/src/lib/indexeddb.class.ts)
359+
```typescript
360+
export class IndexedDB<
361+
StoreSchema extends object,
362+
Name extends string = string,
363+
StoreNames extends keyof StoreSchema = keyof StoreSchema,
364+
Version extends number = number
365+
> { ... }
366+
```
367+
368+
[GitHub](https://github.com/angular-package/indexeddb/blob/main/src/lib/indexeddb.ts)
148369

149370
<br>
150371

demo/src/app/config.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// config.ts
2+
// import { IndexedDB } from "@angular-package/indexeddb";
3+
import { IndexedDB } from "./indexeddb";
4+
5+
// Config.
6+
export const IDB_CONFIG = IndexedDB.config({
7+
name: 'databasename',
8+
storeNames: ['storename1', 'storename2'],
9+
store: IndexedDB.store({
10+
'storename1': {
11+
keyPath: 'id',
12+
autoIncrement: false,
13+
index: [
14+
{ name: "name", keyPath: "name", options: { unique: false } },
15+
]
16+
},
17+
'storename2': {
18+
keyPath: "id",
19+
autoIncrement: true,
20+
index: [
21+
{ name: "name", keyPath: "name", options: { unique: false } },
22+
{ name: "position", keyPath: "position", options: { unique: false } },
23+
{ name: "weight", keyPath: "weight", options: { unique: false } },
24+
{ name: "symbol", keyPath: "symbol", options: { unique: false } }, // change to unique
25+
],
26+
},
27+
}),
28+
version: 1
29+
});

demo/src/app/example.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// example.ts
2+
// import { IndexedDB } from "@angular-package/indexeddb";
3+
import { IndexedDB } from "./indexeddb";
4+
5+
import { IDB_CONFIG } from './config';
6+
7+
// Initialize.
8+
const indexeddb = new IndexedDB<
9+
// Create store interface.
10+
{
11+
storename1: {
12+
id: number,
13+
name: string
14+
},
15+
storename2: {
16+
id: number,
17+
name: string,
18+
position: number,
19+
weight: number,
20+
symbol: string
21+
}
22+
}
23+
>(
24+
IDB_CONFIG.name,
25+
IDB_CONFIG.storeNames,
26+
IDB_CONFIG.store,
27+
IDB_CONFIG.version
28+
);
29+
30+
// Add by method
31+
indexeddb.query.method({
32+
'add': {
33+
'storename2': {
34+
value: {
35+
'id': 1,
36+
'name': 'name',
37+
'position': 1,
38+
'symbol': 'N',
39+
'weight': 100
40+
},
41+
'onsuccess': (result) => console.log(result),
42+
'onerror': (ev) => console.log(`error`, ev),
43+
}
44+
}
45+
})
46+
47+
// Get
48+
indexeddb.query.method({
49+
'get': {
50+
'storename2': {
51+
'query': 1,
52+
'onsuccess': (result => console.log(result)),
53+
'onerror': () => console.log(`error`)
54+
},
55+
}
56+
});
57+
58+
// Add by store
59+
indexeddb.query.store({
60+
'storename1': {
61+
'add': {
62+
'value': {
63+
'id': 2,
64+
'name': 'item'
65+
},
66+
'onsuccess': (result) => console.log(result)
67+
}
68+
}
69+
})
70+
71+
// Get
72+
indexeddb.query.store({
73+
'storename1': {
74+
'get': {
75+
'query': 2,
76+
'onsuccess': result => console.log(result)
77+
}
78+
}
79+
})

0 commit comments

Comments
 (0)