Skip to content

Commit 6e09543

Browse files
committed
doc: add documentation string for user-facing methods
1 parent 1e99b02 commit 6e09543

File tree

1 file changed

+42
-6
lines changed

1 file changed

+42
-6
lines changed

src/core/dao.lisp

+42-6
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,29 @@
139139
(:method :before ((obj record-timestamps-mixin))
140140
(let ((now (local-time:now)))
141141
(setf (object-created-at obj) now)
142-
(setf (object-updated-at obj) now))))
142+
(setf (object-updated-at obj) now)))
143+
(:documentation "Insert the object OBJ into the DB."))
143144

144145
(defgeneric create-dao (class &rest initargs)
145146
(:method ((class-name symbol) &rest initargs)
146147
(apply #'create-dao (find-class class-name) initargs))
147148
(:method ((class dao-table-class) &rest initargs)
148149
(let ((obj (apply #'make-instance class initargs)))
149150
(setf (dao-synced obj) nil)
150-
(save-dao obj))))
151+
(save-dao obj)))
152+
(:documentation "Create an object of class CLASS with INITARGS and save it into the DB.
153+
154+
Example:
155+
156+
(mito:create-dao 'user :name \"Eitaro Fukamachi\" :email \"[email protected]\")
157+
158+
same as:
159+
160+
(defvar me
161+
(make-instance 'user :name \"Eitaro Fukamachi\" :email \"[email protected]\"))
162+
;=> USER
163+
164+
(mito:insert-dao me)"))
151165

152166
(defgeneric update-dao (obj &key columns)
153167
(:method ((obj dao-class) &key columns)
@@ -167,7 +181,8 @@
167181
(:method :before ((obj record-timestamps-mixin) &key columns)
168182
(declare (ignore columns))
169183
(let ((now (local-time:now)))
170-
(setf (object-updated-at obj) now))))
184+
(setf (object-updated-at obj) now)))
185+
(:documentation "Update the object OBJ into the DB."))
171186

172187
(defgeneric delete-dao (obj)
173188
(:method ((obj dao-class))
@@ -184,7 +199,8 @@
184199
`(:= ,(unlispify key) ,(slot-value obj key)))
185200
primary-key)))))
186201
(setf (dao-synced obj) nil)))
187-
(values)))
202+
(values))
203+
(:documentation "Delete the object OBJ from the DB."))
188204

189205
(defgeneric delete-by-values (class &rest fields-and-values)
190206
(:method ((class symbol) &rest fields-and-values)
@@ -194,13 +210,16 @@
194210
(execute-sql
195211
(sxql:delete-from (sxql:make-sql-symbol (table-name class))
196212
(where-and fields-and-values class))))
197-
(values)))
213+
(values))
214+
(:documentation "Delete the records of class CLASS matching FIELDS-AND-VALUES from the DB.
215+
For example: (mito:delete-by-values 'user :id 1)"))
198216

199217
(defgeneric save-dao (obj)
200218
(:method ((obj dao-class))
201219
(if (dao-synced obj)
202220
(update-dao obj)
203-
(insert-dao obj))))
221+
(insert-dao obj)))
222+
(:documentation "Save the object OBJ into the DB."))
204223

205224
(defstruct mito-cursor
206225
cursor
@@ -342,6 +361,23 @@
342361
(defparameter *want-cursor* nil)
343362

344363
(defmacro select-dao (class &body clauses)
364+
"Build custom queries with SxQL.
365+
366+
Example:
367+
368+
(select-dao 'tweet
369+
(where (:like :status \"%Japan%\")))
370+
371+
You can use \"includes\" to eagerly load another table and prevent the \"N+1 query\" performance problem.
372+
373+
Example:
374+
375+
(defvar *tweets-contain-japan*
376+
(select-dao 'tweet
377+
(includes 'user)
378+
(where (:like :status \"%Japan%\"))))
379+
380+
See the SxQL documentation for the available clauses and operators."
345381
(with-gensyms (sql clause results include-classes foreign-class)
346382
(once-only (class)
347383
`(#+sb-package-locks locally #+sb-package-locks (declare (sb-ext:disable-package-locks sxql:where))

0 commit comments

Comments
 (0)