bwgjoseph / mongoose-vs-ottoman

feature comparison between mongoose and ottoman
0 stars 1 forks source link

support for schema.paths #58

Closed bwgjoseph closed 3 years ago

bwgjoseph commented 3 years ago

Hi,

Is there any way to grab the list of fields that is registered for a given schema?

const myschema = new Schema({ name: string, capacity: number });

// get a list of schemas keys
// output [name, capacity]
Object.keys(myschema.paths);
// myschema.paths actually give the schema definition

I saw on the docs that ottoman has path but not paths method.

AV25242 commented 3 years ago

I am not aware of something like this, but what would be the use case ?

bwgjoseph commented 3 years ago

I could say, write a plugin that only runs if a certain field exists.

if (Object.keys(schema.paths).includes('certainfield')) {
   // do some manipulation to the data
}

or just purely check if certain fields exists

function doesFieldExist(schema, fieldName) {
  return Object.keys(schema.paths).includes(fieldName);
}

I am currently using this checks to perform certain action across different schemas based on what fields exists or not exists.

AV25242 commented 3 years ago

Lino will explore the fields method on Schema

AV25242 commented 3 years ago

from Lino

function doesFieldExist(schema: Schema, fieldName: string): boolean {
  return !!schema.path(fieldName)
}
bwgjoseph commented 3 years ago

Thank you for the suggestion, it works as expected