set method
创建或添加数据
Implementation
Future<DbUpdateResponse> set(dynamic data) async {
if (_id == null) {
return DbUpdateResponse(
code: CloudBaseExceptionCode.INVALID_PARAM,
message: 'docId不能为空',
);
}
if (data == null) {
return DbUpdateResponse(
code: CloudBaseExceptionCode.INVALID_PARAM,
message: '参数必需是非空对象',
);
}
if (data['_id'] != null) {
return DbUpdateResponse(
code: CloudBaseExceptionCode.INVALID_PARAM,
message: '不能更新_id的值',
);
}
var hasOperator = _checkOperatorMixed(data);
if (hasOperator) {
return DbUpdateResponse(
code: CloudBaseExceptionCode.DATABASE_REQUEST_FAILED,
message: 'update operator complicit',
);
}
data = Serializer.encode(data);
Map<String, dynamic> query = {'_id': this._id};
Map<String, dynamic> params = {
'query': query,
'data': data,
'multi': false,
'merge': false,
'upsert': true,
'interfaceCallSource': 'SINGLE_SET_DOC',
};
CloudBaseResponse res = await _docRequest(
'database.updateDocument',
params,
);
if (res.code != null) {
return DbUpdateResponse(
code: res.code,
message: res.message,
requestId: res.requestId,
);
}
return DbUpdateResponse(
requestId: res.requestId,
updateId: res.data['upserted_id'],
updated: res.data['updated'],
);
}