-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
713 additions
and
21 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
import { Queryable } from '../implements/index'; | ||
import { LocalStorageQueryable } from '../drivers/linq-fns.localStogare'; | ||
import { GistQueryable } from '../drivers/linq-fns.gits'; | ||
|
||
window['Queryable'] = Queryable | ||
window['Queryable'] = Queryable | ||
window['LocalStorageQueryable'] = LocalStorageQueryable; | ||
window['GistQueryable'] = GistQueryable; |
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,4 @@ | ||
export interface IJsonRepository<T> { | ||
readData(data: Object) | ||
writeData(data: Object); | ||
} |
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,50 @@ | ||
import { JsonQueryable } from './linq-fns.json'; | ||
import { IJsonRepository } from './interfaces/json.repository.interface'; | ||
import axios from 'axios'; | ||
|
||
export class GistQueryable<T> extends JsonQueryable<T> implements IJsonRepository<T>{ | ||
private _dbName = '_lqfnsDb'; | ||
private _gistApiUrl = 'https://api.github.com/gists'; | ||
private _gistFileId; | ||
private _token; | ||
private _fileName; | ||
private jsonFormat = { | ||
"description": "", | ||
"public": true, | ||
"files": { | ||
} | ||
} | ||
|
||
constructor(gistFileId: string, token: string, repoName: string, key?: string) { | ||
super(repoName, key); | ||
this._gistFileId = gistFileId; | ||
this._token = token; | ||
} | ||
|
||
readData() { | ||
return axios.get(`${this._gistApiUrl}/${this._gistFileId}`) | ||
.then((response: any) => { | ||
this._fileName = Object.keys((response.data.files as Object))[0]; | ||
this.updateJsonFormat(response.data); | ||
|
||
const _content = response.data.files[this._fileName].content; | ||
if (!_content) return {}; | ||
if (typeof JSON.parse(_content) === 'string') return JSON.parse(JSON.parse(_content)); | ||
return JSON.parse(_content); | ||
}); | ||
} | ||
|
||
writeData(obj: Object) { | ||
this.jsonFormat.files[this._fileName].content = JSON.stringify(obj); | ||
return axios.patch(`${this._gistApiUrl}/${this._gistFileId}?access_token=${this._token}`, this.jsonFormat) | ||
.then(response => { | ||
return true; | ||
}); | ||
} | ||
|
||
private updateJsonFormat(data) { | ||
this.jsonFormat.description = data.description; | ||
this.jsonFormat.files[this._fileName] = {}; | ||
this.jsonFormat.files[this._fileName].content = data.files[this._fileName].content | ||
} | ||
} |
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,78 @@ | ||
import { BaseRepository } from './base.repository'; | ||
import { IRepository } from './interfaces/repository.interface'; | ||
import { IJsonRepository } from './interfaces/json.repository.interface'; | ||
import { IMethods } from '../intefaces'; | ||
import { Queryable } from '../implements'; | ||
import { Utils } from '../utils'; | ||
const uniqid = require('uniqid'); | ||
|
||
export class JsonQueryable<T> extends BaseRepository<T> implements IJsonRepository<T>, IRepository<T> { | ||
|
||
globalData: {}; | ||
private _repoName = ''; | ||
private _key; | ||
private _keyProp = '__id'; | ||
|
||
readData(): any | Promise<any> { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
writeData(data: Object): void { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
constructor(repoName: string, key?: string) { | ||
super(); | ||
setTimeout(() => { | ||
if (Utils.isPromise(this.readData())) { | ||
(this.readData() as Promise<any>).then(x => this.globalData = x); | ||
} else | ||
this.globalData = this.readData(); | ||
}, 0); | ||
this._repoName = repoName; | ||
this._key = key; | ||
} | ||
|
||
getQuery(): IMethods<T> { | ||
return Queryable.from(this.tableData()); | ||
} | ||
|
||
_add(item: T) { | ||
this.tableData().push(this.prepareData(item)); | ||
} | ||
|
||
_remove<S extends T & { __id }>(item: S) { | ||
const removeItem = !this._key | ||
? this.tableData<S>().find(x => x.__id === item.__id) | ||
: this.tableData<S>().find(x => x[this._key] === item[this._key]); | ||
|
||
const index = this.tableData().indexOf(removeItem); | ||
if (index > -1) this.tableData().splice(index, 1); | ||
} | ||
|
||
_update<S extends T & { __id }>(item: S) { | ||
const removeItem = !this._key | ||
? this.tableData<S>().find(x => x.__id === item.__id) | ||
: this.tableData<S>().find(x => x[this._key] === item[this._key]); | ||
|
||
const index = this.tableData().indexOf(removeItem); | ||
if (index > -1) this.tableData()[index] = item; | ||
} | ||
|
||
private tableData<T>(): T[] { | ||
if (!this.globalData[this._repoName]) | ||
this.globalData[this._repoName] = []; | ||
|
||
return this.globalData[this._repoName]; | ||
} | ||
|
||
private prepareData(item: T) { | ||
if (!this._key) | ||
item[this._keyProp] = uniqid(); | ||
return item; | ||
} | ||
|
||
finalCallback() { | ||
this.writeData(this.globalData); | ||
} | ||
} |
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,24 @@ | ||
import { JsonQueryable } from './linq-fns.json'; | ||
import { IJsonRepository } from './interfaces/json.repository.interface'; | ||
|
||
export class LocalStorageQueryable<T> extends JsonQueryable<T> implements IJsonRepository<T>{ | ||
|
||
private _dbName = '__lqfnsDb'; | ||
|
||
constructor(repoName: string, key?: string) { | ||
super(repoName, key); | ||
} | ||
|
||
readData() { | ||
const dbString = localStorage.getItem(this._dbName || '__lqfnsDb'); | ||
if (dbString) | ||
return JSON.parse(dbString); | ||
return {}; | ||
} | ||
|
||
writeData(obj: Object): boolean { | ||
const dbString = JSON.stringify(obj); | ||
localStorage.setItem(this._dbName, dbString); | ||
return true; | ||
} | ||
} |
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 |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
] | ||
}, | ||
"baseUrl": "./", | ||
"declaration": true, | ||
// "declaration": true, | ||
}, | ||
"exclude": [ | ||
"demo", | ||
|
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