Shopping List Domain Model
Note: This is a work in progress. Use exact versions when installing (e.g. --save-exact
) as backwards-incompatible changes may be introduced.
Shopping List is a series of Offline First demo apps, each built using a different stack. This is a JavaScript implementation of the domain model for the Shopping List app. This package may be used in Vanilla JS, Polymer, React, Preact, Vue.js, Ember.js, React Native, Ionic, Cordova, Electron, and any other JavaScript implementations of the Shopping List demo app.
Installation
This package can be installed via npm:
npm install ibm-shopping-list-model
To use the Shopping List Factory and the Shopping List Repository PouchDB implementation:
const { ShoppingListFactory, ShoppingListRepositoryPouchDB } = require("ibm-shopping-list-model");
Usage
Shopping Lists
Making a New Shopping List
Use a Shopping List Factory to make a new Shopping List:
const { ShoppingListFactory } = require("ibm-shopping-list-model");
const shoppingListFactory = new ShoppingListFactory();
let shoppingList = shoppingListFactory.newShoppingList({
title: "Groceries"
});
console.log(shoppingList._id);
console.log(shoppingList._rev);
console.log(shoppingList.title);
Creating a Shopping List in a Database
Use a Shopping List Repository to create a Shopping List in a database:
const { ShoppingListFactory, ShoppingListRepositoryPouchDB } = require("ibm-shopping-list-model");
const PouchDB = require("pouchdb");
PouchDB.plugin(require("pouchdb-find"));
const shoppingListFactory = new ShoppingListFactory();
const db = new PouchDB("shopping-list");
const shoppingListRepository = new ShoppingListRepositoryPouchDB(db);
let shoppingList = shoppingListFactory.newShoppingList({
title: "Groceries"
});
shoppingListRepository.post(shoppingList).then(shoppingList => {
console.log(shoppingList._id);
console.log(shoppingList._rev);
console.log(shoppingList.title);
});
Reading a Shopping List from a Database
Use a Shopping List Repository to read a Shopping List from the database when you know the _id
value:
const { ShoppingListRepositoryPouchDB } = require("ibm-shopping-list-model");
const PouchDB = require("pouchdb");
PouchDB.plugin(require("pouchdb-find"));
const db = new PouchDB("shopping-list");
const shoppingListRepository = new ShoppingListRepositoryPouchDB(db);
shoppingListRepository.get("list:cj6mj1zfj000001n1ugjfkj33").then(shoppingList => {
console.log(shoppingList._id);
console.log(shoppingList._rev);
});
Modifying a Shopping List
Shopping Lists are Immutable.js Record objects. Use the set
method or the mergeDeep
method to make a modified copy of a Shopping List:
const { ShoppingListFactory } = require("ibm-shopping-list-model");
const shoppingListFactory = new ShoppingListFactory();
let shoppingList = shoppingListFactory.newShoppingList({
title: "Groceries"
});
shoppingList = shoppingList.set("checked", true);
console.log(shoppingList.checked);
shoppingList = shoppingList.mergeDeep({
title: "Groceries for Dinner Party",
checked: false
});
console.log(shoppingList.title);
console.log(shoppingList.checked);
Updating a Shopping List in a Database
Use a Shopping List Repository to update a Shopping List in a database, after saving it to a database:
const { ShoppingListFactory, ShoppingListRepositoryPouchDB } = require("ibm-shopping-list-model");
const PouchDB = require("pouchdb");
PouchDB.plugin(require("pouchdb-find"));
const shoppingListFactory = new ShoppingListFactory();
const db = new PouchDB("shopping-list");
const shoppingListRepository = new ShoppingListRepositoryPouchDB(db);
let shoppingList = shoppingListFactory.newShoppingList({
title: "Groceries"
});
shoppingListRepository.post(shoppingList).then(shoppingList => {
shoppingList = shoppingList.set("checked", true);
return shoppingListRepository.put(shoppingList);
}).then(shoppingList => {
console.log(shoppingList._rev);
console.log(shoppingList.checked);
});
Deleting a Shopping List from a Database
Use a Shopping List Repository to delete a Shopping List from a database, after saving it to a database:
const { ShoppingListFactory, ShoppingListRepositoryPouchDB } = require("ibm-shopping-list-model");
const PouchDB = require("pouchdb");
PouchDB.plugin(require("pouchdb-find"));
const shoppingListFactory = new ShoppingListFactory();
const db = new PouchDB("shopping-list");
const shoppingListRepository = new ShoppingListRepositoryPouchDB(db);
let shoppingList = shoppingListFactory.newShoppingList({
title: "Groceries"
});
shoppingListRepository.post(shoppingList).then(shoppingList => {
console.log(shoppingList._id);
console.log(shoppingList._rev);
console.log(shoppingList.title);
return shoppingListRepository.delete(shoppingList);
}).then(shoppingList => {
console.log(shoppingList._rev);
console.log(shoppingList._deleted);
});
Shopping List Items
Making a New Shopping List Item
Use a Shopping List Factory to make a new Shopping List Item attached to a Shopping List:
const { ShoppingListFactory } = require("ibm-shopping-list-model");
const shoppingListFactory = new ShoppingListFactory();
let shoppingList = shoppingListFactory.newShoppingList({
title: "Groceries"
});
let shoppingListItem = shoppingListFactory.newShoppingListItem({
title: "Mangos"
}, shoppingList);
console.log(shoppingListItem._id);
console.log(shoppingListItem._rev);
console.log(shoppingListItem.title);
Creating a Shopping List Item in a Database
Use a Shopping List Repository to create a Shopping List Item in a database:
const { ShoppingListFactory, ShoppingListRepositoryPouchDB } = require("ibm-shopping-list-model");
const PouchDB = require("pouchdb");
PouchDB.plugin(require("pouchdb-find"));
const shoppingListFactory = new ShoppingListFactory();
const db = new PouchDB("shopping-list");
const shoppingListRepository = new ShoppingListRepositoryPouchDB(db);
let shoppingList = shoppingListFactory.newShoppingList({
title: "Groceries"
});
let shoppingListItem = shoppingListFactory.newShoppingListItem({
title: "Mangos"
}, shoppingList);
shoppingListRepository.post(shoppingList).then(shoppingList => {
return shoppingListRepository.postItem(shoppingListItem);
}).then(shoppingListItem => {
console.log(shoppingListItem._id);
console.log(shoppingListItem._rev);
console.log(shoppingListItem.title);
});
Reading a Shopping List Item from a Database
Use a Shopping List Repository to read a Shopping List Item from the database when you know the _id
value:
const { ShoppingListRepositoryPouchDB } = require("ibm-shopping-list-model");
const PouchDB = require("pouchdb");
PouchDB.plugin(require("pouchdb-find"));
const db = new PouchDB("shopping-list");
const shoppingListRepository = new ShoppingListRepositoryPouchDB(db);
shoppingListRepository.getItem("item:cj6mn7e36000001p9n14fgk6s").then(shoppingListItem => {
console.log(shoppingListItem._id);
console.log(shoppingListItem._rev);
});
Modifying a Shopping List Item
Shopping List Items are Immutable.js Record objects. Use the set
method or the mergeDeep
method to make a modified copy of a Shopping List Item:
const { ShoppingListFactory } = require("ibm-shopping-list-model");
const shoppingListFactory = new ShoppingListFactory();
let shoppingListItem = shoppingListFactory.newShoppingListItem({
title: "Mangos"
});
shoppingListItem = shoppingListItem.set("checked", true);
console.log(shoppingListItem.checked);
shoppingListItem = shoppingListItem.mergeDeep({
title: "Organic Mangos",
checked: false
});
console.log(shoppingListItem.title);
console.log(shoppingListItem.checked);
Updating a Shopping List Item in a Database
Use a Shopping List Repository to update a Shopping List Item in a database, after saving it to a database:
const { ShoppingListFactory, ShoppingListRepositoryPouchDB } = require("ibm-shopping-list-model");
const PouchDB = require("pouchdb");
PouchDB.plugin(require("pouchdb-find"));
const shoppingListFactory = new ShoppingListFactory();
const db = new PouchDB("shopping-list");
const shoppingListRepository = new ShoppingListRepositoryPouchDB(db);
let shoppingList = shoppingListFactory.newShoppingList({
title: "Groceries"
});
let shoppingListItem = shoppingListFactory.newShoppingListItem({
title: "Mangos"
}, shoppingList);
shoppingListRepository.post(shoppingList).then(shoppingList => {
return shoppingListRepository.postItem(shoppingListItem);
}).then(shoppingListItem => {
shoppingListItem = shoppingListItem.set("checked", true);
return shoppingListRepository.putItem(shoppingListItem);
}).then(shoppingListItem => {
console.log(shoppingListItem._rev);
console.log(shoppingListItem.checked);
});
Deleting a Shopping List Item from a Database
Use a Shopping List Repository to delete a Shopping List Item from a database, after saving it to a database:
const { ShoppingListFactory, ShoppingListRepositoryPouchDB } = require("ibm-shopping-list-model");
const PouchDB = require("pouchdb");
PouchDB.plugin(require("pouchdb-find"));
const shoppingListFactory = new ShoppingListFactory();
const db = new PouchDB("shopping-list");
const shoppingListRepository = new ShoppingListRepositoryPouchDB(db);
let shoppingList = shoppingListFactory.newShoppingList({
title: "Groceries"
});
let shoppingListItem = shoppingListFactory.newShoppingListItem({
title: "Mangos"
}, shoppingList);
shoppingListRepository.post(shoppingList).then(shoppingList => {
return shoppingListRepository.postItem(shoppingListItem);
}).then(shoppingListItem => {
console.log(shoppingListItem._id);
console.log(shoppingListItem._rev);
console.log(shoppingListItem.title);
return shoppingListRepository.deleteItem(shoppingListItem);
}).then(shoppingListItem => {
console.log(shoppingListItem._rev);
console.log(shoppingListItem._deleted);
});
Shopping List Item Lists
Making a New Shopping List Item List
Use a Shopping List Factory to make a new Shopping List Item List:
const { ShoppingListFactory } = require("ibm-shopping-list-model");
const shoppingListFactory = new ShoppingListFactory();
let shoppingList = shoppingListFactory.newShoppingList({
title: "Groceries"
});
let shoppingListItem01 = shoppingListFactory.newShoppingListItem({
title: "Mangos"
}, shoppingList);
let shoppingListItem02 = shoppingListFactory.newShoppingListItem({
title: "Oranges"
}, shoppingList);
let groceriesItemList = shoppingListFactory.newShoppingListItemList([
shoppingListItem01,
shoppingListItem02
]);
console.log(groceriesItemList.get(0).title);
console.log(groceriesItemList.get(1).title);
Modifying a Shopping List Item List
Shopping List Item Lists are Immutable.js Lists objects. Use the push
method, the delete
method, or other persistent change List methods to make a modified copy of a Shopping List Item List:
const { ShoppingListFactory } = require("ibm-shopping-list-model");
const shoppingListFactory = new ShoppingListFactory();
let shoppingList = shoppingListFactory.newShoppingList({
title: "Groceries"
});
let shoppingListItem01 = shoppingListFactory.newShoppingListItem({
title: "Mangos"
}, shoppingList);
let shoppingListItem02 = shoppingListFactory.newShoppingListItem({
title: "Oranges"
}, shoppingList);
let groceriesItemList = shoppingListFactory.newShoppingListItemList([
shoppingListItem01,
shoppingListItem02
]);
console.log(groceriesItemList.get(0).title);
console.log(groceriesItemList.get(1).title);
let shoppingListItem03 = shoppingListFactory.newShoppingListItem({
title: "Pears"
}, shoppingList);
groceriesItemList = groceriesItemList.push(shoppingListItem03);
console.log(groceriesItemList.get(0).title);
console.log(groceriesItemList.get(1).title);
console.log(groceriesItemList.get(2).title);
groceriesItemList = groceriesItemList.delete(0);
console.log(groceriesItemList.get(0).title);
console.log(groceriesItemList.get(1).title);
Reading a Shopping List Item List from a Database
Use a Shopping List Repository to read a Shopping List Item List from the database when you know the _id
value of the parent Shopping List:
const { ShoppingListFactory, ShoppingListRepositoryPouchDB } = require("ibm-shopping-list-model");
const PouchDB = require("pouchdb");
PouchDB.plugin(require("pouchdb-find"));
const shoppingListFactory = new ShoppingListFactory();
const db = new PouchDB("shopping-list");
const shoppingListRepository = new ShoppingListRepositoryPouchDB(db);
let shoppingList = shoppingListFactory.newShoppingList({
title: "Groceries"
});
let shoppingListItem01 = shoppingListFactory.newShoppingListItem({
title: "Mangos"
}, shoppingList);
let shoppingListItem02 = shoppingListFactory.newShoppingListItem({
title: "Oranges"
}, shoppingList);
let shoppingListItem03 = shoppingListFactory.newShoppingListItem({
title: "Pears"
}, shoppingList);
let groceriesItemList = shoppingListFactory.newShoppingListItemList([
shoppingListItem01,
shoppingListItem02,
shoppingListItem03
]);
shoppingListRepository.ensureIndexes().then(result => {
return shoppingListRepository.post(shoppingList);
}).then(shoppingList => {
return shoppingListRepository.postItemsBulk(groceriesItemList);
}).then(groceriesItemList => {
return shoppingListRepository.getItemList(shoppingList._id);
}).then(groceriesItemList => {
console.log(groceriesItemList.get(0).title);
console.log(groceriesItemList.get(1).title);
console.log(groceriesItemList.get(2).title);
});