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

@janiscommerce/api-save

v8.0.0

Published

A package to handle Janis Save APIs

Downloads

1,930

Readme

API Save

Build Status Coverage Status npm version

A package to handle Janis Save APIs

Installation

npm install @janiscommerce/api-save

Usage

'use strict';

const { ApiSaveData } = require('@janiscommerce/api-save');
const { struct } = require('@janiscommerce/superstruct');

const MyRelatedModel = require('../../models/my-related-model');
const someAsyncTask = require('./async-task');

module.exports = class MyApiSaveData extends ApiSaveData {

	static get relationshipsParameters() {
		return {
			children: {
				modelClass: MyRelatedModel,
				mainIdentifierField: 'dbFieldForMainEntity',
				secondaryIdentifierField: 'dbFieldForRelatedEntity',
				shouldClean: false
			}
		};
	}

	static get idStruct() {
		return struct.optional('string|number');
	}

	static get mainStruct() {
		return struct.partial({
			name: 'string',
			description: 'string?',
			status: 'number'
		});
	}

	static get relationshipsStruct() {
		return struct.partial({
			children: ['string']
		});
	}

	postStructValidate() {
		return someAsyncTask(this.dataToSave.main);
	}

	format({ someField, ...restOfTheRecord }) {
		return {
			...restOfTheRecord,
			someField: `prefix-${someField}`
		};
	}

	async shouldSave(formattedItem) {
		const currentItem = await this.getCurrent();
		return formattedItem.someField !== currentItem.someField;
	}

	postSaveHook(id, savedData) {
		return someAsyncTask(id, savedData);
	}

};

API

The following getters and methods can be used to customize and validate your Save API. All of them are optional, however you're encouraged to implement static get mainStruct() so you don't save trash data in your DB.

static get relationshipsParameters()

You need to use this in case you're saving relationships with other models (mostly for relational databases) If you don't have any relationship, there's no need to implement it.

This getter must return an object mapping the name of the field that contains the relationship (must be a key in the struct's relationships property) to the parameters of that relationship. The parameters contain the following properties:

  • modelClass: The class of the model that should save this relationship
  • mainIdentifierField: The field name where the main ID should be saved
  • secondaryIdentifierField: The field name where the related ID should be saved
  • shouldClean: Indicates if previous relationships should be removed. Optional, defaults to false

static get idStruct()

This is used to validate the ID received as path parameter. Defaults to an optional string or number.

static get mainStruct()

This is used to validate the data received in the request, checking the data to be saved in the main entity. Defaults to an object with any property.

static get relationshipsStruct()

This is used to validate the data received in the request, checking the data to be passed to the relationships. Defaults to an object partial with no properties.

async postStructValidate()

This is used to validate the data received in the request, making additional validation even injecting data to the received data. If it returns a Promise, it will be awaited.

format(record)

You can use this to format your main record before it's saved. For example, mapping user friendly values to DB friendly values, add default values, etc. If it returns a Promise, it will be awaited.

async shouldSave(formattedItem)

This an optional method allows you to validate if saving the item is really necessary. This method is called after formatting the item with format().

  • If you return false, the model will not be called for insert the new item or update the current. The API will response the status code 200 adding the id if received at the response body.
  • If you return false on an API Post (without recordId) the API will set the status code 204 No Content .

async getCurrent()

You can use this to obtain the current item for DB. It only works when the API receives the id in the Endpoint (API PUT or PATCH) This method will throw an Error if is used in an API POST (without recordId)

async postSaveHook(id, record)

You can use this to perform a task after saving your main record. For example, emitting an event, logging something, etc. If it returns a Promise, it will be awaited.