@cn-shell/aws-utils
Version:
A Cloud Native extension for AWS
191 lines (190 loc) • 5.25 kB
JavaScript
;
var __createBinding =
(this && this.__createBinding) ||
(Object.create
? function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, {
enumerable: true,
get: function() {
return m[k];
},
});
}
: function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
});
var __setModuleDefault =
(this && this.__setModuleDefault) ||
(Object.create
? function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}
: function(o, v) {
o["default"] = v;
});
var __importStar =
(this && this.__importStar) ||
function(mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null)
for (var k in mod)
if (k !== "default" && Object.hasOwnProperty.call(mod, k))
__createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault =
(this && this.__importDefault) ||
function(mod) {
return mod && mod.__esModule ? mod : { default: mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const cn_shell_1 = __importDefault(require("cn-shell"));
const AWS = __importStar(require("./main"));
const table = process.env["TEST_TABLE"];
class App extends cn_shell_1.default {
constructor(name) {
super(name);
this._table1 = new AWS.DDB.Table("Test", {
region: "eu-west-1",
table: table === undefined ? "UNKNOWN" : table,
partitionKey: "dataType",
sortKey: "dataKey",
});
}
async stop() {}
async healthCheck() {
return true;
}
async start() {
return true;
}
async putTest() {
await this._table1.putItems([
{
dataType: "asset",
dataKey: "xxx",
data: "123",
},
]);
let qryParams = {
partitionKeyValue: "asset",
sortCriteria: {
operator: "EQ",
value: "xxx",
},
};
let results = await this._table1.query(qryParams);
this.info("%j", results);
}
async queryTest() {
let params = {
partitionKeyValue: "device-type",
// sortCriteria: {
// operator: "GT",
// value: Date.now() - 24 * 60 * 60 * 1000 * 15,
// },
attributes: ["gateway", "sensors.type"],
};
let results = await this._table1.query(params);
if (results.Items === undefined) {
return;
}
console.info("%j", results.Items);
// for (let i = 0; i < results.Items.length; i++) {
// let item = results.Items[i];
// console.log("%j", item);
// }
}
async queryTest2() {
let params = {
partitionKeyValue: "thing",
sortCriteria: {
operator: "EQ",
value: "CREEVX#POC-AC1#ORG-1#SITE-1#00137A10000033D1",
},
};
let results = await this._table1.query(params);
if (results.Items === undefined) {
return;
}
for (let i = 0; i < results.Items.length; i++) {
let item = results.Items[i];
if (item.enabled) {
console.log("%j", item);
}
}
}
async updateTest() {
let upParams = {
key: { partitionKeyValue: "asset", sortKeyValue: "xxx" },
set: { data: 123, other: undefined },
};
let res1 = await this._table1.updateItem(upParams);
this.info("%j", res1);
let qryParams = {
partitionKeyValue: "asset",
sortCriteria: {
operator: "EQ",
value: "xxx",
},
};
let results = await this._table1.query(qryParams);
this.info("%j", results);
}
async counterTest() {
let value = await this._table1.getNextAtomicCounter("atomic1");
console.log("counter: %s", value);
sleep(1);
}
async newQry(key) {
return this._table1.getItem(key);
}
async newPut(item) {
return this._table1.putItem(item);
}
async newDelete(key) {
return this._table1.deleteItem(key);
}
async scanTest() {
let results = await this._table1.scan();
if (results.Items === undefined) {
return;
}
console.info("%j", results.Items);
console.info("%j", JSON.stringify(results.Items).length);
// for (let i = 0; i < results.Items.length; i++) {
// let item = results.Items[i];
// console.log("%j", item);
// }
}
}
async function sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
let app = new App("App");
app.start();
(async () => {
// await app.scanTest();
// let item = await app.newQry({ partitionKeyValue: "1", sortKeyValue: "1" });
// console.log("%j", item);
// let success = await app.newPut({ id: "1", sort: "1" });
// console.log(success);
// item = await app.newQry({ partitionKeyValue: "1", sortKeyValue: "1" });
// console.log("%j", item);
// success = await app.newDelete({ partitionKeyValue: "1", sortKeyValue: "1" });
// console.log(success);
// success = await app.newDelete({ partitionKeyValue: "1", sortKeyValue: "1" });
// console.log(success);
// item = await app.newQry({ partitionKeyValue: "1", sortKeyValue: "1" });
// console.log("%j", item);
await app.putTest();
// await app.queryTest();
await app.updateTest();
// await app.counterTest();
})();