Skip to content
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

feat: support for mongoose v8 #1241

Merged
merged 11 commits into from
Jan 22, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: revert unecessary changes
Dogan AY committed Jan 16, 2025
commit 319208bfe1420f06c8b9421c32648cfd2bddcfd6
30 changes: 29 additions & 1 deletion packages/datasource-mongoose/src/utils/schema/fields.ts
Original file line number Diff line number Diff line change
@@ -99,7 +99,7 @@ export default class FieldsGenerator {

/** Compute column type from CleanSchema */
private static getColumnType(field: SchemaNode): ColumnType {
const columnType = VersionManager.getColumnTypeRec(field as any);
const columnType = this.getColumnTypeRec(field as any);

// Enum fields are promoted to enum instead of string _only_ if they are at the root of the
// record.
@@ -114,6 +114,34 @@ export default class FieldsGenerator {
return columnType;
}

/** Build ColumnType from CleanSchema recursively */
private static getColumnTypeRec(field: SchemaNode): ColumnType {
if (field instanceof SchemaType) {
if (field.instance === 'Buffer') {
return 'Binary';
}

if (['String', 'Number', 'Date', 'Boolean'].includes(field.instance)) {
return field.instance as PrimitiveTypes;
}

if ([VersionManager.ObjectIdTypeName, 'Decimal128'].includes(field.instance)) {
return 'String';
}

return 'Json';
}

if (field['[]']) {
return [this.getColumnTypeRec(field['[]'])];
}

return Object.entries(field).reduce(
(memo, [name, subSchema]) => ({ ...memo, [name]: this.getColumnTypeRec(subSchema) }),
{},
);
}

/** Get enum validator from field definition */
private static getEnumValues(field: SchemaType): string[] {
return field.options?.enum instanceof Array ? field.options.enum : field.options?.enum?.values;
42 changes: 0 additions & 42 deletions packages/datasource-mongoose/src/utils/version-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import type { ColumnType, PrimitiveTypes } from '@forestadmin/datasource-toolkit';

import mongoose, { Schema, SchemaType } from 'mongoose';

const dynamicSchema = new mongoose.Schema({ objectId: mongoose.Schema.Types.ObjectId });
@@ -22,43 +19,4 @@ export default class VersionManager {
field instanceof SchemaType
);
}

private static getBasicType(type: string) {
if (type === 'Buffer') {
return 'Binary';
}

if (['String', 'Number', 'Date', 'Boolean'].includes(type)) {
return type as PrimitiveTypes;
}

if ([VersionManager.ObjectIdTypeName, 'Decimal128'].includes(type)) {
return 'String';
}

return 'Json';
}

/** Build ColumnType from CleanSchema recursively */
public static getColumnTypeRec<T = unknown>(field: SchemaType<T>): ColumnType {
if (mongoose.version.startsWith('8') && (field as any).paths) {
return this.getColumnTypeRec((field as any).paths);
}

if (field instanceof SchemaType) {
return this.getBasicType(field.instance);
}

if (field['[]']) {
return [VersionManager.getColumnTypeRec(field['[]'])];
}

return Object.entries(field).reduce(
(memo, [name, subSchema]) => ({
...memo,
[name]: VersionManager.getColumnTypeRec(subSchema as SchemaType),
}),
{},
);
}
}
Loading