@@ -788,15 +788,19 @@ Note that the Image -> Comment and Post -> Comment relations define a scope, `co
```js
image.getComments()
// SELECT "id", "title", "commentable", "commentableId", "createdAt", "updatedAt" FROM "comments" AS "comment" WHERE "comment"."commentable" = 'image' AND "comment"."commentableId" = 1;
// SELECT "id", "title", "commentable", "commentableId", "createdAt", "updatedAt" FROM "comments" AS
// "comment" WHERE "comment"."commentable" = 'image' AND "comment"."commentableId" = 1;
// UPDATE "comments" SET "commentableId"=1,"commentable"='image',"updatedAt"='2018-04-17 05:38:43.948 +00:00' WHERE "id" IN (1)
// UPDATE "comments" SET "commentableId"=1,"commentable"='image',"updatedAt"='2018-04-17 05:38:43.948
// +00:00' WHERE "id" IN (1)
```
The `getItem` utility function on `Comment` completes the picture - it simply converts the `commentable` string into a call to either `getImage` or `getPost`, providing an abstraction over whether a comment belongs to a post or an image. You can pass a normal options object as a parameter to `getItem(options)` to specify any where conditions or includes.
Most likely the type you are trying to implement is already included in [DataTypes](/manual/data-types.html). If a new datatype is not included, this manual will show how to write it yourself.
findOrCreate returns an array containing the object that was found or created and a boolean that will be true if a new object was created and false if not, like so:
findOrCreate returns an array containing the object that was found or created and a boolean that
will be true if a new object was created and false if not, like so:
[ {
username: 'sdepold',
...
...
@@ -56,7 +57,10 @@ User
},
true ]
In the example above, the array spread on line 3 divides the array into its 2 parts and passes them as arguments to the callback function defined beginning at line 39, which treats them as "user" and "created" in this case. (So "user" will be the object from index 0 of the returned array and "created" will equal "true".)
In the example above, the array spread on line 3 divides the array into its 2 parts and passes them
as arguments to the callback function defined beginning at line 39, which treats them as "user" and
"created" in this case. (So "user" will be the object from index 0 of the returned array and
The array returned by findOrCreate gets spread into its 2 parts by the array spread on line 3, and the parts will be passed as 2 arguments to the callback function beginning on line 69, which will then treat them as "user" and "created" in this case. (So "user" will be the object from index 0 of the returned array and "created" will equal "false".)
The array returned by findOrCreate gets spread into its 2 parts by the array spread on line 3, and
the parts will be passed as 2 arguments to the callback function beginning on line 69, which will
then treat them as "user" and "created" in this case. (So "user" will be the object from index 0
of the returned array and "created" will equal "false".)