EZ Objects
We're just getting started, check back later.
Example
const table = {
tableName: 'people',
className: 'Person',
fields: [
{
name: 'id',
type: 'int',
default: -1
},
{
name: 'firstName',
type: 'string'
},
{
name: 'lastName',
type: 'string'
},
{
name: 'checkingBalance',
type: 'float'
},
{
name: 'permissions',
type: 'Array'
},
{
name: 'favoriteDay',
type: 'Date'
}
]
};
function createObject(table) {
let parent;
if ( typeof window !== 'undefined' )
parent = window;
else
parent = global;
parent[table.className] = class {
constructor(data = {}) {
table.fields.forEach((col) => {
if ( col.type == 'int' ) {
this[col.name] = (arg) => {
if ( arg === undefined )
return this[`_${col.name}`];
else if ( typeof arg == 'number' )
this[`_${col.name}`] = parseInt(arg);
else
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`);
return this;
};
}
else if ( col.type == 'float' ) {
this[col.name] = (arg) => {
if ( arg === undefined )
return this[`_${col.name}`];
else if ( typeof arg == 'number' )
this[`_${col.name}`] = parseFloat(arg);
else
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`);
return this;
};
}
else if ( col.type == 'string' ) {
this[col.name] = (arg) => {
if ( arg === undefined )
return this[`_${col.name}`];
else if ( typeof arg == 'string' )
this[`_${col.name}`] = arg.toString();
else
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`);
return this;
};
}
else if ( col.type == 'Array' ) {
this[col.name] = (arg) => {
if ( arg === undefined )
return this[`_${col.name}`];
else if ( typeof arg == 'object' && arg.constructor.name == col.type )
this[`_${col.name}`] = arg;
else
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`);
return this;
};
}
else {
this[col.name] = (arg) => {
if ( arg === undefined )
return this[`_${col.name}`];
else if ( arg === null || ( typeof arg == 'object' && arg.constructor.name == col.type ) )
this[`_${col.name}`] = arg;
else
throw new Error(`${table.className}.${col.name}(${typeof arg}): Invalid signature.`);
return this;
};
}
});
table.fields.forEach((col) => {
if ( col.type == 'int' || col.type == 'float' )
this[col.name](data[col.name] || col.default || 0);
else if ( col.type == 'string' )
this[col.name](data[col.name] || col.default || '');
else if ( col.type == 'Array' )
this[col.name](data[col.name] || col.default || []);
else
this[col.name](data[col.name] || col.default || null);
});
}
}
Object.defineProperty(parent[table.className], 'name', {value: table.name});
}
createObject(table);
const a = new Person();
console.log(a);
const b = new Person({
id: 1,
firstName: 'Rich',
lastName: 'Lowe',
checkingBalance: 4.87,
permissions: [1, 2, 3],
favoriteDay: new Date('01-01-2018')
});
console.log(b);
Output
{ id: [Function],
firstName: [Function],
lastName: [Function],
checkingBalance: [Function],
permissions: [Function],
favoriteDay: [Function],
_id: -1,
_firstName: '',
_lastName: '',
_checkingBalance: 0,
_permissions: [],
_favoriteDay: null }
{ id: [Function],
firstName: [Function],
lastName: [Function],
checkingBalance: [Function],
permissions: [Function],
favoriteDay: [Function],
_id: 1,
_firstName: 'Rich',
_lastName: 'Lowe',
_checkingBalance: 4.87,
_permissions: [ 1, 2, 3 ],
_favoriteDay: 2018-01-01T06:00:00.000Z }