Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function canModifyUsers({ adminUser }: { adminUser: AdminUser }): Promise<
export default {
dataSource: 'maindb',
table: 'adminuser',
resourceId: 'users',
resourceId: 'adminuser',
label: 'Users',
recordLabel: (r) => `👤 ${r.email}`,
options: {
Expand Down Expand Up @@ -44,7 +44,7 @@ export default {
{
name: 'created_at',
type: AdminForthDataTypes.DATETIME,
showIn: [{
showIn: {
edit: false,
create: false,
},
Expand Down
10 changes: 5 additions & 5 deletions adminforth/commands/createApp/templates/index.ts.hbs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import express from 'express';
import AdminForth, { Filters } from 'adminforth';
import usersResource from "./resources/users";
import usersResource from "./resources/adminuser";

const ADMIN_BASE_URL = '';

export const admin = new AdminForth({
baseUrl: ADMIN_BASE_URL,
auth: {
usersResourceId: 'users',
usersResourceId: 'adminuser',
usernameField: 'email',
passwordHashField: 'password_hash',
rememberMeDays: 30,
Expand Down Expand Up @@ -51,7 +51,7 @@ export const admin = new AdminForth({
{
label: 'Users',
icon: 'flowbite:user-solid',
resourceId: 'users'
resourceId: 'adminuser'
}
],
});
Expand All @@ -68,8 +68,8 @@ if (import.meta.url === `file://${process.argv[1]}`) {
admin.express.serve(app)

admin.discoverDatabases().then(async () => {
if (!await admin.resource('users').get([Filters.EQ('email', 'adminforth')])) {
await admin.resource('users').create({
if (!await admin.resource('adminuser').get([Filters.EQ('email', 'adminforth')])) {
await admin.resource('adminuser').create({
email: 'adminforth',
password_hash: await AdminForth.Utils.generatePasswordHash('adminforth'),
role: 'superadmin',
Expand Down
4 changes: 2 additions & 2 deletions adminforth/commands/createApp/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ async function writeTemplateFiles(dirname, cwd, options) {
data: { dbUrl, prismaDbUrl },
},
{
src: 'users.ts.hbs',
dest: 'resources/users.ts',
src: 'adminuser.ts.hbs',
dest: 'resources/adminuser.ts',
data: {},
},
{
Expand Down
18 changes: 9 additions & 9 deletions adminforth/documentation/blog/2024-10-01-ai-blog/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ declare var process : {
export const admin = new AdminForth({
baseUrl: '/admin',
auth: {
usersResourceId: 'user', // resource to get user during login
usersResourceId: 'adminuser', // resource to get user during login
usernameField: 'email', // field where username is stored, should exist in resource
passwordHashField: 'passwordHash',
},
Expand Down Expand Up @@ -249,7 +249,7 @@ export const admin = new AdminForth({
{
label: 'Users',
icon: 'flowbite:user-solid',
resourceId: 'user',
resourceId: 'adminuser',
}
],
});
Expand Down Expand Up @@ -278,7 +278,7 @@ if (import.meta.url === `file://${process.argv[1]}`) {
Sorts.DESC('createdAt'),
);
const authorIds = [...new Set(posts.map((p: any) => p.authorId))];
const authors = (await admin.resource('user').list(Filters.IN('id', authorIds)))
const authors = (await admin.resource('adminuser').list(Filters.IN('id', authorIds)))
.reduce((acc: any, a: any) => {acc[a.id] = a; return acc;}, {});
posts.forEach((p: any) => {
const author = authors[p.authorId];
Expand Down Expand Up @@ -309,8 +309,8 @@ if (import.meta.url === `file://${process.argv[1]}`) {
admin.express.serve(app)

admin.discoverDatabases().then(async () => {
if (!await admin.resource('user').get([Filters.EQ('email', 'adminforth@adminforth.dev')])) {
await admin.resource('user').create({
if (!await admin.resource('adminuser').get([Filters.EQ('email', 'adminforth@adminforth.dev')])) {
await admin.resource('adminuser').create({
email: 'adminforth@adminforth.dev',
passwordHash: await AdminForth.Utils.generatePasswordHash('adminforth'),
});
Expand All @@ -325,16 +325,16 @@ if (import.meta.url === `file://${process.argv[1]}`) {

## Step 5: Create resources

Create `res` folder. Create `./res/user.ts` file with following content:
Create `res` folder. Create `./res/adminuser.ts` file with following content:

```ts title="./res/users.ts"
```ts title="./res/adminuser.ts"
import AdminForth, { AdminForthDataTypes } from 'adminforth';
import { randomUUID } from 'crypto';
import UploadPlugin from '@adminforth/upload';

export default {
dataSource: 'maindb',
table: 'user',
table: 'adminuser',
label: 'Users',
recordLabel: (r: any) => `👤 ${r.email}`,
columns: [
Expand Down Expand Up @@ -501,7 +501,7 @@ export default {
{
name: 'authorId',
foreignResource: {
resourceId: 'user',
resourceId: 'adminuser',
},
showIn: {
list: false,
Expand Down
10 changes: 5 additions & 5 deletions adminforth/documentation/docs/tutorial/001-gettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ myadmin/
│ ├── package.json # For any custom npm packages you will use in Vue files
│ └── tsconfig.json # Tsconfig for Vue project (adds completion for AdminForth core components)
├── resources
│ └── users.ts # Example resource file for users management
│ └── adminuser.ts # Example resource file for users management
├── schema.prisma # Prisma schema file for database schema
├── index.ts # Main entry point: configures AdminForth & starts the server
├── package.json # Project dependencies
Expand Down Expand Up @@ -128,7 +128,7 @@ Also in AdminForth you can define in "Vue" way:

## Adding an `apartments` Model

So far, our freshly generated AdminForth project includes a default `adminuser` model and a corresponding `users` resource.
So far, our freshly generated AdminForth project includes a default `adminuser` model and a corresponding `adminuser` resource.

Let’s expand our app to suport managment of **`apartments`** model. Adding new resource will involve next steps:

Expand Down Expand Up @@ -294,7 +294,7 @@ export default {
{
name: 'realtor_id',
foreignResource: {
resourceId: 'users',
resourceId: 'adminuser',
}
}
],
Expand Down Expand Up @@ -417,8 +417,8 @@ if (import.meta.url === `file://${process.argv[1]}`) {
...

admin.discoverDatabases().then(async () => {
if (!await admin.resource('users').get([Filters.EQ('email', 'adminforth')])) {
await admin.resource('users').create({
if (!await admin.resource('adminuser').get([Filters.EQ('email', 'adminforth')])) {
await admin.resource('adminuser').create({
email: 'adminforth',
password_hash: await AdminForth.Utils.generatePasswordHash('adminforth'),
role: 'superadmin',
Expand Down
14 changes: 7 additions & 7 deletions adminforth/documentation/docs/tutorial/01-helloWorld.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ import AdminForth, { AdminForthDataTypes, AdminUser, Filters } from 'adminforth'
export const admin = new AdminForth({
baseUrl: '',
auth: {
usersResourceId: 'users', // resource to get user during login
usersResourceId: 'adminuser', // resource to get user during login
usernameField: 'email', // field where username is stored, should exist in resource
passwordHashField: 'passwordHash',
},
Expand All @@ -143,8 +143,8 @@ export const admin = new AdminForth({
resources: [
{
dataSource: 'maindb',
table: 'user',
resourceId: 'users',
table: 'adminuser',
resourceId: 'adminuser',
label: 'Users',
recordLabel: (r: any) => `👤 ${r.email}`,
columns: [
Expand Down Expand Up @@ -242,7 +242,7 @@ export const admin = new AdminForth({
{
name: 'authorId',
foreignResource: {
resourceId: 'users',
resourceId: 'adminuser',
},
showIn: {
edit: false,
Expand Down Expand Up @@ -275,7 +275,7 @@ export const admin = new AdminForth({
{
label: 'Users',
icon: 'flowbite:user-solid',
resourceId: 'users',
resourceId: 'adminuser',
}
],
});
Expand All @@ -296,8 +296,8 @@ if (import.meta.url === `file://${process.argv[1]}`) {
admin.express.serve(app)

admin.discoverDatabases().then(async () => {
if (!await admin.resource('users').get([Filters.EQ('email', 'adminforth')])) {
await admin.resource('users').create({
if (!await admin.resource('adminuser').get([Filters.EQ('email', 'adminforth')])) {
await admin.resource('adminuser').create({
email: 'adminforth',
passwordHash: await AdminForth.Utils.generatePasswordHash('adminforth'),
role: 'superadmin',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ columns: [

## Virtual columns for editing.

Another usecase of `virtual` columns is to add new fields in edit and create view. In the [Getting started](/docs/tutorial/001-gettingStarted.md) we used this feature to add `password` field to the `users` resource.
Another usecase of `virtual` columns is to add new fields in edit and create view. In the [Getting started](/docs/tutorial/001-gettingStarted.md) we used this feature to add `password` field to the `adminuser` resource.
Thing is that password itself can't be stored in the database, but instead their hash is stored.
So we need to add `password` field to the `users` resource and make it `virtual` so it will not be stored in the database.
So we need to add `password` field to the `adminuser` resource and make it `virtual` so it will not be stored in the database.

```ts title="./resources/users.ts"
```ts title="./resources/adminuser.ts"
...
resourceId: 'users',
resourceId: 'adminuser',
...
columns: [
...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ You can use `options.allowedActions` on resource to limit access to the resource

If you want to disable deletion of the resource records for all users:

```ts title="./resources/users.ts"
```ts title="./resources/adminuser.ts"
{
...
resourceId: 'users',
resourceId: 'adminuser',
...
//diff-add
options: {
Expand Down Expand Up @@ -69,7 +69,7 @@ import type { AdminUser } from 'adminforth';

Let's disable creating and editing of new users for all users apart from users with role `superadmin`, and at the same time disable deletion for all users:

```ts title="./resources/users.ts"
```ts title="./resources/adminuser.ts"
//diff-add
import type { AdminUser } from 'adminforth';

Expand All @@ -82,7 +82,7 @@ async function canModifyUsers({ adminUser }: { adminUser: AdminUser }): Promise<

{
...
resourceId: 'users',
resourceId: 'adminuser',
...
options: {
allowedActions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ E.g. create group "Blog" with Items who link to resource "posts" and "categories
{
label: 'Users',
icon: 'flowbite:folder-duplicate-outline',
resourceId: 'users',
resourceId: 'adminuser',
},
},
...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This allows you to make basic operations on the data with AdminForth without usi

## Usage

Basically you just import `Filters`, `Sorts` from the `adminforth` package and call the awaitable methods on the `admin.resource('users')`.
Basically you just import `Filters`, `Sorts` from the `adminforth` package and call the awaitable methods on the `admin.resource('adminuser')`.

```ts
import { Filters, Sorts } from 'adminforth';
Expand All @@ -25,7 +25,7 @@ const admin = new AdminForth({
});

// get the resource object
await admin.resource('users').get([Filters.EQ('id', '1234')]);
await admin.resource('adminuser').get([Filters.EQ('id', '1234')]);
```

Here we will show you how to use the Data API with simple examples.
Expand All @@ -44,7 +44,7 @@ Signature:
Get item by ID:

```ts
const user = await admin.resource('users').get(
const user = await admin.resource('adminuser').get(
[Filters.EQ('id', '1234')]
);
```
Expand All @@ -61,7 +61,7 @@ const schoolExists = !!(await admin.resource('schools').get(
Get user with name 'John' and role not 'SuperAdmin'

```ts
const user = await admin.resource('users').get(
const user = await admin.resource('adminuser').get(
[Filters.EQ('name', 'John'), Filters.NEQ('role', 'SuperAdmin')]
);
```
Expand All @@ -83,21 +83,21 @@ Signature:
Get 15 latest users which role is not Admin:

```ts
const users = await admin.resource('users').list(
const users = await admin.resource('adminuser').list(
[Filters.NEQ('role', 'Admin')], 15, 0, Sorts.DESC('createdAt')
);
```

Get 10 oldest users (with highest age):

```ts
const users = await admin.resource('users').list([], 10, 0, Sorts.ASC('age'));
const users = await admin.resource('adminuser').list([], 10, 0, Sorts.ASC('age'));
```

Get next page of oldest users:

```ts
const users = await admin.resource('users').list([], 10, 10, Sorts.ASC('age'));
const users = await admin.resource('adminuser').list([], 10, 10, Sorts.ASC('age'));
```

Get 10 schools, sort by rating first, then oldest by founded year:
Expand Down Expand Up @@ -162,7 +162,7 @@ const dailyReports = await Promise.all(
const dateEnd = new Date(dateStart);
dateEnd.setDate(dateEnd.getDate() + 1);

return admin.resource('users').count(
return admin.resource('adminuser').count(
[Filters.GTE('createdAt', dateStart.toISOString()), Filters.LT('createdAt', dateEnd.toISOString())]
);
})
Expand Down Expand Up @@ -217,10 +217,10 @@ Golden rule: create one index per query you are going to use often or where you
For example if you have two queries:

```ts
const users = await admin.resource('users').list(
const users = await admin.resource('adminuser').list(
[Filters.NEQ('role', 'Admin')], 15, 0, Sorts.DESC('createdAt')
);
const users = await admin.resource('users').list(
const users = await admin.resource('adminuser').list(
[Filters.EQ('name', 'John'), Filters.NEQ('role', 'SuperAdmin')]
);
```
Expand Down
Loading