forked from tsai/budibase
committed by
GitHub
36 changed files with 332 additions and 365 deletions
@ -1,5 +1,9 @@ |
|||||
|
let Pouch |
||||
|
|
||||
module.exports.setDB = pouch => { |
module.exports.setDB = pouch => { |
||||
module.exports.CouchDB = pouch |
Pouch = pouch |
||||
} |
} |
||||
|
|
||||
module.exports.CouchDB = null |
module.exports.getDB = dbName => { |
||||
|
return new Pouch(dbName) |
||||
|
} |
||||
|
|||||
@ -0,0 +1,35 @@ |
|||||
|
const { DocumentTypes, ViewNames, StaticDatabases } = require("./utils") |
||||
|
const { getDB } = require("./index") |
||||
|
|
||||
|
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: {}, |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
exports.createUserEmailView = async () => { |
||||
|
const db = getDB(StaticDatabases.GLOBAL.name) |
||||
|
let designDoc |
||||
|
try { |
||||
|
designDoc = await db.get("_design/database") |
||||
|
} catch (err) { |
||||
|
// no design doc, make one
|
||||
|
designDoc = DesignDoc() |
||||
|
} |
||||
|
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) |
||||
|
} |
||||
@ -1,35 +0,0 @@ |
|||||
exports.StaticDatabases = { |
|
||||
USER: { |
|
||||
name: "user-db", |
|
||||
}, |
|
||||
} |
|
||||
|
|
||||
const DocumentTypes = { |
|
||||
USER: "us", |
|
||||
APP: "app", |
|
||||
} |
|
||||
|
|
||||
exports.DocumentTypes = DocumentTypes |
|
||||
|
|
||||
const UNICODE_MAX = "\ufff0" |
|
||||
const SEPARATOR = "_" |
|
||||
|
|
||||
/** |
|
||||
* Generates a new user ID based on the passed in email. |
|
||||
* @param {string} email The email which the ID is going to be built up of. |
|
||||
* @returns {string} The new user ID which the user doc can be stored under. |
|
||||
*/ |
|
||||
exports.generateUserID = email => { |
|
||||
return `${DocumentTypes.USER}${SEPARATOR}${email}` |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* Gets parameters for retrieving users, this is a utility function for the getDocParams function. |
|
||||
*/ |
|
||||
exports.getUserParams = (email = "", otherProps = {}) => { |
|
||||
return { |
|
||||
...otherProps, |
|
||||
startkey: `${DocumentTypes.USER}${SEPARATOR}${email}`, |
|
||||
endkey: `${DocumentTypes.USER}${SEPARATOR}${email}${UNICODE_MAX}`, |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue