{"size": 1219, "ext": "js", "max_stars_repo_path": "app/data-provider/temporary-upload-file-provider.js", "max_stars_repo_name": "freelogfe/freelog-resource-provider", "max_stars_repo_stars_event_min_datetime": "2017-10-11T02:12:12.000Z", "max_stars_repo_stars_event_max_datetime": "2019-01-21T16:34:34.000Z", "max_issues_repo_path": "app/data-provider/temporary-upload-file-provider.js", "max_issues_repo_name": "yuliang0912/freelog-resource-provider", "max_issues_repo_issues_event_min_datetime": "2019-12-13T07:36:00.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-03T22:51:07.000Z", "max_forks_repo_path": "app/data-provider/temporary-upload-file-provider.js", "max_forks_repo_name": "freelogfe/freelog-resource-provider", "max_forks_repo_forks_event_min_datetime": "2017-10-11T02:28:32.000Z", "max_forks_repo_forks_event_max_datetime": "2017-10-11T02:28:32.000Z"}
{"max_stars_count": 3, "max_issues_count": 7, "max_forks_count": 1, "avg_line_length": 27.7045454545, "max_line_length": 93, "alphanum_fraction": 0.6086956522}
'use strict'

const lodash = require('lodash')
const MongoBaseOperation = require('egg-freelog-database/lib/database/mongo-base-operation')

module.exports = class ResourceTreeProvider extends MongoBaseOperation {

    constructor(app) {
        super(app.model.TemporaryUploadFileInfo)
    }

    /**
     * 存在则更新,否则创建
     * @param uploadFileInfo
     * @returns {*}
     */
    findOrCreate(uploadFileInfo) {

        const condition = lodash.pick(uploadFileInfo, ['userId', 'resourceType', 'sha1'])

        return super.findOneAndUpdate(condition, uploadFileInfo, {new: true}).then(model => {
            return model || super.create(uploadFileInfo)
        })
    }

    //事务测试
    async test(uploadFileInfo) {
        let session = await super.model.startSession()
        session.startTransaction()
        let model = null
        try {
            model = await super.create([uploadFileInfo], {session})
            await super.create([uploadFileInfo], {session})
            await session.commitTransaction();
            session.endSession();
        } catch (e) {
            await session.abortTransaction();
            session.endSession();
            throw e
        }
        return model[0]
    }
}
