Skip to content

Instantly share code, notes, and snippets.

@tgandrews
Created May 8, 2019 13:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgandrews/a79cc76fca248c25a4ff9c5396166baf to your computer and use it in GitHub Desktop.
Save tgandrews/a79cc76fca248c25a4ff9c5396166baf to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
/* eslint-disable import/unambiguous,no-console,no-new */
const dynamoose = require("dynamoose");
dynamoose.AWS.config.update({
accessKeyId: "dummy",
secretAccessKey: "dummy",
region: "eu-west-1"
});
dynamoose.local("http://localhost:8050");
const Cat = dynamoose.model("Cat", {
id: { type: Number, hashKey: true },
name: String
});
const seedCats = async () => {
for (let i = 0; i < 10; ++i) {
const cat = new Cat({ id: i, name: `Cat ${i}` });
await cat.save();
}
};
const getCat = async id =>
new Promise((res, rej) => {
Cat.queryOne({ id: { eq: id } })
.descending()
.consistent()
.exec((err, cat) => {
if (err) {
rej(err);
return;
}
res(cat);
});
});
const listCats = () =>
new Promise((res, rej) => {
Cat.scan()
.consistent()
.exec((err, cats) => {
if (err) {
rej(err);
return;
}
res(cats);
});
});
const doWork = async () => {
console.log("Creating cats...");
await seedCats();
console.log("...done");
console.log("List cats");
const catList = await listCats();
let readCats = [];
for (let i = 0; i < catList.length; i++) {
readCats.push(await getCat(catList[i].id));
}
console.log("...done");
console.log("Read cats...");
readCats.forEach(catModel => {
console.log("id:", catModel.id, "originalId:", catModel.originalItem().id);
});
};
doWork();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment