14.0.0 • Published 3 years ago

sengi-engine v14.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

Sengi Engine

This package is part of the Sengi family.

npm.io npm

The core Sengi engine.

It interprets the field type definitions, doc type definitions and clients.

It validates and actions input requests, by issuing queries and instructions to the underlying datastore.

It mutates documents according to the defined operations and patch policy, performs validation checks, and upserts modified documents back to the data store.

Parameter and document validation is performed by Jsonotron.

Installation

npm install sengi-engine

Usage

This package is used by Sengi service implementations, such as sengi-express. Unless you are creating a new public interface for Sengi then you probably don't want to use this package directly.

To instantiate a Sengi engine you have to provide the following parameters:

  • schemas An array of schemas that may be referenced by parameter or filter objects.

  • userSchema A schema that defines the user object that is passed to the doc type implementation and authorise functions.

  • docTypes - An array of Sengi DocType objects as defined in the sengi-interfaces package.

  • clients - An array of Sengi Client objects as defined in the sengi-interfaces package.

  • docStore - An object that implements the Sengi document store interface. There are implementation for in-memory, Microsoft Azure Cosmos, Amazon AWS DynamoDB and MongoDB.

  • onPreSaveDoc - A function (props: PreSaveDocCallbackProps) => void that is invoked just before a document is saved. The doc that is passed to this function is subsequently passed to the document store to be persisted.

  • onPreQueryDocs - A function (props: PreQueryDocsCallbackProps) => void that is invoked just before a query is executed.

  • onSavedDoc - A function (props: SavedDocCallbackProps) => void that is invoked just after a document is persisted to the document store.

  • onDeleteDoc - A function (props: DeletedDocCallbackProps) => void that is invoked just after a document is deleted.

  • log True if each request should be logged to the console.

  • getMillisecondsSinceEpoch A function () => number that returns the number of milliseconds since the epoch. A default implementation is provided so this is used primarily for testing.

  • getIdFromUser A function (user: User) => string that returns the id of the given user object. This is used by the engine to populate the docCreatedByUserId and docLastUpdatedByUserId common fields. If this function is not provided then the value <undefined> will be used instead.

The following example shows how to setup a Sengi-engine using the in-memory document store.

const sengi = new Sengi({
  docTypes: [], // array of objects that implement DocType interface
  clients: [], // array of objects that implement Client interface
  docStore: new MemDocStore({ docs: [], generateDocVersionFunc: () => 'xxxx' })
})

Methods

The Sengi engine provides the following methods.

Method NameParametersDescription
getDocTypeNameFromPluralNamedocTypePluralName: stringReturns the singular doc type name for the given plural name, or null if not found.
getDocTypePluralNameFromNamedocTypeName: stringReturns the plural doc type name for the given singular name, or null if not found.
getApiKeysLoadedFromEnvCountReturns the number of api keys loaded from environment variables.
getApiKeysNotFoundInEnvCountReturns the number of api keys that were dropped because they referenced unknown environment variables.
createDocumentprops: CreateDocumentPropsCreates a new document using a doc type constructor. Returns { isNew }.
deleteDocumentprops: DeleteDocumentPropsDeletes an existing document. Returns { isDeleted }.
operateOnDocumentprops: OperateOnDocumentPropsOperates on an existing document. Returns { isUpdated }.
patchDocumentprops: PatchDocumentPropsPatches an existing document with a merge patch. Use null to delete fields. Returns { isUpdated }.
queryDocumentsprops: QueryDocumentsPropsExecutes a query across a set of documents, typically an aggregate like sum or max.
replaceDocumentprops: ReplaceDocumentPropsReplaces (or inserts) a document, without using the doc type constructor. Returns { isNew }.
selectDocumentsByFilterprops: SelectDocumentsByFilterPropsSelects a set of documents using a filter. Returns { deprecations, docs }.
selectDocumentsByIdsprops: SelectDocumentsByIdsPropsSelects a set of documents using an array of document ids. Returns { deprecations, docs }.
selectDocumentsprops: SelectDocumentsPropsSelects all documents of a specified doc type. Returns { deprecations, docs }.

For the document-centric methods The properties isNew, isDeleted and isUpdated are booleans.

For the query-centric methods the property deprecations is an object that contains a key for each field that was requested and is also deprecated. The value is a comment describing the reason for the deprecation and/or the fields to request instead.

For they query-centric methods the docs property is an array of Doc objects. The objects will only contain the requested fields.

Development

Tests are written using Jest with 100% coverage.

npm test

Continuous Deployment

Any pushes or pull-requests on non-master branches will trigger the test runner.

Any pushes to master will cause the family of libraries to be re-published.

14.0.0

3 years ago

13.3.1

3 years ago

13.3.0

3 years ago

13.2.0

3 years ago

13.2.1

3 years ago

13.1.0

3 years ago

13.0.1

3 years ago

13.0.0

3 years ago

12.0.0

3 years ago

11.2.0

3 years ago

11.2.1

3 years ago

11.1.1

3 years ago

11.0.0

3 years ago

11.1.0

3 years ago

10.0.0

3 years ago

9.1.0

3 years ago

9.0.0

3 years ago

8.5.0

3 years ago

8.4.4

3 years ago

8.4.3

3 years ago

8.4.2

3 years ago

8.4.1

3 years ago

8.4.0

3 years ago

8.3.0

3 years ago

8.2.0

3 years ago

8.1.0

3 years ago

8.0.0

3 years ago

8.1.1

3 years ago

7.7.3

3 years ago

7.7.2

3 years ago

7.7.1

3 years ago

7.7.0

3 years ago

7.6.1

3 years ago

7.6.0

3 years ago

7.5.0

3 years ago

7.4.0

3 years ago

7.3.2

3 years ago

7.3.1

3 years ago

7.3.0

3 years ago

7.2.1

3 years ago

7.2.0

3 years ago

7.1.0

3 years ago

7.0.0

3 years ago

6.7.0

3 years ago

6.6.0

3 years ago

6.5.0

3 years ago

6.4.1

3 years ago

6.3.3

3 years ago

6.3.2

3 years ago

6.3.1

3 years ago

6.3.0

3 years ago

6.2.0

3 years ago