2021-08-04 11:02:24 +02:00
|
|
|
const { DocumentTypes, ViewNames, StaticDatabases } = require("./utils")
|
|
|
|
const { getDB } = require("./index")
|
2021-04-20 18:17:44 +02:00
|
|
|
|
|
|
|
function DesignDoc() {
|
|
|
|
return {
|
|
|
|
_id: "_design/database",
|
|
|
|
// view collation information, read before writing any complex views:
|
|
|
|
// https://docs.couchdb.org/en/master/ddocs/views/collation.html#collation-specification
|
|
|
|
views: {},
|
|
|
|
}
|
|
|
|
}
|
2021-04-19 18:31:47 +02:00
|
|
|
|
2021-08-04 11:02:24 +02:00
|
|
|
exports.createUserEmailView = async () => {
|
|
|
|
const db = getDB(StaticDatabases.GLOBAL.name)
|
2021-04-20 18:17:44 +02:00
|
|
|
let designDoc
|
|
|
|
try {
|
|
|
|
designDoc = await db.get("_design/database")
|
|
|
|
} catch (err) {
|
|
|
|
// no design doc, make one
|
|
|
|
designDoc = DesignDoc()
|
|
|
|
}
|
2021-04-19 18:31:47 +02:00
|
|
|
const view = {
|
|
|
|
// if using variables in a map function need to inject them before use
|
|
|
|
map: `function(doc) {
|
|
|
|
if (doc._id.startsWith("${DocumentTypes.USER}")) {
|
|
|
|
emit(doc.email, doc._id)
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
}
|
|
|
|
designDoc.views = {
|
|
|
|
...designDoc.views,
|
|
|
|
[ViewNames.USER_BY_EMAIL]: view,
|
|
|
|
}
|
|
|
|
await db.put(designDoc)
|
|
|
|
}
|