mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	repository now uses upsert instead of insert/replace
This commit is contained in:
		| @@ -103,7 +103,7 @@ async function updateEntity(entity) { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     await sql.transactional(async () => { |     await sql.transactional(async () => { | ||||||
|         await sql.replace(entityName, clone); |         await sql.upsert(entityName, primaryKeyName, clone); | ||||||
|  |  | ||||||
|         const primaryKey = entity[primaryKeyName]; |         const primaryKey = entity[primaryKeyName]; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -9,25 +9,48 @@ function setDbConnection(connection) { | |||||||
|     dbConnection = connection; |     dbConnection = connection; | ||||||
| } | } | ||||||
|  |  | ||||||
| async function insert(table_name, rec, replace = false) { | async function insert(tableName, rec, replace = false) { | ||||||
|     const keys = Object.keys(rec); |     const keys = Object.keys(rec); | ||||||
|     if (keys.length === 0) { |     if (keys.length === 0) { | ||||||
|         log.error("Can't insert empty object into table " + table_name); |         log.error("Can't insert empty object into table " + tableName); | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     const columns = keys.join(", "); |     const columns = keys.join(", "); | ||||||
|     const questionMarks = keys.map(p => "?").join(", "); |     const questionMarks = keys.map(p => "?").join(", "); | ||||||
|  |  | ||||||
|     const query = "INSERT " + (replace ? "OR REPLACE" : "") + " INTO " + table_name + "(" + columns + ") VALUES (" + questionMarks + ")"; |     const query = "INSERT " + (replace ? "OR REPLACE" : "") + " INTO " + tableName + "(" + columns + ") VALUES (" + questionMarks + ")"; | ||||||
|  |  | ||||||
|     const res = await execute(query, Object.values(rec)); |     const res = await execute(query, Object.values(rec)); | ||||||
|  |  | ||||||
|     return res.lastID; |     return res.lastID; | ||||||
| } | } | ||||||
|  |  | ||||||
| async function replace(table_name, rec) { | async function replace(tableName, rec) { | ||||||
|     return await insert(table_name, rec, true); |     return await insert(tableName, rec, true); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | async function upsert(tableName, primaryKey, rec) { | ||||||
|  |     const keys = Object.keys(rec); | ||||||
|  |     if (keys.length === 0) { | ||||||
|  |         log.error("Can't upsert empty object into table " + tableName); | ||||||
|  |         return; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     const columns = keys.join(", "); | ||||||
|  |  | ||||||
|  |     let i = 0; | ||||||
|  |  | ||||||
|  |     const questionMarks = keys.map(p => ":" + i++).join(", "); | ||||||
|  |  | ||||||
|  |     i = 0; | ||||||
|  |  | ||||||
|  |     const updateMarks = keys.map(key => `${key} = :${i++}`).join(", "); | ||||||
|  |  | ||||||
|  |     const query = `INSERT INTO ${tableName} (${columns}) VALUES (${questionMarks})  | ||||||
|  |                    ON CONFLICT (${primaryKey}) DO UPDATE SET ${updateMarks}`; | ||||||
|  |  | ||||||
|  |     await execute(query, Object.values(rec)); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function beginTransaction() { | async function beginTransaction() { | ||||||
| @@ -213,5 +236,6 @@ module.exports = { | |||||||
|     getColumn, |     getColumn, | ||||||
|     execute, |     execute, | ||||||
|     executeScript, |     executeScript, | ||||||
|     transactional |     transactional, | ||||||
|  |     upsert | ||||||
| }; | }; | ||||||
		Reference in New Issue
	
	Block a user