ramirez/src/data/snippets.js

59 lines
1.1 KiB
JavaScript
Raw Normal View History

const moment = require('moment');
const knex = require('../knex');
2017-12-31 19:16:05 -05:00
const Snippet = require('./Snippet');
/**
* @param {String} trigger
* @returns {Promise<Snippet>}
*/
async function getSnippet(trigger) {
const snippet = await knex('snippets')
.where('trigger', trigger)
.first();
return (snippet ? new Snippet(snippet) : null);
}
/**
* @param {String} trigger
* @param {String} body
* @returns {Promise<void>}
*/
async function addSnippet(trigger, body, createdBy = 0) {
if (await getSnippet(trigger)) return;
return knex('snippets').insert({
trigger,
body,
created_by: createdBy,
created_at: moment.utc().format('YYYY-MM-DD HH:mm:ss')
});
}
/**
* @param {String} trigger
2017-12-31 19:16:05 -05:00
* @returns {Promise<void>}
*/
async function deleteSnippet(trigger) {
return knex('snippets')
.where('trigger', trigger)
.delete();
}
2017-12-31 19:16:05 -05:00
/**
* @returns {Promise<Snippet[]>}
*/
async function getAllSnippets() {
const snippets = await knex('snippets')
.select();
return snippets.map(s => new Snippet(s));
}
module.exports = {
get: getSnippet,
add: addSnippet,
del: deleteSnippet,
all: getAllSnippets,
};