PHP 8.2.30
Preview: Resource.js Size: 7.07 KB
/proc/thread-self/root/home/byroehnu/.trash/node_modules11/@adminjs/prisma/lib/Resource.js

/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
/* eslint-disable class-methods-use-this */
/* eslint-disable no-param-reassign */
import { BaseResource, BaseRecord, flat } from 'adminjs';
import { Property } from './Property.js';
import { lowerCase } from './utils/helpers.js';
import { convertFilter, convertParam } from './utils/converters.js';
import { getEnums } from './utils/get-enums.js';
export class Resource extends BaseResource {
    client;
    model;
    enums;
    manager;
    propertiesObject;
    idProperty;
    constructor(args) {
        super(args);
        const { model, client, clientModule } = args;
        this.model = model;
        this.client = client;
        this.enums = getEnums(clientModule);
        this.manager = this.client[lowerCase(model.name)];
        this.propertiesObject = this.prepareProperties();
        this.idProperty = this.properties().find((p) => p.isId());
    }
    databaseName() {
        return 'prisma';
    }
    databaseType() {
        return this.client._engineConfig?.activeProvider ?? 'database';
    }
    id() {
        return this.model.name;
    }
    properties() {
        return [...Object.values(this.propertiesObject)];
    }
    property(path) {
        return this.propertiesObject[path] ?? null;
    }
    build(params) {
        return new BaseRecord(flat.unflatten(params), this);
    }
    async count(filter) {
        return this.manager.count({
            where: convertFilter(this.model.fields, filter),
        });
    }
    async find(filter, params = {}) {
        const { limit = 10, offset = 0, sort = {} } = params;
        const orderBy = this.buildSortBy(sort);
        const results = await this.manager.findMany({
            where: convertFilter(this.model.fields, filter),
            skip: offset,
            take: limit,
            orderBy,
        });
        return results.map((result) => new BaseRecord(this.prepareReturnValues(result), this));
    }
    buildSortBy(sort = {}) {
        let { sortBy: path } = sort;
        const { direction = 'desc' } = sort;
        if (!path)
            path = this.idProperty.path();
        const [basePath, sortBy] = path.split('.');
        const sortByProperty = this.property(basePath);
        if (sortByProperty?.column.relationName
            && sortByProperty?.column.kind === 'object'
            && sortByProperty.column.relationToFields?.length) {
            return {
                [basePath]: {
                    [sortBy ?? sortByProperty.column.relationToFields[0]]: direction,
                },
            };
        }
        return {
            [basePath]: direction,
        };
    }
    async findOne(id) {
        const idProperty = this.properties().find((property) => property.isId());
        if (!idProperty)
            return null;
        const result = await this.manager.findUnique({
            where: {
                [idProperty.path()]: convertParam(idProperty, this.model.fields, id),
            },
        });
        if (!result)
            return null;
        return new BaseRecord(this.prepareReturnValues(result), this);
    }
    async findMany(ids) {
        const idProperty = this.properties().find((property) => property.isId());
        if (!idProperty)
            return [];
        const results = await this.manager.findMany({
            where: {
                [idProperty.path()]: {
                    in: ids.map((id) => convertParam(idProperty, this.model.fields, id)),
                },
            },
        });
        return results.map((result) => new BaseRecord(this.prepareReturnValues(result), this));
    }
    async create(params) {
        const preparedParams = this.prepareParams(params);
        const result = await this.manager.create({ data: preparedParams });
        return this.prepareReturnValues(result);
    }
    async update(pk, params = {}) {
        const idProperty = this.properties().find((property) => property.isId());
        if (!idProperty)
            return {};
        const preparedParams = this.prepareParams(params);
        const result = await this.manager.update({
            where: {
                [idProperty.path()]: convertParam(idProperty, this.model.fields, pk),
            },
            data: preparedParams,
        });
        return this.prepareReturnValues(result);
    }
    async delete(id) {
        const idProperty = this.properties().find((property) => property.isId());
        if (!idProperty)
            return;
        await this.manager.delete({
            where: {
                [idProperty.path()]: convertParam(idProperty, this.model.fields, id),
            },
        });
    }
    static isAdapterFor(args) {
        const { model, client } = args;
        return (!!model?.name
            && !!model?.fields.length
            && !!client?.[lowerCase(model.name)]);
    }
    prepareProperties() {
        const { fields = [] } = this.model;
        const properties = fields.reduce((memo, field) => {
            if (field.isReadOnly
                || (field.relationName && !field.relationFromFields?.length)) {
                return memo;
            }
            const property = new Property(field, Object.keys(memo).length, this.enums);
            memo[property.path()] = property;
            return memo;
        }, {});
        return properties;
    }
    prepareParams(params) {
        const preparedParams = {};
        for (const property of this.properties()) {
            const param = flat.get(params, property.path());
            const key = property.path();
            // eslint-disable-next-line no-continue
            if (param === undefined)
                continue;
            const type = property.type();
            const foreignColumnName = property.foreignColumnName();
            if (type === 'reference' && foreignColumnName) {
                preparedParams[foreignColumnName] = convertParam(property, this.model.fields, param);
                // eslint-disable-next-line no-continue
                continue;
            }
            if (property.isArray()) {
                preparedParams[key] = param
                    ? param.map((p) => convertParam(property, this.model.fields, p))
                    : param;
            }
            else {
                preparedParams[key] = convertParam(property, this.model.fields, param);
            }
        }
        return preparedParams;
    }
    prepareReturnValues(params) {
        const preparedValues = {};
        for (const property of this.properties()) {
            const param = flat.get(params, property.path());
            const key = property.path();
            if (param !== undefined && property.type() !== 'reference') {
                preparedValues[key] = param;
                // eslint-disable-next-line no-continue
                continue;
            }
            const foreignColumnName = property.foreignColumnName();
            // eslint-disable-next-line no-continue
            if (!foreignColumnName)
                continue;
            preparedValues[key] = params[foreignColumnName];
        }
        return preparedValues;
    }
}
//# sourceMappingURL=Resource.js.map

Directory Contents

Dirs: 1 × Files: 15

Name Size Perms Modified Actions
utils DIR
- drwxr-xr-x 2026-02-28 00:28:58
Edit Download
486 B lrw-r--r-- 2026-02-28 00:28:54
Edit Download
1.01 KB lrw-r--r-- 2026-02-28 00:27:46
Edit Download
1.07 KB lrw-r--r-- 2026-02-28 00:28:38
Edit Download
2.97 KB lrw-r--r-- 2026-02-28 00:28:58
Edit Download
2.87 KB lrw-r--r-- 2026-02-28 00:28:08
Edit Download
569 B lrw-r--r-- 2026-02-28 00:28:42
Edit Download
735 B lrw-r--r-- 2026-02-28 00:28:58
Edit Download
2.07 KB lrw-r--r-- 2026-02-28 00:28:14
Edit Download
2.19 KB lrw-r--r-- 2026-02-28 00:28:44
Edit Download
2.01 KB lrw-r--r-- 2026-02-28 00:29:00
Edit Download
7.07 KB lrw-r--r-- 2026-02-28 00:28:18
Edit Download
7.14 KB lrw-r--r-- 2026-02-28 00:28:44
Edit Download
222 B lrw-r--r-- 2026-02-28 00:29:00
Edit Download
44 B lrw-r--r-- 2026-02-28 00:28:22
Edit Download
102 B lrw-r--r-- 2026-02-28 00:28:46
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).