feathers-fs
Use the FeathersJS service interface to read and write data in the file system.
Installation
npm install feathers-fs --save
Documentation
The feathers-js
module lets you use the FeathersJS service interface methods to read and write JSON-able data in the file system. It creates .json
files, by default, but is extendable to be able to read or write any text-based file format that is serializable to/from JSON or a JavaScript object literal. Here's a basic example:
const ffs = ;const jsonService = app
API
service(options) -> FeathersService
Sets up the plugin with the provided options
.
options {Object}
- must contain aroot
param.root {String}
required - the root path for all files that will be read or written.type {String}
- the file type/extension that this service will handle. default:json
. When creating a file, if a file extension is not provided, this will be appended.cache {Boolean}
- whether the require module cache should be used. default:false
. By default, the require cache for the individual file will be cleared both before and after reading the file.
Service API
Two of the service methods are implemented:
create(data) -> Promise
Creates a file using the provided data.
data {Object}
- must containpath
anddata
attributes.path {String}
required - the full path, including file name. If an extension is not provided, the extension provided asoptions.type
will automatically be appended.data {Object|Array}
required - The data to be written to the file. Thecreate
method returns a promise that resolves to thedata
that was written to file.
get(path, params) -> Promise
Reads a file from the provided path.
path {String}
required - the full path, relative to theoptions.root
of the file to be read.params.cache {Boolean}
- Whether or not this specific file lookup should use therequire
cache. Defaults tooptions.cache
Theget
method returns a promise that resolves to the data that was read from the file.
Handling Other File Types
The service can be extended to handle file types other than .json
. When you create an instance of the service class, all attributes passed in the options
will be available at this.options
. Generally, both of the following methods will need to be overridden:
readFromFile(path) -> promise
The readFromFile
method is used to read the file at path
and format it as an object literal.
path {String}
- the full file path of the file to be read, including theoptions.root
. It must return a promise that resolves to an object literal.
toFileString(data) -> promise
The toFileString
method is used to convert the provided data
into a string to be written to disk.
data {Object}
- the object literal containing the data that will be formatted. It must return a promise that resolves to a string.
Example of Extending the Service for CSV
Here's a stubbed-out example of what extending a CSV service would look like.
const BaseService = Service;const fs = ; // Converts the CSV file to an object literal. { return { // Use the path to read the file contents. fs; }; } // Converts an object literal to a string ready for storing to the file system. { return { // Use a node package to convert the data to a string. try let formatted = ; return ; catch error return ; }; } const serviceOptions = root: 'src/services' type: 'csv' // Some CSV plugins require an array of fields. // The fields are available inside the override methods as shown above. fields: 'first' 'second' 'third';const csvService = app
Complete Example
Here's an example of a Feathers server that uses feathers-fs
.
const feathers = ;const rest = ;const hooks = ;const bodyParser = ;const errorHandler = ;const feathersFs = ; // Initialize the applicationconst app = // Needed for parsing bodies (login) // Initialize your feathers plugin ; const dataToStore = to: 'marshall' from: 'marshall' body: 'Stop talking to that rubber ducky!' to: 'marshall' from: 'marshall' body: `...unless you're rubber duck debugging.` ; // Store the data at `/src/services/messages/messages.seed.json`app // Retrieve the data at the provided path.app ; app; console;
License
Copyright (c) 2016
Licensed under the MIT license.