Sven Deckers
Sven Deckers

Reputation: 301

When creating a feathers service with nedb, is it possible to define a unique index?

I made a feathers service using nedb with feathers generate service Let's say the service is called warehouses and I want to have a unique index on the name. The Nedb docs tell me I can create a unique index with db.ensureIndex, but I cannot seem to find WHERE in my feathers code I should do this. Can anyone point me in the right direction please?

Upvotes: 1

Views: 359

Answers (1)

Sven Deckers
Sven Deckers

Reputation: 301

Oh, so I think I just found it :-) I'll leave it here if anyone else encounters this.

Just open the model file under /src/models/<servicename>.model.js.

And then adjust like so:

const NeDB = require('nedb');
const path = require('path');

module.exports = function (app) {
  const dbPath = app.get('nedb');
  const Model = new NeDB({
    filename: path.join(dbPath, 'warehouses.db'),
    autoload: true
  });

  // here it is: create a unique index on the name
  Model.ensureIndex({ fieldName: 'name', unique: true })

  return Model;
};

Upvotes: 1

Related Questions