Exposed some ways to explore databases.

git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@3879 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
Cory McWilliams 2022-05-25 23:45:52 +00:00
parent e5160b9d2c
commit 842633f6d1
2 changed files with 37 additions and 0 deletions

View File

@ -199,6 +199,13 @@ async function getProcessBlob(blobId, key, options) {
var db = new Database(process.credentials.session.name + ':' + key);
return Object.fromEntries(Object.keys(db).map(x => [x, db[x].bind(db)]));
};
imports.my_shared_database = function(packageName, key) {
var db = new Database(':shared:' + process.credentials.session.name + ':' + packageName + ':' + key);
return Object.fromEntries(Object.keys(db).map(x => [x, db[x].bind(db)]));
};
imports.databases = function() {
return [].concat(databases.list(':shared:' + process.credentials.session.name + ':%'), databases.list(process.credentials.session.name + ':%'));
};
}
if (options.packageOwner && options.packageName) {
imports.shared_database = function(key) {

View File

@ -27,6 +27,7 @@ static JSValue _database_exchange(JSContext* context, JSValueConst this_val, int
static JSValue _database_remove(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _database_get_all(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _database_get_like(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv);
static JSValue _databases_list(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data);
void tf_database_register(JSContext* context, sqlite3* sqlite)
{
@ -46,6 +47,9 @@ void tf_database_register(JSContext* context, sqlite3* sqlite)
JSValue constructor = JS_NewCFunctionData(context, _database_create, 0, 0, 1, data);
JS_SetConstructorBit(context, constructor, true);
JS_SetPropertyStr(context, global, "Database", constructor);
JSValue databases = JS_NewObject(context);
JS_SetPropertyStr(context, global, "databases", databases);
JS_SetPropertyStr(context, databases, "list", JS_NewCFunctionData(context, _databases_list, 0, 0, 1, data));
JS_FreeValue(context, global);
}
@ -269,3 +273,29 @@ JSValue _database_get_like(JSContext* context, JSValueConst this_val, int argc,
}
return result;
}
static JSValue _databases_list(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv, int magic, JSValue* data)
{
int64_t value = 0;
JS_ToInt64(context, &value, data[0]);
sqlite3* db = (sqlite3*)(intptr_t)value;
JSValue array = JS_UNDEFINED;
sqlite3_stmt* statement;
if (sqlite3_prepare(db, "SELECT DISTINCT id FROM properties WHERE id LIKE ?", -1, &statement, NULL) == SQLITE_OK)
{
const char* pattern = JS_ToCString(context, argv[0]);
if (sqlite3_bind_text(statement, 1, pattern, -1, NULL) == SQLITE_OK)
{
array = JS_NewArray(context);
uint32_t index = 0;
while (sqlite3_step(statement) == SQLITE_ROW)
{
JS_SetPropertyUint32(context, array, index++, JS_NewStringLen(context, (const char*)sqlite3_column_text(statement, 0), sqlite3_column_bytes(statement, 0)));
}
}
JS_FreeCString(context, pattern);
sqlite3_finalize(statement);
}
return array;
}