Cloud Objects and Cloud Functions

Cloud functions are the smallest deployable units in a serverless backend. Their core mental model is event-driven execution. An event arrives, a client identifies a function and supplies context, and the runtime routes the request to a handler. The handler usually exposes a main entry point.

// cloudfunctions/user/index.js
const db = uniCloud.database();

exports.main = async (event, context) => {
  const { action, userId, data } = event;

  if (action === 'get') {
    const res = await db.collection('users').doc(userId).get();
    return { code: 0, data: res.data };
  }

  if (action === 'update') {
    await db.collection('users').doc(userId).update(data);
    return { code: 0, data: { updated: 1 } };
  }

  return { code: 400, message: 'Unknown action' };
};

The client sends a function name and payload:

const res = await uniCloud.callFunction({
  name: 'user',
  data: { action: 'get', userId: '1001' }
});

if (res.result.code === 0) {
  console.log(res.result.data);
}

This pattern is powerful because it supports many trigger types. Functions can run on demand, through schedules, through webhooks, or through internal calls. They are also naturally isolated and scalable.

However, the event contract can become noisy. Developers often define action names, response envelopes, and error codes. That is useful when multiple teams share a backend, but it adds ceremony for simple cases.

Cloud Objects: Encapsulated Business Operations

Cloud objects take a step toward more familiar object-oriented or method-oriented programming. They wrap one or more related operations behind a named interface. The client imports the object and calls methods directly.

// cloudobjects/user.obj.js
const db = uniCloud.database();

module.exports = {
  async get(userId) {
    if (!userId) throw new Error('userId is required');
    const res = await db.collection('users').doc(userId).get();
    return res.data;
  },

  async update(userId, data) {
    await db.collection('users').doc(userId).update(data);
    return { updated: 1 };
  }
};

Client usage:

const user = uniCloud.importObject('user');

try {
  const info = await user.get('1001');
  await user.update('1001', { nickname: 'Tom' });
} catch (e) {
  console.error(e.message);
}

The object style reads more like ordinary async methods. Parameters are explicit, return values are direct, and exceptions can be handled with standard control flow. Interceptors such as _before and _after can enforce cross-cutting concerns.

Where the Difference Matters

Dimension Cloud Function Cloud Object
Invocation uniCloud.callFunction uniCloud.importObject, then method call
Parameter passing Unified event Method arguments
Return value Often needs envelope Returns business data
Error handling Error codes or throws Recommended throws, client catches
Organization One function per entry One object with methods
Interception Manual Built-in _before / _after
Best fit Scheduled tasks, URL endpoints, single features, internal calls Business modules, CRUD, logic aggregation

The distinction is mostly about abstraction level. A cloud function is a low-level execution primitive. A cloud object is a higher-level organizational layer. Under the hood, both may run in the same serverless environment