Changeset View
Changeset View
Standalone View
Standalone View
lib/app/metadata.js
const COLLECTION_NAME = "_metadata"; | const COLLECTION_NAME = "_metadata"; | ||||
module.exports = app => ({ | module.exports = app => ({ | ||||
db_collection_name: COLLECTION_NAME, | db_collection_name: COLLECTION_NAME, | ||||
async get(key) { | async get(key) { | ||||
const matches = await app.Datastore.find(COLLECTION_NAME, { key }); | const matches = await app.Datastore.find(COLLECTION_NAME, { key }); | ||||
if (matches.length) { | if (matches.length) { | ||||
return matches[0].value; | return matches[0].value; | ||||
} else { | |||||
undefined; | |||||
} | } | ||||
}, | }, | ||||
async set(key, value) { | async set(key, value) { | ||||
const matches = await app.Datastore.find(COLLECTION_NAME, { key }); | const matches = await app.Datastore.find(COLLECTION_NAME, { key }); | ||||
if (matches.length) { | if (matches.length) { | ||||
await app.Datastore.update( | await app.Datastore.update( | ||||
COLLECTION_NAME, | COLLECTION_NAME, | ||||
{ key: key }, | { key }, | ||||
{ $set: { value: value } } | { $set: { value } } | ||||
); | ); | ||||
} else { | } else { | ||||
await app.Datastore.insert(COLLECTION_NAME, { key, value }); | await app.Datastore.insert(COLLECTION_NAME, { key, value }); | ||||
} | } | ||||
}, | }, | ||||
}); | }); |