Netsuite - Suitescript 2.X: Inbound shipment "items" sublist update - netsuite

I need to update a sublist ('items') field ('receivinglocation') on inbound shipment entry.
When i create an inbound shipment from user interface, i need to update all item line locations from a body field ('customlocation').
I've tried to do that throught a Clientscript:
/**
* #NApiVersion 2.x
* #NScriptType ClientScript
* #NModuleScope SameAccount
*/
define(['N/currentRecord'],
/**
* #param{currentRecord} currentRecord
*/
function(currentRecord) {
/**
* Validation function to be executed when record is saved.
*
* #param {Object} scriptContext
* #param {Record} scriptContext.currentRecord - Current form record
* #returns {boolean} Return true if record is valid
*
* #since 2015.2
*/
function saveRecord(scriptContext) {
try {
var lines = scriptContext.currentRecord.getLineCount({sublistId: 'items'});
var mainLocationValue = scriptContext.currentRecord.getValue({
fieldId: 'customlocation'
});
if (mainLocationValue != '' && mainLocationValue != null) {
for (var i=0; i<lines; i++){
scriptContext.currentRecord.selectLine({sublistId: 'items',line: i});
scriptContext.currentRecord.setCurrentSublistValue({sublistId: 'items',fieldId: 'receivinglocation',value: mainLocationValue,ignoreFieldChange: true});
scriptContext.currentRecord.commitLine({sublistId: 'items'});
}
}
}
catch (e) {
log.debug({
title: 'Error Details',
details: e
})
}
}
return {
saveRecord: saveRecord
};
});
But the error is always the same from the validation form:
"FAILED_FORM_VALIDATION" - "Form validation failed. You cannot submit this record"

Not sure if this is where your error is coming from, but you do have to return true from the saveRecord() function to allow NetSuite to save the record. You can see a note to this effect directly in the JSDoc section of the script you posted:
#returns {boolean} Return true if record is valid

Related

How to set a sublist field value from a form field on edit

I have a Client Script that is currently working that sets the Service Item of a time tracking sublist line from the 'item' field on a Case Record. This happens on lineInit and works when I add a new line.
The issue I have is I would like it to work on the first line. When the record loads there is a line already populated with some data, but does not have the Service Item filled in.
If anyone could point me in the right direction, I would appreciate it. could this work on pageInit, or fieldChanged - like have the service item update after I update another line field?
/**
* #NApiVersion 2.x
* #NScriptType ClientScript
* #NModuleScope SameAccount
*/
define([],
function() {
/**
* Function to be executed when field is slaved.
*
* #param {Object} scriptContext
* #param {Record} scriptContext.currentRecord - Current form record
* #param {string} scriptContext.sublistId - Sublist name
* #param {string} scriptContext.fieldId - Field name
*
* #since 2015.2
*/
function lineInit(scriptContext) {
if (scriptContext.sublistId !== 'timeitem'){
console.log('test3');
return;
}
/* {N/currentRecord.CurrentRecord} */
var rec = scriptContext.currentRecord;
/* {string} The Service Item in the Body, if any */
var bodyItem = rec.getValue({fieldId: 'item'});
/* {string} The class that has been set at the line level, if any */
var lineServiceItem = rec.getCurrentSublistValue({
sublistId: 'timeitem',
fieldId: 'item'
});
/* IF there IS a value at the body level, and there is NOT a value at the line */
if (bodyItem && !lineServiceItem) {
console.log('test4');
/* Set the line value to the body value */
rec.setCurrentSublistValue({
sublistId: 'timeitem',
fieldId: 'item',
value: bodyItem
});
}
}
return {
lineInit: lineInit
};
});
The first line (or current line in edit mode) will have to be populated during pageInit.
function setLineItem(context, isPageInit) {
if (isPageInit || (context.sublistId === 'timeitem')) {
// set value
}
}
function pageInit(context) {
setLineItem(context, true);
}
function lineInit(context) {
setLineItem(context);
}

Changing a field value on beforesubmit

I've looked all over but can't seem to find how to modify an existing record using Suitescript. Essentially I want to be able to set a field on the record that sets the value of a boolean field based on the value of a different boolean field in the same record
This is my code.
/**
* #NApiVersion 2.0
* #NScriptType UserEventScript
* #NModuleScope Public
*/
var exports = {};
function beforeSubmit(scriptContext) {
if (Type = "Expense Report"){
if (CCC = 1){
record.setFieldValue(Corporate card, 1);
nlapiSubmitRecord(record);
}}
}
exports.beforeSubmit = beforeSubmit;
});

Where to store my node-schedules

I'm very new to Node/Express and I'm making an appointment system. I want my users to make an appointment for the day they want and my system sends them notification on that exact time. I found "node-schedule" module which is really good for this task but I don't know where to implemented this. Is there anyway to store all tasks in my app.js or is it enough to just create a node-schedule task every time I hit certain end point for example:
router.get('/', function(req, res, next) {
var j = schedule.scheduleJob(date, function(){
send notification();
});
res.send(200);
}
Note: I do not want to run a constant for loop on my sql table to check for dates
You'll need to persist your application data to some form of permanent storage by either writing to local files using something like SQLite, running your own database server (like MongoDB) or using a cloud-based storage service like Amazon SimpleDb.
Each of these options (and many others) have npm modules you can use to read/write/delete persistent data. For examples, see MongoDb, SQLite3, and SimpleDb, all available using npm on npmjs.com.
UPDATE
Per your comment below: Well, you did ask where you could store your scheduled events. ;)
To persist all scheduled events so they survive possible server failure, you'd need to create a storable data structure to represent them and for each event, create a new instance of your representation and store it to your persistent storage (MySQL).
Typically, you'd use something along the lines of:
{
when:DateTime -- timestamp when the event should fire
what:Action -- what this event should do
args:Arguments -- arguments to pass to Action
pending:Boolean=true -- if false, this event has already fired
}
When you initialize your server, you would query your persistent storage for all events where pending===true and using the results, initialize instances of the node-schedule module.
When you need to schedule a new event while your server was running, you'd create a new event representation, write it into persistent storage and create a new instance of node-schedule using it.
Finally, and most importantly for customer happiness, when a scheduled event completes successfully, just before your event handler (the Action mentioned above) completes, it would need to mark the persistent version of the event it is handling as pending:false so you don't fire any event more than once.
So for example:
'use strict';
var scheduler = require('node-schedule');
/**
* Storable Representation of a Scheduled Event
*
* #param {string|Date} when
* #param {string} what
* #param {array.<string>} [args=[]]
* #param {boolean} [pending=true]
*
* #property {Date} PersistentEvent.when - the datetime this event should fire.
* #property {string} PersistentEvent.what - the name of the action to run (must match key of PersistentEvent.Actions)
* #property {array} PersistentEvent.args - args to pass to action event handler.
* #property {boolean} PersistentEvent.pending - if true, this event has not yet fired.
*
* #constructor
*
* #example
*
* var PersistentEvent = require('PersistentEvent'),
* mysql = require('mysql'),
* conn = mysql.createConnection({ ... });
*
* conn.connect();
*
* // at some point when initializing your app...
*
* // assign your persistent storage connection...
* PersistentEvent.setStore(conn);
*
* // load all pending event from persistent storage...
* PersistentEvent.loadAll$(function (err) {
* if (err) {
* throw new Error('failed to load all PersistentEvents: ' + err);
* }
*
* // from this point on, all persistent events are loaded and running.
*
* });
*/
var PersistentEvent = function (when, what, args, pending) {
// initialize
PersistentEvent.Cache.push(this.init({
when: when,
what: what,
args: args,
pending: pending
}));
};
// ==== PersistentEvent Static Methods ====
/**
* Pre-defined action event handlers.
* <p>
* Where the property key will be used to match the PersistentEvent.what property,
* and the property value is a event handler function that accepts an optional
* array of args and a callback (provided by PersistentEvent.prototype.schedule)
* </p>
*
* #property {object}
* #property {function} Actions.doSomething
* #property {function} Actions.doSomethingElse
*
* #static
*/
PersistentEvent.Actions = {
doSomething: function (args, cb) {
// defaults
args = args || [];
// TODO check specific args here ...
var result = true,
err = null;
// do your action here, possibly with passed args
cb(err, result);
},
doSomethingElse: function (args, cb) {
// defaults
args = args || [];
// TODO check specific args here ...
var result = true,
err = null;
// do your action here, possibly with passed args
cb(err, result);
}
};
/**
* Cache of all PersistentEvents
*
* #type {Array.<PersistentEvent>}
* #static
*/
PersistentEvent.Cache = [];
// Data Management
/**
* Connection to persistent storage.
* TODO - This should be abstracted to handle other engines that MySQL.
* #property {object}
* #static
*/
PersistentEvent.StorageConnection = null;
/**
* Sets the storage connection used to persist events.
*
* #param {object} storageConnection
* #static
*/
PersistentEvent.setStore = function (storageConnection) { // set the persistent storage connection
// TODO - check args here...
// Note: this function isn't really needed unless you're using other kinds of storage engines
// where you'd want to test what engine was used and mutate this interface accordingly.
PersistentEvent.StorageConnection = storageConnection;
};
/**
* Saves a PersistentEvent to StorageConnection.
*
* #param {PersistentEvent} event - event to save
* #param {function} cb - callback on complete
* #static
*/
PersistentEvent.save$ = function (event, cb) {
var conn = PersistentEvent.StorageConnection;
if (null === conn) {
throw new Error('requires a StorageConnection');
}
// TODO - check for active connection here...
// TODO - check args here...
conn.query('INSERT INTO TABLE when = :when, what = :what, args = :args, pending = :pending', event, cb);
};
/**
* Loads all PersistentEvents from StorageConnection.
* #param {function} cb -- callback on complete
* #static
*/
PersistentEvent.loadAll$ = function (cb) {
var conn = PersistentEvent.StorageConnection;
if (null === conn) {
throw new Error('requires a StorageConnection');
}
// check for active connection here...
// check args here...
conn.query('QUERY * FROM TABLE WHERE pending = true', function (err, results) {
if (err) {
return cb(err);
}
results.forEach(function (result) {
// TODO: check for existence of required fields here...
var event = new PersistentEvent(result.when, result.what, result.args, true);
event.schedule();
});
cb(null);
});
};
// ==== PersistentEvent Methods ====
/**
* Initialize an instance of PersistentEvent.
*
* #param {object} opts
* #return {PersistentEvent}
*/
Event.prototype.init = function (opts) {
// check args
if ('object' !== typeof opts) {
throw new Error('opts must be an object');
}
// set defaults
opts.args = opts.args || [];
opts.pending = opts.pending || true;
// convert string to Date, if required
if ('string' === typeof opts.when) {
opts.when = new Date(opts.when);
}
// check that opts contains needed properties
if (!opts.when instanceof Date) {
throw new Error('when must be a string representation of a Date or a Date object');
}
if ('string' !== typeof opts.what) {
throw new Error('what must be a string containing an action name');
}
if (!Array.isArray(opts.args)) {
throw new Error('args must be an array');
}
if ('boolean' !== typeof opts.pending) {
throw new Error('pending must be a boolean');
}
// set our properties
var self = this;
Object.keys(opts).forEach(function (key) {
if (opts.hasOwnProperty(key)) {
self = opts[key];
}
});
return this;
};
/**
* Override for Object.toString()
* #returns {string}
*/
PersistentEvent.prototype.toString = function () {
return JSON.stringify(this);
};
/**
* Schedule the event to run.<br/>
* <em>Side-effect: saves event to persistent storage.</em>
*/
PersistentEvent.prototype.schedule = function () {
var self = this,
handler = Actions[this.what];
if ('function' !== typeof handler) {
throw new Error('no handler found for action:' + this.what);
}
PersistentEvent.save$(self, function () {
self._event = scheduler.scheduleJob(self.when, function () {
handler(self.args, function (err, result) {
if (err) {
console.error('event ' + self + ' failed:' + err);
}
self.setComplete();
});
});
});
};
/**
* Sets this event complete.<br/>
* <em>Side-effect: saves event to persistent storage.</em>
*/
PersistentEvent.prototype.setComplete = function () {
var self = this;
delete this._event;
this.pending = false;
PersistentEvent.save$(this, function (err) {
if (err) {
console.error('failed to save event ' + self + ' :' + err);
}
});
};
Note that is a first-pass boilerplate to show you one way of designing a solution to your problem. It will require further effort on your part to run.
You can have a cron job every morning that will pick all the appointments for that day and schedule pushes for them. This way you'll have to query the DB once that too on the time when the server load is minimal.

Excluding all categories except one from indexing - joomla smart search

I would like to have joomla automatically re-index all the articles that are in a certain category (and subcategory) without indexing the rest. I am not looking for a cron job. I want joomla to do this when I hit the indexing button.
In other words, all the articles that are not in that particular category or its child subcategories should not be indexed and thus not show up in the search results.
EDIT (more info): This is for a specific site where I know the category id(s). I should probably also add that it is a bilingual site, the search module is appearing and indexing twice on the same page for each language. I.e. it indexes and appears on the English 'blog' page and the German 'blog' page.
Is this possible or do I need to manually delete the indexed files I don't want to show up?
EDIT: The discover tool is not working, here is the xml file to try and find out why.
<?xml version="1.0" encoding="utf-8"?>
<extension version="3.1" type="plugin" group="finder" method="upgrade">
<name>plg_finder_content</name>
<author>Joomla! Project</author>
<creationDate>August 2011</creationDate>
<copyright>(C) 2005 - 2014 Open Source Matters. All rights reserved.</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<authorEmail>admin#joomla.org</authorEmail>
<authorUrl>www.joomla.org</authorUrl>
<version>3.0.0</version>
<description>PLG_FINDER_CONTENT_XML_DESCRIPTION</description>
<scriptfile>script.php</scriptfile>
<files>
<file plugin="content">article_selectedcategories.php</file>
<filename>index.html</filename>
</files>
<languages>
<language tag="en-GB">language/en-GB/en-GB.plg_finder_content.ini</language>
<language tag="en-GB">language/en-GB/en-GB.plg_finder_content.sys.ini</language>
</languages>
</extension>
EDIT The code from the php to find out why it indexes everything and not just category 31.
<?php
/**
* #package Joomla.Plugin
* #subpackage Finder.Content
*
* #copyright Copyright (C) 2005 - 2014 Open Source Matters, Inc. All rights reserved.
* #license GNU General Public License version 2 or later; see LICENSE
*/
defined('JPATH_BASE') or die;
require_once JPATH_ADMINISTRATOR . '/components/com_finder/helpers/indexer/adapter.php';
/**
* Smart Search adapter for com_content.
*
* #package Joomla.Plugin
* #subpackage Finder.Content
* #since 2.5
*/
class PlgFinderContent extends FinderIndexerAdapter
{
/**
* The plugin identifier.
*
* #var string
* #since 2.5
*/
protected $context = 'Content';
/**
* The extension name.
*
* #var string
* #since 2.5
*/
protected $extension = 'com_content';
/**
* The sublayout to use when rendering the results.
*
* #var string
* #since 2.5
*/
protected $layout = 'article';
/**
* The type of content that the adapter indexes.
*
* #var string
* #since 2.5
*/
protected $type_title = 'Article';
/**
* The table name.
*
* #var string
* #since 2.5
*/
protected $table = '#__content';
/**
* Load the language file on instantiation.
*
* #var boolean
* #since 3.1
*/
protected $autoloadLanguage = true;
/**
* Method to update the item link information when the item category is
* changed. This is fired when the item category is published or unpublished
* from the list view.
*
* #param string $extension The extension whose category has been updated.
* #param array $pks A list of primary key ids of the content that has changed state.
* #param integer $value The value of the state that the content has been changed to.
*
* #return void
*
* #since 2.5
*/
public function onFinderCategoryChangeState($extension, $pks, $value)
{
// Make sure we're handling com_content categories.
if ($extension == 'com_content')
{
$this->categoryStateChange($pks, $value);
}
}
/**
* Method to remove the link information for items that have been deleted.
*
* #param string $context The context of the action being performed.
* #param JTable $table A JTable object containing the record to be deleted
*
* #return boolean True on success.
*
* #since 2.5
* #throws Exception on database error.
*/
public function onFinderAfterDelete($context, $table)
{
if ($context == 'com_content.article')
{
$id = $table->id;
}
elseif ($context == 'com_finder.index')
{
$id = $table->link_id;
}
else
{
return true;
}
// Remove item from the index.
return $this->remove($id);
}
/**
* Smart Search after save content method.
* Reindexes the link information for an article that has been saved.
* It also makes adjustments if the access level of an item or the
* category to which it belongs has changed.
*
* #param string $context The context of the content passed to the plugin.
* #param JTable $row A JTable object.
* #param boolean $isNew True if the content has just been created.
*
* #return boolean True on success.
*
* #since 2.5
* #throws Exception on database error.
*/
public function onFinderAfterSave($context, $row, $isNew)
{
// We only want to handle articles here.
if ($context == 'com_content.article' || $context == 'com_content.form')
{
// Check if the access levels are different.
if (!$isNew && $this->old_access != $row->access)
{
// Process the change.
$this->itemAccessChange($row);
}
// Reindex the item.
$this->reindex($row->id);
}
// Check for access changes in the category.
if ($context == 'com_categories.category')
{
// Check if the access levels are different.
if (!$isNew && $this->old_cataccess != $row->access)
{
$this->categoryAccessChange($row);
}
}
return true;
}
/**
* Smart Search before content save method.
* This event is fired before the data is actually saved.
*
* #param string $context The context of the content passed to the plugin.
* #param JTable $row A JTable object.
* #param boolean $isNew If the content is just about to be created.
*
* #return boolean True on success.
*
* #since 2.5
* #throws Exception on database error.
*/
public function onFinderBeforeSave($context, $row, $isNew)
{
// We only want to handle articles here.
if ($context == 'com_content.article' || $context == 'com_content.form')
{
// Query the database for the old access level if the item isn't new.
if (!$isNew)
{
$this->checkItemAccess($row);
}
}
// Check for access levels from the category.
if ($context == 'com_categories.category')
{
// Query the database for the old access level if the item isn't new.
if (!$isNew)
{
$this->checkCategoryAccess($row);
}
}
return true;
}
/**
* Method to update the link information for items that have been changed
* from outside the edit screen. This is fired when the item is published,
* unpublished, archived, or unarchived from the list view.
*
* #param string $context The context for the content passed to the plugin.
* #param array $pks An array of primary key ids of the content that has changed state.
* #param integer $value The value of the state that the content has been changed to.
*
* #return void
*
* #since 2.5
*/
public function onFinderChangeState($context, $pks, $value)
{
// We only want to handle articles here.
if ($context == 'com_content.article' || $context == 'com_content.form')
{
$this->itemStateChange($pks, $value);
}
// Handle when the plugin is disabled.
if ($context == 'com_plugins.plugin' && $value === 0)
{
$this->pluginDisable($pks);
}
}
/**
* Method to index an item. The item must be a FinderIndexerResult object.
*
* #param FinderIndexerResult $item The item to index as an FinderIndexerResult object.
* #param string $format The item format. Not used.
*
* #return void
*
* #since 2.5
* #throws Exception on database error.
*/
protected function index(FinderIndexerResult $item, $format = 'html')
{
$item->setLanguage();
// Check if the extension is enabled.
if (JComponentHelper::isEnabled($this->extension) == false)
{
return;
}
// Initialise the item parameters.
$registry = new JRegistry;
$registry->loadString($item->params);
$item->params = JComponentHelper::getParams('com_content', true);
$item->params->merge($registry);
$registry = new JRegistry;
$registry->loadString($item->metadata);
$item->metadata = $registry;
// Trigger the onContentPrepare event.
$item->summary = FinderIndexerHelper::prepareContent($item->summary, $item->params);
$item->body = FinderIndexerHelper::prepareContent($item->body, $item->params);
// Build the necessary route and path information.
$item->url = $this->getURL($item->id, $this->extension, $this->layout);
$item->route = ContentHelperRoute::getArticleRoute($item->slug, $item->catslug, $item->language);
$item->path = FinderIndexerHelper::getContentPath($item->route);
// Get the menu title if it exists.
$title = $this->getItemMenuTitle($item->url);
// Adjust the title if necessary.
if (!empty($title) && $this->params->get('use_menu_title', true))
{
$item->title = $title;
}
// Add the meta-author.
$item->metaauthor = $item->metadata->get('author');
// Add the meta-data processing instructions.
$item->addInstruction(FinderIndexer::META_CONTEXT, 'metakey');
$item->addInstruction(FinderIndexer::META_CONTEXT, 'metadesc');
$item->addInstruction(FinderIndexer::META_CONTEXT, 'metaauthor');
$item->addInstruction(FinderIndexer::META_CONTEXT, 'author');
$item->addInstruction(FinderIndexer::META_CONTEXT, 'created_by_alias');
// Translate the state. Articles should only be published if the category is published.
$item->state = $this->translateState($item->state, $item->cat_state);
// Add the type taxonomy data.
$item->addTaxonomy('Type', 'Article');
// Add the author taxonomy data.
if (!empty($item->author) || !empty($item->created_by_alias))
{
$item->addTaxonomy('Author', !empty($item->created_by_alias) ? $item->created_by_alias : $item->author);
}
// Add the category taxonomy data.
$item->addTaxonomy('Category', $item->category, $item->cat_state, $item->cat_access);
// Add the language taxonomy data.
$item->addTaxonomy('Language', $item->language);
// Get content extras.
FinderIndexerHelper::getContentExtras($item);
// Index the item.
$this->indexer->index($item);
}
/**
* Method to setup the indexer to be run.
*
* #return boolean True on success.
*
* #since 2.5
*/
protected function setup()
{
// Load dependent classes.
include_once JPATH_SITE . '/components/com_content/helpers/route.php';
return true;
}
/**
* Method to get the SQL query used to retrieve the list of content items.
*
* #param mixed $query A JDatabaseQuery object or null.
*
* #return JDatabaseQuery A database object.
*
* #since 2.5
*/
protected function getListQuery($query = null)
{
$db = JFactory::getDbo();
// Check if we can use the supplied SQL query.
$query = $query instanceof JDatabaseQuery ? $query : $db->getQuery(true)
->select('a.id, a.title, a.alias, a.introtext AS summary, a.fulltext AS body')
->select('a.state, a.catid, a.created AS start_date, a.created_by')
->select('a.created_by_alias, a.modified, a.modified_by, a.attribs AS params')
->select('a.metakey, a.metadesc, a.metadata, a.language, a.access, a.version, a.ordering')
->select('a.publish_up AS publish_start_date, a.publish_down AS publish_end_date')
->select('c.title AS category, c.published AS cat_state, c.access AS cat_access');
$query->where('a.catid = 31');
// Handle the alias CASE WHEN portion of the query
$case_when_item_alias = ' CASE WHEN ';
$case_when_item_alias .= $query->charLength('a.alias', '!=', '0');
$case_when_item_alias .= ' THEN ';
$a_id = $query->castAsChar('a.id');
$case_when_item_alias .= $query->concatenate(array($a_id, 'a.alias'), ':');
$case_when_item_alias .= ' ELSE ';
$case_when_item_alias .= $a_id.' END as slug';
$query->select($case_when_item_alias);
$case_when_category_alias = ' CASE WHEN ';
$case_when_category_alias .= $query->charLength('c.alias', '!=', '0');
$case_when_category_alias .= ' THEN ';
$c_id = $query->castAsChar('c.id');
$case_when_category_alias .= $query->concatenate(array($c_id, 'c.alias'), ':');
$case_when_category_alias .= ' ELSE ';
$case_when_category_alias .= $c_id.' END as catslug';
$query->select($case_when_category_alias)
->select('u.name AS author')
->from('#__content AS a')
->join('LEFT', '#__categories AS c ON c.id = a.catid')
->join('LEFT', '#__users AS u ON u.id = a.created_by');
return $query;
}
}
I added the whole thing, just search for "where" to find the part that I changed. Everything else I did not touch.
Oh I think you are looking at the xml not the php.
// Check if we can use the supplied SQL query.
$query = $query instanceof JDatabaseQuery ? $query : $db->getQuery(true)
->select('a.id, a.title, a.alias, a.introtext AS summary, a.fulltext AS body')
->select('a.state, a.catid, a.created AS start_date, a.created_by')
->select('a.created_by_alias, a.modified, a.modified_by, a.attribs AS params')
->select('a.metakey, a.metadesc, a.metadata, a.language, a.access, a.version, a.ordering')
->select('a.publish_up AS publish_start_date, a.publish_down AS publish_end_date')
->select('c.title AS category, c.published AS cat_state, c.access AS cat_access');
If you want to exclude category 7 add
$query->where('a.catid <> 7');
after the last select.
Update ... or if you only want catgory 7
$query->where('a.catid = 7');
or if you only want category 7 and its children you would use IN ( comma separated list ).
Update
so this is what i'm getting for the generated query
SELECT a.id, a.title, a.alias, a.introtext AS summary, a.fulltext AS body,a.state, a.catid, a.created AS start_date, a.created_by,a.created_by_alias, a.modified, a.modified_by, a.attribs AS params,a.metakey, a.metadesc, a.metadata, a.language, a.access, a.version, a.ordering,a.publish_up AS publish_start_date, a.publish_down AS publish_end_date,c.title AS category, c.published AS cat_state, c.access AS cat_access, CASE WHEN CHAR_LENGTH(a.alias) != 0 THEN CONCAT_WS(':', a.id, a.alias) ELSE a.id END as slug, CASE WHEN CHAR_LENGTH(c.alias) != 0 THEN CONCAT_WS(':', c.id, c.alias) ELSE c.id END as catslug,u.name AS author
FROM a1pbr_content AS a
LEFT JOIN a1pbr_categories AS c ON c.id = a.catid
LEFT JOIN a1pbr_users AS u ON u.id = a.created_by
WHERE a.catid = 31
And that does give me just articles with catid of 31.
WHen you are indexing are you using the index button or doing it on save?

How to overload mongoose model instance in nodejs & Typescript

I'm trying to change the save() method, but I don't find where I can overload it. I use typescript and node.js.
For the moment, I have a UserModel that contains a mongoose.Schema and a mongoose.Model.
When I call UserModel.getModel() I retrieve the mongoose.Model from the UserModel.
I basically use a DAO to retrieve the Model class object.
user = message.getDataByKey('user');
user.save(function(err, data) {
// do stuff
});
I want to overload automatically the user object to use my own .save() method to check if there is error and always handle them by the same way.
When I set the Model, I do it like that:
public static model: any = model.Models.MongooseModel.getNewInstance(UserModel.modelName, UserModel._schema);
And in the parent:
public static getNewInstance(modelName, schema){
var Model: any = mongoose.model(modelName, schema);
// Overload methods.
//console.log(new Model());
// Return overloaded Model class.
return Model;
}
I would like to know if there is any way to overload the Model to make sure that each new instance from it will have my own .save method.
I thought use the statics/methods (methods actually, I guess) but it's empty or I know that the final object will have save/remove/update methods. So I don't know why it's not already into the object, I tried to console.log(Model and new Model()) but no save() method.
So I'm a little desappointed, maybe I missed something.
The fact is, I can't update directly the new Model() because they will be created later, in another context, I need to update the Model directly to make sure that the new instance from this model will have my extra function.
And I don't want to rewrite the basic .save() method, I just want to overload it to add extra validation.
Any idea? I'm kinda lost here, it's not that easy. Thx.
I found a solution to do so, I'm using typescript so I'll post both .ts and .js to everybody understand.
I use CommonJs compilation
Model.ts (Super model, parent of all models)
///<reference path='./../../lib/def/defLoader.d.ts'/>
/**
* Package that contains all Models used to interact with the database.
* TODO Use options http://mongoosejs.com/docs/guide.html
*/
export module Models {
/**
* Interface for all Models, except the parent class.
*/
export interface IModel{
/**
* Name of the model.
* It's a helper to always get the name, from instance or static.
* MUST start by uppercase letter!
*/
modelName: string;
/**
* Contains the static value of the public schema as object.
* It's a helper to always get the schema, from instance or static.
*/
schema: mongoose.Schema;
/**
* Contains the static value of the object used to manipulate an instance of the model.
* It's a helper to always get the model, from instance or static.
*/
model: any;
}
/**
* Parent class for all models.
* A model contains a mongoose schema and a mongoose model and other things.
*/
export class Model{
/**
* Suffix used to load automatically models.
*/
public static suffix: string = 'Model';
/**
* Suffix used to load automatically models.
* It's a helper to always get the schema, from instance or static.
*/
public suffix: string;
/**
* Name of the model.
* MUST start by uppercase letter!
*/
public static modelName: string = '';
/**
* Readable schema as object.
*/
public static schema: any;
/**
* Schema as mongoose Schema type.
*/
public static Schema: mongoose.Schema;
/**
* The mongoose model that uses the mongoose schema.
*/
public static model: any;
/**
* Use static values as instance values.
*/
constructor(){
// Use static values as instance values.
this.suffix = Model.suffix;
}
/**
* Returns a new mongoose.Schema customized instance.
* #param ChildModel Child model that made the call.
* #returns {*}
* #see http://mongoosejs.com/docs/2.7.x/docs/methods-statics.html
*/
public static getNewSchemaInstance(ChildModel): mongoose.Schema{
var schema: any = new mongoose.Schema(ChildModel.schema, {collection: ChildModel.modelName.toLowerCase()});
// Overload methods.
//schema.methods.toObject = function(callback){}
// Return overloaded instance.
return schema;
}
/**
* Retrieves a new Model instance and overload it to add statics methods available for all Models.
* #param ChildModel
* #returns {*}
* #see http://mongoosejs.com/docs/2.7.x/docs/methods-statics.html
*/
public static getNewModelInstance(ChildModel): any{
// Get the Model class.
var Model: any = mongoose.model(ChildModel.modelName, ChildModel.Schema);
/**
**************************************************************************************************
************************ Extended Model static methods for all Models ****************************
**************************************************************************************************
*/
/**
* Handler for all database/mongoose errors.
* #param err Error.
* #param data Data. Contains the model and the emitter. (+ more)
* #param callback Callback function to execute.
*/
Model.errorHandler = (err: any, data: any, callback: (message: (any) => any) => any) => {
// Extract data.
var _Model = data.model;
var __function = data.__function;
var __line = data.__line;
// Will contains the error.
var message:any = [];
// Mongo error.
if(err && err.name && err.name == 'MongoError'){
var _err = MongoError.parseMongoError(err);
if(err.code == 11000){
// Duplicate key on create.
message[0] = '__19';
message[1] = [_err.value, _err.field];
}else if(err.code == 11001){
// Duplicate key on update.
message[0] = '__20';
message[1] = [_err.value, _err.field];
}else{
// Non-managed mongo error.
if(dev()){
// Return not only the message but also some information about the error.
message[0] = [];
// Message. [0][1] could be args.
message[0][0] = '__21';
// Data.
message[1] = {
err: err,
model: _Model.modelName
};
}else{
message = '__21';
}
}
fs.appendFile(__config.path.base + __config.mongo.error.log, new Date() + ': ' + JSON.stringify({error: err, model: _Model.modelName, _err: _err}) + '\n');
}else if(err && err.name && err.name == 'ValidationError'){
// Validation error from mongoose.
var _err = MongoError.parseValidationError(err);
message[0] = [];
// Message. [0][1] could be args.
message[0][0] = '__24';
message[0][1] = [_err[0].value, _err[0].field, _err[0].type];
if(dev()){
// Will be send as args but not displayed in the message.
message[1] = {
err: _err,
model: _Model.modelName
};
}
fs.appendFile(__config.path.base + __config.mongo.error.log, new Date() + ': ' + JSON.stringify({error: err, model: _Model.modelName, _err: _err}) + '\n');
}else{
// Another error? I don't know if that could happens, but manage it anyway.
message[0] = '__22';
if(dev()){
message[1] = [err, _Model.modelName];// Will be send as args but not displayed in the message.
}
fs.appendFile(__config.path.base + __config.mongo.error.log, new Date() + ': ' + JSON.stringify({error: err, model: _Model.modelName}) + '\n');
}
callback(message);// return an error.
};
/**
* Check if the object exists and returns it in this case.
* #param object Object to find.
* #param callback Callback to execute.
* #return
* err Error if it happens. [null]
* found Found object or false.
*/
Model.exists = (object, callback): any => {
// If object is null or false or empty or whatever, don't do the research, the result could be wrong!
if(!object){
callback (null, false);
}else{
Model.findOne(object, function (err, found) {
if (err){
Model.errorHandler(err, ChildModel, callback);
}else if (found){
callback(null, found);
}else{
callback (null, false);
}
});
}
};
// Return overloaded instance.
return Model;
}
}
/**
* Class that manage MongoDb errors, used statically.
*/
export class MongoError{
/**
* Parse a mongo error to returns data from it because Mongo returns really bad errors.
* #param err The mongo error.
* #returns {*}
*/
public static parseMongoError(err): any{
var _err: any = {};
var _message: string = err.err;
if(err.code == 11000 || err.code == 11001){
var message = _message.split(':');
// Get the table where the error was generated.
_err.table = message[1].split('.')[1];
// Get the field name where the error was generated.
_err.field = message[1].split('.')[2].split(' ')[0].replace('$', '');
_err.field = _err.field.substr(0, _err.field.lastIndexOf('_'));
// Get the
_err.value = message[3].split('"')[1].replace('\\', '');
}
return _err;
}
/**
* Parse a mongoose validation error, probably generated during a save/update function.
* #param err The mongoose error.
* #returns {*}
*/
public static parseValidationError(err): any{
var _errors: any = new Array();
var i = 0;
for(var error in err.errors){
_errors[i] = [];
_errors[i]['field'] = err.errors[error]['path'];
_errors[i]['value'] = err.errors[error]['value'];
_errors[i]['type'] = err.errors[error]['type'];
i++;
}
return _errors;
}
}
}
The JS version:
http://pastebin.com/xBTr1ZVe
Error messages (__21, etc.) are:
"__19": "Unable to add the element, the value **_$0** for the field _$1 already exists, it cannot be duplicated.",
"__20": "Unable to update the element, the value **_$0** for the field _$1 already exists, it cannot be duplicated.",
"__21": "Unable to execute the requested operation. Database error 21. Please report to an administrator.",
"__22": "Unable to execute the requested operation. Database error 22. Please report to an administrator.",
"__23": "Validation error, the requested operation was aborted. Please check that all your data are correct and retry. Please report to an administrator. (code 23)",
"__24": "Unable to perform the operation, the value ***_$0** for the field _$1 didn't pass successfully the validation. Error: _$2",
Basically all my models should manage by themself these exception, of course. But if I forgot to do it, I'll get a managed exception, better.
Now I'll post a real Model, UserModel inheriting the parent Model.
///<reference path='./../../lib/def/defLoader.d.ts'/>
import model = require('./Model');
export module Models {
/**
* Model used to manage users.
* The model is primary static, but, to make it easy to use, some things are also stored for each instance.
* That allows the code to use both Model or instance of Model such as:
* Model.schema
* model.Schema
*/
export class UserModel extends model.Models.Model implements model.Models.IModel{
/**
*************************************************************************************************
****************************** Public methods & attributes **************************************
*************************************************************************************************
*/
/**
* Name of the model.
* MUST start by uppercase letter!
*/
public static modelName: string = 'User';
/**
* Readable schema as object.
*/
public static schema: any = require('../schemas/userSchema.js');
/**
* Schema as mongoose Schema type.
*/
public static Schema: mongoose.Schema = model.Models.Model.getNewSchemaInstance(UserModel);
/**
* The mongoose Model that uses the mongoose schema.
*/
public static model: any = model.Models.Model.getNewModelInstance(UserModel);
/**
* Helpers to always get the property, from instance or static.
*/
public modelName: string = UserModel.modelName;
public schema: mongoose.Schema = UserModel.schema;
public model: mongoose.Model<any> = UserModel.model;
/**
*************************************************************************************************
***************************** Extended methods & attributes **************************************
*************************************************************************************************
*/
/**
* These fields are protected, the user password is required to access to them.
* These fields are basically shared between applications.
* #private
*/
private static _protectedFields: string[] = [
'login',
'email'
];
/**
* Method to use to hash the user password.
*/
private static _passwordHashMethod: string = 'sha256';
/**
* Digest to use to hash the user password.
*/
private static _passwordDigest: string = 'hex';
/**
* Returns the protected fields.
* #returns {string[]}
*/
public static getProtectedFields(): string[]{
return this._protectedFields;
}
/**
* Hash a user password depending on the password hash configuration. Currently SHA256 in hexadecimal.
* Assuming crypto is global.
* #param password User password.
* #returns {string} Hashed password.
*/
public static hashPassword(password: string): string{
return crypto
.createHash(UserModel._passwordHashMethod)
.update(password)
.digest(UserModel._passwordDigest)
}
}
/**
* Don't forget that some methods such as exists() are written in the Model class and available for all Models.
* The following methods belong ONLY to the mongoose model instance, not to the Model class itself!
*
*************************************************************************************************
******************************** Extended Model methods *****************************************
*************************************************************************************************
*/
/**
* Connect a user to the game.
* #param user User to check. {}
* #param callback Callback to execute.
*/
UserModel.model.checkAuthentication = (user, callback) => {
// Force to provide login and password.
UserModel.model.exists({login: user.login, password: UserModel.hashPassword(user.password)}, function(err, userFound){
// Load public profile.
UserModel.model._getProtectedInformation(userFound, function(userPublic){
// Provides only public fields.
callback(new __message("__17", {err: err, user: userPublic}, !err && userFound ? true: false));
});
});
};
/**
* Get the protected fields for the found user.
* #param user User to find.
* #param callback Callback to execute.
*/
UserModel.model.getProtectedInformation = (user, callback) => {
// We are looking for an unique user.
UserModel.model.exists(user, function(err, userFound){
if(err){
UserModel.model.errorHandler(err, UserModel, callback);
}else{
// Load public profile.
UserModel.model._getProtectedInformation(userFound, function(userPublic){
// Provides only public fields.
callback(new __message('', {err: err, user: userPublic}, err ? false: true));
});
}
});
};
/**
* Get the protected fields of a user.
* #param user Instance of model.
* #param callback Callback to execute.
* #private
*/
UserModel.model.hashPassword = (user, callback): any => {
var err = false;
if(user && user.password){
user.password = UserModel.hashPassword(user.password);
}else{
err = true;
}
callback(new __message(err ? '__18': '', {user: user}, err ? false: true));
};
/**
*************************************************************************************************
*************************** Methods to use only locally (private) *******************************
*************************************************************************************************
*/
/**
* Get the protected fields of a user.
* #param user Instance of model.
* #param callback Callback to execute.
* #private
*/
UserModel.model._getProtectedInformation = (user, callback): any => {
var userPublic = {};
// Get fields to share.
var publicFields = UserModel.getProtectedFields();
// Fill the userPublic var with public fields only.
for(var field in publicFields){
userPublic[publicFields[field]] = user[publicFields[field]];
}
callback(userPublic);
};
}
The JS version:
http://pastebin.com/0hiaMH25
The schema:
/**
* Schema ued to create a user.
* #see http://mongoosejs.com/docs/2.7.x/docs/schematypes.html
*/
module.exports = userSchema = {
/**
* User Login, used as id to connect between all our platforms.
*/
login: {
type: 'string',
//match: /^[a-zA-Z0-9_-]{'+userSchema.login.check.minLength+','+userSchema.login.check.maxLength+'}$/,
trim: true,
required: true,
notEmpty: true,
unique: true,
check: {
minLength: 4,
maxLength: 16
}
},
/**
* User email.
*/
email: {
type: 'string',
lowercase: true,
match: /^[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/,
required: true,
notEmpty: true,
unique: true,
check: {
minLength: 6,
maxLength: 30
}
},
/**
* User private password, the one hashed in SHA512 and stored on the database.
*/
password: {
type: 'string',
required: true,
check: {
length: 128
}
},
/**
* Salt to use to decrypt the password.
*/
passwordSalt: {
type: 'string',
check: {
length: 64
}
},
/**
* Password sent from user interface but hashed before be send on the network.
* Used to basically connect an user or generate the final password.
* Not stored in the DB.
*/
passwordProtected: {
type: 'string',
check: {
length: 64
}
},
/**
* Password wrote by the user on the GUI, not hashed or encrypted.
* Will be encrypted to respect the "passwordProtected" rules.
* Not stored in the DB.
*/
passwordPublic: {
type: 'string',
check: {
minLength: 8,
maxLength: 25
}
},
/**
* User banned status (Temporary of Definitive)
*/
banned: {
temporary : {
type : "number",
default : Date.now
},
definitive: {
type: 'boolean',
default: false
}
},
/**
* User right
*/
right : {
admin : {
type : "boolean",
default : false,
required: true
},
moderator : {
type : "boolean",
default : false,
required: true
}
}
};
So, what the code does?
Basically, in the Model.getNewModelInstance() I bind to the created model the errorHandler method that I will call if I found a DB error in the controller.
**UserController.js**
User.exists({email: user.email}, function(err, emailFound){
// If we got an err => Don't find couple User/pass
if (err) {
User.errorHandler(err, {model: User, __filename: __filename,__function: __function || 'subscription#exists', __line: __line}, function(err){
res.json(__format.response(err));
});
)
});
The __filename and so on are global functions that I use to get the current data, useful to debug. I'm still looking for a way to add this automatically but so far I couldn't. The __function doesn't exists when the function is anonymous. But it helps me to debug.
Any suggestion? That's a lot of piece of code.

Resources