npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

sengi-engine

v14.0.0

Published

The Sengi engine.

Downloads

17

Readme

Sengi Engine

This package is part of the Sengi family.

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 Name | Parameters | Description ---|---|--- getDocTypeNameFromPluralName | docTypePluralName: string | Returns the singular doc type name for the given plural name, or null if not found. getDocTypePluralNameFromName | docTypeName: string | Returns the plural doc type name for the given singular name, or null if not found. getApiKeysLoadedFromEnvCount | | Returns the number of api keys loaded from environment variables. getApiKeysNotFoundInEnvCount | | Returns the number of api keys that were dropped because they referenced unknown environment variables. createDocument | props: CreateDocumentProps | Creates a new document using a doc type constructor. Returns { isNew }. deleteDocument | props: DeleteDocumentProps | Deletes an existing document. Returns { isDeleted }. operateOnDocument | props: OperateOnDocumentProps | Operates on an existing document. Returns { isUpdated }. patchDocument | props: PatchDocumentProps | Patches an existing document with a merge patch. Use null to delete fields. Returns { isUpdated }. queryDocuments | props: QueryDocumentsProps | Executes a query across a set of documents, typically an aggregate like sum or max. replaceDocument | props: ReplaceDocumentProps | Replaces (or inserts) a document, without using the doc type constructor. Returns { isNew }. selectDocumentsByFilter | props: SelectDocumentsByFilterProps | Selects a set of documents using a filter. Returns { deprecations, docs }. selectDocumentsByIds | props: SelectDocumentsByIdsProps | Selects a set of documents using an array of document ids. Returns { deprecations, docs }. selectDocuments | props: SelectDocumentsProps | Selects 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.