Skip to content

Commit

Permalink
feat: azure cosmos compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
jmikrut committed Nov 24, 2021
1 parent 91fae55 commit 6fd5ac2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/collections/operations/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,28 @@ async function find(incomingArgs: Arguments): Promise<PaginatedDocs> {
// Find
// /////////////////////////////////////

let { sort } = args;
let sortParam: Record<string, string>;

if (!sort) {
if (!args.sort) {
if (collectionConfig.timestamps) {
sort = '-createdAt';
sortParam = { createdAt: 'desc' };
} else {
sort = '-_id';
sortParam = { _id: 'desc' };
}
} else if (sort === 'id' || sort === '-id') {
sort = sort.replace('id', '_id');
} else if (args.sort.indexOf('-') === 0) {
sortParam = {
[args.sort.substring(1)]: 'desc',
};
} else {
sortParam = {
[args.sort]: 'asc',
};
}

const optionsToExecute = {
page: page || 1,
limit: limit || 10,
sort,
sort: sortParam,
lean: true,
leanWithId: true,
useEstimatedCount,
Expand Down
5 changes: 5 additions & 0 deletions src/mongoose/buildSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ const buildSchema = (config: SanitizedConfig, configFields: Field[], buildSchema
}
});

if (buildSchemaOptions?.options?.timestamps) {
indexFields.push({ createdAt: 1 });
indexFields.push({ updatedAt: 1 });
}

const schema = new Schema(fields, options);
indexFields.forEach((index) => {
schema.index(index);
Expand Down
2 changes: 1 addition & 1 deletion src/mongoose/sanitizeFormattedValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const sanitizeQueryValue = (schemaType: SchemaType, path: string, operato
}

if (operator === 'like' && path !== '_id') {
formattedValue = { $regex: formattedValue, $options: '-i' };
formattedValue = { $regex: formattedValue, $options: 'i' };
}

if (operator === 'exists') {
Expand Down

0 comments on commit 6fd5ac2

Please sign in to comment.