-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
数据库api支持新增绑定块,并设置属性值 #12996
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labels
Comments
用 setAttributeViewBlockAttr 吧 |
setAttributeViewBlockAttr 还要传入cellID,已经传入keyID、rowID了,cellID能否api自己获取呢,作为一个可选参数,而不是必须输入的参数 |
已知rowID和keyid,如何获取cellID let res = await fetchSyncPost("/api/av/getAttributeViewKeys", {
id: '20211116001448-ny4lvyw'
});
const foundItem = res.data.find(item => item.avID === "20241017094451-2urncs9"); //avid
if (foundItem && foundItem.keyValues) {
// 步骤2:在 keyValues 中查找特定 key.id 的项
const specificKey = foundItem.keyValues.find(kv => kv.key.id === "20241102151935-gypad0k"); // keyid
// 步骤3:获取 values 数组的第一个元素的 id
if (specificKey && specificKey.values && specificKey.values.length > 0) {
console.log(specificKey.values[0].id)
//return specificKey.values[0].id;
}
} |
绑定块,同时添加属性 avID = '20241017094451-2urncs9'
avblockID = '20241102151044-yhvye4t'; // 数据库块id
keyID = '20241102151935-gypad0k' // 文本列
docId = '20211116001448-ny4lvyw' //文档
// 数据库绑定块 //
const docids = [docId] // 文档id
const srcs = docids.map(docId => ({
"id": docId,
"isDetached": false,
}));
const input = {
"avID": avID,
"blockID": avblockID,
'srcs': srcs
}
await fetchSyncPost('/api/av/addAttributeViewBlocks', input)
// 已知rowID和keyid,如何获取cellID //
let getAttributeViewKeys = await fetchSyncPost("/api/av/getAttributeViewKeys", {
id: docId
});
// 步骤1:找到特定 avID 的项
const foundItem = getAttributeViewKeys.data.find(item => item.avID === avID); //avid
let cellID;
if (foundItem && foundItem.keyValues) {
// 步骤2:在 keyValues 中查找特定 key.id 的项
const specificKey = foundItem.keyValues.find(kv => kv.key.id === keyID); // keyid
// 步骤3:获取 values 数组的第一个元素的 id
if (specificKey && specificKey.values && specificKey.values.length > 0) {
cellID = specificKey.values[0].id;
}
}
// 设置属性
await fetchSyncPost("/api/av/setAttributeViewBlockAttr", {
avID: avID,
keyID: keyID,
rowID: docId,
cellID: cellID,
value: {
"text": {
"content": '📂Research\n📂Project\n📂Area\n📂Resources\n📂Life'
}
},
}); |
添加日记,自动添加到指定数据库,并自动给Summary文本列添加值 (async ()=>{
window.addEventListener('keydown', function(event) {
// 检查是否按下了 Alt 键和数字 5 键
if (event.altKey && event.key === '5') {
// 模拟按钮点击
console.log('Alt + 5 被按下了!');
dailynoteAddDatabase();
}
});
async function dailynoteAddDatabase() {
//设置日记自动存放的数据库块id
const dbBlockId = '20240911002857-lgav146';
// 获取当前选择笔记本
const boxid = window.siyuan.storage["local-dailynoteid"];
// 设置文本列keyID
const keyID = '20240601175531-vjfh3qj';
// boxid
if (boxid === '20241028223345-d9ifjv0') {
// 调用/api/filetree/createDailyNote获得日记id
const create_dailynote_result =await fetchSyncPost('/api/filetree/createDailyNote', {notebook: boxid, app: siyuan.ws.app.appId})
const docId = create_dailynote_result.data.id;
// 添加日记到数据库中
const db = await getDataBySql(`SELECT * FROM blocks where type ='av' and id='${dbBlockId}'`);
if (db.length === 0) error("未找到数据库文档块,请检查数据库文档块id是否正确");
const avID = db.map(av => getDataAvIdFromHtml(av.markdown))[0];
// 组装文档数据参数
const srcs = {
"id": docId,
"isDetached": false,
};
const input = {
"avID": avID,
"blockID": dbBlockId,
'srcs': srcs
}
await fetchSyncPost('/api/av/addAttributeViewBlocks', input)
// ------------已知rowID(docID)和keyid,如何获取cellID ------------ //
let getAttributeViewKeys = await fetchSyncPost("/api/av/getAttributeViewKeys", {
id: docId
});
// 步骤1:找到特定 avID 的项
const foundItem = getAttributeViewKeys.data.find(item => item.avID === avID); //avid
let cellID;
if (foundItem && foundItem.keyValues) {
// 步骤2:在 keyValues 中查找特定 key.id 的项
const specificKey = foundItem.keyValues.find(kv => kv.key.id === keyID); // keyid
// 步骤3:获取 values 数组的第一个元素的 id
if (specificKey && specificKey.values && specificKey.values.length > 0) {
cellID = specificKey.values[0].id;
}
}
// ------------设置属性 ------------ //
await fetchSyncPost("/api/av/setAttributeViewBlockAttr", {
avID: avID,
keyID: keyID,
rowID: docId,
cellID: cellID,
value: {
"text": {
"content": '📂Research\n📂Project\n📂Area\n📂Resources\n📂Life'
}
},
});
}
function getDataAvIdFromHtml(htmlString) {
// 使用正则表达式匹配data-av-id的值
const match = htmlString.match(/data-av-id="([^"]+)"/);
if (match && match[1]) {
return match[1]; // 返回匹配的值
}
return ""; // 如果没有找到匹配项,则返回空
}
async function getDataBySql(sql) {
const result = await fetchSyncPost('/api/query/sql', { "stmt": sql });
if (result.code !== 0) {
console.error("查询数据库出错", result.msg);
return [];
}
return result.data;
}
async function fetchSyncPost(url, data, returnType = 'json') {
const init = {
method: "POST",
};
if (data) {
if (data instanceof FormData) {
init.body = data;
} else {
init.body = JSON.stringify(data);
}
}
try {
const res = await fetch(url, init);
const res2 = returnType === 'json' ? await res.json() : await res.text();
return res2;
} catch (e) {
console.log(e);
return returnType === 'json' ? { code: e.code || 1, msg: e.message || "", data: null } : "";
}
}
}
})(); |
cellID 参数去掉了,下个 dev 版试试看。 |
ok! |
可以的,感谢! (async ()=>{
window.addEventListener('keydown', function(event) {
// 检查是否按下了 Alt 键和数字 5 键
if (event.altKey && event.key === '5') {
// 模拟按钮点击
console.log('Alt + 5 被按下了!');
dailynoteAddDatabase();
}
});
async function dailynoteAddDatabase() {
//设置日记自动存放的数据库块id
const dbBlockId = '20240911002857-lgav146';
// 设置哪个笔记本创建日记才添加到数据库,其他笔记本创建日记代码不作用
const notebookID = '20241028223345-d9ifjv0'
// 设置文本列keyID
const keyID = '20240601175531-vjfh3qj';
// 设置文本列要设置的模板
const content = '📂Research\n📂Project\n📂Area\n📂Resources\n📂Life';
// 获取当前选择笔记本
const boxid = window.siyuan.storage["local-dailynoteid"];
if (boxid === notebookID) {
// 调用/api/filetree/createDailyNote获得日记id
const create_dailynote_result =await fetchSyncPost('/api/filetree/createDailyNote', {notebook: boxid, app: siyuan.ws.app.appId})
const docID = create_dailynote_result.data.id;
// 添加日记到数据库中
const db = await getDataBySql(`SELECT * FROM blocks where type ='av' and id='${dbBlockId}'`);
if (db.length === 0) error("未找到数据库文档块,请检查数据库文档块id是否正确");
const avID = db.map(av => getDataAvIdFromHtml(av.markdown))[0];
// 检测dailynote是否已经在数据库里
let isInresult = await fetchSyncPost("/api/av/getAttributeViewKeys", {
id: docID
});
let foundItem1 = isInresult.data.find(item => item.avID === avID);
if (foundItem1 && foundItem1.keyValues) {
console.log(docID+"已经在数据库")
return;
}
// 组装文档数据参数
const srcs = [{
"id": docID,
"isDetached": false,
}];
const input = {
"avID": avID,
'srcs': srcs
}
await fetchSyncPost('/api/av/addAttributeViewBlocks', input)
// ------------设置属性 ------------ //
await fetchSyncPost("/api/av/setAttributeViewBlockAttr", {
avID: avID,
keyID: keyID,
rowID: docID,
value: {
"text": {
"content": content
}
},
});
}
function getDataAvIdFromHtml(htmlString) {
// 使用正则表达式匹配data-av-id的值
const match = htmlString.match(/data-av-id="([^"]+)"/);
if (match && match[1]) {
return match[1]; // 返回匹配的值
}
return ""; // 如果没有找到匹配项,则返回空
}
async function getDataBySql(sql) {
const result = await fetchSyncPost('/api/query/sql', { "stmt": sql });
if (result.code !== 0) {
console.error("查询数据库出错", result.msg);
return [];
}
return result.data;
}
async function fetchSyncPost(url, data, returnType = 'json') {
const init = {
method: "POST",
};
if (data) {
if (data instanceof FormData) {
init.body = data;
} else {
init.body = JSON.stringify(data);
}
}
try {
const res = await fetch(url, init);
const res2 = returnType === 'json' ? await res.json() : await res.text();
return res2;
} catch (e) {
console.log(e);
return returnType === 'json' ? { code: e.code || 1, msg: e.message || "", data: null } : "";
}
}
}
})();
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In what scenarios do you need this feature?
/api/av/appendAttributeViewDetachedBlocksWithValue 只能添加非绑定块并设置属性,不支持添加绑定块时设置属性
用/api/av/setAttributeViewBlockAttr有点过于麻烦
Describe the optimal solution
新增api,支持新增绑定块,并设置各列的属性
Describe the candidate solution
No response
Other information
No response
The text was updated successfully, but these errors were encountered: