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 implementation 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.put(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 a 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);
});
Finding a List of Shopping Lists from a Database
Use a Shopping List Repository to find a List of Shopping Lists from 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 shoppingList01 = shoppingListFactory.newShoppingList({
title: "Groceries"
});
let shoppingList02 = shoppingListFactory.newShoppingList({
title: "Camping Supplies"
});
let listOfShoppingLists = shoppingListFactory.newListOfShoppingLists([
shoppingList01,
shoppingList02
]);
shoppingListRepository.ensureIndexes().then(result => {
return shoppingListRepository.putBulk(listOfShoppingLists);
}).then(listOfShoppingLists => {
return shoppingListRepository.find();
}).then(listOfShoppingLists => {
console.log(listOfShoppingLists.get(0).title);
console.log(listOfShoppingLists.get(1).title);
});
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.put(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 (and all of its Shopping List Items) 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.ensureIndexes().then(result => {
return shoppingListRepository.put(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.put(shoppingList).then(shoppingList => {
return shoppingListRepository.putItem(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 a 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);
});
Finding a List of Shopping List Items for a Shopping List from a Database
Use a Shopping List Repository to find a List of Shopping List Items from a 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 listOfGroceriesItems = shoppingListFactory.newListOfShoppingListItems([
shoppingListItem01,
shoppingListItem02,
shoppingListItem03
]);
shoppingListRepository.ensureIndexes().then(result => {
return shoppingListRepository.put(shoppingList);
}).then(shoppingList => {
return shoppingListRepository.putItemsBulk(listOfGroceriesItems);
}).then(listOfGroceriesItems => {
return shoppingListRepository.findItems({
selector: {
type: "item",
list: shoppingList._id
}
});
}).then(listOfGroceriesItems => {
console.log(listOfGroceriesItems.get(0).title);
console.log(listOfGroceriesItems.get(1).title);
console.log(listOfGroceriesItems.get(2).title);
});
Finding a Count of Shopping List Items for a Shopping List from a Database
Use a Shopping List Repository to find a count of Shopping List Items from a 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 listOfGroceriesItems = shoppingListFactory.newListOfShoppingListItems([
shoppingListItem01,
shoppingListItem02,
shoppingListItem03
]);
shoppingListRepository.ensureIndexes().then(result => {
return shoppingListRepository.put(shoppingList);
}).then(shoppingList => {
return shoppingListRepository.putItemsBulk(listOfGroceriesItems);
}).then(listOfGroceriesItems => {
return shoppingListRepository.findItemsCountByList({
selector: {
type: "item",
list: shoppingList._id
},
fields: [ "list" ]
});
}).then(itemsCount => {
console.log(itemsCount.get(shoppingList._id));
});
Finding a Count of Checked Shopping List Items for a Shopping List from a Database
Use a Shopping List Repository to find a count of checked Shopping List Items from a 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",
checked: true
}, shoppingList);
let shoppingListItem02 = shoppingListFactory.newShoppingListItem({
title: "Oranges",
checked: true
}, shoppingList);
let shoppingListItem03 = shoppingListFactory.newShoppingListItem({
title: "Pears"
}, shoppingList);
let listOfGroceriesItems = shoppingListFactory.newListOfShoppingListItems([
shoppingListItem01,
shoppingListItem02,
shoppingListItem03
]);
shoppingListRepository.ensureIndexes().then(result => {
return shoppingListRepository.put(shoppingList);
}).then(shoppingList => {
return shoppingListRepository.putItemsBulk(listOfGroceriesItems);
}).then(listOfGroceriesItems => {
return shoppingListRepository.findItemsCountByList({
selector: {
type: "item",
list: shoppingList._id,
checked: true
},
fields: [ "list" ]
});
}).then(itemsCount => {
console.log(itemsCount.get(shoppingList._id));
});
Finding a Count of Shopping List Items by Shopping List from a Database
Use a Shopping List Repository to find a count of Shopping List Items by Shopping List from 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 shoppingList01 = shoppingListFactory.newShoppingList({
title: "Groceries"
});
let shoppingList02 = shoppingListFactory.newShoppingList({
title: "Camping Supplies"
});
let listOfShoppingLists = shoppingListFactory.newListOfShoppingLists([
shoppingList01,
shoppingList02
]);
let shoppingListItem01 = shoppingListFactory.newShoppingListItem({
title: "Mangos"
}, shoppingList01);
let shoppingListItem02 = shoppingListFactory.newShoppingListItem({
title: "Oranges"
}, shoppingList01);
let shoppingListItem03 = shoppingListFactory.newShoppingListItem({
title: "Pears"
}, shoppingList01);
let shoppingListItem04 = shoppingListFactory.newShoppingListItem({
title: "Carabiners"
}, shoppingList02);
let shoppingListItem05 = shoppingListFactory.newShoppingListItem({
title: "Socks"
}, shoppingList02);
let listOfShoppingListItems = shoppingListFactory.newListOfShoppingListItems([
shoppingListItem01,
shoppingListItem02,
shoppingListItem03,
shoppingListItem04,
shoppingListItem05
]);
shoppingListRepository.ensureIndexes().then(result => {
return shoppingListRepository.putBulk(listOfShoppingLists);
}).then(listOfShoppingLists => {
return shoppingListRepository.putItemsBulk(listOfShoppingListItems);
}).then(listOfShoppingListItems => {
return shoppingListRepository.findItemsCountByList();
}).then(itemsCount => {
console.log(itemsCount.get(shoppingList01._id));
console.log(itemsCount.get(shoppingList02._id));
});
Finding a Count of Checked Shopping List Items by Shopping List from a Database
Use a Shopping List Repository to find a count of checked Shopping List Items by Shopping List from 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 shoppingList01 = shoppingListFactory.newShoppingList({
title: "Groceries"
});
let shoppingList02 = shoppingListFactory.newShoppingList({
title: "Camping Supplies"
});
let listOfShoppingLists = shoppingListFactory.newListOfShoppingLists([
shoppingList01,
shoppingList02
]);
let shoppingListItem01 = shoppingListFactory.newShoppingListItem({
title: "Mangos",
checked: true
}, shoppingList01);
let shoppingListItem02 = shoppingListFactory.newShoppingListItem({
title: "Oranges",
checked: true
}, shoppingList01);
let shoppingListItem03 = shoppingListFactory.newShoppingListItem({
title: "Pears"
}, shoppingList01);
let shoppingListItem04 = shoppingListFactory.newShoppingListItem({
title: "Carabiners",
checked: true
}, shoppingList02);
let shoppingListItem05 = shoppingListFactory.newShoppingListItem({
title: "Socks"
}, shoppingList02);
let listOfShoppingListItems = shoppingListFactory.newListOfShoppingListItems([
shoppingListItem01,
shoppingListItem02,
shoppingListItem03,
shoppingListItem04,
shoppingListItem05
]);
shoppingListRepository.ensureIndexes().then(result => {
return shoppingListRepository.putBulk(listOfShoppingLists);
}).then(listOfShoppingLists => {
return shoppingListRepository.putItemsBulk(listOfShoppingListItems);
}).then(listOfShoppingListItems => {
return shoppingListRepository.findItemsCountByList({
selector: {
type: "item",
checked: true
},
fields: [ "list" ]
});
}).then(itemsCount => {
console.log(itemsCount.get(shoppingList01._id));
console.log(itemsCount.get(shoppingList02._id));
});
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.put(shoppingList).then(shoppingList => {
return shoppingListRepository.putItem(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.put(shoppingList).then(shoppingList => {
return shoppingListRepository.putItem(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 listOfGroceriesItems = shoppingListFactory.newListOfShoppingListItems([
shoppingListItem01,
shoppingListItem02
]);
console.log(listOfGroceriesItems.get(0).title);
console.log(listOfGroceriesItems.get(1).title);
Modifying Lists of Shopping Lists or Shopping List Items
Lists of Shopping Lists and Shopping List Items 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 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 listOfGroceriesItems = shoppingListFactory.newListOfShoppingListItems([
shoppingListItem01,
shoppingListItem02
]);
console.log(listOfGroceriesItems.get(0).title);
console.log(listOfGroceriesItems.get(1).title);
let shoppingListItem03 = shoppingListFactory.newShoppingListItem({
title: "Pears"
}, shoppingList);
listOfGroceriesItems = listOfGroceriesItems.push(shoppingListItem03);
console.log(listOfGroceriesItems.get(0).title);
console.log(listOfGroceriesItems.get(1).title);
console.log(listOfGroceriesItems.get(2).title);
listOfGroceriesItems = listOfGroceriesItems.delete(0);
console.log(listOfGroceriesItems.get(0).title);
console.log(listOfGroceriesItems.get(1).title);