-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Json Ripper and Data Providers (#221)
* fix/removeRelationshipLikeJsonApiSpecification * Json Ripper and Data Provider tested. Package json updated, last version of Jest used * jest types updated * getAll done for new JsonRipper * ripper ready for get() * circle ci require 1 node * json ripper fixed for hasOne = null * proposal: collection have resources[] or basicresources[] (#224) * Feature/without memory service (#226) * ContentType added (#227) * feature/dexie-work-with-elements-and-collections (#228) * Improve typing and test cahememory (#229) * fix/get-with-include (#230)
- Loading branch information
Showing
58 changed files
with
2,000 additions
and
1,255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export interface IObject { | ||
[key: string]: any; | ||
} | ||
|
||
export interface IElement { | ||
key: string; | ||
content: IObject; | ||
} | ||
|
||
export type TableNameType = 'collections' | 'elements'; | ||
|
||
export interface IDataProvider { | ||
getElement(key: string, table_name: TableNameType): Promise<IObject | Array<IObject>>; | ||
getElements(keys: Array<string>, table_name: TableNameType): Promise<Array<IObject>>; | ||
saveElements(elements: Array<IElement>, table_name: TableNameType): Promise<void>; | ||
updateElements(key_start_with: string, new_data: IObject, table_name: TableNameType): Promise<void>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { IDataProvider, IObject, IElement } from './data-provider'; | ||
import Dexie from 'dexie'; | ||
|
||
export class DexieDataProvider implements IDataProvider { | ||
private static db: Dexie; | ||
|
||
public constructor() { | ||
if (DexieDataProvider.db) { | ||
return; | ||
} | ||
DexieDataProvider.db = new Dexie('dexie_data_provider'); | ||
DexieDataProvider.db.version(1).stores({ | ||
collections: '', | ||
elements: '' | ||
}); | ||
} | ||
|
||
public async getElement(key: string, table_name = 'elements'): Promise<IObject | Array<IObject>> { | ||
await DexieDataProvider.db.open(); | ||
const data = await DexieDataProvider.db.table(table_name).get(key); | ||
if (data === undefined) { | ||
throw new Error(key + ' not found.'); | ||
} | ||
|
||
return data; | ||
} | ||
|
||
public async getElements(keys: Array<string>, table_name = 'elements'): Promise<Array<IObject>> { | ||
let data = {}; | ||
await DexieDataProvider.db | ||
.table(table_name) | ||
.where(':id') | ||
.anyOf(keys) | ||
.each(element => { | ||
data[element.data.type + '.' + element.data.id] = element; | ||
}); | ||
|
||
// we need to maintain same order, database return ordered by key | ||
return keys.map(key => { | ||
return data[key]; | ||
}); | ||
} | ||
|
||
// @todo implement dexie.modify(changes) | ||
// @todo test | ||
public async updateElements(key_start_with: string, changes: IObject, table_name = 'elements'): Promise<void> { | ||
return DexieDataProvider.db.open().then(async () => { | ||
if (key_start_with === '') { | ||
return DexieDataProvider.db.table(table_name).clear(); | ||
} else { | ||
return DexieDataProvider.db | ||
.table(table_name) | ||
.where(':id') | ||
.startsWith(key_start_with) | ||
.delete() | ||
.then(() => undefined); | ||
} | ||
}); | ||
} | ||
|
||
public async saveElements(elements: Array<IElement>, table_name = 'elements'): Promise<void> { | ||
let keys: Array<string> = []; | ||
let items = elements.map(element => { | ||
keys.push(element.key); | ||
|
||
return element.content; | ||
}); | ||
|
||
return DexieDataProvider.db.open().then(() => { | ||
DexieDataProvider.db.table(table_name).bulkPut(items, keys); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.