rubberguppe/store/object.js

30 lines
574 B
JavaScript
Raw Normal View History

'use strict'
const connection = require('./connection')
module.exports = {
2019-09-22 05:20:37 +00:00
get,
save
}
function get (id) {
return connection.getDb()
.collection('objects')
2019-09-22 05:20:37 +00:00
.find({ id: id })
.limit(1)
2019-09-22 05:20:37 +00:00
.project({ _id: 0, _meta: 0 })
.next()
}
async function save (object) {
const db = connection.getDb()
const exists = await db.collection('objects')
.find({ id: object.id })
.project({ _id: 1 })
.limit(1)
.hasNext()
if (exists) {
return false
}
2019-09-22 05:20:37 +00:00
return db.collection('objects')
.insertOne(object, { forceServerObjectId: true })
2019-09-22 05:20:37 +00:00
}