1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-30 13:29:56 +00:00

Expose tiddler_ids in bag and recipe listings

This commit is contained in:
Jeremy Ruston 2024-03-17 16:34:45 +00:00
parent 7eaa9b8aec
commit 3d485f0706
4 changed files with 23 additions and 22 deletions

View File

@ -28,11 +28,11 @@ exports.handler = function(request,response,state) {
// Get the parameters // Get the parameters
var bag_name = $tw.utils.decodeURIComponentSafe(state.params[0]), var bag_name = $tw.utils.decodeURIComponentSafe(state.params[0]),
bag_name_2 = $tw.utils.decodeURIComponentSafe(state.params[1]), bag_name_2 = $tw.utils.decodeURIComponentSafe(state.params[1]),
titles = bag_name === bag_name_2 && $tw.mws.store.getBagTiddlers(bag_name); bagTiddlers = bag_name === bag_name_2 && $tw.mws.store.getBagTiddlers(bag_name);
if(bag_name === bag_name_2 && titles) { if(bag_name === bag_name_2 && bagTiddlers) {
// If application/json is requested then this is an API request, and gets the response in JSON // If application/json is requested then this is an API request, and gets the response in JSON
if(request.headers.accept && request.headers.accept.indexOf("application/json") !== -1) { if(request.headers.accept && request.headers.accept.indexOf("application/json") !== -1) {
state.sendResponse(200,{"Content-Type": "application/json"},JSON.stringify(titles),"utf8"); state.sendResponse(200,{"Content-Type": "application/json"},JSON.stringify(bagTiddlers),"utf8");
} else { } else {
// This is not a JSON API request, we should return the raw tiddler content // This is not a JSON API request, we should return the raw tiddler content
response.writeHead(200, "OK",{ response.writeHead(200, "OK",{
@ -49,7 +49,8 @@ exports.handler = function(request,response,state) {
var html = $tw.mws.store.adminWiki.renderTiddler("text/html","$:/plugins/tiddlywiki/multiwikiserver/templates/get-bag",{ var html = $tw.mws.store.adminWiki.renderTiddler("text/html","$:/plugins/tiddlywiki/multiwikiserver/templates/get-bag",{
variables: { variables: {
"bag-name": bag_name, "bag-name": bag_name,
"bag-titles": JSON.stringify(titles) "bag-titles": JSON.stringify(bagTiddlers.map(bagTiddler => bagTiddler.title)),
"bag-tiddlers": JSON.stringify(bagTiddlers)
} }
}); });
response.write(html); response.write(html);

View File

@ -121,7 +121,7 @@ SqlTiddlerDatabase.prototype.createBag = function(bag_name,description,accesscon
}; };
/* /*
Returns array of {recipe_name:,description:,bag_names: []} Returns array of {recipe_name:,recipe_id:,description:,bag_names: []}
*/ */
SqlTiddlerDatabase.prototype.listRecipes = function() { SqlTiddlerDatabase.prototype.listRecipes = function() {
const rows = this.engine.runStatementGetAll(` const rows = this.engine.runStatementGetAll(`
@ -375,7 +375,7 @@ Get the titles of the tiddlers in a bag. Returns an empty array for bags that do
*/ */
SqlTiddlerDatabase.prototype.getBagTiddlers = function(bag_name) { SqlTiddlerDatabase.prototype.getBagTiddlers = function(bag_name) {
const rows = this.engine.runStatementGetAll(` const rows = this.engine.runStatementGetAll(`
SELECT DISTINCT title SELECT DISTINCT title, tiddler_id
FROM tiddlers FROM tiddlers
WHERE bag_id IN ( WHERE bag_id IN (
SELECT bag_id SELECT bag_id
@ -387,11 +387,11 @@ SqlTiddlerDatabase.prototype.getBagTiddlers = function(bag_name) {
`,{ `,{
$bag_name: bag_name $bag_name: bag_name
}); });
return rows.map(value => value.title); return rows;
}; };
/* /*
Get the titles of the tiddlers in a recipe as {title:,bag_name:}. Returns null for recipes that do not exist Get the titles of the tiddlers in a recipe as {title:,tiddler_id:,bag_name:}. Returns null for recipes that do not exist
*/ */
SqlTiddlerDatabase.prototype.getRecipeTiddlers = function(recipe_name) { SqlTiddlerDatabase.prototype.getRecipeTiddlers = function(recipe_name) {
const rowsCheckRecipe = this.engine.runStatementGetAll(` const rowsCheckRecipe = this.engine.runStatementGetAll(`
@ -403,9 +403,9 @@ SqlTiddlerDatabase.prototype.getRecipeTiddlers = function(recipe_name) {
return null; return null;
} }
const rows = this.engine.runStatementGetAll(` const rows = this.engine.runStatementGetAll(`
SELECT title, bag_name SELECT title, tiddler_id, bag_name
FROM ( FROM (
SELECT t.title, b.bag_name, MAX(rb.position) AS position SELECT t.title, t.tiddler_id, b.bag_name, MAX(rb.position) AS position
FROM bags AS b FROM bags AS b
INNER JOIN recipe_bags AS rb ON b.bag_id = rb.bag_id INNER JOIN recipe_bags AS rb ON b.bag_id = rb.bag_id
INNER JOIN recipes AS r ON rb.recipe_id = r.recipe_id INNER JOIN recipes AS r ON rb.recipe_id = r.recipe_id

View File

@ -74,12 +74,12 @@ function runSqlDatabaseTests(engine) {
}); });
// Verify what we've got // Verify what we've got
expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-rho")).toEqual([ expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-rho")).toEqual([
{ title: 'Another Tiddler', bag_name: 'bag-alpha' }, { title: 'Another Tiddler', tiddler_id: 1, bag_name: 'bag-alpha' },
{ title: 'Hello There', bag_name: 'bag-beta' } { title: 'Hello There', tiddler_id: 3, bag_name: 'bag-beta' }
]); ]);
expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-sigma")).toEqual([ expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-sigma")).toEqual([
{ title: 'Another Tiddler', bag_name: 'bag-alpha' }, { title: 'Another Tiddler', tiddler_id: 1, bag_name: 'bag-alpha' },
{ title: 'Hello There', bag_name: 'bag-gamma' } { title: 'Hello There', tiddler_id: 4, bag_name: 'bag-gamma' }
]); ]);
expect(sqlTiddlerDatabase.getRecipeTiddler("Hello There","recipe-rho").tiddler).toEqual({ title: "Hello There", text: "I'm in beta", tags: "four five six" }); expect(sqlTiddlerDatabase.getRecipeTiddler("Hello There","recipe-rho").tiddler).toEqual({ title: "Hello There", text: "I'm in beta", tags: "four five six" });
expect(sqlTiddlerDatabase.getRecipeTiddler("Missing Tiddler","recipe-rho")).toEqual(null); expect(sqlTiddlerDatabase.getRecipeTiddler("Missing Tiddler","recipe-rho")).toEqual(null);
@ -90,17 +90,17 @@ function runSqlDatabaseTests(engine) {
// Delete a tiddlers to ensure the underlying tiddler in the recipe shows through // Delete a tiddlers to ensure the underlying tiddler in the recipe shows through
sqlTiddlerDatabase.deleteTiddler("Hello There","bag-beta"); sqlTiddlerDatabase.deleteTiddler("Hello There","bag-beta");
expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-rho")).toEqual([ expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-rho")).toEqual([
{ title: 'Another Tiddler', bag_name: 'bag-alpha' }, { title: 'Another Tiddler', tiddler_id: 1, bag_name: 'bag-alpha' },
{ title: 'Hello There', bag_name: 'bag-alpha' } { title: 'Hello There', tiddler_id: 2, bag_name: 'bag-alpha' }
]); ]);
expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-sigma")).toEqual([ expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-sigma")).toEqual([
{ title: 'Another Tiddler', bag_name: 'bag-alpha' }, { title: 'Another Tiddler', tiddler_id: 1, bag_name: 'bag-alpha' },
{ title: 'Hello There', bag_name: 'bag-gamma' } { title: 'Hello There', tiddler_id: 4, bag_name: 'bag-gamma' }
]); ]);
expect(sqlTiddlerDatabase.getRecipeTiddler("Hello There","recipe-beta")).toEqual(null); expect(sqlTiddlerDatabase.getRecipeTiddler("Hello There","recipe-beta")).toEqual(null);
sqlTiddlerDatabase.deleteTiddler("Another Tiddler","bag-alpha"); sqlTiddlerDatabase.deleteTiddler("Another Tiddler","bag-alpha");
expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-rho")).toEqual([ { title: 'Hello There', bag_name: 'bag-alpha' } ]); expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-rho")).toEqual([ { title: 'Hello There', tiddler_id: 2, bag_name: 'bag-alpha' } ]);
expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-sigma")).toEqual([ { title: 'Hello There', bag_name: 'bag-gamma' } ]); expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-sigma")).toEqual([ { title: 'Hello There', tiddler_id: 4, bag_name: 'bag-gamma' } ]);
// Save a recipe tiddler // Save a recipe tiddler
expect(sqlTiddlerDatabase.saveRecipeTiddler({title: "More", text: "None"},"recipe-rho")).toEqual({tiddler_id: 7, bag_name: 'bag-beta'}); expect(sqlTiddlerDatabase.saveRecipeTiddler({title: "More", text: "None"},"recipe-rho")).toEqual({tiddler_id: 7, bag_name: 'bag-beta'});
expect(sqlTiddlerDatabase.getRecipeTiddler("More","recipe-rho").tiddler).toEqual({title: "More", text: "None"}); expect(sqlTiddlerDatabase.getRecipeTiddler("More","recipe-rho").tiddler).toEqual({title: "More", text: "None"});

View File

@ -83,7 +83,7 @@ function runSqlStoreTests(engine) {
expect(new Set(Object.keys(saveBagResult))).toEqual(new Set(["tiddler_id"])); expect(new Set(Object.keys(saveBagResult))).toEqual(new Set(["tiddler_id"]));
expect(typeof(saveBagResult.tiddler_id)).toBe("number"); expect(typeof(saveBagResult.tiddler_id)).toBe("number");
expect(store.getBagTiddlers("bag-alpha")).toEqual(["Another Tiddler"]); expect(store.getBagTiddlers("bag-alpha")).toEqual([{title: "Another Tiddler", tiddler_id: 1}]);
var getBagTiddlerResult = store.getBagTiddler("Another Tiddler","bag-alpha"); var getBagTiddlerResult = store.getBagTiddler("Another Tiddler","bag-alpha");
expect(typeof(getBagTiddlerResult.tiddler_id)).toBe("number"); expect(typeof(getBagTiddlerResult.tiddler_id)).toBe("number");
@ -128,7 +128,7 @@ function runSqlStoreTests(engine) {
expect(typeof(saveRecipeResult.tiddler_id)).toBe("number"); expect(typeof(saveRecipeResult.tiddler_id)).toBe("number");
expect(saveRecipeResult.bag_name).toBe("bag-beta"); expect(saveRecipeResult.bag_name).toBe("bag-beta");
expect(store.getRecipeTiddlers("recipe-rho")).toEqual([{title: "Another Tiddler", bag_name: "bag-beta"}]); expect(store.getRecipeTiddlers("recipe-rho")).toEqual([{title: "Another Tiddler", tiddler_id: 1, bag_name: "bag-beta"}]);
var getRecipeTiddlerResult = store.getRecipeTiddler("Another Tiddler","recipe-rho"); var getRecipeTiddlerResult = store.getRecipeTiddler("Another Tiddler","recipe-rho");
expect(typeof(getRecipeTiddlerResult.tiddler_id)).toBe("number"); expect(typeof(getRecipeTiddlerResult.tiddler_id)).toBe("number");