yortus/DBFFile

Add support to edit a record

Closed this issue · 2 comments

Hi @yortus , I have an use case in which I need to edit a record, do you think that this could be useful for the library?

In a positive case, I'm thinking that maybe we could return an array of some sort of records objects that could have their own offset. This way we can edit an object and then just call .update() over it.
f.i.

const dbf = DBFHandlerFile('dbf-path'); // please note that this is a different class
const records = dbf.getAll(); // this cold return an array of DBFRecordHandler

console.log(records[0]); // prints { field1: 10, field2: 'text-field' }
records[0].update('field1', 20); // this could throw an exception if the field doesn't exist
records[0].save(); // save the record

What do you think about? (sorry if this looks like a lot of work)

Hey @lordrip, yes its seems useful to be able to update records in an existing dbf file.

I'd prefer an API that keeps records as plain objects though - in the example above every record has methods and hence must derive from a base class as well as keep track of its attached file object. It's getting a bit enterprise ORM-ish to me!

The approach I'd prefer would be something like the following:

  • keep records as plain objects
  • no new classes
  • define a new symbol once in the library for tracking record indices - eg const INDEX = Symbol('record index')
  • modify readRecords to add a non-enumerable INDEX property to each record object it generates, whose value is the physical record index in the file the record was read from. This should keep the changes non-breaking.
  • add a new method to the DBFFile class - something like updateRecord(record: object): Promise<DBFFile> which requires the INDEX property to be present so it knows where in the file to overwrite the record.

To use it, you'd use DBFFile.open(...) and .readRecords(...) to first fetch your records, then call .updateRecord(...) to modify individual records in the existing file.

Would that solve your use case?

Yup, that would fit the use case as well. BTW, really cool approach that you suggest 👍, as soon as I get free from pending stuff I'll try to take a look to the implementation. Thanks mate.