ElectricCollectionConfig

Interface: ElectricCollectionConfig<TExplicit, TSchema, TFallback>

定义于: packages/electric-db-collection/src/electric.ts:73

Electric 集合配置选项的接口

备注

类型解析遵循优先级顺序

  1. 如果您通过泛型参数提供显式类型,则会使用该类型
  2. 如果未提供显式类型但提供了 schema,则会推断出 schema 的输出类型
  3. 如果未提供显式类型也未提供 schema,则会使用后备类型

您应该提供一个显式类型 OR 一个 schema,但不要两者都提供,因为它们会冲突。

类型参数

TExplicit extends Row<unknown> = Row<unknown>

集合中项目的显式类型(最高优先级)

TSchema 继承自 StandardSchemaV1 = never

用于验证和类型推断的模式类型(第二优先级)

TFallback extends Row<unknown> = Row<unknown>

如果没有提供显式类型或模式类型,则回退类型

属性

getKey()

ts
getKey: (item) => string | number;
getKey: (item) => string | number;

定义于: packages/electric-db-collection/src/electric.ts:90

参数

item

ResolveType

Returns (返回)

string | number


id?

ts
optional id: string;
optional id: string;

定义于: packages/electric-db-collection/src/electric.ts:88

所有标准的 Collection 配置属性


onDelete()?

ts
optional onDelete: (params) => Promise<{
  txid: number | number[];
}>;
optional onDelete: (params) => Promise<{
  txid: number | number[];
}>;

定义于: packages/electric-db-collection/src/electric.ts:246

可选的异步处理函数,在删除操作之前调用。必须返回一个包含 txid 数字或 txid 数组的对象。

参数

params

DeleteMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>

包含事务和集合信息的对象

Returns (返回)

Promise<{ txid: number | number[]; }>

解析为包含 txid 或 txids 的对象的 Promise

示例

ts
// Basic Electric delete handler - MUST return { txid: number }
onDelete: async ({ transaction }) => {
  const mutation = transaction.mutations[0]
  const result = await api.todos.delete({
    id: mutation.original.id
  })
  return { txid: result.txid } // Required for Electric sync matching
}
// Basic Electric delete handler - MUST return { txid: number }
onDelete: async ({ transaction }) => {
  const mutation = transaction.mutations[0]
  const result = await api.todos.delete({
    id: mutation.original.id
  })
  return { txid: result.txid } // Required for Electric sync matching
}
ts
// Delete handler with multiple items - return array of txids
onDelete: async ({ transaction }) => {
  const deletes = await Promise.all(
    transaction.mutations.map(m =>
      api.todos.delete({
        where: { id: m.key }
      })
    )
  )
  return { txid: deletes.map(d => d.txid) } // Array of txids
}
// Delete handler with multiple items - return array of txids
onDelete: async ({ transaction }) => {
  const deletes = await Promise.all(
    transaction.mutations.map(m =>
      api.todos.delete({
        where: { id: m.key }
      })
    )
  )
  return { txid: deletes.map(d => d.txid) } // Array of txids
}
ts
// Delete handler with batch operation - single txid
onDelete: async ({ transaction }) => {
  const idsToDelete = transaction.mutations.map(m => m.original.id)
  const result = await api.todos.deleteMany({
    ids: idsToDelete
  })
  return { txid: result.txid } // Single txid for batch operation
}
// Delete handler with batch operation - single txid
onDelete: async ({ transaction }) => {
  const idsToDelete = transaction.mutations.map(m => m.original.id)
  const result = await api.todos.deleteMany({
    ids: idsToDelete
  })
  return { txid: result.txid } // Single txid for batch operation
}
ts
// Delete handler with optimistic rollback
onDelete: async ({ transaction }) => {
  const mutation = transaction.mutations[0]
  try {
    const result = await api.deleteTodo(mutation.original.id)
    return { txid: result.txid }
  } catch (error) {
    // Transaction will automatically rollback optimistic changes
    console.error('Delete failed, rolling back:', error)
    throw error
  }
}
// Delete handler with optimistic rollback
onDelete: async ({ transaction }) => {
  const mutation = transaction.mutations[0]
  try {
    const result = await api.deleteTodo(mutation.original.id)
    return { txid: result.txid }
  } catch (error) {
    // Transaction will automatically rollback optimistic changes
    console.error('Delete failed, rolling back:', error)
    throw error
  }
}

onInsert()?

ts
optional onInsert: (params) => Promise<{
  txid: number | number[];
}>;
optional onInsert: (params) => Promise<{
  txid: number | number[];
}>;

定义于: packages/electric-db-collection/src/electric.ts:141

可选的异步处理函数,在插入操作之前调用。必须返回一个包含 txid 数字或 txid 数组的对象。

参数

params

InsertMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>

包含事务和集合信息的对象

Returns (返回)

Promise<{ txid: number | number[]; }>

解析为包含 txid 或 txids 的对象的 Promise

示例

ts
// Basic Electric insert handler - MUST return { txid: number }
onInsert: async ({ transaction }) => {
  const newItem = transaction.mutations[0].modified
  const result = await api.todos.create({
    data: newItem
  })
  return { txid: result.txid } // Required for Electric sync matching
}
// Basic Electric insert handler - MUST return { txid: number }
onInsert: async ({ transaction }) => {
  const newItem = transaction.mutations[0].modified
  const result = await api.todos.create({
    data: newItem
  })
  return { txid: result.txid } // Required for Electric sync matching
}
ts
// Insert handler with multiple items - return array of txids
onInsert: async ({ transaction }) => {
  const items = transaction.mutations.map(m => m.modified)
  const results = await Promise.all(
    items.map(item => api.todos.create({ data: item }))
  )
  return { txid: results.map(r => r.txid) } // Array of txids
}
// Insert handler with multiple items - return array of txids
onInsert: async ({ transaction }) => {
  const items = transaction.mutations.map(m => m.modified)
  const results = await Promise.all(
    items.map(item => api.todos.create({ data: item }))
  )
  return { txid: results.map(r => r.txid) } // Array of txids
}
ts
// Insert handler with error handling
onInsert: async ({ transaction }) => {
  try {
    const newItem = transaction.mutations[0].modified
    const result = await api.createTodo(newItem)
    return { txid: result.txid }
  } catch (error) {
    console.error('Insert failed:', error)
    throw error // This will cause the transaction to fail
  }
}
// Insert handler with error handling
onInsert: async ({ transaction }) => {
  try {
    const newItem = transaction.mutations[0].modified
    const result = await api.createTodo(newItem)
    return { txid: result.txid }
  } catch (error) {
    console.error('Insert failed:', error)
    throw error // This will cause the transaction to fail
  }
}
ts
// Insert handler with batch operation - single txid
onInsert: async ({ transaction }) => {
  const items = transaction.mutations.map(m => m.modified)
  const result = await api.todos.createMany({
    data: items
  })
  return { txid: result.txid } // Single txid for batch operation
}
// Insert handler with batch operation - single txid
onInsert: async ({ transaction }) => {
  const items = transaction.mutations.map(m => m.modified)
  const result = await api.todos.createMany({
    data: items
  })
  return { txid: result.txid } // Single txid for batch operation
}

onUpdate()?

ts
optional onUpdate: (params) => Promise<{
  txid: number | number[];
}>;
optional onUpdate: (params) => Promise<{
  txid: number | number[];
}>;

定义于: packages/electric-db-collection/src/electric.ts:189

可选的异步处理函数,在更新操作之前调用。必须返回一个包含 txid 数字或 txid 数组的对象。

参数

params

UpdateMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>

包含事务和集合信息的对象

Returns (返回)

Promise<{ txid: number | number[]; }>

解析为包含 txid 或 txids 的对象的 Promise

示例

ts
// Basic Electric update handler - MUST return { txid: number }
onUpdate: async ({ transaction }) => {
  const { original, changes } = transaction.mutations[0]
  const result = await api.todos.update({
    where: { id: original.id },
    data: changes // Only the changed fields
  })
  return { txid: result.txid } // Required for Electric sync matching
}
// Basic Electric update handler - MUST return { txid: number }
onUpdate: async ({ transaction }) => {
  const { original, changes } = transaction.mutations[0]
  const result = await api.todos.update({
    where: { id: original.id },
    data: changes // Only the changed fields
  })
  return { txid: result.txid } // Required for Electric sync matching
}
ts
// Update handler with multiple items - return array of txids
onUpdate: async ({ transaction }) => {
  const updates = await Promise.all(
    transaction.mutations.map(m =>
      api.todos.update({
        where: { id: m.original.id },
        data: m.changes
      })
    )
  )
  return { txid: updates.map(u => u.txid) } // Array of txids
}
// Update handler with multiple items - return array of txids
onUpdate: async ({ transaction }) => {
  const updates = await Promise.all(
    transaction.mutations.map(m =>
      api.todos.update({
        where: { id: m.original.id },
        data: m.changes
      })
    )
  )
  return { txid: updates.map(u => u.txid) } // Array of txids
}
ts
// Update handler with optimistic rollback
onUpdate: async ({ transaction }) => {
  const mutation = transaction.mutations[0]
  try {
    const result = await api.updateTodo(mutation.original.id, mutation.changes)
    return { txid: result.txid }
  } catch (error) {
    // Transaction will automatically rollback optimistic changes
    console.error('Update failed, rolling back:', error)
    throw error
  }
}
// Update handler with optimistic rollback
onUpdate: async ({ transaction }) => {
  const mutation = transaction.mutations[0]
  try {
    const result = await api.updateTodo(mutation.original.id, mutation.changes)
    return { txid: result.txid }
  } catch (error) {
    // Transaction will automatically rollback optimistic changes
    console.error('Update failed, rolling back:', error)
    throw error
  }
}

schema?

ts
optional schema: TSchema;
optional schema: TSchema;

定义于: packages/electric-db-collection/src/electric.ts:89


shapeOptions

ts
shapeOptions: ShapeStreamOptions<GetExtensions<ResolveType<TExplicit, TSchema, TFallback>>>;
shapeOptions: ShapeStreamOptions<GetExtensions<ResolveType<TExplicit, TSchema, TFallback>>>;

定义于: packages/electric-db-collection/src/electric.ts:81

ElectricSQL ShapeStream 的配置选项


sync?

ts
optional sync: SyncConfig<ResolveType<TExplicit, TSchema, TFallback>, string | number>;
optional sync: SyncConfig<ResolveType<TExplicit, TSchema, TFallback>, string | number>;

定义于: packages/electric-db-collection/src/electric.ts:91

我们的合作伙伴
Code Rabbit
Electric
Prisma
订阅 Bytes

您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。

Bytes

无垃圾邮件。您可以随时取消订阅。

订阅 Bytes

您的每周 JavaScript 资讯。每周一免费发送给超过 10 万开发者。

Bytes

无垃圾邮件。您可以随时取消订阅。