Comparing version 2.0.0 to 2.1.0
20
index.js
@@ -5,6 +5,8 @@ const stream = require('stream'); | ||
module.exports = (knex, config) => { | ||
const columnName = config.columnName || 'value'; | ||
async function open () { | ||
return knex.schema.createTableIfNotExists(config.tableName, function (table) { | ||
table.increments(); | ||
table.jsonb('value'); | ||
table.jsonb(columnName); | ||
table.timestamps(true, true); | ||
@@ -21,4 +23,6 @@ }).then(() => { | ||
async function append (payload) { | ||
const data = {}; | ||
data[columnName] = JSON.stringify(payload); | ||
return knex(config.tableName) | ||
.insert({ value: JSON.stringify(payload) }) | ||
.insert(data) | ||
.returning('id') | ||
@@ -37,6 +41,6 @@ .then((inserted) => { | ||
async function get (offset) { | ||
return knex(config.tableName).first('id', 'value') | ||
return knex(config.tableName).first('id', columnName) | ||
.where({ id: offset.id }) | ||
.then((obj) => { | ||
return JSON.parse(obj.value); | ||
return JSON.parse(obj[columnName]); | ||
}); | ||
@@ -48,4 +52,6 @@ } | ||
ws._write = (payload, enc, cb) => { | ||
const data = {}; | ||
data[columnName] = JSON.stringify(payload); | ||
return knex(config.tableName) | ||
.insert({ value: JSON.stringify(payload) }) | ||
.insert(data) | ||
.then(() => cb()) | ||
@@ -58,7 +64,7 @@ .catch(cb); | ||
function createReadStream (opts = {}) { | ||
return knex(config.tableName).select('id', 'value', 'created_at') | ||
return knex(config.tableName).select('id', columnName, 'created_at') | ||
.where('id', '>=', (opts.offset && opts.offset.id) || 0) | ||
.stream() | ||
.pipe(streamMap({ objectMode: true }, (obj) => { | ||
return { offset: { id: obj.id, timestamp: obj.created_at }, value: JSON.parse(obj.value) }; | ||
return { offset: { id: obj.id, timestamp: obj.created_at }, value: JSON.parse(obj[columnName]) }; | ||
})); | ||
@@ -65,0 +71,0 @@ } |
{ | ||
"name": "knex-log", | ||
"description": "Implementation of a log using knex.", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"author": "Tim Allen <tim@noblesamurai.com>", | ||
@@ -6,0 +6,0 @@ "license": "BSD", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
20719
92
1