How to use webiny-entity - 10 common examples

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-mongodb / src / MongoDbDriver.js View on Github external
MongoDbDriver.__prepareSearchOption(clonedOptions);

        // Get first documents from cursor using each
        const results = await this.getDatabase()
            .collection(this.getCollectionName(entity))
            .find(clonedOptions.query)
            .limit(clonedOptions.limit)
            .skip(clonedOptions.offset)
            .sort(clonedOptions.sort)
            .toArray();

        const totalCount = await this.getDatabase()
            .collection(this.getCollectionName(entity))
            .countDocuments(clonedOptions.query);

        const meta = createPaginationMeta({
            totalCount,
            page: options.page,
            perPage: options.perPage
        });

        return new QueryResult(results, meta);
    }
github webiny / webiny-js / packages / webiny-api / src / lambda / lambda.js View on Github external
const requestSetup = async (config: Object = {}) => {
    // Configure Entity layer
    if (config.entity) {
        Entity.driver = config.entity.driver;
        Entity.crud = config.entity.crud;
    }

    // Check if connection is valid and if Settings table exists - this will tell us if the system is installed.
    if (process.env.NODE_ENV === "development") {
        try {
            await Entity.getDriver().test();
        } catch (e) {
            throw Error(
                `The following error occurred while initializing Entity driver: "${
                    e.message
                }". Did you enter the correct database information?`
            );
        }
    }
};
github webiny / webiny-js / packages / webiny-api / src / lambda / lambda.js View on Github external
const requestSetup = async (config: Object = {}) => {
    // Configure Entity layer
    if (config.entity) {
        Entity.driver = config.entity.driver;
        Entity.crud = config.entity.crud;
    }

    // Check if connection is valid and if Settings table exists - this will tell us if the system is installed.
    if (process.env.NODE_ENV === "development") {
        try {
            await Entity.getDriver().test();
        } catch (e) {
            throw Error(
                `The following error occurred while initializing Entity driver: "${
                    e.message
                }". Did you enter the correct database information?`
            );
        }
    }
};
github webiny / webiny-js / packages / webiny-api / src / lambda / lambda.js View on Github external
const requestSetup = async (config: Object = {}) => {
    // Configure Entity layer
    if (config.entity) {
        Entity.driver = config.entity.driver;
        Entity.crud = config.entity.crud;
    }

    // Check if connection is valid and if Settings table exists - this will tell us if the system is installed.
    if (process.env.NODE_ENV === "development") {
        try {
            await Entity.getDriver().test();
        } catch (e) {
            throw Error(
                `The following error occurred while initializing Entity driver: "${
                    e.message
                }". Did you enter the correct database information?`
            );
        }
    }
};
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-api-security / src / install / plugins / index.js View on Github external
install: async context => {
        const { config } = context;

        // Configure Entity layer
        if (config.entity) {
            Entity.driver = config.entity.driver;
            Entity.crud = config.entity.crud;
        }

        await importData(context);
    }
};
github webiny / webiny-js / packages / webiny-api-security / src / install / plugins / index.js View on Github external
install: async context => {
        const { config } = context;

        // Configure Entity layer
        if (config.entity) {
            Entity.driver = config.entity.driver;
            Entity.crud = config.entity.crud;
        }

        await importData(context);
    }
};
github webiny / webiny-js / packages / webiny-api-security / src / entities / helpers / onSetFactory.js View on Github external
// If not DB ids - load entities by slugs
                if (!EntityClass.isId(value)) {
                    if (typeof value === "string") {
                        query = { slug: value };
                    } else if (value.id) {
                        query = { id: value.id };
                    } else if (value.slug) {
                        query = { slug: value.slug };
                    }
                }

                // TODO: ne bi htio loadati entitet tu jer to je samo populate
                entities[i] = await EntityClass.findOne({ query });
            }

            return new EntityCollection(entities.filter(Boolean));
        }

        return entities;
    };
};
github webiny / webiny-js / packages / webiny-api / src / plugins / graphqlContextEntities.js View on Github external
getPlugins("entity").forEach((plugin: EntityPluginType) => {
            if (!context[plugin.namespace]) {
                context[plugin.namespace] = {
                    entities: {}
                };
            }

            if (typeof plugin.entity === "function") {
                const entityClass = plugin.entity(context);
                entityClass.pool = new EntityPool();
                registerEntityClass({ context, entityClass });
            } else {
                const { name, factory } = plugin.entity;
                context[plugin.namespace].entities[name] = factory(context);
                context[plugin.namespace].entities[name].pool = new EntityPool();

                // We add to entities list, later we'll just do the cleanup - this won't exist.
                registerEntityClass({
                    context,
                    entityClass: context[plugin.namespace].entities[name]
                });
            }
        });
    }
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]);
    }