File size: 4,398 Bytes
b3787d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
'use strict';

const ngModule = angular.module('woServices');
ngModule.service('appConfigLawnchair', LawnchairDAO);
ngModule.service('accountLawnchair', LawnchairDAO);
export default LawnchairDAO;

/**
 * Handles generic caching of JSON objects in a lawnchair adapter
 */
function LawnchairDAO() {}

/**
 * Initialize the lawnchair database
 * @param  {String}   dbName   The name of the database
 * @return {Promise}
 */
LawnchairDAO.prototype.init = function(dbName) {
    return new Promise((resolve, reject) => {
        if (!dbName) {
            throw new Error('Lawnchair DB name must be specified!');
        }

        this._db = new Lawnchair({
            name: dbName
        }, success => success ? resolve() : reject(new Error('Lawnchair initialization ' + dbName + ' failed!')));
    });
};

/**
 * Create or update an object
 * @return {Promise}
 */
LawnchairDAO.prototype.persist = function(key, object) {
    return new Promise((resolve, reject) => {
        if (!key || !object) {
            throw new Error('Key and Object must be set!');
        }

        this._db.save({
            key: key,
            object: object
        }, persisted => (persisted.key !== key) ? reject(new Error('Persisting failed!')) : resolve());
    });
};

/**
 * Persist a bunch of items at once
 * @return {Promise}
 */
LawnchairDAO.prototype.batch = function(list) {
    return new Promise((resolve, reject) => {
        if (!(list instanceof Array)) {
            throw new Error('Input must be of type Array!');
        }

        this._db.batch(list, res => !res ? reject(new Error('Persisting batch failed!')) : resolve());
    });
};

/**
 * Read a single item by its key
 * @return {Promise}
 */
LawnchairDAO.prototype.read = function(key) {
    return new Promise(resolve => {
        if (!key) {
            throw new Error('Key must be specified!');
        }

        this._db.get(key, o => o ? resolve(o.object) : resolve());
    });
};

/**
 * List all the items of a certain type
 * @param type [String] The type of item e.g. 'email'
 * @return {Promise}
 */
LawnchairDAO.prototype.list = function(query, exactMatchOnly) {
    return new Promise(resolve => {
        let matchingKeys = [];

        // validate input
        if ((Array.isArray(query) && query.length === 0) || (!Array.isArray(query) && !query)) {
            throw new Error('Args not is not set!');
        }

        // this method operates on arrays of keys, so normalize input 'key' -> ['key']
        if (!Array.isArray(query)) {
            query = [query];
        }

        // get all keys
        this._db.keys(keys => {
            // check if there are keys in the db that start with the respective query
            matchingKeys = keys.filter(key => query.filter(type =>
                exactMatchOnly ? key === type : key.indexOf(type) === 0).length > 0);

            if (matchingKeys.length === 0) {
                // no matching keys, resolve
                resolve([]);
                return;
            }

            // fetch all items from data-store with matching keys
            this._db.get(matchingKeys, items => resolve(items.map(item => item.object)));
        });
    });
};

/**
 * Removes an object liter from local storage by its key (delete)
 * @return {Promise}
 */
LawnchairDAO.prototype.remove = function(key) {
    return new Promise((resolve, reject) => this._db.remove(key, err => err ? reject(err) : resolve()));
};

/**
 * Removes an object liter from local storage by its key (delete)
 * @return {Promise}
 */
LawnchairDAO.prototype.removeList = function(type) {
    return new Promise(resolve => {
        const matchingKeys = [];

        // validate type
        if (!type) {
            throw new Error('Type is not set!');
        }

        // get all keys that begins with type
        this._db.keys(keys => {
            keys.forEach(key => key.indexOf(type) === 0 && matchingKeys.push(key));
            resolve(matchingKeys);
        });

    }).then(matchingKeys => {
        // remove all matching keys
        const jobs = [];
        matchingKeys.forEach(key => jobs.push(this.remove(key)));

        return Promise.all(jobs);
    });
};

/**
 * Clears the whole local storage cache
 * @return {Promise}
 */
LawnchairDAO.prototype.clear = function() {
    return new Promise((resolve, reject) => this._db.nuke(err => err ? reject(err) : resolve()));
};