2019-09-22 05:05:30 +00:00
|
|
|
'use strict'
|
2019-09-23 17:52:22 +00:00
|
|
|
const connection = require('./connection')
|
2019-09-22 05:05:30 +00:00
|
|
|
module.exports = {
|
2019-09-22 05:20:37 +00:00
|
|
|
get,
|
|
|
|
save
|
2019-09-22 05:05:30 +00:00
|
|
|
}
|
|
|
|
|
2019-09-23 17:52:22 +00:00
|
|
|
function get (id) {
|
|
|
|
return connection.getDb()
|
|
|
|
.collection('objects')
|
2019-09-22 05:20:37 +00:00
|
|
|
.find({ id: id })
|
2019-09-22 05:05:30 +00:00
|
|
|
.limit(1)
|
2019-09-22 05:20:37 +00:00
|
|
|
.project({ _id: 0, _meta: 0 })
|
2019-09-22 05:05:30 +00:00
|
|
|
.next()
|
|
|
|
}
|
|
|
|
|
2019-09-23 17:52:22 +00:00
|
|
|
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')
|
2019-09-23 17:52:22 +00:00
|
|
|
.insertOne(object, { forceServerObjectId: true })
|
2019-09-22 05:20:37 +00:00
|
|
|
}
|