Skip to content

fix: fix filter by autogenerated _id for mongodb #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions adminforth/dataConnectors/baseConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,14 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
limit: 1,
offset: 0,
sort: [],
filters: { operator: AdminForthFilterOperators.AND, subFilters: [{ field: this.getPrimaryKey(resource), operator: AdminForthFilterOperators.EQ, value: id }]},
filters: {
operator: AdminForthFilterOperators.AND,
subFilters: [{
field: this.getPrimaryKey(resource),
operator: AdminForthFilterOperators.EQ,
value: this.setFieldValue(resource.dataSourceColumns.find((col) => col.name === this.getPrimaryKey(resource)), id),
}],
},
});
return data.length > 0 ? data[0] : null;
}
Expand Down Expand Up @@ -191,7 +198,14 @@ export default class AdminForthBaseConnector implements IAdminForthDataSourceCon
process.env.HEAVY_DEBUG && console.log('☝️🪲🪲🪲🪲 checkUnique|||', column, value);
const existingRecord = await this.getData({
resource,
filters: { operator: AdminForthFilterOperators.AND, subFilters: [{ field: column.name, operator: AdminForthFilterOperators.EQ, value }]},
filters: {
operator: AdminForthFilterOperators.AND,
subFilters: [{
field: column.name,
operator: AdminForthFilterOperators.EQ,
value: this.setFieldValue(column, value),
}],
},
limit: 1,
sort: [],
offset: 0,
Expand Down
37 changes: 25 additions & 12 deletions adminforth/dataConnectors/mongo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import dayjs from 'dayjs';
import { MongoClient } from 'mongodb';
import { Decimal128 } from 'bson';
import { Decimal128, ObjectId } from 'bson';
import { IAdminForthDataSourceConnector, IAdminForthSingleFilter, IAdminForthAndOrFilter, AdminForthResource } from '../types/Back.js';
import AdminForthBaseConnector from './baseConnector.js';

Expand Down Expand Up @@ -64,14 +64,6 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
}, {});
}

getPrimaryKey(resource) {
for (const col of resource.dataSourceColumns) {
if (col.primaryKey) {
return col.name;
}
}
}

getFieldValue(field, value) {
if (field.type == AdminForthDataTypes.DATETIME) {
if (!value) {
Expand All @@ -89,6 +81,11 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
return !!value;
} else if (field.type == AdminForthDataTypes.DECIMAL) {
return value?.toString();
} else if (field.name === '_id' && !field.fillOnCreate) {
Copy link
Contributor

@ivictbor ivictbor Apr 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Astaldos will it brake something if we will remove && !field.fillOnCreate? I mean for the case if user returns object id in fillOnCreate.

// value is supposed to be an ObjectId or string representing it
if (typeof value === 'object') {
return value?.toString();
}
}

return value;
Expand All @@ -111,6 +108,21 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS
return value ? true : false;
} else if (field.type == AdminForthDataTypes.DECIMAL) {
return Decimal128.fromString(value?.toString());
} else if (field.name === '_id' && !field.fillOnCreate) {
// value is supposed to be an ObjectId
if (!ObjectId.isValid(value)) {
return null;
}
if (typeof value === 'string' || typeof value === 'number') {
// if string or number - turn it into ObjectId
return new ObjectId(value);
} else if (typeof value === 'object') {
// assume it is a correct ObjectId
return value;
}

// unsupported type for ObjectId
return null;
}
return value;
}
Expand Down Expand Up @@ -205,13 +217,14 @@ class MongoConnector extends AdminForthBaseConnector implements IAdminForthDataS

async updateRecordOriginalValues({ resource, recordId, newValues }) {
const collection = this.client.db().collection(resource.table);
await collection.updateOne({ [this.getPrimaryKey(resource)]: recordId }, { $set: newValues });
const primaryKeyColumn = resource.dataSourceColumns.find((col) => col.name === this.getPrimaryKey(resource));
await collection.updateOne({ [primaryKeyColumn.name]: this.setFieldValue(primaryKeyColumn, recordId) }, { $set: newValues });
}

async deleteRecord({ resource, recordId }): Promise<boolean> {
const primaryKey = this.getPrimaryKey(resource);
const collection = this.client.db().collection(resource.table);
const res = await collection.deleteOne({ [primaryKey]: recordId });
const primaryKeyColumn = resource.dataSourceColumns.find((col) => col.name === this.getPrimaryKey(resource));
const res = await collection.deleteOne({ [primaryKeyColumn.name]: this.setFieldValue(primaryKeyColumn, recordId) });
return res.deletedCount > 0;
}

Expand Down