Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: update mixin to use couchbaseX (#56)
Co-authored-by: jakangah <jakangah@gmail.com>
- Loading branch information
Showing
4 changed files
with
68 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |