Skip to content

Commit

Permalink
Update drivers
Browse files Browse the repository at this point in the history
  • Loading branch information
jinhduong committed Apr 28, 2018
1 parent cd2935c commit 9c53c26
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 110 deletions.
96 changes: 7 additions & 89 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# 👴 linq-fns
.NET LINQ for Javascript, written by TypeScript.
- Provide `Queryable<T>`, it's reusable, also variable and use `Iterator collection` for holding query and execute.
- Contains almost the original .NET and some extends methods.
- Support `Promise` like as a input source.
- All `APIs` like a Javascript native methods so easily, simply implementation.
# linq-fns
.NET LINQ functions for Javascript, written by TypeScript.
- ⚡ Provide `Queryable<T>`, it's reusable, also variable and use `Predicate collection` for holding query and execute later.
- 🔨 Contains almost the original .NET and some extends methods.
- 🔨 Support `Promise` like as a input source.
- 🙅 All `APIs` like a Javascript native methods so easily, simply implementation.
- 📊 Have some drivers for `firebase realtime db` or any storage libraries...

### Basic example
```ts
Expand All @@ -27,89 +28,6 @@ asyncData.then(data => {
});
```

### Sample data ⚽
This document will use below data to make examples :
```ts
let players = new Promise((resolve, reject) => {
// skills: attack, stamia, speed, shoot
console.log('get players...');
setTimeout(() => {
resolve([
{ name: 'Ronaldo', overall: 96, nationId: 1, skills: [96, 85, 87, 91] },
{ name: 'Messi', overall: 98, nationId: 2, skills: [97, 85, 91, 93] },
{ name: 'Mbappe', overall: 86, nationId: 3, skills: [89, 81, 95, 83] },
{ name: 'Matial', overall: 81, nationId: 3, skills: [81, 80, 89, 81] },
{ name: 'Salah', overall: 89, nationId: 4, skills: [88, 82, 97, 86] }
]);
}, 1000);
})

let nations: Promise<{ id, name, areaId }[]> = new Promise(resolve => {
console.log('get nations...');
setTimeout(() => {
resolve([
{ id: 1, name: 'Portugal', areaId: 1 },
{ id: 2, name: 'Argentina', areaId: 2 },
{ id: 3, name: 'France', areaId: 1 },
{ id: 4, name: 'Egypt', areaId: 3 }
]);
}, 2000);
})

let continents = new Promise<{ id, areaName }[]>(resolve => {
console.log('get continents...');
setTimeout(() => {
resolve([
{ id: 1, areaName: 'Euro' },
{ id: 2, areaName: 'South America' },
]);
}, 2300);
})
```

### Queryable
- `.from` => `Promise<any>`:
All data source will be converted to `Promise` notwithstanding that source is Promise or noramlly array data. Then once we call `toList()`, `count()`, `first()`... or any `execute methods` it will be return a `Promise`.

```ts
let query = Queryable
.from(nations)
.join(continents, (x, y) => x.areaId === y.id)
.groupBy(o => o.y.areaName)

const asyncData = query.count() // Will return Promise<number>
asyncData.then(num => {
console.log(num);
// 2
})
```

- `.fromSync` => `any`:
It's often use inside sub-query which are after all data already retrieved successfully or you make sure the input source is sync data like as `Array`.
``` ts
let query = Queryable
.from(nations)
.join(continents, (x, y) => x.areaId === y.id)
.groupBy(o => o.y.areaName)
.select(x => {
return {
area: x.key,
total: Queryable.fromSync(x.items).count() // Here will return number, not Promise<number>
}
})
const asyncData = query.toList() // Will return Promise<{area:string, total:number}>
asyncData.then(data => {
console.log(data);
// [
// {area: 'Euro': total: 2},
// {area:'South Ameria', total: 1}
// ]
});
```

> **> More**: at `from` method, all input sources at `from, join, leftJoin` methods,... will be converted to **Promise** and use **Promise.all** to **execute** and then use **Iterator collection** to query data and once faced **excuting methods** like as `toList(), first()` it will return data which be inside a Promise.

### Process
- [x] from
- [x] where
Expand Down
40 changes: 19 additions & 21 deletions src/drivers/linq-fns-firebase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,31 @@ import { IMethods } from '../intefaces';
const admin = require('firebase-admin');

export class FireBaseQueryale {

_db: database.Database;

constructor(db: database.Database) {
this._db = db;
}

getQuery<T>(name: string, limitType?: 'first' | 'last', limit?: number): IMethods<T> {
const promise = this.getPromiseSource(name, limitType, limit);
return Queryable.from(promise) as IMethods<T>;
}

getPromiseSource<T>(name: string, limitType?: 'first' | 'last', limit?: number): Promise<T[]> {
let _methodName = (limitType === 'first' ? 'limitToFirst' : 'limitToLast');
const ref = this.getRefObject(name);
const promise: Promise<T[]> = new Promise((resolve, reject) => {
(limitType ? ref[_methodName](limit) : ref)['on']('value', (snapshot) => {
const _data: Object = snapshot.val();
if (!_data)
return resolve([]);
const _arrayedData = Object.keys(_data).map(prop => {
_data[prop]['__id'] = prop;
return _data[prop];
});
resolve(_arrayedData);
});
});
return promise;
getRepository<T>(repoName: string,
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) => {
resolve(snapshot.val());
});
else {
const query = predicate(ref);
query.on(action, (snapshot) => {
resolve(snapshot.val());
});
}
}));
}

private getRefObject(name: string): database.Reference {
Expand Down

0 comments on commit 9c53c26

Please sign in to comment.