|
|
@ -1,20 +1,42 @@ |
|
|
const { DocumentTypes, ViewNames } = require("./utils") |
|
|
const { |
|
|
|
|
|
DocumentTypes, |
|
|
|
|
|
ViewNames, |
|
|
|
|
|
DeprecatedViews, |
|
|
|
|
|
SEPARATOR, |
|
|
|
|
|
} = require("./utils") |
|
|
const { getGlobalDB } = require("../tenancy") |
|
|
const { getGlobalDB } = require("../tenancy") |
|
|
|
|
|
|
|
|
|
|
|
const DESIGN_DB = "_design/database" |
|
|
|
|
|
|
|
|
function DesignDoc() { |
|
|
function DesignDoc() { |
|
|
return { |
|
|
return { |
|
|
_id: "_design/database", |
|
|
_id: DESIGN_DB, |
|
|
// view collation information, read before writing any complex views:
|
|
|
// view collation information, read before writing any complex views:
|
|
|
// https://docs.couchdb.org/en/master/ddocs/views/collation.html#collation-specification
|
|
|
// https://docs.couchdb.org/en/master/ddocs/views/collation.html#collation-specification
|
|
|
views: {}, |
|
|
views: {}, |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
exports.createUserEmailView = async () => { |
|
|
async function removeDeprecated(db, viewName) { |
|
|
|
|
|
if (!DeprecatedViews[viewName]) { |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
try { |
|
|
|
|
|
const designDoc = await db.get(DESIGN_DB) |
|
|
|
|
|
for (let deprecatedNames of DeprecatedViews[viewName]) { |
|
|
|
|
|
delete designDoc.views[deprecatedNames] |
|
|
|
|
|
} |
|
|
|
|
|
await db.put(designDoc) |
|
|
|
|
|
} catch (err) { |
|
|
|
|
|
// doesn't exist, ignore
|
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
exports.createNewUserEmailView = async () => { |
|
|
const db = getGlobalDB() |
|
|
const db = getGlobalDB() |
|
|
let designDoc |
|
|
let designDoc |
|
|
try { |
|
|
try { |
|
|
designDoc = await db.get("_design/database") |
|
|
designDoc = await db.get(DESIGN_DB) |
|
|
} catch (err) { |
|
|
} catch (err) { |
|
|
// no design doc, make one
|
|
|
// no design doc, make one
|
|
|
designDoc = DesignDoc() |
|
|
designDoc = DesignDoc() |
|
|
@ -22,7 +44,7 @@ exports.createUserEmailView = async () => { |
|
|
const view = { |
|
|
const view = { |
|
|
// if using variables in a map function need to inject them before use
|
|
|
// if using variables in a map function need to inject them before use
|
|
|
map: `function(doc) {
|
|
|
map: `function(doc) {
|
|
|
if (doc._id.startsWith("${DocumentTypes.USER}")) { |
|
|
if (doc._id.startsWith("${DocumentTypes.USER}${SEPARATOR}")) { |
|
|
emit(doc.email.toLowerCase(), doc._id) |
|
|
emit(doc.email.toLowerCase(), doc._id) |
|
|
} |
|
|
} |
|
|
}`,
|
|
|
}`,
|
|
|
@ -81,7 +103,7 @@ exports.createUserBuildersView = async () => { |
|
|
|
|
|
|
|
|
exports.queryGlobalView = async (viewName, params, db = null) => { |
|
|
exports.queryGlobalView = async (viewName, params, db = null) => { |
|
|
const CreateFuncByName = { |
|
|
const CreateFuncByName = { |
|
|
[ViewNames.USER_BY_EMAIL]: exports.createUserEmailView, |
|
|
[ViewNames.USER_BY_EMAIL]: exports.createNewUserEmailView, |
|
|
[ViewNames.BY_API_KEY]: exports.createApiKeyView, |
|
|
[ViewNames.BY_API_KEY]: exports.createApiKeyView, |
|
|
[ViewNames.USER_BY_BUILDERS]: exports.createUserBuildersView, |
|
|
[ViewNames.USER_BY_BUILDERS]: exports.createUserBuildersView, |
|
|
} |
|
|
} |
|
|
@ -98,6 +120,7 @@ exports.queryGlobalView = async (viewName, params, db = null) => { |
|
|
} catch (err) { |
|
|
} catch (err) { |
|
|
if (err != null && err.name === "not_found") { |
|
|
if (err != null && err.name === "not_found") { |
|
|
const createFunc = CreateFuncByName[viewName] |
|
|
const createFunc = CreateFuncByName[viewName] |
|
|
|
|
|
await removeDeprecated(db, viewName) |
|
|
await createFunc() |
|
|
await createFunc() |
|
|
return exports.queryGlobalView(viewName, params) |
|
|
return exports.queryGlobalView(viewName, params) |
|
|
} else { |
|
|
} else { |
|
|
|