2.0.0 • Published 5 years ago

uttori-storage-provider-json-memory v2.0.0

Weekly downloads
1
License
MIT
Repository
github
Last release
5 years ago

view on npm npm module downloads Build Status Dependency Status Coverage Status

Uttori Storage Provider - JSON Memory

Uttori Storage Provider using JavaScript objects in memory. This does NOT persist or restore data.

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-memory

Config

{
  // Registration Events
  events: {
    add: ['storage-add'],
    delete: ['storage-delete'],
    get: ['storage-get'],
    getHistory: ['storage-get-history'],
    getRevision: ['storage-get-revision'],
    query: ['storage-query'],
    update: ['storage-update'],
    validateConfig: ['validate-config'],
  },
}

Example

const { StorageProvider } = require('uttori-storage-provider-json-memory');
const s = new StorageProvider();
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 = s.getQuery('SELECT tags FROM documents WHERE slug IS_NOT_NULL ORDER BY slug ASC LIMIT 1');
➜  results === [
      { tags: ['Example Tag'] },
    ]

API Reference

Classes

Typedefs

StorageProvider

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

Kind: global class
Properties

NameTypeDescription
documentsArray.<UttoriDocument>The collection of documents.
historyObjectThe collection of document histories indexes.
historiesObjectThe collection of document revisions by index.

new StorageProvider()

Creates an instance of StorageProvider.

Example (Init StorageProvider)

const storageProvider = new StorageProvider();

storageProvider.documents : Array.<UttoriDocument>

this.documents All documents.

Kind: instance property of StorageProvider

storageProvider.all() ⇒ Array

Returns all documents.

Kind: instance method of StorageProvider
Returns: Array - All documents.
Example

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

storageProvider.getQuery(query) ⇒ Array

Returns all documents matching a given query.

Kind: instance method of StorageProvider
Returns: Array - The items matching the supplied query.

ParamTypeDescription
queryStringThe conditions on which documents should be returned.

storageProvider.get(slug) ⇒ UttoriDocument

Returns a document for a given slug.

Kind: instance method of StorageProvider
Returns: UttoriDocument - The returned UttoriDocument.

ParamTypeDescription
slugStringThe slug of the document to be returned.

storageProvider.getHistory(slug) ⇒ Object

Returns the history of edits for a given slug.

Kind: instance method of StorageProvider
Returns: Object - The returned history object.

ParamTypeDescription
slugstringThe slug of the document to get history for.

storageProvider.getRevision(params) ⇒ UttoriDocument

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

Kind: instance method of StorageProvider
Returns: UttoriDocument - The returned revision of the document.

ParamTypeDescription
paramsObject
params.slugStringThe slug of the document to be returned.
params.revisionString | NumberThe unix timestamp of the history to be returned.

storageProvider.add(document)

Saves a document to the file system.

Kind: instance method of StorageProvider

ParamTypeDescription
documentUttoriDocumentThe document to be added to the collection.

storageProvider.updateValid(params) ℗

Updates a document and saves to the file system.

Kind: instance method of StorageProvider
Access: private

ParamTypeDescription
paramsObject
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.update(params)

Updates a document and figures out how to save to the file system. Calling with a new document will add that document.

Kind: instance method of StorageProvider

ParamTypeDescription
paramsObject
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(slug)

Removes a document from the file system.

Kind: instance method of StorageProvider

ParamTypeDescription
slugStringThe slug identifying the document.

storageProvider.reset()

Resets to the initial state.

Kind: instance method of StorageProvider

storageProvider.updateHistory(params)

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

Kind: instance method of StorageProvider

ParamTypeDescription
paramsObject
params.slugStringThe slug of the document to update history for.
params.contentUttoriDocumentThe revision of the document to be saved.
params.originalSlugStringThe original slug identifying the document, or the slug if it has not changed.

UttoriDocument

Kind: global typedef
Properties

NameTypeDefaultDescription
slugStringThe unique identifier for the document.
titleString''The unique identifier for the document.
createDateNumber | DateThe creation date of the document.
updateDateNumber | DateThe last date the document was updated.
tagsArray.<String>[]The unique identifier for the document.
customDataObject{}Any extra meta data for the document.

Tests

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

npm install
npm test
DEBUG=Uttori* npm test

Misc.

You can see the various speeds of the array shuffles used for RANDOM sorting on perf.link;

Contributors

License