Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ezobjects

Package Overview
Dependencies
Maintainers
1
Versions
133
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ezobjects - npm Package Compare versions

Comparing version 2.4.3 to 2.5.0

64

example-readme.js

@@ -6,3 +6,4 @@ const ezobjects = require('./index');

/**
* Load external MySQL configuration which uses the following JSON format:
* Load external MySQL configuration which uses the following JSON
* format:
* {

@@ -19,6 +20,3 @@ * "host" : "localhost",

* Create a connection object for the MySQL database using our MySQL
* module async/await wrapper. Currently, using the ezobjects MySQL
* database is the only option, but future versions may seek to expand
* the option to other databases, or at least allow the standard mysql
* module to work.
* module async/await wrapper.
*/

@@ -48,5 +46,6 @@ const db = new ezobjects.MySQLConnection(configMySQL);

* 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 a class that is linked to a MySQL table and
* therefore it will need the MySQL configuration of the `id` property.
* 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.
*/

@@ -126,13 +125,40 @@ ezobjects.createObject(configDatabaseRecord);

(async () => {
/** 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);
try {
/** Create table if it doesn't already exist */
await ezobjects.createTable(db, configUser);
/** Close database connection */
db.close();
})();
/** 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();
}
})();
const docket = require('docket-parser');
docket.title(`EZ Objects v2.4.3`);
docket.title(`EZ Objects v2.5.0`);
docket.linkClass('text-success');
docket.parseFiles(['index.js', 'mysql-connection.js']);
docket.generateDocs('docs');
{
"name": "ezobjects",
"version": "2.4.3",
"version": "2.5.0",
"description": "Easy dynamic object generation with optional MySQL table linking",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -1,2 +0,2 @@

# EZ Objects v2.4.3
# EZ Objects v2.5.0

@@ -13,4 +13,5 @@ EZ Objects is a Node.js module (that can also be usefully browserify'd) that aims to save you lots of time

/**
* Create a customized object on the global (node) or window (browser)
* namespace.
* Create a customized object called DatabaseRecord on the
* global (node) or window (browser) namespace with a single
* property called `id`.
*/

@@ -23,69 +24,21 @@ ezobjects.createObject({

});
/**
* There now exists a class called DatabaseRecord which has the
* following signatures:
*
* @signature new DatabaseRecord([data])
* @param data PlainObject
* @description Create a new DatabaseRecord object and initialize it
* using either defaults or any provided key/value pairs in the plain
* object `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.
*
* @signature new DatabaseRecord([data])
* @param data string
* @description Create a new DatabaseRecord object and initialize it
* using either defaults or any provided key/value pairs in the JSON
* encoded string `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.
*
* @signature init([data])
* @param data PlainObject
* @description Initialize this object using either defaults or any
* provided key/value pairs in the plain object `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:
*
* @signature myProperty()
* @returns mixed
* @description Get the value of the property.
*
* @signature myProperty(value)
* @param value mixed
* @throws TypeError if `value` is not of the correct javascript data
* type for myProperty
* @returns this
* @description Set the value of the property, throwing an error if the
* javascript data type does not match the configuration, this is how
* the strict typing is implemented. This signature returns `this` to
* allow for set call chaining.
*/
const record = new DatabaseRecord();
console.log(record);
```
In this short snippet, we have effectively created a new class named `DatabaseRecord`. The class
has a constructor, an initializer, and a getter/setter method for `id`. The constructor calls the
initializer (named `init`), which sets the value of `id` to the default for type **number**, being
zero in this case. You could also explicitly pass a default by adding a `default` key to the property,
should you so desire. We've also added a transform function that will take any value passed to the `id`
setter and apply parseInt() to it.
### Expected Output
```
DatabaseRecord { _id: 0 }
```
## MySQL Example w/ Extended Object
**Importing Note:** You must have a unique integer property named `id` to be able to use the MySQL
functionality of EZ Objects.
**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.
```javascript
const ezobjects = require('./index');
const ezobjects = require('ezobjects');
const fs = require('fs');

@@ -108,6 +61,3 @@ const moment = require('moment');

* Create a connection object for the MySQL database using our MySQL
* module async/await wrapper. Currently, using the ezobjects MySQL
* database is the only option, but future versions may seek to expand
* the option to other databases, or at least allow the standard mysql
* module to work.
* module async/await wrapper.
*/

@@ -138,5 +88,5 @@ const db = new ezobjects.MySQLConnection(configMySQL);

* property, but it has the MySQL configuration properties on `id`
* because it will be extended by a class that is linked to a MySQL
* table and therefore it will need the MySQL configuration of the
* `id` property.
* 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.
*/

@@ -204,52 +154,2 @@ ezobjects.createObject(configDatabaseRecord);

/**
* The User object has all of the signatures listed in the comments
* for the Basic Example above, but also has the following signatures
* added
* since it has a tableName defined:
*
* @signature delete(db)
* @param db MySQLConnection
* @description Delete the record in database `db`, table `tableName`,
* that has its `id` field equal to the `id` property of this object.
*
* @signature insert(db)
* @param db MySQLConnection
* @description Insert this object's property values into the database
* `db`, table `tableName`, and store the resulting insertId in the
* `id` property of this object.
*
* @signature load(db, id)
* @param db MySQLConnection
* @param id number The value of the `id` property of the record you
* wish to load
* @description Load the record in database `db`, table `tableName`,
* that has its `id` field equal to provided `id` parameter.
*
* @signature load(db, fieldValue)
* @param db MySQLConnection
* @param fieldValue mixed The value of the `stringSearchField`
* property of the record you wish to load
* @description Load the record in database `db`, table `tableName`,
* that has its `stringSearchField` field equal to provided `id`
* parameter. Here, the actual field name of `stringSearchField` is
* provided in the object configuration, see the more detailed
* specifications below.
*
* @signature load(url)
* @param url The URL of a back-end that provides JSON data compatible
* with this object's initializer
* @description Load the JSON-encoded data obtained from `url` using
* this object's initializer. This signature is useful only when your
* classes are standalone browserify'd and requires you to implement a
* backend at `url` that will output the JSON. This signature requires
* you have jQuery loaded prior to use.
*
* @signature update(db)
* @param db MySQLConnection
* @description Update the record in database `db`, table `tableName`,
* with its `id` field equal to the `id` property of this object, using
* this object's property values.
*/
/** Create new user, initializing with object passed to constructor */

@@ -267,13 +167,40 @@ const user = new User({

(async () => {
/** 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);
try {
/** Create table if it doesn't already exist */
await ezobjects.createTable(db, configUser);
/** Close database connection */
db.close();
/** 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();
}
})();

@@ -293,177 +220,147 @@ ```

_favoriteDay: 2018-01-01T06:00:00.000Z }
```
In this snippet, we've created two classes, DatabaseRecord and User. User extends DatabaseRecord and is also associated with
a MySQL table called `users`. Each property is given a JavaScript `type` and a MySQL `mysqlType`. Additional MySQL property
configuration options can be provided, which are outlined in more detail below. A BTREE index is also added on the lastName column
for faster searching by lastName. While not required, the moment library is used to help translate date formats between MySQL
and JavaScript.
Since the User class configuration provided a `tableName` property, it will automatically have additional methods created that are
not present in a basic EZ Object. The additional methods are delete(db), insert(db), load(db, id), and update(db). These methods can
be used to delete the MySQL record corresponding to the object, insert the object properties as a new MySQL record, load a MySQL record
into the object properties, or update an existing MySQL record using the object properties. Transforms can be used to validate or
manipulate the property values when they are get or set in the object, or when they are saved or loaded from the database.
## Various Uses of EZ Objects
### Constructor Default
```javascript
const a = new User();
console.log(a);
```
### Expected Output
```
User {
_id: 0,
_firstName: '',
_lastName: '',
_checkingBalance: 0,
_permissions: [],
_favoriteDay: null }
_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 }
```
### Using Initializer Object
## Basic EZ Object Method Signatures
```javascript
const b = new User({
id: 1,
firstName: 'Rich',
lastName: 'Lowe',
checkingBalance: 4.32,
permissions: [1, 3, 5],
favoriteDay: new Date('01-01-2018')
});
These are the object method signatures even the most basic of EZ Objects will have:
console.log(b);
```
### new DatabaseRecord([data])
* **Parameters:** data PlainObject (optional)
* **Description:** Create a new DatabaseRecord object and initialize it using either defaults or any provided key/value pairs in the plain object `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.
### Expected Output
### new DatabaseRecord([data])
* **Parameters:** data string (optional)
* **Description:** Create a new DatabaseRecord object and initialize it using either defaults or any provided key/value pairs in the JSON encoded string `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.
```
User {
_id: 1,
_firstName: 'Rich',
_lastName: 'Lowe',
_checkingBalance: 4.32,
_permissions: [ 1, 3, 5 ],
_favoriteDay: 2018-01-01T06:00:00.000Z }
```
### init([data])
* **Parameters:** data PlainObject
* **Description:** Initialize this object using either defaults or any provided key/value pairs in the plain object `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:
### Using Auto-generated Setters
### myProperty()
* **Returns:** mixed
* **Description:** Get the value of the property.
### @signature myProperty(value)
* **Parameters:** value mixed
* **Throws:** TypeError if `value` is not of the correct javascript data type for myProperty
* **Returns:** this
* **Description:** Set the value of the property, throwing an error if the javascript data type does not match the configuration, this is howthe strict typing is implemented. This signature returns `this` to allow for set call chaining.
```javascript
const c = new User();
## MySQL EZ Object Method Signatures
c.id(2);
c.firstName('Bert');
c.lastName('Reynolds');
c.checkingBalance(91425518.32);
c.permissions([1, 4]);
c.favoriteDay(new Date('06-01-2017'));
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:
console.log(c);
```
### delete(db)
* **Parameters:** db MySQLConnection
* **Description:** Delete the record in database `db`, table `tableName`, that has its `id` field equal to the `id` property of this object.
### Expected Output
### insert(db)
* **Parameters:** db MySQLConnection
* **Description:** Insert this object's property values into the database `db`, table `tableName`, and store the resulting insertId in the `id` property of this object.
```
User {
_id: 2,
_firstName: 'Bert',
_lastName: 'Reynolds',
_checkingBalance: 91425518.32,
_permissions: [ 1, 4 ],
_favoriteDay: 2017-06-01T05:00:00.000Z }
```
### load(db, id)
* **Parameters:** db MySQLConnection
* **Parameters:** id number The value of the `id` property of the record you wish to load
* **Description:** Load the record in database `db`, table `tableName`, that has its `id` field equal to provided `id` parameter.
### Using Auto-generated Getters
### load(db, fieldValue)
* **Parameters:** db MySQLConnection
* **Parameters:** fieldValue mixed The value of the `stringSearchField` property of the record you wish to load
* **Description:** Load the record in database `db`, table `tableName`, that has its `stringSearchField` field equal to provided `id` parameter. Here, the actual field name of `stringSearchField` is provided in the object configuration, see the more detailed specifications below.
```javascript
console.log(`ID: ${c.id()}`);
console.log(`First Name: ${c.firstName()}`);
console.log(`Last Name: ${c.lastName()}`);
console.log(`Checking Balance: $${c.checkingBalance()}`);
console.log(`Permissions: ${c.permissions().join(`, `)}`);
console.log(`Favorite Day: ${c.favoriteDay().toString()}`);
```
### load(url)
* **Parameters:** url The URL of a back-end that provides JSON data compatible with this object's initializer
* **Description:** Load the JSON-encoded data obtained from `url` using this object's initializer.
* **Note:** This signature is useful only when your classes are standalone browserify'd and requires you to implement a
* backend at `url` that will output the JSON. This signature also requires you have jQuery loaded prior to use.
### Expected Output
### update(db)
* **Parameters:** db MySQLConnection
* **Description:** Update the record in database `db`, table `tableName`, with its `id` field equal to the `id` property of this object, using this object's property values.
```
ID: 2
First Name: Bert
Last Name: Reynolds
Checking Balance: $91425518.32
Permissions: 1, 4
Favorite Day: Thu Jun 01 2017 00:00:00 GMT-0500 (CDT)
```
## Module Exports
#### See example.js and example-mysql.js for more!
The EZ Objects module exports two functions and a MySQL class object:
## Module Specification
### ezobjects.createTable(db, obj)**
Creates a MySQL table corresponding to the configuration outlined in `obj`, if it doesn't already exist
### The module has three exports:
### ezobjects.createObject(obj)**
Creates an ES6 class corresponding to the configuration outlined in `obj`, with constructor, initializer, getters, setters, and also delete, insert, load, and update if `tableName` is configured
**ezobjects.createTable(db, obj)**
* Creates a MySQL table corresponding to the configuration outlined in `obj`, if it doesn't already exist
### ezobjects.MySQLConnection(config)**
A MySQL database connection wrapper that uses the standard mysql package and wraps it with async/await and transaction helpers
**ezobjects.createObject(obj)**
* Creates an ES6 class corresponding to the configuration outlined in `obj`, with constructor, initializer, getters, setters, and also delete, insert, load, and update if `tableName` is configured
## Configuration Specifications
**ezobjects.MySQLConnection(config)**
* A MySQL database connection wrapper that uses the standard mysql package and wraps it with async/await and transaction helpers
See the following for how to configure your EZ Objects:
### An object configuration can have the following:
* tableName - string - (optional) Provide if object should be linked with MySQL database table
* className - string - (required) Name of the class
* extends - object - (optional) The object that the new object should be extended from [required to extend object]
* extendsConfig - object - (optional) The EZ Object configuration for the object that is being extended from [required to extend object]
* stringSearchField - string (optional) The name of a unique property of type `string` that you want to be able to load with as an alternative to `id`
* properties - Array - (required) An array of properties that the object (and MySQL table, if applicable) should contain
* indexes - Array - (optional) An array of indexes that should be created in the MySQL table, if applicable
* `tableName` - `string` - (optional) Provide if object should be linked with MySQL database table
* `className` - `string` - (required) Name of the class
* `extends` - `mixed` - (optional) The object that the new object should be extended from [required to extend object]
* `extendsConfig` - `object` - (optional) The EZ Object configuration for the object that is being extended from [required to extend object]
* `stringSearchField` - `string` - (optional) The name of a unique property of type `string` that you want to be able to load with as an alternative to `id`
* `properties` - `Array` - (required) An array of properties that the object (and MySQL table, if applicable) should contain
* `indexes` - `Array` - (optional) An array of indexes that should be created in the MySQL table, if applicable
### A property configuration can have the following:
* name - string - (required) Name of the property, must conform to both JavaScript and MySQL rules
* type - string - (required) JavaScript data type for the property
* mysqlType - string - (optional) MySQL data type for the property [required for MySQL table association]
* length - number - (optional) MySQL data length for the property [required for MySQL table association on some data types like VARCHAR]
* decimals - number - (optional) Number of decimals that should be provided for certain data types when SELECT'ed from the MySQL table
* primary - boolean - (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]
* unique - boolean - (optional) Indicates the property is a UNIQUE KEY in the MySQL table
* null - boolean - (optional) Indicates the property can be NULL [default is properties must be NOT NULL]
* default - mixed - (optional) Sets the default value for the property in the class object
* mysqlDefault - mixed - (optional) Sets the default value for the property in the MySQL table, assuming its of the correct type
* unsigned - boolean - (optional) Indicates the property should be unsigned in the MySQL table
* zerofill - boolean - (optional) Indicates the property should be zero-filled in the MySQL table
* comment - string - (optional) Indicates the property should note the provided comment in the MySQL table
* charsetName - string - (optional) Indicates the property should use the provided charset in the MySQL table
* collationName - string - (optional) Indicates the property should use the provided collation in the MySQL table
* autoIncrement - boolean - (optional) Indicates the property should be auto-incremented in the MySQL table
* getTransform - function - (optional) Function that transforms and returns the property value prior to getting
* setTransform - function - (optional) Function that transforms and returns the property value prior to setting
* saveTransform - function - (optional) Function that transforms and returns the property value prior to saving in the database
* loadTransform - function - (optional) Function that transforms and returns the property value after loading from the database
* `name` - `string` - (required) Name of the property, must conform to both JavaScript and MySQL rules
* `type` - `string` - (required) JavaScript data type for the property
* `mysqlType` - `string` - (optional) MySQL data type for the property [required for MySQL table association]
* `length` - `number` - (optional) MySQL data length for the property [required for MySQL table association on some data types like VARCHAR]
* `decimals` - `number` - (optional) Number of decimals that should be provided for certain data types when SELECT'ed from the MySQL table
* `primary` - `boolean` - (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]
* `unique` - `boolean` - (optional) Indicates the property is a UNIQUE KEY in the MySQL table
* `null` - `boolean` - (optional) Indicates the property can be NULL [default is properties must be NOT NULL]
* `default` - `mixed` - (optional) Sets the default value for the property in the class object
* `mysqlDefault` - `mixed` - (optional) Sets the default value for the property in the MySQL table, assuming its of the correct type
* `unsigned` - `boolean` - (optional) Indicates the property should be unsigned in the MySQL table
* `zerofill` - `boolean` - (optional) Indicates the property should be zero-filled in the MySQL table
* `comment` - `string` - (optional) Indicates the property should note the provided comment in the MySQL table
* `charsetName` - `string` - (optional) Indicates the property should use the provided charset in the MySQL table
* `collationName` - `string` - (optional) Indicates the property should use the provided collation in the MySQL table
* `autoIncrement` - `boolean` - (optional) Indicates the property should be auto-incremented in the MySQL table
* `getTransform` - `function` - (optional) Function that transforms and returns the property value prior to getting
* `setTransform` - `function` - (optional) Function that transforms and returns the property value prior to setting
* `saveTransform` - `function` - (optional) Function that transforms and returns the property value prior to saving in the database
* `loadTransform` - `function` - (optional) Function that transforms and returns the property value after loading from the database
### An index configuration can have the following (for MySQL table association only):
* name - string - (required) Name of the index, can be arbitrary, but must be unique and not PRIMARY
* type - string - (optional) Index type, can be BTREE or HASH, defaults to BTREE
* keyBlockSize - number - (optional) Indicates the index should use the provided key block size
* withParser - string - (optional) Indicates the index should use the provided parser
* visible - boolean - (optional) Indicates the index should be visible
* invisible - boolean - (optional) Indicates the index should be invisible
* `name` - `string` - (required) Name of the index, can be arbitrary, but must be unique and not PRIMARY
* `type` - `string` - (optional) Index type, can be BTREE or HASH, defaults to BTREE
* `keyBlockSize` - `number` - (optional) Indicates the index should use the provided key block size
* `withParser` - `string` - (optional) Indicates the index should use the provided parser
* `visible` - `boolean` - (optional) Indicates the index should be visible
* `invisible` - `boolean` - (optional) Indicates the index should be invisible
### Default intiailizations for different JavaScript types
* number - 0
* string - ''
* boolean - false
* Array - []
* `number` - 0
* `string` - ''
* `boolean` - false
* `Array` - []
* anything else - null

Sorry, the diff of this file is not supported yet

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