-
Notifications
You must be signed in to change notification settings - Fork 0
docs: add README.md #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,320 @@ | ||
| # Alibaba Cloud TableStore SDK | ||
|
|
||
| A clean, TypeScript-native SDK for Alibaba Cloud TableStore (OTS). | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| pnpm install alicloud-tablestore | ||
| # or | ||
| bun add alicloud-tablestore | ||
| ``` | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ```typescript | ||
| import { Client, GetRow, PutRow } from "alicloud-tablestore"; | ||
| import { createPrimaryKey, createAttribute } from "alicloud-tablestore"; | ||
|
|
||
| // Initialize client | ||
| const client = new Client({ | ||
| endpoint: "your-instance.cn-hangzhou.ots.aliyuncs.com", | ||
| accessKeyID: "YOUR_ACCESS_KEY_ID", | ||
| accessKeySecret: "YOUR_ACCESS_KEY_SECRET", | ||
| instanceName: "your-instance", | ||
| }); | ||
|
|
||
| // Put a row | ||
| const putRow = new PutRow(client); | ||
| const putResult = await putRow.do({ | ||
| tableName: "users", | ||
| primaryKey: [createPrimaryKey("id", "user123")], | ||
| attributes: [ | ||
| createAttribute("name", "John Doe"), | ||
| createAttribute("age", 30), | ||
| createAttribute("score", 95.5), | ||
| ], | ||
| }); | ||
|
|
||
| console.log(PutRow.response(putResult)); | ||
|
|
||
| // Get a row | ||
| const getRow = new GetRow(client); | ||
| const getResult = await getRow.do({ | ||
| tableName: "users", | ||
| primaryKey: [createPrimaryKey("id", "user123")], | ||
| maxVersions: 1, | ||
| }); | ||
|
|
||
| console.log(GetRow.response(getResult)); | ||
| ``` | ||
|
|
||
| ## API Reference | ||
|
|
||
| All operators follow the same pattern: `new Operator(client)` → `operator.do(data)` → `Operator.response(result)` | ||
|
|
||
| ### GetRow | ||
|
|
||
| Read a single row from table. | ||
|
|
||
| ```typescript | ||
| import { GetRow } from "alicloud-tablestore"; | ||
|
|
||
| const getRow = new GetRow(client); | ||
| const result = await getRow.do({ | ||
| tableName: "users", | ||
| primaryKey: [createPrimaryKey("id", "123")], | ||
| columnsToGet: ["name", "age"], // optional | ||
| maxVersions: 1, | ||
| }); | ||
|
|
||
| const response = GetRow.response(result); | ||
| ``` | ||
|
|
||
| ### PutRow | ||
|
|
||
| Insert or overwrite a row. | ||
|
|
||
| ```typescript | ||
| import { PutRow, RowExistenceExpectation } from "alicloud-tablestore"; | ||
|
|
||
| const putRow = new PutRow(client); | ||
| const result = await putRow.do({ | ||
| tableName: "users", | ||
| primaryKey: [createPrimaryKey("id", "123")], | ||
| attributes: [ | ||
| createAttribute("name", "Alice"), | ||
| createAttribute("age", 25), | ||
| ], | ||
| condition: { | ||
| rowExistence: RowExistenceExpectation.IGNORE, // or EXPECT_EXIST, EXPECT_NOT_EXIST | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ### UpdateRow | ||
|
|
||
| Update specific columns in an existing row. | ||
|
|
||
| ```typescript | ||
| import { UpdateRow } from "alicloud-tablestore"; | ||
|
|
||
| const updateRow = new UpdateRow(client); | ||
| const result = await updateRow.do({ | ||
| tableName: "users", | ||
| primaryKey: [createPrimaryKey("id", "123")], | ||
| columnToUpdate: { | ||
| put: [createAttribute("name", "Bob")], | ||
| delete: [createAttribute("age", 25)], | ||
| increment: [], // increment columns by value | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ### DeleteRow | ||
|
|
||
| Remove a row from table. | ||
|
|
||
| ```typescript | ||
| import { DeleteRow, RowExistenceExpectation } from "alicloud-tablestore"; | ||
|
|
||
| const deleteRow = new DeleteRow(client); | ||
| const result = await deleteRow.do({ | ||
| tableName: "users", | ||
| primaryKey: [createPrimaryKey("id", "123")], | ||
| condition: { | ||
| rowExistence: RowExistenceExpectation.EXPECT_EXIST, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ### BatchGetRow | ||
|
|
||
| Read multiple rows in a single request (supports multiple tables). | ||
|
|
||
| ```typescript | ||
| import { BatchGetRow } from "alicloud-tablestore"; | ||
|
|
||
| const batchGetRow = new BatchGetRow(client); | ||
| const result = await batchGetRow.do({ | ||
| tables: [ | ||
| { | ||
| tableName: "users", | ||
| primaryKeys: [ | ||
| [createPrimaryKey("id", "123")], | ||
| [createPrimaryKey("id", "456")], | ||
| ], | ||
| maxVersions: 1, | ||
| }, | ||
| { | ||
| tableName: "orders", | ||
| primaryKeys: [[createPrimaryKey("order_id", "789")]], | ||
| maxVersions: 1, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| const response = BatchGetRow.response(result); | ||
| ``` | ||
|
|
||
| ### BatchWriteRow | ||
|
|
||
| Batch write operations (put, update, delete) in a single request. | ||
|
|
||
| ```typescript | ||
| import { BatchWriteRow } from "alicloud-tablestore"; | ||
|
|
||
| const batchWriteRow = new BatchWriteRow(client); | ||
| const result = await batchWriteRow.do({ | ||
| tables: [ | ||
| { | ||
| tableName: "users", | ||
| rows: [ | ||
| { | ||
| type: "PUT", | ||
| condition: { rowExistence: RowExistenceExpectation.IGNORE }, | ||
| primaryKey: [createPrimaryKey("id", "123")], | ||
| attributes: [createAttribute("name", "Charlie")], | ||
| }, | ||
| { | ||
| type: "DELETE", | ||
| condition: { rowExistence: RowExistenceExpectation.IGNORE }, | ||
| primaryKey: [createPrimaryKey("id", "456")], | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
| ``` | ||
|
|
||
| ### GetRange | ||
|
|
||
| Query rows within a range of primary keys. | ||
|
|
||
| ```typescript | ||
| import { GetRange, Direction } from "alicloud-tablestore"; | ||
|
|
||
| const getRange = new GetRange(client); | ||
| const result = await getRange.do({ | ||
| tableName: "users", | ||
| direction: Direction.FORWARD, // or Direction.BACKWARD | ||
| inclusiveStartPrimaryKey: createPrimaryKey("id", "100"), | ||
| exclusiveEndPrimaryKey: createPrimaryKey("id", "200"), | ||
BlackHole1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| limit: 100, | ||
| maxVersions: 1, | ||
| }); | ||
|
|
||
| const response = GetRange.response(result); | ||
| ``` | ||
|
|
||
| ## Helper Functions | ||
|
|
||
| ### createPrimaryKey | ||
|
|
||
| Create a primary key cell. | ||
|
|
||
| ```typescript | ||
| import { createPrimaryKey } from "alicloud-tablestore"; | ||
|
|
||
| // String primary key | ||
| createPrimaryKey("id", "user123"); | ||
|
|
||
| // Double primary key | ||
| createPrimaryKey("timestamp", 1234567890); | ||
|
|
||
| // Integer primary key | ||
| createPrimaryKey("bignum", BigInt("9007199254740991")); | ||
| ``` | ||
|
|
||
| ### createAttribute | ||
|
|
||
| Create an attribute column. | ||
|
|
||
| ```typescript | ||
| import { createAttribute, VariantType } from "alicloud-tablestore"; | ||
|
|
||
| // String attribute | ||
| createAttribute("name", "John"); | ||
|
|
||
| // Double attribute | ||
| createAttribute("age", 30); | ||
|
|
||
| // Boolean attribute | ||
| createAttribute("isActive", true); | ||
|
|
||
| // Binary attribute | ||
| createAttribute("data", new Uint8Array([1, 2, 3])); | ||
|
|
||
| // With timestamp (for versioning) | ||
| createAttribute("name", "John", VariantType.STRING); | ||
| ``` | ||
|
|
||
| ## Advanced Features | ||
|
|
||
| ### Filters | ||
|
|
||
| Use filters to conditionally process rows based on column values. | ||
|
|
||
| ```typescript | ||
| import { singleColumnCondition } from "alicloud-tablestore/builder/filter"; | ||
| import { ComparatorType } from "alicloud-tablestore"; | ||
|
|
||
| const result = await getRow.do({ | ||
| tableName: "users", | ||
| primaryKey: [createPrimaryKey("id", "123")], | ||
| filter: singleColumnCondition("age", 18, ComparatorType.CT_GREATER_THAN), | ||
| maxVersions: 1, | ||
| }); | ||
| ``` | ||
|
|
||
| ### Conditions | ||
|
|
||
| Set conditions for write operations. | ||
|
|
||
| ```typescript | ||
| import { RowExistenceExpectation } from "alicloud-tablestore"; | ||
|
|
||
| await putRow.do({ | ||
| tableName: "users", | ||
| primaryKey: [createPrimaryKey("id", "123")], | ||
| attributes: [createAttribute("name", "Alice")], | ||
| condition: { | ||
| rowExistence: RowExistenceExpectation.EXPECT_NOT_EXIST, // fail if row exists | ||
| columnCondition: singleColumnCondition("status", "active", ComparatorType.CT_EQUAL), | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ### Transactions | ||
|
|
||
| All operations support `transactionID` parameter for atomic operations. | ||
|
|
||
| ```typescript | ||
| await putRow.do({ | ||
| tableName: "users", | ||
| primaryKey: [createPrimaryKey("id", "123")], | ||
| attributes: [createAttribute("name", "Alice")], | ||
| transactionID: "your-transaction-id", | ||
| }); | ||
| ``` | ||
|
|
||
| ### Return Content | ||
|
|
||
| Control what data is returned from write operations. | ||
|
|
||
| ```typescript | ||
| import { ReturnType } from "alicloud-tablestore"; | ||
|
|
||
| await putRow.do({ | ||
| tableName: "users", | ||
| primaryKey: [createPrimaryKey("id", "123")], | ||
| attributes: [createAttribute("name", "Alice")], | ||
| returnContent: { | ||
| returnType: ReturnType.RT_PK, // RT_NONE, RT_PK, RT_AFTER_MODIFY | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| ## License | ||
|
|
||
| MIT | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.