Skip to content

Commit

Permalink
Update firebase drivers write data
Browse files Browse the repository at this point in the history
  • Loading branch information
jinhduong committed Apr 28, 2018
1 parent 1f450a7 commit db2b8ec
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 14 deletions.
31 changes: 26 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
- 📊 Includes some simple drivers. (like as `firebase real db`)

```js
npm i linq-fns --save
npm install linq-fns --save
```

### Basic example
> This version just alpha so if have any problem, don't hesitate to let me know. 👋
### Basic exampl
#### Node or browser
```ts
// ES6
Expand Down Expand Up @@ -51,14 +53,28 @@ admin.initializeApp({
});

const db = admin.database();
const firebaseQuery = new FireBaseQueryale(db);
const postsQuery = new FireBaseQueryale(db,'<yourdb>.posts');

// READ AND QUERY DATA
// ES5 Promise
postsQuery.getQuery().where('...').select('...').toList().then(x=>'...');

// Async/await
const data = await postsQuery.getQuery().where('...').select('...').toList();

const postsQuery = firebaseQuery.getRepository('<rootTable>.<childTables>.<child...>');
// Then using like Queryable Apis
// WRITE DATA
// Just call not execute to server
postsQuery.add(item);
postsQuery.remove(item);
postsQuery.update(item);

// Call this to execute 3 above methods
postsQuery.commitChanges();

```

### Process
#### 1.Methods
- [x] from
- [x] where
- [x] select
Expand Down Expand Up @@ -97,6 +113,11 @@ const postsQuery = firebaseQuery.getRepository('<rootTable>.<childTables>.<child
- [x] aggregate
- [x] toList : `Promise<T[]>`

#### 2. Drivers
- [x] Firebase
- [ ] Localstorage
- [ ] ...

### License

[MIT License](http:https://opensource.org/licenses/MIT)
63 changes: 63 additions & 0 deletions src/drivers/base.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { IRepository } from './interfaces/repository.interface';
import { database } from './database.interface';
import { IMethods } from '../intefaces';

export class BaseRepository<T> implements IRepository<T> {

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

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

add(item: T, autoCommit = false): void {
if (autoCommit) {
this._add(item);
return;
}
this._methods.push({ name: 'ADD', item: item })
}

remove(item: T, autoCommit = false): void {
if (autoCommit) {
this._remove(item);
return;
}
this._methods.push({ name: 'DEL', item: item })
}

update(item: T, autoCommit = false): void {
if (autoCommit) {
this._update(item);
return;
}
this._methods.push({ name: 'UPD', item: item })
}

commitChanges(): void {
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);
default: throw new Error("Method just only add, update, delete");
}
}
this._methods = [];
}

_add(item: T): void {
throw new Error("Method not implemented.");
}

_remove(item: T): void {
throw new Error("Method not implemented.");
}

_update(item: T): void {
throw new Error("Method not implemented.");
}
}
17 changes: 17 additions & 0 deletions src/drivers/interfaces/repository.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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>;
add(item: T, autoCommit?: boolean): void;
remove(item: T, autoCommit?: boolean): void;
update(item: T, autoCommit?: boolean): void;
commitChanges(): void;

// methods implements
_add(item: T, cb?: (rs: any) => any): void;
_remove(item: T, cb?: (rs: any) => any): void;
_update(item: T, cb?: (rs: any) => any): void;
}
48 changes: 39 additions & 9 deletions src/drivers/linq-fns-firebase.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,67 @@
import { database } from './database.interface';
import { Queryable } from '..';
import { IMethods } from '../intefaces';
import { IRepository } from './interfaces/repository.interface';
import { BaseRepository } from './base.repository';

const admin = require('firebase-admin');
export class FireBaseQueryale<T> extends BaseRepository<T> implements IRepository<T> {

export class FireBaseQueryale {
private _repoName = '';
private _ref: database.Reference;
private _db: database.Database;

_db: database.Database;

constructor(db: database.Database) {
constructor(db: database.Database, repoName: string) {
super();
this._db = db;
this._repoName = repoName;
this._ref = this.getRefObject(this._repoName);
}

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

const ref = this.getRefObject(repoName);
return Queryable.from(
new Promise<T[]>((resolve, reject) => {
if (!predicate)
ref.on(action, (snapshot) => {
this._ref.on(action, (snapshot) => {
resolve(this.convert(snapshot.val()));
});
else {
const query = predicate(ref);
const query = predicate(this._ref);
query.on(action, (snapshot) => {
resolve(this.convert(snapshot.val()));
});
}
}));
}

_add(item: T, callback?: (rs: any) => any) {
this._ref.push(item, (rs) => {
if (callback) callback(rs);
});
}

_update<S extends T & { __id: string }>(item: S, callback?: (rs: any) => any) {
const key = item.__id;
const objUpdate = {};

delete item.__id;
objUpdate[key] = item;

this._ref.update(objUpdate, (rs) => {
if (callback) callback(rs);
});
}

_remove<S extends T & { __id: string }>(item: S, callback?: (rs: any) => any) {
this._ref.child(item.__id).remove((rs) => {
if (callback) callback(rs);
});
}

// Private methods

private convert<T>(objData: Object): T[] {
if (!objData) return [];
return Object.keys(objData).map(prop => {
Expand Down

0 comments on commit db2b8ec

Please sign in to comment.