Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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);
}
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?`
);
}
}
};
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?`
);
}
}
};
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?`
);
}
}
};
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);
}
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);
}
};
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);
}
};
// 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;
};
};
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]
});
}
});
}
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]);
}