Skip to content

Collection.prototype

pgte edited this page Dec 30, 2010 · 3 revisions

A Collection has a file it writes to.

A Collection is append-only.

Every element of a collection should be serializable to JSON.

collection.write (record, callback)

Appends a record into the collection.

  • record: the new element to be inserted
  • callback (error, pos, length) - called when writing finishes or an error occurs. Passes in the written file position and the occupied length of the record.

collection.writeAtPos (record, pos, callback)

Writes a record into the collection at the given file position.

  • record: the new element to be inserted
  • pos: the position of the file where it's supposed to be written
  • callback (error, pos, length) - called when writing finishes or an error occurs. Passes in the written position and the occupied length of the record.

collection.fetch (pos, length, callback)

Fetches a record at position pos and length.

  • callback (error, record) - called when fetching finishes or an error occurs. First argument is an error if it occurs. Second argument is the record if found.

collection.read (callback, null_on_end)

Reads entire collection from start to end, invoking callback on each record found.

  • callback (error, record, pos, length) - called each time a record is found or an error occurs. First argument is an error if it occurs. Second argument is the record if found. Third argument is the position the record was found on on the collection. The fourth argument is the length occupied by that record.

collection.end (callback)

Ends the collection by closing the file.

  • callback (error) - called when ending finishes. First argument is an error if it occurs.

collection.filter (filter_function, callback)

Applies filter_function to each element of the collection, invoking callback each time filter_function returns true or an error occurs.

  • filter_function (record) - called for each record. Return true if you want the record selected.
  • callback (error) - called when a record is selected or an error occurs. First argument is an error if it occurs.

collection.position (position)

Position the collection to start writing at specific file position. Next call to write will write on this position.

collection.clear (callback)

Removes all elements from the collection.

  • callback (error) - called when clearing finishes. First argument is an error if it occurs.

collection.destroy (callback)

Destroys the collection by removing the underlaying file.

  • callback (error) - called when removing finishes. First argument is an error if it occurs.

collection.rename (callback)

Renames the collection file.

  • callback (error) - called when renaming finishes. First argument is an error if it occurs.