How to use the webiny-entity.QueryResult function in webiny-entity

To help you get started, we’ve selected a few webiny-entity examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github webiny / webiny-js / packages / webiny-entity-memory / src / memoryDriver.js View on Github external
async save(entity: Entity, params: EntitySaveParams & {}): Promise {
        // Check if table exists.
        if (!this.data[entity.classId]) {
            this.data[entity.classId] = [];
        }

        if (entity.isExisting()) {
            const storedItemIndex = _.findIndex(this.data[entity.classId], { id: entity.id });
            this.data[entity.classId][storedItemIndex] = await entity.toStorage();
            return new QueryResult(true);
        }

        entity.id = mdbid();
        this.data[entity.classId].push(await entity.toStorage());
        return new QueryResult(true);
    }
github webiny / webiny-js / packages / webiny-entity-mongodb / src / MongoDbDriver.js View on Github external
async findOne(entity, options) {
        const clonedOptions = clone(options);
        MongoDbDriver.__preparePerPageOption(clonedOptions);
        MongoDbDriver.__preparePageOption(clonedOptions);
        MongoDbDriver.__prepareSearchOption(clonedOptions);

        const results = await this.getDatabase()
            .collection(this.getCollectionName(entity))
            .find(clonedOptions.query)
            .limit(1)
            .sort(clonedOptions.sort)
            .toArray();

        return new QueryResult(results[0]);
    }
github webiny / webiny-js / packages / webiny-entity-mysql / src / mysqlDriver.js View on Github external
MySQLDriver.__preparePageOption(clonedOptions);
        MySQLDriver.__prepareQueryOption(clonedOptions);
        MySQLDriver.__prepareSearchOption(clonedOptions);

        clonedOptions.calculateFoundRows = true;

        const sql = new Select(clonedOptions, entity).generate();
        const results = await this.getConnection().query([sql, "SELECT FOUND_ROWS() as count"]);

        const meta = createPaginationMeta({
            totalCount: results[1][0].count,
            page: options.page,
            perPage: options.perPage
        });

        return new QueryResult(results[0], meta);
    }
github webiny / webiny-js / packages / webiny-entity-mysql / src / mysqlDriver.js View on Github external
async save(entity: Entity, options: EntitySaveParams & {}): Promise {
        if (entity.isExisting()) {
            const data = await entity.toStorage();
            if (_.isEmpty(data)) {
                return new QueryResult(true);
            }

            const sql = new Update(
                {
                    operators: this.operators,
                    table: this.getTableName(entity),
                    data,
                    where: { id: entity.id },
                    limit: 1
                },
                entity
            ).generate();

            await this.getConnection().query(sql);
            return new QueryResult(true);
        }
github webiny / webiny-js / packages-utils / webiny-entity-memory / src / memoryDriver.js View on Github external
const collection = [];

        this.data[entity.classId].forEach(record => {
            for (const [key, value] of Object.entries(query)) {
                if (value instanceof Array) {
                    if (!value.includes(record[key])) {
                        return true;
                    }
                } else if (record[key] !== value) {
                    return true;
                }
            }
            collection.push(record);
        });

        return new QueryResult(collection, { count: collection.length });
    }
github webiny / webiny-js / packages / webiny-entity-memory / src / memoryDriver.js View on Github external
async find(
        entity: Entity | Class,
        params: EntityFindParams & Object
    ): Promise {
        const records = this.data[entity.classId];
        if (!records) {
            return new QueryResult([]);
        }

        const query = _.get(params, "query", {});
        if (_.isEmpty(query)) {
            return new QueryResult(this.data[entity.classId]);
        }

        const collection = [];

        this.data[entity.classId].forEach(record => {
            for (const [key, value] of Object.entries(query)) {
                if (value instanceof Array) {
                    if (!value.includes(record[key])) {
                        return true;
                    }
                } else if (record[key] !== value) {
github webiny / webiny-js / packages-utils / webiny-entity-memory / src / memoryDriver.js View on Github external
async findOne(entity: Entity, params: EntityFindOneParams & {}): Promise {
        return new QueryResult(_.find(this.data[entity.classId], params.query));
    }
github webiny / webiny-js / packages / webiny-entity-memory / src / memoryDriver.js View on Github external
async delete(entity: Entity, params: EntityDeleteParams & {}): Promise {
        if (!this.data[entity.classId]) {
            return new QueryResult(true);
        }

        const index = _.findIndex(this.data[entity.classId], { id: entity.id });
        if (index > -1) {
            this.data[entity.classId].splice(index, 1);
        }
        return new QueryResult(true);
    }
github webiny / webiny-js / packages / webiny-entity-mongodb / src / MongoDbDriver.js View on Github external
async count(entity, options) {
        const clonedOptions = clone(options);
        MongoDbDriver.__prepareSearchOption(clonedOptions);

        return new QueryResult(
            await this.getDatabase()
                .collection(this.getCollectionName(entity))
                .countDocuments(clonedOptions.query)
        );
    }
github webiny / webiny-js / packages-utils / webiny-entity-mysql / src / mysqlDriver.js View on Github external
async delete(entity: Entity, options: EntityDeleteParams & {}): Promise {
        const id = await entity.getAttribute("id").getStorageValue();
        const sql = new Delete(
            {
                operators: this.operators,
                table: this.getTableName(entity),
                where: { id },
                limit: 1
            },
            entity
        ).generate();

        await this.getConnection().query(sql);
        return new QueryResult(true);
    }