5.0.1 • Published 4 months ago

@uttori/storage-provider-json-file v5.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
4 months ago

view on npm npm module downloads Build Status Coverage Status

Uttori Storage Provider - JSON File

Uttori Storage Provider using JSON files on disk.

This repo exports both a Uttori Plugin compliant Plugin class as well as the underlying StorageProvider class.

Install

npm install --save @uttori/storage-provider-json-file

Config

{
  contentDirectory: '',
  historyDirectory: '',
  extension: 'json',
  spacesDocument: null,
  spacesHistory: null,
  updateTimestamps: true,
  useHistory: true,
  // Registration Events
  events: {
    add: ['storage-add'],
    delete: ['storage-delete'],
    get: ['storage-get'],
    getHistory: ['storage-get-history'],
    getRevision: ['storage-get-revision'],
    getQuery: ['storage-query'],
    update: ['storage-update'],
    validateConfig: ['validate-config'],
  },
}

Example

// When part of UttoriWiki:
import { Plugin as StorageProviderJSON } from '@uttori/storage-provider-json-file';

// When stand alone:
import StorageProvider from '@uttori/storage-provider-json-file';

const s = new StorageProvider({
  contentDirectory: 'example/content',
  historyDirectory: 'example/history',
  extension: 'json',
  spacesDocument: null,
  spacesHistory: null,
});
await s.add({
  title: 'Example Title',
  slug: 'example-title',
  content: '## Example Title',
  html: '',
  updateDate: 1459310452001,
  createDate: 1459310452001,
  tags: ['Example Tag'],
  customData: {
    keyA: 'value-a',
    keyB: 'value-b',
    keyC: 'value-c',
  },
});
const results = await s.getQuery('SELECT tags FROM documents WHERE slug IS_NOT_NULL ORDER BY slug ASC LIMIT 1');
➜  results === [
      { tags: ['Example Tag'] },
    ]
const results = await s.getQuery('SELECT COUNT(*) FROM documents WHERE slug IS_NOT_NULL ORDER BY RANDOM ASC LIMIT -1');
➜  results === 1

API Reference

Classes

Typedefs

StorageProvider

Storage for Uttori documents using JSON files stored on the local file system.

Kind: global class
Properties

NameTypeDescription
configStorageProviderConfigThe configuration object.
documentsRecord.<string, UttoriDocument>The collection of documents where the slug is the key and the value is the document.

new StorageProvider(config)

Creates an instance of StorageProvider.

ParamTypeDescription
configStorageProviderConfigA configuration object.

Example (Init StorageProvider)

const storageProvider = new StorageProvider({ contentDirectory: 'content', historyDirectory: 'history', spacesDocument: 2 });

storageProvider.documents : Record.<string, UttoriDocument>

The collection of documents where the slug is the key and the value is the document.

Kind: instance property of StorageProvider

storageProvider.all ⇒ Promise.<Record.<string, UttoriDocument>>

Returns all documents.

Kind: instance property of StorageProvider
Returns: Promise.<Record.<string, UttoriDocument>> - All documents.
Example

storageProvider.all();
➜ { first-document: { slug: 'first-document', ... }, ...}

storageProvider.getQuery ⇒ Promise.<(Array.<UttoriDocument>|number)>

Returns all documents matching a given query.

Kind: instance property of StorageProvider
Returns: Promise.<(Array.<UttoriDocument>|number)> - Promise object represents all matching documents.

ParamTypeDescription
querystringThe conditions on which documents should be returned.

storageProvider.get ⇒ Promise.<(UttoriDocument|undefined)>

Returns a document for a given slug.

Kind: instance property of StorageProvider
Returns: Promise.<(UttoriDocument|undefined)> - Promise object represents the returned UttoriDocument.

ParamTypeDescription
slugstringThe slug of the document to be returned.

storageProvider.add

Saves a document to the file system.

Kind: instance property of StorageProvider

ParamTypeDescription
documentUttoriDocumentThe document to be added to the collection.

storageProvider.updateValid ℗

Updates a document and saves to the file system.

Kind: instance property of StorageProvider
Access: private

ParamTypeDescription
documentUttoriDocumentThe document to be updated in the collection.
originalSlugstringThe original slug identifying the document, or the slug if it has not changed.

storageProvider.update

Updates a document and figures out how to save to the file system.

Kind: instance property of StorageProvider

ParamTypeDescription
paramsobjectThe params object.
params.documentUttoriDocumentThe document to be updated in the collection.
params.originalSlugstringThe original slug identifying the document, or the slug if it has not changed.

storageProvider.delete

Removes a document from the file system.

Kind: instance property of StorageProvider

ParamTypeDescription
slugstringThe slug identifying the document.

storageProvider.getHistory ⇒ Promise.<Array.<string>>

Returns the history of edits for a given slug.

Kind: instance property of StorageProvider
Returns: Promise.<Array.<string>> - Promise object represents the returned history.

ParamTypeDescription
slugstringThe slug of the document to get history for.

storageProvider.getRevision ⇒ Promise.<(UttoriDocument|undefined)>

Returns a specifc revision from the history of edits for a given slug and revision timestamp.

Kind: instance property of StorageProvider
Returns: Promise.<(UttoriDocument|undefined)> - Promise object represents the returned revision of the document.

ParamTypeDescription
paramsobjectThe params object.
params.slugstringThe slug of the document to be returned.
params.revisionstring | numberThe unix timestamp of the history to be returned.

storageProvider.updateHistory

Updates History for a given slug, renaming the store file and history directory as needed.

Kind: instance property of StorageProvider

ParamTypeDescription
slugstringThe slug of the document to update history for.
contentstringThe revision of the document to be saved.
originalSlugstringThe original slug identifying the document, or the slug if it has not changed.

StorageProvider.ensureDirectory(directory)

Ensure a directory exists, and if not create it.

Kind: static method of StorageProvider

ParamTypeDescription
directorystringThe directory to ensure exists.

UttoriDocument

Kind: global typedef
Properties

NameTypeDescription
slugstringThe unique identifier for the document.
createDatenumber | DateThe creation date of the document.
updateDatenumber | DateThe last date the document was updated.

StorageProviderConfig

Kind: global typedef
Properties

NameTypeDescription
contentDirectorystringThe directory to store documents.
historyDirectorystringThe directory to store document histories.
extensionstringThe file extension to use for file.
updateTimestampsbooleanShould update times be marked at the time of edit.
useHistorybooleanShould history entries be created.
useCachebooleanShould we cache files in memory?
spacesDocumentnumberThe spaces parameter for JSON stringifying documents.
spacesHistorynumberThe spaces parameter for JSON stringifying history.
eventsRecord.<string, Array.<string>>The events to listen for.

Tests

To run the test suite, first install the dependencies, then run npm test:

npm install
npm test
DEBUG=Uttori* npm test

Contributors

License

5.0.1

4 months ago

5.0.0

5 months ago

4.2.1

1 year ago

4.2.0

2 years ago

4.1.1

2 years ago

4.1.0

3 years ago

4.0.0

3 years ago

3.4.3

3 years ago

3.4.2

3 years ago

3.4.1

4 years ago

3.4.0

4 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.0

4 years ago

3.1.0

4 years ago

3.0.0

4 years ago