feat: return normalized mutation results
This commit is contained in:
parent
50fb39fd58
commit
021bb3c700
@ -299,6 +299,17 @@ validated once and never comes from a runtime query."
|
|||||||
(and (listp payload) (assoc key payload))))
|
(and (listp payload) (assoc key payload))))
|
||||||
(push (cons column (etaf-sqlite--record-value payload key)) result))))))
|
(push (cons column (etaf-sqlite--record-value payload key)) result))))))
|
||||||
|
|
||||||
|
(defun etaf-sqlite--mutation-result (connection operation value)
|
||||||
|
"Return a normalized mutation result for OPERATION and VALUE from CONNECTION.
|
||||||
|
|
||||||
|
The result keeps source-specific details behind the Data contract while
|
||||||
|
exposing the inserted identity and affected row count to application code."
|
||||||
|
(list :operation operation
|
||||||
|
:id (when (eq operation 'insert)
|
||||||
|
(caar (sqlite-select connection "SELECT last_insert_rowid()")))
|
||||||
|
:changes (caar (sqlite-select connection "SELECT changes()"))
|
||||||
|
:value value))
|
||||||
|
|
||||||
(defun etaf-sqlite--mutate (database operation payload)
|
(defun etaf-sqlite--mutate (database operation payload)
|
||||||
"Apply one Data mutation OPERATION with PAYLOAD to DATABASE."
|
"Apply one Data mutation OPERATION with PAYLOAD to DATABASE."
|
||||||
(let* ((table (etaf-sqlite--database-table database))
|
(let* ((table (etaf-sqlite--database-table database))
|
||||||
@ -314,7 +325,8 @@ validated once and never comes from a runtime query."
|
|||||||
(etaf-sqlite--transaction
|
(etaf-sqlite--transaction
|
||||||
connection
|
connection
|
||||||
(lambda (transaction-connection)
|
(lambda (transaction-connection)
|
||||||
(pcase operation
|
(let ((value
|
||||||
|
(pcase operation
|
||||||
('insert
|
('insert
|
||||||
(let* ((columns
|
(let* ((columns
|
||||||
(cl-remove-if
|
(cl-remove-if
|
||||||
@ -391,15 +403,19 @@ validated once and never comes from a runtime query."
|
|||||||
(_
|
(_
|
||||||
(signal 'etaf-sqlite-error
|
(signal 'etaf-sqlite-error
|
||||||
(list (format "Unsupported SQLite mutation: %S"
|
(list (format "Unsupported SQLite mutation: %S"
|
||||||
operation)))))))))))
|
operation)))))))
|
||||||
|
(etaf-sqlite--mutation-result
|
||||||
|
transaction-connection operation value))))))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun etaf-sqlite-source (database)
|
(defun etaf-sqlite-source (database)
|
||||||
"Return an `etaf-data-source' backed by DATABASE.
|
"Return an `etaf-data-source' backed by DATABASE.
|
||||||
|
|
||||||
The source accepts equality plist/alist queries and supports `insert',
|
The source accepts equality plist/alist queries and supports `insert',
|
||||||
`replace', `update', and `delete' mutations. Every operation uses a short
|
`replace', `update', and `delete' mutations. Mutation results include the
|
||||||
connection; controllers remain the owner of reactive state and lifecycle."
|
operation, affected row count, and inserted identity when applicable. Every
|
||||||
|
operation uses a short connection; controllers remain the owner of reactive
|
||||||
|
state and lifecycle."
|
||||||
(unless (etaf-sqlite--database-p database)
|
(unless (etaf-sqlite--database-p database)
|
||||||
(signal 'wrong-type-argument
|
(signal 'wrong-type-argument
|
||||||
(list 'etaf-sqlite-database-p database)))
|
(list 'etaf-sqlite-database-p database)))
|
||||||
|
|||||||
@ -60,8 +60,12 @@
|
|||||||
:page-size 20 :auto-load t)))
|
:page-size 20 :auto-load t)))
|
||||||
(unwind-protect
|
(unwind-protect
|
||||||
(progn
|
(progn
|
||||||
(etaf-data-mutate controller 'insert
|
(let ((result (etaf-data-mutate
|
||||||
'(:id 3 :name "Alan" :score 30))
|
controller 'insert
|
||||||
|
'(:id 3 :name "Alan" :score 30))))
|
||||||
|
(should (equal 'insert (plist-get result :operation)))
|
||||||
|
(should (= 3 (plist-get result :id)))
|
||||||
|
(should (= 1 (plist-get result :changes))))
|
||||||
(should (= (etaf-value (etaf-data-total controller)) 3))
|
(should (= (etaf-value (etaf-data-total controller)) 3))
|
||||||
(etaf-data-mutate controller 'update
|
(etaf-data-mutate controller 'update
|
||||||
'(:id 3 :name "Alan Turing" :score 31))
|
'(:id 3 :name "Alan Turing" :score 31))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user