New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

data-defender

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

data-defender

A database-agnostic simple data schema enforcer for node.js

  • 0.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
increased by100%
Maintainers
1
Weekly downloads
 
Created
Source

data-defender

A node.js module to let you define and enforce data constraints.

The module is database-agnostic.

NOTE: This module alone does NOT store data in a database and it does NOT read from any database.

API

.create(schemaName [string])

Returns a schema object to allow you to define constraints on each data property.

It will throw an error if trying to create a schema that has already been created.

Example:

var defender = require('data-defender');
var example = defender.create('example');

example.define('id', {
	type: defender.DATATYPE.UNIQUE
});
example.define('name', {
	type: defender.DATATYPE.STR,
	default: 'No Name',
	max: 30,
	min: 4
});

.get(schemaName [string])

Returns an already defined schema object.

It will throw an exception if trying to get a schema that has not been created.

Example:

var example = defender.get('example');

Schema Object

It is an instance of Struct class.

This object allows you to define constraints on each data property.

.define(PropertyName [string], constraints [object])

If there is an error, it will throw an exception.

constraints:

{
	type: [enum],
	default: [*default value],
	max: [*number or date],
	min: [*number or date] 
}
type [enum]

Defines the data type of the property.

The possible values are:

var defender = require('data-defender');

// number type
defender.DATATYPE.NUM;

// string type
defender.DATATYPE.STR;

// array type
defender.DATATYPE.ARR;

// object type
defender.DATATYPE.OBJ;

// unique type
defender.DATATYPE.UNIQUE;

// date type
defender.DATATYPE.DATE;

// boolean type
defender.DATATYPE.BOOL;

// modified time type
defender.DATATYPE.MOD;

NOTE 1: UNIQUE and MOD data type's value cannot be updated.

NOTE 2: MOD data type property will be updated automatically by updating other properties.

default [*default value]

An optional value to define a default value for the property.

max [*number or date]

An optional value to define the maximum value for the property.

min [*number or date]

An optional value to define the minimum value for the property.

.lockSchema()

Locks the definition of schema. If you call .define() after invoking this function, it will throw an excetion.

.load(data [*object])

It loads the data and returns a data object.

If the given values do not meet the constraints defined by the schema, it will throw an exception.

NOTE: If you define a property that is UNIQUE and pass nothing to .load(), it will auto-generate a unique ID as a string.

Example:

var defender = require('data-defender');
var mySchema = defender.create('mySchema');
mySchema.define('id', {
	type: defender.DATATYPE.UNIQUE
});
mySchema.define('name', {
	type: defender.DATATYPE.STR,
	max: 30,
	min: 1
});
mySchema.define('age', {
	type: defender.DATATYPE.NUM,
	min: 0
});

var dbRecord = mySchema.load(dataFromDatabase);
Data Object

An instance of Data class. This object allows you to change the property values of the data loaded according to the schema constraints defined.

Example:

var defender = require('data-defender');
var mySchema = defender.create('mySchema');
mySchema.define('id', {
	type: defender.DATATYPE.UNIQUE
});
mySchema.define('name', {
	type: defender.DATATYPE.STR,
	max: 30,
	min: 1
});
mySchema.define('age', {
	type: defender.DATATYPE.NUM,
	min: 0
});

var dbRecord = mySchema.load(dataFromDatabase);
dbRecord.update('name', 'New Name');
dbRecord.update('age', dbRecord.get('age') + 4);
.update(propertyName [string], value [mix])

Updates the property value of the name given with the value passed to the function.

.get(propertyName [string])

Returns the current value of the given property.

.toJSON()

Returns all the property along with their values as an object.

Useful when you need to store the data to database etc.

Example:

var dataToSave = myData.toJSON();
saveToDatabase(dataToSave, callback);

Keywords

FAQs

Package last updated on 28 Oct 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc