You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guides/basics/generator.md
+21-1Lines changed: 21 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -28,16 +28,36 @@ Since the generated application is using modern features like ES modules, the Fe
28
28
29
29
First, choose if you want to use JavaScript or TypeScript. When presented with the project name, just hit enter, or enter a name (no spaces). Next, write a short description for your application. Confirm the next questions with the default selection by pressing Enter. When asked about authentication methods, let's include GitHub as well so we can look at adding a "Log In with Github" button.
30
30
31
+
<DatabaseBlockglobal-id="sql">
32
+
33
+
<BlockQuotetype="tip">
34
+
35
+
If you want to use **MongoDB** instead of SQLite (or another SQL database) for this quide, select it in the **Database** dropdown in the main menu.
36
+
37
+
</BlockQuote>
38
+
39
+
</DatabaseBlock>
40
+
31
41
Once you confirm the last prompt, the final selection should look similar to this:
`SQLite` creates an SQL database in a file so we don't need to have a database server running. For any other selection, the database you choose has to be available at the connection string.
// If there is a user (e.g. with authentication), they are only allowed to see their own data
201
+
_id: async (value, user, context) => {
202
+
// We want to be able to get a list of all users but
203
+
// only let a user modify their own data otherwise
204
+
if (context.params.user && context.method !== 'find') {
205
+
return context.params.user._id
206
+
}
207
+
208
+
return value
209
+
}
210
+
}
211
+
})
212
+
```
213
+
214
+
</DatabaseBlock>
215
+
130
216
## Handling messages
131
217
132
218
Next we can look at the messages service schema. We want to include the date when the message was created as `createdAt` and the id of the user who sent it as `userId`. When we get a message back, we also want to populate the `user` with the user data from `userId` so that we can show e.g. the user image and email.
@@ -142,6 +228,8 @@ Update the `src/services/messages/messages.schema.js` file like this:
142
228
143
229
</LanguageBlock>
144
230
231
+
<DatabaseBlockglobal-id="sql">
232
+
145
233
```ts{7,14-16,23-26,43-49,56,66-74}
146
234
import { resolve } from '@feathersjs/schema'
147
235
import { Type, getDataValidator, getValidator, querySyntax } from '@feathersjs/typebox'
// We want to be able to get a list of all messages but
380
+
// only let a user access their own messages otherwise
381
+
if (context.params.user && context.method !== 'find') {
382
+
return context.params.user._id
383
+
}
384
+
385
+
return value
386
+
}
387
+
}
388
+
})
389
+
```
390
+
391
+
</DatabaseBlock>
392
+
224
393
## Creating a migration
225
394
395
+
<DatabaseBlockglobal-id="sql">
396
+
226
397
Now that our schemas and resolvers have everything we need, we also have to update the database with those changes. For SQL databases this is done with migrations. Migrations are a best practise for SQL databases to roll out and undo changes to the data model. Every change we make in a schema will need its corresponding migration step.
227
398
228
399
<BlockQuotetype="warning">
@@ -283,6 +454,18 @@ We can run the migrations on the current database with
283
454
npm run migrate
284
455
```
285
456
457
+
</DatabaseBlock>
458
+
459
+
<DatabaseBlockglobal-id="mongodb">
460
+
461
+
<BlockQuotetype="tip">
462
+
463
+
For MongoDB no migrations are necessary.
464
+
465
+
</BlockQuote>
466
+
467
+
</DatabaseBlock>
468
+
286
469
## What's next?
287
470
288
471
In this chapter we learned about schemas and implemented all the things we need for our chat application. In the next chapter we will learn about [authentication](./authentication.md) and add a "Login with GitHub".
Copy file name to clipboardExpand all lines: docs/guides/basics/services.md
+10Lines changed: 10 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -164,8 +164,18 @@ npx feathers generate service
164
164
165
165
The name for our service is `message` (this is used for variable names etc.) and for the path we use `messages`. Anything else we can confirm with the default:
166
166
167
+
<DatabaseBlockglobal-id="sql">
168
+
167
169

168
170
171
+
</DatabaseBlock>
172
+
173
+
<DatabaseBlockglobal-id="mongodb">
174
+
175
+

176
+
177
+
</DatabaseBlock>
178
+
169
179
This is it, we now have a database backed messages service with authentication enabled.
-`showLogin(error)` will either show the content of loginHTML or, if the login page is already showing, add an error message. This will happen when you try to log in with invalid credentials or sign up with a user that already exists.
238
-
-`showChat()` does several things. First, we add the static chatHTML to the page. Then we get the latest 25 messages from the messages Feathers service (this is the same as the `/messages` endpoint of our chat API) using the Feathers query syntax. Since the list will come back with the newest message first, we need to reverse the data. Then we add each message by calling our `addMessage` function so that it looks like a chat app should — with old messages getting older as you scroll up. After that we get a list of all registered users to show them in the sidebar by calling addUser.
237
+
-`showLogin(error)` will either show the content of loginTemplate or, if the login page is already showing, add an error message. This will happen when you try to log in with invalid credentials or sign up with a user that already exists.
238
+
-`showChat()` does several things. First, we add the static chatTemplate to the page. Then we get the latest 25 messages from the messages Feathers service (this is the same as the `/messages` endpoint of our chat API) using the Feathers query syntax. Since the list will come back with the newest message first, we need to reverse the data. Then we add each message by calling our `addMessage` function so that it looks like a chat app should — with old messages getting older as you scroll up. After that we get a list of all registered users to show them in the sidebar by calling addUser.
0 commit comments