bwgjoseph / mongoose-vs-ottoman

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

data not adhered to the schema should not be saved? #26

Closed bwgjoseph closed 3 years ago

bwgjoseph commented 3 years ago

Hi,

data get gets passed in but does not adhere to the schema should be auto dropped? In mongoose, there's a strict option that we are using to define, if I want it to be less strict.

Meaning save anything that is being passed on, as what a schema-less database should be, but because this is a "schema" driver, then data saved should adhered to the schema that is being provided until otherwise.

const childSchema = new ottoman.Schema({
            name: {
                type: String,
            }
        });
        const parentSchema = new ottoman.Schema({
            children: [childSchema],
        });

        const model = ottoman.model('Airplane', parentSchema);
        model.create({
            children: [
                {
                    name: 'a',
                },
                {
                    name: 'b'
                }
            ],
            child: {
                name: 'c'
            },
        })

        const options = { consistency: SearchConsistency.LOCAL }

        const find = await model.find({}, options);
        console.log('ottoman', JSON.stringify(find.rows, null, 2));

This is the output

// actual output
{
    "_scope": "_default",
    "_type": "Airplane",
    "child": {
      "name": "c"
    },
    "children": [
      {
        "name": "a"
      },
      {
        "name": "b"
      }
    ],
    "id": "f3f8cc49-38e0-42f9-aacd-98f166ecfc8b"
  }

// expected output
{
    "_scope": "_default",
    "_type": "Airplane",
    "children": [
      {
        "name": "a"
      },
      {
        "name": "b"
      }
    ],
    "id": "c4d813ad-6838-46fa-b42f-b5a6ec0969b4"
  }

Thanks

AV25242 commented 3 years ago

Hi @bwgjoseph as far as I remember there is a strict option in Ottoman as well. Let me check

AV25242 commented 3 years ago

@bwgjoseph so Ottoman has this schema option {validationStrategy: VALIDATION_STRATEGY.STRICT} but the purpose is slightly different.

It does not check for extra fields but validates what Schema has, for instance if you have a number but have specified a string, strict will fail even though string can be converted to a number.

I will open a ticket to address the one we were talking about

AV25242 commented 3 years ago

Ticket https://github.com/couchbaselabs/node-ottoman/issues/323 should address this

bwgjoseph commented 3 years ago

Hi,

Seem like there's some issue when using strict: false

const schema = new ottoman.Schema({
    name: String,
}, { strict: false });
const model = ottoman.model('nonstrictschema', schema);
const created = await model.create({ name: 'hello', notInSchema: true });
console.log('created', created);

const find = await model.find({}, { consistency: ottoman.SearchConsistency.LOCAL });
console.log('find', find);

This is returned from create model.create which has the notInSchema field

_Model {
  name: 'hello',
  notInSchema: true,
  id: 'bdb0a15a-9f20-43f7-b8cb-813309c6514e',
  _type: 'nonstrictschema'
}

But using find, it does not return notInSchema field

{
  meta: {
    requestId: '9326bddf-8315-45d8-8476-ef024528d7a0',
    clientContextId: 'a8bd4c4d4a799268',
    status: 'success',
    signature: { '*': '*' },
    profile: undefined,
    metrics: {
      elapsedTime: 15.5699,
      executionTime: 15.4941,
      sortCount: undefined,
      resultCount: 1,
      resultSize: 86,
      mutationCount: undefined,
      errorCount: undefined,
      warningCount: undefined
    }
  },
  rows: [
    _Model {
      _type: 'nonstrictschema',
      id: 'bdb0a15a-9f20-43f7-b8cb-813309c6514e',
      name: 'hello'
    }
  ]
}
AV25242 commented 3 years ago

Thanks @bwgjoseph have notified to the team they will look into it

AV25242 commented 3 years ago

@bwgjoseph this is a bug and will be fixed with next release

AV25242 commented 3 years ago

hello @bwgjoseph alpha.13 was released yesterday, can you please verify.

bwgjoseph commented 3 years ago

Please see #39, seem like there's some issue

AV25242 commented 3 years ago

issue #39 seems to be a configuration issue