• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

vlavrynovych / msr-firebase / 42

11 Dec 2023 09:39AM UTC coverage: 70.946% (+0.6%) from 70.345%
42

push

travis-pro

vlavrynovych
#11: Refactoring and code cleanup

8 of 15 branches covered (0.0%)

Branch coverage included in aggregate %.

11 of 11 new or added lines in 2 files covered. (100.0%)

97 of 133 relevant lines covered (72.93%)

1.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

30.77
/src/service/EntityService.ts
1
import {FirebaseDataService} from "./FirebaseDataService";
1✔
2
import {database} from "firebase-admin";
3
import {IEntity} from "../interface";
4

5
export class EntityService<T extends IEntity> extends FirebaseDataService {
1✔
6
    constructor(public db: database.Database,
6✔
7
                protected root:string) {
6✔
8
        super(db);
6✔
9
    }
10

11
    public getAll():Promise<T[]> {
12
        return this.getList(this.root);
4✔
13
    }
14

15
    getAllAsObject() {
16
        return this.getObject(this.root);
×
17
    }
18

19
    get(key:string) {
20
        return this.getObject(`${this.root}/${key}`);
×
21
    }
22

23
    /**
24
     * @param {Object} obj entity object
25
     * @returns {Promise<String>} promise with string key of created/updated entity
26
     */
27
    async save(obj:T) {
28
        return obj.key ? this.update(obj.key, obj) : this.create(obj);
3!
29
    }
30

31
    async create(obj: T) {
32
        const ref = await this.db.ref(this.root).push(obj);
3✔
33
        return ref.key as string;
3✔
34
    }
35

36
    async updateAll(modifier:ModifierFunction<T>) {
37
        return this.getAll().then(async entities => {
×
38

39
            const results:ModificationResults = {
×
40
                skipped: [],
41
                updated: []
42
            };
43

44
            const tryUpdate = async (entity: T) => {
×
45
                if(!entity.key) return
×
46
                const modifiedEntity:T = modifier(entity);
×
47

48
                if (!modifiedEntity) {
×
49
                    results.skipped.push(entity.key)
×
50
                    return;
×
51
                }
52

53
                await this.save(modifiedEntity);
×
54
                results.updated.push(entity.key)
×
55
            }
56

57
            const tasks = entities.map(entity => tryUpdate(entity));
×
58
            await Promise.all(tasks);
×
59
            return results
×
60
        })
61
    }
62

63
    async update(key: string, obj: T) {
64
        await this.updateObject(`${this.root}/${key}`, obj);
×
65
        return key;
×
66
    }
67

68
    async set(key: string, obj: T) {
69
        await this.setObject(`${this.root}/${key}`, obj);
×
70
        return key;
×
71
    }
72

73
    async remove(key: string) {
74
        await this.db.ref(`${this.root}/${key}`).remove();
3✔
75
        return key;
3✔
76
    }
77

78
    async removeAll(ids:string[]) {
79
        const task = ids.map(id => this.remove(id));
×
80
        await Promise.all(task)
×
81
    }
82

83
    findAllBy(propertyName:string,
84
              value:number | string | boolean | null):Promise<T[]> {
85
        return super.findAllObjectsBy(this.root, propertyName, value);
×
86
    }
87
}
88

89
export type ModifierFunction<T> = {
90
    (entity:T): T
91
}
92

93
export type ModificationResults = {
94
    skipped: string[],
95
    updated: string[]
96
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc