Database methods that mutate tables should return IDs

This commit is contained in:
Jeremy Ruston 2024-03-17 15:06:36 +00:00
parent faa4b9700a
commit 7eaa9b8aec
3 changed files with 32 additions and 19 deletions

View File

@ -87,13 +87,17 @@ SqlTiddlerDatabase.prototype.createTables = function() {
SqlTiddlerDatabase.prototype.listBags = function() {
const rows = this.engine.runStatementGetAll(`
SELECT bag_name, accesscontrol, description
SELECT bag_name, bag_id, accesscontrol, description
FROM bags
ORDER BY bag_name
`);
return rows;
};
/*
Create or update a bag
Returns the bag_id of the bag
*/
SqlTiddlerDatabase.prototype.createBag = function(bag_name,description,accesscontrol) {
accesscontrol = accesscontrol || "";
// Run the queries
@ -103,7 +107,7 @@ SqlTiddlerDatabase.prototype.createBag = function(bag_name,description,accesscon
`,{
$bag_name: bag_name
});
this.engine.runStatement(`
const updateBags = this.engine.runStatement(`
UPDATE bags
SET accesscontrol = $accesscontrol,
description = $description
@ -113,6 +117,7 @@ SqlTiddlerDatabase.prototype.createBag = function(bag_name,description,accesscon
$accesscontrol: accesscontrol,
$description: description
});
return updateBags.lastInsertRowid;
};
/*
@ -120,7 +125,7 @@ Returns array of {recipe_name:,description:,bag_names: []}
*/
SqlTiddlerDatabase.prototype.listRecipes = function() {
const rows = this.engine.runStatementGetAll(`
SELECT r.recipe_name, r.description, b.bag_name, rb.position
SELECT r.recipe_name, r.recipe_id, r.description, b.bag_name, rb.position
FROM recipes AS r
JOIN recipe_bags AS rb ON rb.recipe_id = r.recipe_id
JOIN bags AS b ON rb.bag_id = b.bag_id
@ -134,6 +139,7 @@ SqlTiddlerDatabase.prototype.listRecipes = function() {
currentRecipeIndex += 1;
results.push({
recipe_name: row.recipe_name,
recipe_id: row.recipe_id,
description: row.description,
bag_names: []
});
@ -143,6 +149,10 @@ SqlTiddlerDatabase.prototype.listRecipes = function() {
return results;
};
/*
Create or update a recipe
Returns the recipe_id of the recipe
*/
SqlTiddlerDatabase.prototype.createRecipe = function(recipe_name,bag_names,description) {
// Run the queries
this.engine.runStatement(`
@ -151,7 +161,7 @@ SqlTiddlerDatabase.prototype.createRecipe = function(recipe_name,bag_names,descr
`,{
$recipe_name: recipe_name
});
this.engine.runStatement(`
const updateRecipes = this.engine.runStatement(`
-- Create the entry in the recipes table if required
INSERT OR REPLACE INTO recipes (recipe_name, description)
VALUES ($recipe_name, $description)
@ -170,6 +180,7 @@ SqlTiddlerDatabase.prototype.createRecipe = function(recipe_name,bag_names,descr
$recipe_name: recipe_name,
$bag_names: JSON.stringify(bag_names)
});
return updateRecipes.lastInsertRowid;
};
/*

View File

@ -37,23 +37,23 @@ function runSqlDatabaseTests(engine) {
// Run tests
it("should save and retrieve tiddlers using engine: " + engine, function() {
// Create bags and recipes
sqlTiddlerDatabase.createBag("bag-alpha","Bag alpha");
sqlTiddlerDatabase.createBag("bag-beta","Bag beta");
sqlTiddlerDatabase.createBag("bag-gamma","Bag gamma");
expect(sqlTiddlerDatabase.createBag("bag-alpha","Bag alpha")).toEqual(1);
expect(sqlTiddlerDatabase.createBag("bag-beta","Bag beta")).toEqual(2);
expect(sqlTiddlerDatabase.createBag("bag-gamma","Bag gamma")).toEqual(3);
expect(sqlTiddlerDatabase.listBags()).toEqual([
{ bag_name: 'bag-alpha', accesscontrol: '', description: "Bag alpha" },
{ bag_name: 'bag-beta', accesscontrol: '', description: "Bag beta" },
{ bag_name: 'bag-gamma', accesscontrol: '', description: "Bag gamma" }
{ bag_name: 'bag-alpha', bag_id: 1, accesscontrol: '', description: "Bag alpha" },
{ bag_name: 'bag-beta', bag_id: 2, accesscontrol: '', description: "Bag beta" },
{ bag_name: 'bag-gamma', bag_id: 3, accesscontrol: '', description: "Bag gamma" }
]);
sqlTiddlerDatabase.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"Recipe rho");
sqlTiddlerDatabase.createRecipe("recipe-sigma",["bag-alpha","bag-gamma"],"Recipe sigma");
sqlTiddlerDatabase.createRecipe("recipe-tau",["bag-alpha"],"Recipe tau");
sqlTiddlerDatabase.createRecipe("recipe-upsilon",["bag-alpha","bag-gamma","bag-beta"],"Recipe upsilon");
expect(sqlTiddlerDatabase.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"Recipe rho")).toEqual(1);
expect(sqlTiddlerDatabase.createRecipe("recipe-sigma",["bag-alpha","bag-gamma"],"Recipe sigma")).toEqual(2);
expect(sqlTiddlerDatabase.createRecipe("recipe-tau",["bag-alpha"],"Recipe tau")).toEqual(3);
expect(sqlTiddlerDatabase.createRecipe("recipe-upsilon",["bag-alpha","bag-gamma","bag-beta"],"Recipe upsilon")).toEqual(4);
expect(sqlTiddlerDatabase.listRecipes()).toEqual([
{ recipe_name: 'recipe-rho', bag_names: ["bag-alpha","bag-beta"], description: "Recipe rho" },
{ recipe_name: 'recipe-sigma', bag_names: ["bag-alpha","bag-gamma"], description: "Recipe sigma" },
{ recipe_name: 'recipe-tau', bag_names: ["bag-alpha"], description: "Recipe tau" },
{ recipe_name: 'recipe-upsilon', bag_names: ["bag-alpha","bag-gamma","bag-beta"], description: "Recipe upsilon" }
{ recipe_name: 'recipe-rho', recipe_id: 1, bag_names: ["bag-alpha","bag-beta"], description: "Recipe rho" },
{ recipe_name: 'recipe-sigma', recipe_id: 2, bag_names: ["bag-alpha","bag-gamma"], description: "Recipe sigma" },
{ recipe_name: 'recipe-tau', recipe_id: 3, bag_names: ["bag-alpha"], description: "Recipe tau" },
{ recipe_name: 'recipe-upsilon', recipe_id: 4, bag_names: ["bag-alpha","bag-gamma","bag-beta"], description: "Recipe upsilon" }
]);
expect(sqlTiddlerDatabase.getRecipeBags("recipe-rho")).toEqual(["bag-alpha","bag-beta"]);
expect(sqlTiddlerDatabase.getRecipeBags("recipe-sigma")).toEqual(["bag-alpha","bag-gamma"]);

View File

@ -48,6 +48,7 @@ function runSqlStoreTests(engine) {
expect(store.createBag("bag-alpha", "Bag alpha")).toEqual(null);
expect(store.listBags()).toEqual([{
bag_name: "bag-alpha",
bag_id: 1,
accesscontrol: "",
description: "Bag alpha"
}]);
@ -65,6 +66,7 @@ function runSqlStoreTests(engine) {
expect(store.createBag("bag-alpha", "Different description")).toEqual(null);
expect(store.listBags()).toEqual([{
bag_name: "bag-alpha",
bag_id: 1,
accesscontrol: "",
description: "Different description"
}]);
@ -100,7 +102,7 @@ function runSqlStoreTests(engine) {
expect(store.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"Recipe rho")).toEqual(null);
expect(store.listRecipes()).toEqual([
{ recipe_name: "recipe-rho", bag_names: ["bag-alpha","bag-beta"], description: "Recipe rho" }
{ recipe_name: "recipe-rho", recipe_id: 1, bag_names: ["bag-alpha","bag-beta"], description: "Recipe rho" }
]);
});