"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Collection=void 0;const faas_db_builder_1=require("@alipay/faas-db-builder"),faas_server_utils_1=require("@alipay/faas-server-utils"),faas_common_sdk_1=require("@alipay/faas-common-sdk"),errors_1=require("../errors");class Collection{#e;#r;#a;#s;#t;constructor(t,e,r){this.#e=t,this.#r=e,this.#s=r,this.#a=new faas_db_builder_1.CollectionBuilder(e,{maxAddCount:1e3}),this.#t=new faas_common_sdk_1.Collection(e,{httpclient:r,type:"function",databaseName:t.functionDatabaseName,throwOnNotFound:t.throwOnNotFound})}async add(t){if(Array.isArray(t.data))return this.#i(t);if(!t?.data)throw errors_1.errors.INVALID_PARAM("data","\u4E0D\u80FD\u4E3A\u7A7A");const e=(0,faas_server_utils_1.getAlipayContext)(),{data:r}=t,s={...r,_openid:r._openid||e.OPENID};return this.#t.add({data:s})}async#i(t){const{options:e}=this.#a.add(t),r="POST",s=`${this.#n()}?batchCreate`,i=(0,faas_server_utils_1.getAlipayContext)();e.$create.forEach(a=>{a._openid=a._openid||i.OPENID});const n=await this.#s.request({method:r,path:s,data:e});return{_ids:faas_common_sdk_1.ResponseUtil.handleResponse(n).insertedIds.map(({_id:a})=>a)}}doc(t){return this.#t.doc(t)}limit(t){return this.#t.limit(t)}skip(t){return this.#t.skip(t)}where(t){return this.#t.where(t)}projection(t){return this.#t.projection(t)}field(t){return this.projection(t)}orderBy(t,e){return this.#t.orderBy(t,e)}aggregate(){return this.#t.aggregate()}async update(t){return await this.#t.update(t)}async updateAndReturn(t){return await this.#t.updateAndReturn(t)}async set(t){return await this.#t.set(t)}async remove(){return await this.#t.remove()}async get(){return await this.#t.get()}async count(){return await this.#t.count()}#n(){return`/${this.#e.functionDatabaseName||"faas"}/${this.#r}`}}exports.Collection=Collection;