1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-09 03:19:56 +00:00
TiddlyWiki5/plugins/tiddlywiki/multiwikiserver/modules/sql-tiddler-store.js

196 lines
5.7 KiB
JavaScript
Raw Normal View History

2024-01-02 14:39:14 +00:00
/*\
title: $:/plugins/tiddlywiki/multiwikiserver/sql-tiddler-store.js
type: application/javascript
module-type: library
Higher level functions to perform basic tiddler operations with a sqlite3 database.
This class is largely a wrapper for the sql-tiddler-database.js class, adding the following functionality:
* Synchronising bag and recipe names to the admin wiki
* Handling _canonical_uri tiddlers
2024-01-02 14:39:14 +00:00
\*/
(function() {
2024-01-05 10:58:07 +00:00
/*
Create a tiddler store. Options include:
databasePath - path to the database file (can be ":memory:" to get a temporary database)
2024-01-17 22:41:41 +00:00
adminWiki - reference to $tw.Wiki object into which entity state tiddlers should be saved
2024-01-05 10:58:07 +00:00
*/
2024-01-02 14:39:14 +00:00
function SqlTiddlerStore(options) {
2024-01-05 10:58:07 +00:00
options = options || {};
2024-01-17 22:41:41 +00:00
this.adminWiki = options.adminWiki || $tw.wiki;
this.entityStateTiddlerPrefix = "$:/state/multiwikiserver/";
// Create the database
this.databasePath = options.databasePath || ":memory:";
var SqlTiddlerDatabase = require("$:/plugins/tiddlywiki/multiwikiserver/sql-tiddler-database.js").SqlTiddlerDatabase;
this.sqlTiddlerDatabase = new SqlTiddlerDatabase({
databasePath: this.databasePath
});
this.sqlTiddlerDatabase.createTables();
this.updateAdminWiki();
2024-01-02 14:39:14 +00:00
}
SqlTiddlerStore.prototype.close = function() {
this.sqlTiddlerDatabase.close();
this.sqlTiddlerDatabase = undefined;
2024-01-02 14:39:14 +00:00
};
2024-01-17 22:41:41 +00:00
SqlTiddlerStore.prototype.saveEntityStateTiddler = function(tiddler) {
this.adminWiki.addTiddler(new $tw.Tiddler(tiddler,{title: this.entityStateTiddlerPrefix + tiddler.title}));
};
SqlTiddlerStore.prototype.updateAdminWiki = function() {
// Update bags
for(const bagInfo of this.listBags()) {
this.saveEntityStateTiddler({
title: "bags/" + bagInfo.bag_name,
"bag-name": bagInfo.bag_name,
text: ""
});
}
// Update recipes
for(const recipeInfo of this.listRecipes()) {
this.saveEntityStateTiddler({
title: "recipes/" + recipeInfo.recipe_name,
"recipe-name": recipeInfo.recipe_name,
text: "",
2024-01-23 12:52:40 +00:00
list: $tw.utils.stringifyList(this.getRecipeBags(recipeInfo.recipe_name).map(bag_name => {
return this.entityStateTiddlerPrefix + "bags/" + bag_name;
}))
});
}
};
/*
Given tiddler fields, tiddler_id and a bagname, return the tiddler fields after the following process:
- If the text field is over a threshold, modify the tiddler to use _canonical_uri
- Apply the tiddler_id as the revision field
- Apply the bag_name as the bag field
*/
SqlTiddlerStore.prototype.processOutgoingTiddler = function(tiddlerFields,tiddler_id,bag_name,recipe_name) {
if((tiddlerFields.text || "").length > 10 * 1024 * 1024) {
return Object.assign({},tiddlerFields,{
revision: "" + tiddler_id,
bag: bag_name,
text: undefined,
_canonical_uri: recipe_name
? `/wiki/${recipe_name}/recipes/${recipe_name}/tiddlers/${title}`
: `/wiki/${bag_name}/bags/${bag_name}/tiddlers/${title}`
});
} else {
return Object.assign({},tiddlerFields,{
revision: "" + tiddler_id,
bag: bag_name
});
}
};
2024-01-02 14:39:14 +00:00
SqlTiddlerStore.prototype.logTables = function() {
this.sqlTiddlerDatabase.logTables();
2024-01-02 14:39:14 +00:00
};
2024-01-17 22:41:41 +00:00
SqlTiddlerStore.prototype.listBags = function() {
return this.sqlTiddlerDatabase.listBags();
2024-01-17 22:41:41 +00:00
};
2024-01-05 10:58:07 +00:00
SqlTiddlerStore.prototype.createBag = function(bagname) {
this.sqlTiddlerDatabase.createBag(bagname);
2024-01-17 22:41:41 +00:00
this.saveEntityStateTiddler({
title: "bags/" + bagname,
"bag-name": bagname,
text: ""
});
};
SqlTiddlerStore.prototype.listRecipes = function() {
return this.sqlTiddlerDatabase.listRecipes();
2024-01-02 14:39:14 +00:00
};
2024-01-05 10:58:07 +00:00
SqlTiddlerStore.prototype.createRecipe = function(recipename,bagnames) {
this.sqlTiddlerDatabase.createRecipe(recipename,bagnames);
2024-01-17 22:41:41 +00:00
this.saveEntityStateTiddler({
title: "recipes/" + recipename,
"recipe-name": recipename,
text: "",
2024-01-23 12:52:40 +00:00
list: $tw.utils.stringifyList(bagnames.map(bag_name => {
return this.entityStateTiddlerPrefix + "bags/" + bag_name;
}))
2024-01-17 22:41:41 +00:00
});
2024-01-02 14:39:14 +00:00
};
/*
Returns {tiddler_id:}
*/
2024-01-19 19:52:57 +00:00
SqlTiddlerStore.prototype.saveBagTiddler = function(tiddlerFields,bagname) {
return this.sqlTiddlerDatabase.saveBagTiddler(tiddlerFields,bagname);
2024-01-02 14:39:14 +00:00
};
/*
Returns {tiddler_id:,bag_name:}
*/
SqlTiddlerStore.prototype.saveRecipeTiddler = function(tiddlerFields,recipename) {
return this.sqlTiddlerDatabase.saveRecipeTiddler(tiddlerFields,recipename);
};
2024-01-02 14:39:14 +00:00
SqlTiddlerStore.prototype.deleteTiddler = function(title,bagname) {
this.sqlTiddlerDatabase.deleteTiddler(title,bagname);
2024-01-02 14:39:14 +00:00
};
/*
returns {tiddler_id:,tiddler:}
*/
SqlTiddlerStore.prototype.getBagTiddler = function(title,bagname) {
var tiddlerInfo = this.sqlTiddlerDatabase.getBagTiddler(title,bagname);
if(tiddlerInfo) {
return Object.assign(
{},
tiddlerInfo,
{
tiddler: this.processOutgoingTiddler(tiddlerInfo.tiddler,tiddlerInfo.tiddler_id,bagname,null)
});
} else {
return null;
}
};
/*
Returns {bag_name:, tiddler: {fields}, tiddler_id:}
*/
SqlTiddlerStore.prototype.getRecipeTiddler = function(title,recipename) {
var tiddlerInfo = this.sqlTiddlerDatabase.getRecipeTiddler(title,recipename);
return Object.assign(
{},
tiddlerInfo,
{
tiddler: this.processOutgoingTiddler(tiddlerInfo.tiddler,tiddlerInfo.tiddler_id,tiddlerInfo.bag_name,recipename)
});
2024-01-02 14:39:14 +00:00
};
2024-01-19 19:25:58 +00:00
/*
Get the titles of the tiddlers in a bag. Returns an empty array for bags that do not exist
*/
SqlTiddlerStore.prototype.getBagTiddlers = function(bagname) {
return this.sqlTiddlerDatabase.getBagTiddlers(bagname);
2024-01-19 19:25:58 +00:00
};
2024-01-02 14:39:14 +00:00
/*
Get the titles of the tiddlers in a recipe as {title:,bag_name:}. Returns an empty array for recipes that do not exist
2024-01-02 14:39:14 +00:00
*/
SqlTiddlerStore.prototype.getRecipeTiddlers = function(recipename) {
return this.sqlTiddlerDatabase.getRecipeTiddlers(recipename);
2024-01-02 14:39:14 +00:00
};
/*
Get the names of the bags in a recipe. Returns an empty array for recipes that do not exist
*/
SqlTiddlerStore.prototype.getRecipeBags = function(recipename) {
return this.sqlTiddlerDatabase.getRecipeBags(recipename);
};
2024-01-02 14:39:14 +00:00
exports.SqlTiddlerStore = SqlTiddlerStore;
})();