diff --git a/src/options.js b/src/options.js new file mode 100644 index 0000000..2d1f8fa --- /dev/null +++ b/src/options.js @@ -0,0 +1,28 @@ +/** + * @typedef MongoTenantOptions + * @property {string} tenantIdGetter + * @property {string} accessorMethod + * @property {*} tenantIdType + * @property {string} tenantIdKey + * @property {boolean} requireTenantId + */ + +/** + * Sanitize plugin options + * @param {object} [input] + * @param {string} [input.tenantIdGetter=tenantId] + * @param {string} [input.accessorMethod=String] + * @param {*} [input.tenantIdType=byTenant] + * @param {string} [input.tenantIdKey=getTenantId] + * @param {string} [input.requireTenantId=true] + * @returns {MongoTenantOptions} + */ +const options = (input = {}) => ({ + tenantIdKey: input.tenantIdKey || 'tenantId', + tenantIdType: input.tenantIdType || String, + accessorMethod: input.accessorMethod || 'byTenant', + tenantIdGetter: input.tenantIdGetter || 'getTenantId', + requireTenantId: input.requireTenantId === true, +}); + +module.exports = options; diff --git a/src/options.test.js b/src/options.test.js new file mode 100644 index 0000000..6081339 --- /dev/null +++ b/src/options.test.js @@ -0,0 +1,21 @@ +const options = require('./options'); + +describe('options', () => { + const assertCompleteOptions = (value) => { + expect(value).toHaveProperty('tenantIdKey'); + expect(value).toHaveProperty('tenantIdType'); + expect(value).toHaveProperty('accessorMethod'); + expect(value).toHaveProperty('tenantIdGetter'); + expect(value).toHaveProperty('requireTenantId'); + }; + + it('creates default options', () => { + const result = options(); + assertCompleteOptions(result); + }); + + it('adds default option values', () => { + const result = options({tenantIdKey: 'tenant_id'}); + assertCompleteOptions(result); + }); +});