Class

Transaction

kiss.data.Transaction(configopt) → {object}

kiss.data.Transaction

By default, KissJS is built to work with MongoDb, which is a NoSQL databases. NoSQL are often denormalized. What does it mean?

It means that when there are relationships between Models, some data is voluntarily duplicated accross documents to avoid joints. For example, imagine a Project document which is connected to many Tasks documents.

In that situation, a common practice is to duplicate the project name within all its connected tasks. But then, what happens if you change the project name afterwards? That's exactly where the complex stuff begins!

You will have to update all the connected documents by yourself. But instead of requesting the server for each update, you can batch your updates into a single Transaction.

This is the purpose of this class.

Constructor

# new Transaction(configopt) → {object}

Parameters:
Name Type Attributes Description
config object <optional>
id object <optional>

Optional transaction id

operations object <optional>

List of operations to perform in the transaction (can be added later with addOperation method)

userId object <optional>

User who triggered the transaction, in case we need to timestamp the updated records

View Source common/dataTransaction.js, line 62

this

object
Example
// A transaction looks like this.
// Each operation contains the target model, target record, and updates to perform on the record
{
	"id": "98650fb1-9288-4be9-a611-394211e9fff9",
	"operations": [{
     "action": "update",
		"modelId": "d620b995-89a1-4f1b-a4c6-a4a11949de94",
		"recordId": "6ab2f4fd-e6f3-4fc3-998f-96629e7ef109",
		"updates": {
			"SJg2oX@w": "New project name"
		}
	}, {
     "action": "update",
		"modelId": "01f6c940-e247-4d85-9f35-e3d59ea49289",
		"recordId": "5eb85fe3-2634-466c-839f-08423fc1cac1",
		"updates": {
			"yUbNrXw9": "New project name"
		}
	}, {
     "action": "update",
		"modelId": "01f6c940-e247-4d85-9f35-e3d59ea49289",
		"recordId": "5ae68056-f099-473b-8f5f-af9eeec9ddff",
		"updates": {
			"yUbNrXw9": "New project name"
		}
	}, {
     "action": "update",
		"modelId": "01f6c940-e247-4d85-9f35-e3d59ea49289",
		"recordId": "1f7f9d6a-2cbc-42f1-80c4-8ad795141493",
		"updates": {
			"yUbNrXw9": "New project name"
		}
	}]
}

Methods

# addOperation(operation) → {object}

Add an operation to the Transaction. A single operation can contain multiple field updates.

Parameters:
Name Type Description
operation object
action string

Only "update" at the moment. Will support "insert" and "delete" in the future.

modelId string

The target model (allow to batch the operations by database table)

recordId string

The record to update

updates object

The update to apply to the record

View Source common/dataTransaction.js, line 93

The transaction itself (makes the method chainable)

object
Example
myTransaction.add({
 action: "update",
 modelId: "project",
 recordId: "5eb85fe3-2634-466c-839f-08423fc1cac1",
 updates: {
     name: "New project name",
     status: "pending"
 }
})

# async process() → {Array.<object>}

Process a transaction

View Source common/dataTransaction.js, line 104

Array of operations in case of success, empty Array otherwise

Array.<object>