Skip to content

Commit

Permalink
Add localStogare and gist drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
jinhduong committed Apr 30, 2018
1 parent 48b46e9 commit 95a86f1
Show file tree
Hide file tree
Showing 12 changed files with 713 additions and 21 deletions.
534 changes: 527 additions & 7 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@
"typedoc": "^0.11.1",
"typescript": "^2.8.1",
"webpack": "^4.6.0",
"webpack-bundle-analyzer": "^2.11.1",
"webpack-cli": "^2.0.15"
},
"dependencies": {
"tslib": "^1.9.0"
"axios": "^0.18.0",
"tslib": "^1.9.0",
"uniqid": "^4.1.1"
}
}
6 changes: 5 additions & 1 deletion src/browser/linq-fns.ts
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;
15 changes: 10 additions & 5 deletions src/drivers/base.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ export class BaseRepository<T> implements IRepository<T> {

private _methods: { name: 'ADD' | 'UPD' | 'DEL', item: T }[] = [];

getQuery(predicate: (
getQuery(predicate?: (
ref: database.Reference) => database.Query,
action: "value" | "child_added" | "child_changed" | "child_moved" | "child_removed"): IMethods<T> {
action?: "value" | "child_added" | "child_changed" | "child_moved" | "child_removed"): IMethods<T> {
throw new Error("Method not implemented.");
}

Expand Down Expand Up @@ -40,12 +40,13 @@ export class BaseRepository<T> implements IRepository<T> {
for (let i = 0, li = this._methods.length; i < li; i++) {
const _tmp = this._methods[i];
switch (_tmp.name) {
case 'ADD': this._add(_tmp.item);
case 'UPD': this._update(_tmp.item);
case 'DEL': this._remove(_tmp.item);
case 'ADD': this._add(_tmp.item); break;
case 'UPD': this._update(_tmp.item); break;
case 'DEL': this._remove(_tmp.item); break;
default: throw new Error("Method just only add, update, delete");
}
}
this.finalCallback();
this._methods = [];
}

Expand All @@ -60,4 +61,8 @@ export class BaseRepository<T> implements IRepository<T> {
_update(item: T): void {
throw new Error("Method not implemented.");
}

finalCallback(): void {
return;
}
}
4 changes: 4 additions & 0 deletions src/drivers/interfaces/json.repository.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface IJsonRepository<T> {
readData(data: Object)
writeData(data: Object);
}
8 changes: 5 additions & 3 deletions src/drivers/interfaces/repository.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { IMethods } from '../../intefaces/index';
import { database } from '../database.interface';

export interface IRepository<T> {
getQuery(
predicate: (ref: database.Reference) => database.Query,
action: database.EventType): IMethods<T>;
getQuery: {
(predicate?: (ref: database.Reference) => database.Query, action?: database.EventType): IMethods<T>;
(): IMethods<T>;
};
add(item: T, autoCommit?: boolean): void;
remove(item: T, autoCommit?: boolean): void;
update(item: T, autoCommit?: boolean): void;
commitChanges(): void;
finalCallback(): void;

// methods implements
_add(item: T, cb?: (rs: any) => any): void;
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/linq-fns-firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class FireBaseQueryale<T> extends BaseRepository<T> implements IRepositor
}

getQuery(
predicate: (ref: database.Reference) => database.Query,
predicate?: (ref: database.Reference) => database.Query,
action: database.EventType = "value"): IMethods<T> {

return Queryable.from(
Expand Down
50 changes: 50 additions & 0 deletions src/drivers/linq-fns.gits.ts
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
}
}
78 changes: 78 additions & 0 deletions src/drivers/linq-fns.json.ts
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);
}
}
24 changes: 24 additions & 0 deletions src/drivers/linq-fns.localStogare.ts
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;
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
]
},
"baseUrl": "./",
"declaration": true,
// "declaration": true,
},
"exclude": [
"demo",
Expand Down
6 changes: 4 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
const path = require('path');
const HTMLWebpackPlugin = require('html-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;

module.exports = {
entry: {
'linq-fns': './dist/browser/linq-fns.js',
},
plugins: [
new HTMLWebpackPlugin({
title: 'Code Splitting'
})
title: 'linq-fns'
}),
new BundleAnalyzerPlugin()
],
output: {
filename: `[name].min.js`,
Expand Down

0 comments on commit 95a86f1

Please sign in to comment.