Skip to content

Commit

Permalink
feat: update mixin to use couchbaseX (#56)
Browse files Browse the repository at this point in the history
Co-authored-by: jakangah <jakangah@gmail.com>
  • Loading branch information
jakangah and jakangah committed Jan 13, 2021
1 parent 4da48e4 commit a4d26f9
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 15 deletions.
18 changes: 4 additions & 14 deletions backends/couchbase.js
@@ -1,30 +1,20 @@
'use strict';

var convertToAbsExpiry = function(ttl) {
return Math.floor(Date.now() / 1000) + ttl;
};

const { isCouchbaseVersionSupported, calculateExpiry } = require('../utils/couchbase');
/**
* Same syntax as a mixin.
*/
module.exports = function(Model, options) {

if (options.ttl != null && options.ttl) {
// Set TTL before save.
Model.observe('before save', function(ctx, next) {
// @see https://github.com/Wiredcraft/loopback-connector-couchbase3
if (/^couchbase[35]$/.test(ctx.Model.getConnector().name)) {
// @see http://docs.couchbase.com/sdk-api/couchbase-node-client-2.1.2/Bucket.html#touch
if (options.ttl > 30 * 24 * 60 * 60) {
ctx.options.expiry = convertToAbsExpiry(options.ttl);
} else {
ctx.options.expiry = options.ttl;
}
const connectorName = ctx.Model.getConnector().name;
if (isCouchbaseVersionSupported(connectorName)) {
ctx.options.expiry = calculateExpiry(options.ttl);
return next();
}
// Nothing else supported for now.
next();
});
}

};
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "loopback-cache",
"version": "0.2.2",
"version": "0.2.3",
"description": "Cache solutions for Loopback",
"author": {
"name": "Wiredcraft",
Expand Down
28 changes: 28 additions & 0 deletions test/utils.couchbase.test.js
@@ -0,0 +1,28 @@
"use strict";

require("should");
const { isCouchbaseVersionSupported } = require("../utils/couchbase.js");

describe("Couchbase Utils", () => {
describe("isCouchbaseVersionSupported", () => {
it("it should return true for couchbase3", () => {
const isSupported = isCouchbaseVersionSupported("couchbase3");
isSupported.should.equal(true);
});

it("it should return true for couchbase5", () => {
const isSupported = isCouchbaseVersionSupported("couchbase5");
isSupported.should.equal(true);
});

it("it should return true for couchbaseX", () => {
const isSupported = isCouchbaseVersionSupported("couchbaseX");
isSupported.should.equal(true);
});

it("it should return false for couchbase1 (not supported)", () => {
const isSupported = isCouchbaseVersionSupported("couchbase1");
isSupported.should.equal(false);
});
});
});
35 changes: 35 additions & 0 deletions utils/couchbase.js
@@ -0,0 +1,35 @@
'use strict';


/**
* converts ttl too epoch time
* @param {*} ttl
*/
const convertToAbsExpiry = (ttl) => {
return Math.floor(Date.now() / 1000) + ttl;
}


/**
* checked the couchbase version to determine if its supported
* currettly supports 3,5,X(5)
* @param {*} version
*/
exports.isCouchbaseVersionSupported = (version) => {
// @see https://github.com/Wiredcraft/loopback-connector-couchbase3
// @see https://github.com/Wiredcraft/loopback-connector-couchbaseX
return /^couchbase[35X]$/.test(version)
}


/**
* @see http://docs.couchbase.com/sdk-api/couchbase-node-client-2.1.2/Bucket.html#touch
* @param {*} ttl
*/
exports.calculateExpiry = (ttl) => {
if (ttl > 30 * 24 * 60 * 60) {
return convertToAbsExpiry(ttl);
}else{
return ttl;
}
}

0 comments on commit a4d26f9

Please sign in to comment.