Skip to content

Commit 2f12ca3

Browse files
authored
Merge pull request #563 from devforth/feature/AdminForth/1435/migrate-getting-started-to-adm
feat: update getting started guide with resource creation instruction…
2 parents 78a617e + 70dae64 commit 2f12ca3

File tree

1 file changed

+81
-6
lines changed

1 file changed

+81
-6
lines changed

adminforth/documentation/docs/tutorial/001-gettingStarted.md

Lines changed: 81 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -180,68 +180,106 @@ pnpm makemigration --name add-apartments ; pnpm migrate:local
180180

181181
### Step3. Create the `apartments` resource
182182

183-
Create a new file `apartments.ts` in the `resources/` folder:
183+
Use command to create a new file `apartments.ts` in the `resources/` folder
184+
185+
```bash
186+
npx adminforth resource
187+
```
188+
189+
After the resource file is generated, extend it with display and validation settings.
190+
191+
- Use recordLabel to control how each record is represented in lists and relations.
192+
- Apply fillOnCreate to automatically populate fields during creation (e.g., generated IDs or timestamps).
193+
- Add minLength and maxLength to string fields to enforce input constraints.
194+
- Use enum to limit fields to predefined values and render them as dropdowns in the UI.
195+
- Configure allowedActions to control which operations are available for the resource (editing, deleting, viewing, and filtering).
196+
197+
To properly apply these changes, refer to the example below and adjust the configuration according to your settings
184198

185199
```ts title="./resources/apartments.ts"
186200
import { AdminForthDataTypes, AdminForthResourceInput } from 'adminforth';
187201

188202
export default {
189203
dataSource: 'maindb',
190204
table: 'apartments',
205+
//diff-remove
206+
resourceId: 'apartments'
207+
//diff-add
191208
resourceId: 'aparts', // resourceId is defaulted to table name but you can redefine it like this e.g.
192209
// in case of same table names from different data sources
193210
label: 'Apartments', // label is defaulted to table name but you can change it
211+
//diff-add
194212
recordLabel: (r) => `🏡 ${r.title}`,
195213
columns: [
196214
{
197215
name: 'id',
198216
type: AdminForthDataTypes.STRING,
217+
//diff-add
199218
label: 'Identifier', // if you wish you can redefine label, defaulted to uppercased name
200219
showIn: { // show column in filter and in show page
220+
//diff-add
201221
list: false,
222+
//diff-add
202223
edit: false,
224+
//diff-add
203225
create: false,
204226
},
205227
primaryKey: true,
228+
//diff-add
206229
fillOnCreate: ({ initialRecord, adminUser }) => Math.random().toString(36).substring(7), // called during creation to generate content of field, initialRecord is values user entered, adminUser object of user who creates record
207230
},
208231
{
209232
name: 'title',
210233
required: true,
211234
showIn: { all: true }, // all available options
212235
type: AdminForthDataTypes.STRING,
236+
//diff-add
213237
maxLength: 255, // you can set max length for string fields
238+
//diff-add
214239
minLength: 3, // you can set min length for string fields
215240
},
216241
{
217242
name: 'created_at',
218243
type: AdminForthDataTypes.DATETIME,
219244
allowMinMaxQuery: true,
220245
showIn: { create: false },
246+
//diff-add
221247
fillOnCreate: ({ initialRecord, adminUser }) => (new Date()).toISOString(),
222248
},
223249
{
224250
name: 'price',
251+
//diff-add
225252
inputSuffix: 'USD', // you can add a suffix to an input field that will be displayed when creating or editing records
253+
//diff-add
226254
allowMinMaxQuery: true, // use better experience for filtering e.g. date range, set it only if you have index on this column or if you sure there will be low number of rows
255+
//diff-add
227256
editingNote: 'Price is in USD', // you can put a note near field on editing or creating page
228257
},
229258
{
230259
name: 'square_meter',
231260
label: 'Square',
232261
allowMinMaxQuery: true,
262+
//diff-add
233263
minValue: 1, // you can set min /max value for number columns so users will not be able to enter more/less
264+
//diff-add
234265
maxValue: 1000,
235266
},
236267
{
237268
name: 'number_of_rooms',
238269
allowMinMaxQuery: true,
270+
//diff-add
239271
enum: [
272+
//diff-add
240273
{ value: 1, label: '1 room' },
274+
//diff-add
241275
{ value: 2, label: '2 rooms' },
276+
//diff-add
242277
{ value: 3, label: '3 rooms' },
278+
//diff-add
243279
{ value: 4, label: '4 rooms' },
280+
//diff-add
244281
{ value: 5, label: '5 rooms' },
282+
//diff-add
245283
],
246284
},
247285
{
@@ -251,60 +289,102 @@ export default {
251289
},
252290
{
253291
name: 'country',
292+
//diff-add
254293
enum: [{
294+
//diff-add
255295
value: 'US',
296+
//diff-add
256297
label: 'United States'
298+
//diff-add
257299
}, {
300+
//diff-add
258301
value: 'DE',
302+
//diff-add
259303
label: 'Germany'
304+
//diff-add
260305
}, {
306+
//diff-add
261307
value: 'FR',
308+
//diff-add
262309
label: 'France'
310+
//diff-add
263311
}, {
312+
//diff-add
264313
value: 'GB',
314+
//diff-add
265315
label: 'United Kingdom'
316+
//diff-add
266317
}, {
318+
//diff-add
267319
value: 'NL',
320+
//diff-add
268321
label: 'Netherlands'
322+
//diff-add
269323
}, {
324+
//diff-add
270325
value: 'IT',
326+
//diff-add
271327
label: 'Italy'
328+
//diff-add
272329
}, {
330+
//diff-add
273331
value: 'ES',
332+
//diff-add
274333
label: 'Spain'
334+
//diff-add
275335
}, {
336+
//diff-add
276337
value: 'DK',
338+
//diff-add
277339
label: 'Denmark'
340+
//diff-add
278341
}, {
342+
//diff-add
279343
value: 'PL',
344+
//diff-add
280345
label: 'Poland'
346+
//diff-add
281347
}, {
348+
//diff-add
282349
value: 'UA',
350+
//diff-add
283351
label: 'Ukraine'
352+
//diff-add
284353
}, {
354+
//diff-add
285355
value: null,
356+
//diff-add
286357
label: 'Not defined'
358+
//diff-add
287359
}],
288360
},
289361
{
290362
name: 'listed',
363+
//diff-add
291364
required: true, // will be required on create/edit
292365
},
293366
{
294367
name: 'realtor_id',
295368
foreignResource: {
296369
resourceId: 'adminuser',
370+
//diff-add
297371
searchableFields: ["id", "email"], // fields available for search in filter
298372
}
299373
}
300374
],
301375
options: {
302376
listPageSize: 12,
377+
//diff-add
303378
allowedActions: {
379+
//diff-add
304380
edit: true,
381+
//diff-add
305382
delete: true,
383+
//diff-add
306384
show: true,
385+
//diff-add
307386
filter: true,
387+
//diff-add
308388
},
309389
},
310390
} as AdminForthResourceInput;
@@ -322,11 +402,6 @@ import apartmentsResource from "./resources/apartments.js";
322402
...
323403
export const admin = new AdminForth({
324404
...
325-
resources: [
326-
usersResource,
327-
//diff-add
328-
apartmentsResource,
329-
],
330405
menu: [
331406
//diff-add
332407
{

0 commit comments

Comments
 (0)