mirror of
				https://github.com/Unleash/unleash.git
				synced 2025-10-27 11:02:16 +01:00 
			
		
		
		
	* feat: move secrets to settings * feat: Add better support for detailed db options. Added db field in options to allow better control of db-options. Especially important to allow special chars in database password which might lead to an invaid url when defined as a database-url. * fix: integrate logger with knex logger * fix: remove secret option from all examples * fix: more options.js unit tests * fix: added settings-store e2e tests
		
			
				
	
	
		
			53 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			53 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
/* eslint camelcase: "off" */
 | 
						|
'use strict';
 | 
						|
 | 
						|
const TABLE = 'settings';
 | 
						|
 | 
						|
class SettingStore {
 | 
						|
    constructor(db, getLogger) {
 | 
						|
        this.db = db;
 | 
						|
        this.logger = getLogger('settings-store.js');
 | 
						|
    }
 | 
						|
 | 
						|
    updateRow(name, content) {
 | 
						|
        return this.db(TABLE)
 | 
						|
            .where('name', name)
 | 
						|
            .update({
 | 
						|
                content: JSON.stringify(content),
 | 
						|
            });
 | 
						|
    }
 | 
						|
 | 
						|
    insertNewRow(name, content) {
 | 
						|
        return this.db(TABLE).insert({ name, content });
 | 
						|
    }
 | 
						|
 | 
						|
    insert(name, content) {
 | 
						|
        return this.db(TABLE)
 | 
						|
            .count('*')
 | 
						|
            .where('name', name)
 | 
						|
            .map(row => ({ count: row.count }))
 | 
						|
            .then(rows => {
 | 
						|
                if (rows[0].count > 0) {
 | 
						|
                    return this.updateRow(name, content);
 | 
						|
                } else {
 | 
						|
                    return this.insertNewRow(name, content);
 | 
						|
                }
 | 
						|
            });
 | 
						|
    }
 | 
						|
 | 
						|
    async get(name) {
 | 
						|
        const result = await this.db
 | 
						|
            .select()
 | 
						|
            .from(TABLE)
 | 
						|
            .where('name', name);
 | 
						|
 | 
						|
        if (result.length > 0) {
 | 
						|
            return result[0].content;
 | 
						|
        } else {
 | 
						|
            return undefined;
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 | 
						|
 | 
						|
module.exports = SettingStore;
 |