Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
ezobjects
Advanced tools
EZ Objects is a Node.js module (that can also be usefully browserify'd) that aims to save you lots of time writing class objects. All you have to do is create simple configurations for each of your objects and then call the library function(s). Let's start by showing a basic example:
const ezobjects = require('ezobjects');
/**
* Create a customized object called DatabaseRecord on the
* global (node) or window (browser) namespace with a single
* property called `id`.
*/
ezobjects.createObject({
className: 'DatabaseRecord',
properties: [
{ name: 'id', type: 'number', setTransform: x => parseInt(x) }
]
});
const record = new DatabaseRecord();
console.log(record);
DatabaseRecord { _id: 0 }
ezobjects.createObject({
className: 'User',
extends: DatabaseRecord,
properties: [
{ name: 'username', type: 'string' },
{ name: 'firstName', type: 'string' },
{ name: 'lastName', type: 'string' },
{ name: 'checkingBalance', type: 'number', setTransform: x => parseFloat(x) },
{ name: 'permissions', type: 'Array' },
{ name: 'favoriteDay', type: 'Date' }
]
});
const user = new User();
console.log(user);
User {
_id: 0,
_username: '',
_firstName: '',
_lastName: '',
_checkingBalance: 0,
_permissions: [],
_favoriteDay: null }
Important Notes: Your object must have a unique integer property named id
to be able to use the MySQL
functionality of EZ Objects. You must also use EZ Object's MySQLConnection class for your database connection.
const ezobjects = require('ezobjects');
const fs = require('fs');
const moment = require('moment');
/**
* Load external MySQL configuration which uses the following JSON
* format:
* {
* "host" : "localhost",
* "user" : "ezobjects",
* "password" : "myPassword",
* "database" : "ezobjects"
* }
*/
const configMySQL = JSON.parse(fs.readFileSync('mysql-config.json'));
/**
* Create a connection object for the MySQL database using our MySQL
* module async/await wrapper.
*/
const db = new ezobjects.MySQLConnection(configMySQL);
/**
* Configure a new EZ Object called DatabaseRecord with one 'id'
* property that contains additional MySQL configuration settings.
*/
const configDatabaseRecord = {
className: 'DatabaseRecord',
properties: [
{
name: 'id',
type: 'number',
mysqlType: 'int',
autoIncrement: true,
primary: true,
setTransform: x => parseInt(x)
}
]
};
/**
* Create the DatabaseRecord object -- Note: This object is not
* linked to a MySQL table directory, and therefore has no `tableName`
* property, but it has the MySQL configuration properties on `id`
* because it will be extended by another object that is linked to
* a MySQL table and therefore it will need the MySQL configuration
* of the `id` property.
*/
ezobjects.createObject(configDatabaseRecord);
/**
* Configure a new EZ Object called User that extends from the
* DatabaseRecord object and adds several additional properties and
* a MySQL index.
*/
const configUser = {
tableName: 'users',
className: 'User',
extends: DatabaseRecord,
extendsConfig: configDatabaseRecord,
properties: [
{
name: 'username',
type: 'string',
mysqlType: 'varchar',
length: 20
},
{
name: 'firstName',
type: 'string',
mysqlType: 'varchar',
length: 20
},
{
name: 'lastName',
type: 'string',
mysqlType: 'varchar',
length: 20
},
{
name: 'checkingBalance',
type: 'number',
mysqlType: 'double',
setTransform: x => parseFloat(x)
},
{
name: 'permissions',
type: 'Array',
mysqlType: 'text',
saveTransform: x => x.join(','),
loadTransform: x => x.split(',').map(x => parseInt(x))
},
{
name: 'favoriteDay',
type: 'Date',
mysqlType: 'datetime',
saveTransform: x => moment(x).format('Y-MM-DD HH:mm:ss'),
loadTransform: x => new Date(x)
}
],
indexes: [
{ name: 'lastName', type: 'BTREE', columns: [ 'lastName' ] }
]
};
/** Create the User object */
ezobjects.createObject(configUser);
/** Create new user, initializing with object passed to constructor */
const user = new User({
username: 'richlowe',
firstName: 'Rich',
lastName: 'Lowe',
checkingBalance: 4.32,
permissions: [1, 3, 5],
favoriteDay: new Date('01-01-2018')
});
/** Test if user is an instance of DatabaseRecord */
console.log(ezobjects.instanceOf(user, 'DatabaseRecord'));
/** Self-executing async wrapper so we can await results */
(async () => {
try {
/** Create table if it doesn't already exist */
await ezobjects.createTable(db, configUser);
/** Insert user into the database */
await user.insert(db);
/** Log user (should have automatically incremented ID now) */
console.log(user);
/** Change the property values a bit */
user.checkingBalance(50.27);
user.firstName('Richard');
user.favoriteDay(new Date('09-01-2019'));
/** Update user in the database */
await user.update(db);
/** Log user (should have `checkingBalance` of 50.27) */
console.log(user);
/** Create another user */
const anotherUser = new User();
/** Assuming ID of last user was 1, load record from database */
await anotherUser.load(db, 1);
/** Log anotherUser */
console.log(anotherUser);
/** Delete the user from the database */
await anotherUser.delete(db);
} catch ( err ) {
console.log(err.message);
} finally {
/** Close database connection */
db.close();
}
})();
true
User {
_id: 1,
_username: 'richlowe',
_firstName: 'Rich',
_lastName: 'Lowe',
_checkingBalance: 4.32,
_permissions: [ 1, 3, 5 ],
_favoriteDay: 2018-01-01T06:00:00.000Z }
User {
_id: 1,
_username: 'richlowe',
_firstName: 'Richard',
_lastName: 'Lowe',
_checkingBalance: 50.27,
_permissions: [ 1, 3, 5 ],
_favoriteDay: 2019-09-01T05:00:00.000Z }
User {
_id: 1,
_username: 'richlowe',
_firstName: 'Richard',
_lastName: 'Lowe',
_checkingBalance: 50.27,
_permissions: [ 1, 3, 5 ],
_favoriteDay: 2019-09-01T05:00:00.000Z }
These are the object method signatures even the most basic of EZ Objects will have:
PlainObject
- (optional)data
. Keys can either be equal to the name of a property, or they can be have an underscore before the name of a property, as would be the case if you were to JSON.stringify() and then JSON.parse() an EZ Object. This allows for easy transferability in cases where JSON is used as the transfer medium.string
- (optional)data
. Keys can either be equal to the name of a property, or they can be have an underscore before the name of a property, as would be the case if you were to JSON.stringify() an EZ Object. This allows for easy transferability in cases where JSON is used as the transfer medium.PlainObject
data
. This is also the method used by the constructor.In addition, each property you define will have a single method that is a getter and setter, and it will have the following signatures:
mixed
mixed
TypeError
if value
is not of the correct javascript data type for myPropertythis
to allow for set call chaining.These are the object method signatures that will additionally be provided if your configuration contains a tableName
,
meaning it's intended to be linked to a MySQL table:
MySQLConnection
db
, table tableName
, that has its id
field equal to the id
property of this object.MySQLConnection
db
, table tableName
, and store the resulting insertId in the id
property of this object.MySQLConnection
id
property of the record you wish to loaddb
, table tableName
, that has its id
field equal to provided id
parameter.MySQLConnection
mixed
- The value of the stringSearchField
property of the record you wish to loaddb
, table tableName
, that has its stringSearchField
field equal to provided fieldValue
parameter. Here, the actual field name of stringSearchField
is provided in the object configuration, see the configuration section below.string
- The URL of a back-end that provides JSON data compatible with this object's initializerurl
using this object's initializer.url
that will output the JSON. This signature also requires you have jQuery loaded prior to use.MySQLConnection
db
, table tableName
, with its id
field equal to the id
property of this object, using this object's property values.The EZ Objects module exports two functions and a MySQL class object:
A function that creates a MySQL table corresponding to the configuration outlined in objectConfig
, if it doesn't already exist
A function that creates an ES6 class corresponding to the configuration outlined in objectConfig
, with constructor, initializer, getters, setters, and also delete, insert, load, and update if tableName
is configured
A MySQL database connection class that wraps the standard mysql object and provides it with async/await functionality and transaction helpers
See the following for how to configure your EZ Objects:
string
- (required) Name of the classArray
- (required) An array of property configurations that the object (and MySQL table, if applicable) should have corresponding properties formixed
- (optional) The object that the new object should be extended from [required to extend object]object
- (optional) The EZ Object configuration for the object that is being extended from [required to extend object for use with MySQL table link]string
- (optional) Provide if object should be linked with MySQL database tablestring
- (optional) The name of a unique property of type string
that you want to be able to load with as an alternative to id
Array
- (optional) An array of MySQL index configurations that should be created in the MySQL tablestring
- (required) Name of the property, must conform to both JavaScript and MySQL rulesstring
- (required) JavaScript data type for the propertymixed
- (optional) Sets the default value for the property in the class objectfunction
- (optional) Function that transforms and returns the property value prior to gettingfunction
- (optional) Function that transforms and returns the property value prior to settingstring
- (optional) MySQL data type for the property [required for MySQL table association]number
- (optional) MySQL data length for the property [required for MySQL table association on some data types like VARCHAR]number
- (optional) Number of decimals that should be provided for certain data types when SELECT'ed from the MySQL tableboolean
- (optional) Indicates the property is a PRIMARY KEY in the MySQL table [required for MySQL table association on at least one property in the table]boolean
- (optional) Indicates the property is a UNIQUE KEY in the MySQL tableboolean
- (optional) Indicates the property can be NULL [default is properties must be NOT NULL]mixed
- (optional) Sets the default value for the property in the MySQL table, assuming its of the correct typeboolean
- (optional) Indicates the property should be unsigned in the MySQL tableboolean
- (optional) Indicates the property should be zero-filled in the MySQL tablestring
- (optional) Indicates the property should note the provided comment in the MySQL tablestring
- (optional) Indicates the property should use the provided charset in the MySQL tablestring
- (optional) Indicates the property should use the provided collation in the MySQL tableboolean
- (optional) Indicates the property should be auto-incremented in the MySQL tablefunction
- (optional) Function that transforms and returns the property value prior to saving in the databasefunction
- (optional) Function that transforms and returns the property value after loading from the databasestring
- (required) Name of the index, can be arbitrary, but must be unique and not PRIMARYArray
- (required) An array of strings containing property names to be indexedstring
- (optional) Index type, can be BTREE or HASH, defaults to BTREEnumber
- (optional) Indicates the index should use the provided key block sizestring
- (optional) Indicates the index should use the provided parserboolean
- (optional) Indicates the index should be visibleboolean
- (optional) Indicates the index should be invisiblenumber
- 0string
- ''boolean
- falseArray
- []Please open an issue on the GitHub repository if you find any broken functionality or other bugs/errors. Feature requests will also be accepted, but are not guaranteed to be implemented.
MIT Licensed
FAQs
Easy Auto-Generated Strictly-Typed JavaScript Class Objects
The npm package ezobjects receives a total of 147 weekly downloads. As such, ezobjects popularity was classified as not popular.
We found that ezobjects demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.