@uttori/storage-provider-json-memory
TypeScript icon, indicating that this package has built-in type declarations

5.0.0 • Public • Published

view on npm npm module downloads Build Status Dependency Status Coverage Status Tree-Shaking Support Dependency Count Minified + GZip Minified

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

{
  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-memory';

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

const s = new StorageProvider();
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

StorageProvider

Storage for Uttori documents using JSON objects in memory.

Typedefs

UttoriDocument
StorageProviderConfig

StorageProvider

Storage for Uttori documents using JSON objects in memory.

Kind: global class
Properties

Name Type Description
documents Array.<UttoriDocument> The collection of documents.
history object The collection of document histories indexes.
histories object The collection of document revisions by index.

new StorageProvider([config])

Creates an instance of StorageProvider.

Param Type Description
[config] StorageProviderConfig A configuration object.

Example (Init StorageProvider)

const storageProvider = new StorageProvider();

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.history : Record.<string, Array.<string>>

The collection of document histories indexes.

Kind: instance property of StorageProvider

storageProvider.histories : Record.<string, UttoriDocument>

The collection of document revisions by timestamp.

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.<(number|Array.<UttoriDocument>)>

Returns all documents matching a given query.

Kind: instance property of StorageProvider
Returns: Promise.<(number|Array.<UttoriDocument>)> - The items matching the supplied query.

Param Type Description
query string The 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)> - The returned UttoriDocument.

Param Type Description
slug string The slug of the document to be returned.

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

Returns the history of edits for a given slug.

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

Param Type Description
slug string The 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)> - The returned revision of the document.

Param Type Description
params object The params object.
params.slug string The slug of the document to be returned.
params.revision string | number The unix timestamp of the history to be returned.

storageProvider.add

Saves a document to internal array.

Kind: instance property of StorageProvider

Param Type Description
document UttoriDocument The document to be added to the collection.

storageProvider.updateValid ℗

Updates a document and saves to memory.

Kind: instance property of StorageProvider
Access: private

Param Type Description
params object The params object.
params.document UttoriDocument The document to be updated in the collection.
params.originalSlug string The 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 memory. Calling with a new document will add that document.

Kind: instance property of StorageProvider

Param Type Description
params object The params object.
params.document UttoriDocument The document to be updated in the collection.
params.originalSlug string The original slug identifying the document, or the slug if it has not changed.

storageProvider.delete

Removes a document from memory.

Kind: instance property of StorageProvider

Param Type Description
slug string The slug identifying the document.

storageProvider.updateHistory

Updates History for a given slug, renaming the key and history key as needed.

Kind: instance property of StorageProvider

Param Type Description
params object The params object.
params.slug string The slug of the document to update history for.
params.content UttoriDocument The revision of the document to be saved.
[params.originalSlug] string The original slug identifying the document, or the slug if it has not changed.

storageProvider.reset()

Resets to the initial state.

Kind: instance method of StorageProvider

UttoriDocument

Kind: global typedef
Properties

Name Type Description
slug string The unique identifier for the document.
[createDate] number The creation date of the document.
[updateDate] number The last date the document was updated.

StorageProviderConfig

Kind: global typedef
Properties

Name Type Description
[updateTimestamps] boolean Should update times be marked at the time of edit.
[useHistory] boolean Should history entries be created.
[events] Record.<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

Misc.

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

Contributors

License

Readme

Keywords

Package Sidebar

Install

npm i @uttori/storage-provider-json-memory

Weekly Downloads

1

Version

5.0.0

License

MIT

Unpacked Size

114 kB

Total Files

36

Last publish

Collaborators

  • matthewcallis