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

demmings / gsSQL / 4027440288

pending completion
4027440288

push

github

cdemmigs
#22.  linter.

1254 of 1328 branches covered (94.43%)

Branch coverage included in aggregate %.

9725 of 10121 relevant lines covered (96.09%)

505.61 hits per line

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

91.56
/src/ScriptSettings.js
1
//  Remove comments for testing in NODE
1✔
2
/*  *** DEBUG START ***
1✔
3
export { ScriptSettings };
1✔
4
import { PropertiesService } from "./SqlTest.js";
1✔
5
//  *** DEBUG END  ***/
1✔
6

1✔
7
/** Stores settings for the SCRIPT.  Long term cache storage for small tables.  */
1✔
8
class ScriptSettings {      //  skipcq: JS-0128
1✔
9
    /**
1✔
10
     * For storing cache data for very long periods of time.
1✔
11
     */
1✔
12
    constructor() {
1✔
13
        this.scriptProperties = PropertiesService.getScriptProperties();
3✔
14
    }
3✔
15

1✔
16
    /**
1✔
17
     * Get script property using key.  If not found, returns null.
1✔
18
     * @param {String} propertyKey 
1✔
19
     * @returns {any}
1✔
20
     */
1✔
21
    get(propertyKey) {
1✔
22
        const myData = this.scriptProperties.getProperty(propertyKey);
6✔
23

6✔
24
        if (myData === null)
6✔
25
            return null;
6✔
26

4✔
27
        /** @type {PropertyData} */
4✔
28
        const myPropertyData = JSON.parse(myData);
4✔
29

4✔
30
        return PropertyData.getData(myPropertyData);
4✔
31
    }
6✔
32

1✔
33
    /**
1✔
34
     * Put data into our PROPERTY cache, which can be held for long periods of time.
1✔
35
     * @param {String} propertyKey - key to finding property data.
1✔
36
     * @param {any} propertyData - value.  Any object can be saved..
1✔
37
     * @param {Number} daysToHold - number of days to hold before item is expired.
1✔
38
     */
1✔
39
    put(propertyKey, propertyData, daysToHold = 1) {
1✔
40
        //  Create our object with an expiry time.
3✔
41
        const objData = new PropertyData(propertyData, daysToHold);
3✔
42

3✔
43
        //  Our property needs to be a string
3✔
44
        const jsonData = JSON.stringify(objData);
3✔
45

3✔
46
        try {
3✔
47
            this.scriptProperties.setProperty(propertyKey, jsonData);
3✔
48
        }
3✔
49
        catch (ex) {
3!
50
            throw new Error("Cache Limit Exceeded.  Long cache times have limited storage available.  Only cache small tables for long periods.");
×
51
        }
×
52
    }
3✔
53

1✔
54
    /**
1✔
55
     * 
1✔
56
     * @param {Object} propertyDataObject 
1✔
57
     * @param {Number} daysToHold 
1✔
58
     */
1✔
59
    putAll(propertyDataObject, daysToHold = 1) {
1✔
60
        const keys = Object.keys(propertyDataObject);
1✔
61

1✔
62
        for (const key of keys) {
1✔
63
            this.put(key, propertyDataObject[key], daysToHold);
1✔
64
        }
1✔
65
    }
1✔
66

1✔
67
    /**
1✔
68
     * Removes script settings that have expired.
1✔
69
     * @param {Boolean} deleteAll - true - removes ALL script settings regardless of expiry time.
1✔
70
     */
1✔
71
    expire(deleteAll) {
1✔
72
        const allKeys = this.scriptProperties.getKeys();
2✔
73

2✔
74
        for (const key of allKeys) {
2✔
75
            const myData = this.scriptProperties.getProperty(key);
2✔
76

2✔
77
            if (myData !== null) {
2✔
78
                let propertyValue = null;
2✔
79
                try {
2✔
80
                    propertyValue = JSON.parse(myData);
2✔
81
                }
2✔
82
                catch (e) {
2!
83
                    Logger.log(`Script property data is not JSON. key=${key}`);
×
84
                }
×
85

2✔
86
                if (propertyValue !== null && (PropertyData.isExpired(propertyValue) || deleteAll)) {
2!
87
                    this.scriptProperties.deleteProperty(key);
×
88
                    Logger.log(`Removing expired SCRIPT PROPERTY: key=${key}`);
×
89
                }
×
90
            }
2✔
91
        }
2✔
92
    }
2✔
93
}
1✔
94

1✔
95
/** Converts data into JSON for getting/setting in ScriptSettings. */
1✔
96
class PropertyData {
1✔
97
    /**
1✔
98
     * 
1✔
99
     * @param {any} propertyData 
1✔
100
     * @param {Number} daysToHold 
1✔
101
     */
1✔
102
    constructor(propertyData, daysToHold) {
1✔
103
        const someDate = new Date();
3✔
104

3✔
105
        /** @property {String} */
3✔
106
        this.myData = JSON.stringify(propertyData);
3✔
107
        /** @property {Date} */
3✔
108
        this.expiry = someDate.setMinutes(someDate.getMinutes() + daysToHold * 1440);
3✔
109
    }
3✔
110

1✔
111
    /**
1✔
112
     * 
1✔
113
     * @param {PropertyData} obj 
1✔
114
     * @returns {any}
1✔
115
     */
1✔
116
    static getData(obj) {
1✔
117
        let value = null;
4✔
118
        try {
4✔
119
            if (!PropertyData.isExpired(obj))
4✔
120
                value = JSON.parse(obj.myData);
4✔
121
        }
4✔
122
        catch (ex) {
4!
123
            Logger.log(`Invalid property value.  Not JSON: ${ex.toString()}`);
×
124
        }
×
125

4✔
126
        return value;
4✔
127
    }
4✔
128

1✔
129
    /**
1✔
130
     * 
1✔
131
     * @param {PropertyData} obj 
1✔
132
     * @returns {Boolean}
1✔
133
     */
1✔
134
    static isExpired(obj) {
1✔
135
        const someDate = new Date();
6✔
136
        const expiryDate = new Date(obj.expiry);
6✔
137
        return (expiryDate.getTime() < someDate.getTime())
6✔
138
    }
6✔
139
}
1✔
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

© 2025 Coveralls, Inc