edit-icon download-icon

Single-row operations

Last Updated: Apr 08, 2018

The Table Store SDK provides the following single-row operation APIs: PutRow, GetRow, UpdateRow, and DeleteRow.

PutRow

Inserts data into the specified row.

API

  1. /**
  2. * Insert data in the specified row. If this row does not exist, a new row is added. If the row exists, the original row is overwritten.
  3. */
  4. putRow(params, callback)

Example

  1. var TableStore = require('../index.js');
  2. var Long = TableStore.Long;
  3. var client = require('./client');
  4. var currentTimeStamp = Date.now();
  5. var params = {
  6. tableName: "sampleTable",
  7. condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
  8. primaryKey: [{ 'gid': Long.fromNumber(20013) }, { 'uid': Long.fromNumber(20013) }],
  9. attributeColumns: [
  10. { 'col1': 'Table Store' },
  11. { 'col2': '2', 'timestamp': currentTimeStamp },
  12. { 'col3': 3.1 },
  13. { 'col4': -0.32 },
  14. { 'col5': Long.fromNumber(123456789) }
  15. ],
  16. returnContent: { returnType: TableStore.ReturnType.Primarykey }
  17. };
  18. client.putRow(params, function (err, data) {
  19. if (err) {
  20. console.log('error:', err);
  21. return;
  22. }
  23. console.log('success:', data);
  24. });
  • RowExistenceExpectation.IGNORE indicates that new data is still inserted no matter whether the specified row exists or not. If the inserted data is the same as the existing data, the existing data is overwritten.

  • RowExistenceExpectation.EXPECT_EXIST indicates that new data is inserted only when the specified row exists. The existing data is overwritten.

  • RowExistenceExpectation.EXPECT_NOT_EXIST indicates that data is inserted only when the specified row does not exist.

Note: Obtain the full sample codes at PutRow@GitHub.

GetRow

This API reads a single data row based on a given primary key.

API

  1. /**
  2. * Read a single data row based on a given primary key.
  3. */
  4. getRow(params, callback)

Example

Read a data row.

  1. var TableStore = require('../index.js');
  2. var Long = TableStore.Long;
  3. var client = require('./client');
  4. var params = {
  5. tableName: "sampleTable",
  6. primaryKey: [{ 'gid': Long.fromNumber(20004) }, { 'uid': Long.fromNumber(20004) }],
  7. maxVersions: 2
  8. };
  9. var condition = new TableStore.CompositeCondition(TableStore.LogicalOperator.AND);
  10. condition.addSubCondition(new TableStore.SingleColumnCondition('name', 'john', TableStore.ComparatorType.EQUAL));
  11. condition.addSubCondition(new TableStore.SingleColumnCondition('addr', 'china', TableStore.ComparatorType.EQUAL));
  12. params.columnFilter = condition;
  13. client.getRow(params, function (err, data) {
  14. if (err) {
  15. console.log('error:', err);
  16. return;
  17. }
  18. console.log('success:', data);
  19. });

Note: Obtain the full sample codes at GetRow@GitHub.

UpdateRow

Updates the data of the specified row. If the row does not exist, a new row is added. If the row exists, the values of the specified columns are added, modified, or deleted based on the request content.

API

  1. /**
  2. * Update the data of the specified row. If the row does not exist, a new row is added. If the row exists, the values of the specified columns are added, modified, or deleted based on the request content.
  3. */
  4. updateRow(params, callback)

Example

Update a data row.

  1. var TableStore = require('../index.js');
  2. var Long = TableStore.Long;
  3. var client = require('./client');
  4. var params = {
  5. tableName: "sampleTable",
  6. condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
  7. primaryKey: [{ 'gid': Long.fromNumber(9) }, { 'uid': Long.fromNumber(90) }],
  8. updateOfAttributeColumns: [
  9. { 'PUT': [{ 'col4': Long.fromNumber(4) }, { 'col5': '5' }, { 'col6': Long.fromNumber(6) }] },
  10. { 'DELETE': [{ 'col1': Long.fromNumber(1496826473186) }] },
  11. { 'DELETE_ALL': ['col2'] }
  12. ]
  13. };
  14. client.updateRow(params,
  15. function (err, data) {
  16. if (err) {
  17. console.log('error:', err);
  18. return;
  19. }
  20. console.log('success:', data);
  21. });

Note: Obtain the full sample codes at UpdateRow@GitHub.

DeleteRow

API

  1. /**
  2. * Delete a data row.
  3. */
  4. deleteRow(params, callback)

Example

Delete a data row.

  1. var TableStore = require('../index.js');
  2. var Long = TableStore.Long;
  3. var client = require('./client');
  4. var params = {
  5. tableName: "sampleTable",
  6. condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
  7. primaryKey: [{ 'gid': Long.fromNumber(8) }, { 'uid': Long.fromNumber(80) }]
  8. };
  9. client.deleteRow(params, function (err, data) {
  10. if (err) {
  11. console.log('error:', err);
  12. return;
  13. }
  14. console.log('success:', data);
  15. });

Note: Obtain the full sample codes at DeleteRow@GitHub.

Thank you! We've received your feedback.