1
1
/*
2
2
* @Author : Maslow<[email protected] >
3
3
* @Date : 2021-07-30 10:30:29
4
- * @LastEditTime : 2021-09-10 11:43:37
4
+ * @LastEditTime : 2021-09-10 22:19:43
5
5
* @Description :
6
6
*/
7
7
8
8
import { Constants } from "../constants"
9
9
import { DatabaseAgent } from "../lib/db-agent"
10
10
import { compileTs2js } from 'cloud-function-engine/dist/utils'
11
11
import { CloudFunctionStruct } from "cloud-function-engine"
12
- import { ClientSession , ObjectId } from 'mongodb'
12
+ import { ClientSession } from 'mongodb'
13
13
import * as assert from 'assert'
14
14
import { logger } from "../lib/logger"
15
15
import { ApplicationStruct , getApplicationDbAccessor } from "./application"
@@ -130,55 +130,29 @@ export async function deployFunctions(appid: string, functions: FunctionStruct[]
130
130
* @returns
131
131
*/
132
132
async function _deployOneFunction ( func : FunctionStruct , session : ClientSession ) {
133
-
134
- await _processFunctionWithSameNameButNotId ( func , session )
135
-
136
133
const db = DatabaseAgent . sys_accessor . db
137
- const r = await db . collection ( Constants . cn . functions ) . findOne ( { _id : new ObjectId ( func . _id ) } , { session } )
138
-
139
134
const data = {
140
- ...func
135
+ ...func ,
141
136
}
142
-
143
- // if exists function
144
- if ( r ) {
145
- delete data [ '_id' ]
146
- const ret = await db . collection ( Constants . cn . functions ) . updateOne ( { _id : r . _id } , {
137
+ delete data [ '_id' ]
138
+ const r = await db . collection ( Constants . cn . functions )
139
+ . updateOne ( {
140
+ appid : func . appid ,
141
+ name : func . name
142
+ } , {
147
143
$set : data
148
144
} , { session } )
149
145
150
- assert ( ret . matchedCount , `deploy: update function ${ func . name } occurred error` )
146
+ // return if exists & updated
147
+ if ( r . matchedCount ) {
148
+ logger . debug ( `deploy function: found an exists function ${ func . name } & updated it, matchedCount ${ r . matchedCount } ` )
151
149
return
152
150
}
153
151
154
152
// if new function
155
- data . _id = new ObjectId ( data . _id ) as any
153
+ const ret = await db . collection ( Constants . cn . functions )
154
+ . insertOne ( data as any , { session } )
156
155
157
- const ret = await db . collection ( Constants . cn . functions ) . insertOne ( data as any , { session } )
158
156
assert ( ret . insertedId , `deploy: add function ${ func . name } occurred error` )
159
- }
160
-
161
- /**
162
- * Remove functions which have same name but different _id.
163
- * @param func the function to be processing
164
- * @param session the mongodb session for transaction operations
165
- */
166
- async function _processFunctionWithSameNameButNotId ( func : FunctionStruct , session : ClientSession ) {
167
- const db = DatabaseAgent . sys_accessor . db
168
-
169
- // remove functions
170
- const r = await db . collection ( Constants . cn . functions ) . findOneAndDelete (
171
- {
172
- _id : {
173
- $ne : new ObjectId ( func . _id )
174
- } ,
175
- name : func . name
176
- } ,
177
- { session } )
178
-
179
- if ( ! r . value ) {
180
- return
181
- }
182
-
183
- logger . warn ( `delete local func ${ r ?. value ?. _id } with same name with (${ func . _id } :${ func . name } ) but different id ` )
157
+ logger . debug ( `deploy function: inserted a function (${ func . name } ), insertedId ${ ret . insertedId } ` )
184
158
}
0 commit comments