Skip to content

Commit

Permalink
add browser sample,modify some code to support compression
Browse files Browse the repository at this point in the history
  • Loading branch information
伯箫 committed Dec 5, 2017
1 parent 1581465 commit 63c4b27
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- 阿里云表格存储是阿里云自主研发的NoSQL数据存储服务,提供海量结构化数据的存储和实时访问。

## 版本
- 当前版本:4.0.8
- 当前版本:4.0.9

## 安装

Expand Down
40 changes: 40 additions & 0 deletions browser.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
## 在浏览器端使用

现在 tablestore-js-sdk 只支持特定的实例在浏览器端调用, 如果需要在浏览器中使用,请通过钉钉联系我们,将您的实例在后端做一下处理。
使用方式请参考 sample/browser/index.html。

### 如何 build

目前在浏览器端运行的 sdk 还在测试阶段, 如果有问题请随时提出, 如果需要自己转换js sdk请按照以下步骤操作:

- git clone https://github.com/aliyun/aliyun-tablestore-nodejs-sdk.git
- cd aliyun-tablestore-nodejs-sdk
- npm install
- bower install
- npm install -g browserify

```sh
browserify browser.js > tablestore-js-sdk.js
```

使用uglijs压缩:
```sh
uglifyjs tablestore-js-sdk.js -o tablestore-js-sdk.min.js
```

## 初始化

```javascript
考虑安全问题,请使用 STS token 初始化 TableStore Client
```

```javascript
var stsTokenClient = new TableStore.Client({
accessKeyId: "sts token 中的 accessKeyId",
secretAccessKey: "sts token 中的 secretAccessKey",
securityToken: "sts token 中的 securityToken",
endpoint: ' <your endpoint>',
instancename: '<your instance name>'
});
```

24 changes: 12 additions & 12 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,35 +154,35 @@ TableStore.Client = inherit({
/**
* 根据给定的表结构信息创建相应的表。
*/
createTable(params, callback) {
createTable: function createTable(params, callback) {
return this.makeRequest('createTable', params, callback);
},

/**
* 获取当前实例下已创建的所有表的表名。
*/
listTable(params, callback) {
listTable: function listTable(params, callback) {
return this.makeRequest('listTable', params, callback);
},

/**
* 删除本实例下指定的表。
*/
deleteTable(params, callback) {
deleteTable: function deleteTable(params, callback) {
return this.makeRequest('deleteTable', params, callback);
},

/**
* 更新指定表的预留读吞吐量或预留写吞吐量设置。
*/
updateTable(params, callback) {
updateTable: function describeTable(params, callback) {
return this.makeRequest('updateTable', params, callback);
},

/**
* 查询指定表的结构信息和预留读/写吞吐量设置信息。
*/
describeTable(params, callback) {
describeTable: function describeTable(params, callback) {
return this.makeRequest('describeTable', params, callback);
},

Expand All @@ -194,49 +194,49 @@ TableStore.Client = inherit({
/**
* 根据给定的主键读取单行数据。
*/
getRow(params, callback) {
getRow: function getRow(params, callback) {
return this.makeRequest('getRow', params, callback);
},

/**
* 插入数据到指定的行,如果该行不存在,则新增一行;若该行存在,则覆盖原有行。
*/
putRow(params, callback) {
putRow: function putRow(params, callback) {
return this.makeRequest('putRow', params, callback);
},

/**
* 更新指定行的数据。如果该行不存在,则新增一行;若该行存在,则根据请求的内容在这一行中新增、修改或者删除指定列的值。
*/
updateRow(params, callback) {
updateRow: function updateRow(params, callback) {
return this.makeRequest('updateRow', params, callback);
},

/**
* 删除一行数据。
*/
deleteRow(params, callback) {
deleteRow: function deleteRow(params, callback) {
return this.makeRequest('deleteRow', params, callback);
},

/**
* 读取指定主键范围内的数据。
*/
getRange(params, callback) {
getRange: function getRange(params, callback) {
return this.makeRequest('getRange', params, callback);
},

/**
* 批量读取一个或多个表中的若干行数据。
*/
batchGetRow(params, callback) {
batchGetRow: function batchGetRow(params, callback) {
return this.makeRequest('batchGetRow', params, callback);
},

/**
* 批量修改行
*/
batchWriteRow(params, callback) {
batchWriteRow: function batchWriteRow(params, callback) {
return this.makeRequest('batchWriteRow', params, callback);
}

Expand Down
6 changes: 3 additions & 3 deletions lib/protocol/encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ TableStore.encoder = {
}
},

_makeCompositeCondition(condition) {
_makeCompositeCondition: function (condition) {
var proto = new tsFilterProtos.CompositeColumnValueFilter();
proto.combinator = condition.combinator;
if (proto.combinator === undefined || proto.combinator === null) {
Expand All @@ -340,7 +340,7 @@ TableStore.encoder = {
return proto.toBuffer();
},

_makeSingleColumnCondition(condition) {
_makeSingleColumnCondition: function (condition) {
var proto = new tsFilterProtos.SingleColumnValueFilter();

proto.comparator = condition.comparator;
Expand All @@ -355,7 +355,7 @@ TableStore.encoder = {
return proto.toBuffer();
},

_makeColumnPaginationFilter(condition) {
_makeColumnPaginationFilter: function (condition) {
var proto = new tsFilterProtos.ColumnPaginationFilter();
proto.offset = condition.offset;
proto.limit = condition.limit;
Expand Down
4 changes: 2 additions & 2 deletions lib/protocol/plain_buffer_coded_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ TableStore.PlainBufferCodedInputStream = inherit({
} else if (columnType == TableStore.plainBufferConsts.VT_DOUBLE) {
var result = this.inputStream.readDoubleAndInt64();
var int64 = result.int64LE;
var double = result.double;
var doubleVal = result.doubleVal;
cellCheckSum = TableStore.plainBufferCrc8.crcInt8(cellCheckSum, TableStore.plainBufferConsts.VT_DOUBLE);
cellCheckSum = TableStore.plainBufferCrc8.crcInt64Buf(cellCheckSum, int64.toBuffer());
this.readTag();

columnVal = double;
columnVal = doubleVal;
} else {
throw new Error("Unsupported column type: " + columnType);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/protocol/plain_buffer_crc8.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ TableStore.plainBufferCrc8 = {
return this.update(crc, bytes_);
},

crcInt8: function (crc, byte) {
return CRC8_TABLE[((crc & 0xff) ^ byte)];
crcInt8: function (crc, byte_) {
return CRC8_TABLE[((crc & 0xff) ^ byte_)];
},

crcInt32: function (crc, byte) {
crcInt32: function (crc, byte_) {
for (var i = 0; i < 4; i++) {
crc = this.crcInt8(crc, (byte >> (i * 8)) & 0xff);
crc = this.crcInt8(crc, (byte_ >> (i * 8)) & 0xff);
}
return crc;
},
Expand Down
4 changes: 2 additions & 2 deletions lib/protocol/plain_buffer_stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ TableStore.PlainBufferInputStream = inherit({

readDoubleAndInt64: function () {
var buf = this.readRawLittleEndian64();
var double = buf.readDoubleLE(0);
var doubleVal = buf.readDoubleLE(0);
var int64LE = new int64buffer.Int64LE(buf);

return { double: double, int64LE: int64LE };
return { doubleVal: doubleVal, int64LE: int64LE };
},

readInt32: function () {
Expand Down
34 changes: 34 additions & 0 deletions samples/browser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="zh-CN">

<head>
<meta charset="UTF-8">
<title>tablestore-js-sdk demo</title>
</head>

<body>
<script src="./tablestore-js-sdk-4.0.9.min.js"></script>
<script>
var Long = TableStore.Long;

var client = new TableStore.Client({
accessKeyId: "sts token accessKeyId",
secretAccessKey: "sts token secretAccessKey",
securityToken: "sts token securityToken",
endpoint: ' <your endpoint>',
instancename: '<your instance name>'
});

client.listTable({}, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});


</script>
</body>

</html>
1 change: 1 addition & 0 deletions samples/browser/tablestore-js-sdk-4.0.9.min.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions samples/getRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ var client = require('./client');

var params = {
tableName: "sampleTable",
primaryKey: [{ 'gid': Long.fromNumber(20004) }, { 'uid': Long.fromNumber(20004) }],
maxVersions: 2
primaryKey: [{ 'gid': Long.fromNumber(20013) }, { 'uid': Long.fromNumber(20013) }],
};
var condition = new TableStore.CompositeCondition(TableStore.LogicalOperator.AND);
condition.addSubCondition(new TableStore.SingleColumnCondition('name', 'john', TableStore.ComparatorType.EQUAL));
Expand Down

0 comments on commit 63c4b27

Please sign in to comment.