NodeJS ORM to Access Magento 2 Adobe Commerce Data

Yegor Shytikov
4 min readSep 8, 2021

The Nodejento Connector for Magento enables you to create modern JavaScript applications and scripts that use JS Sequalize Object-Relational Mappings of the Magento 2 database.

The rich ecosystem of Node JS modules lets you get to work quickly and integrate your systems effectively. With the Nodejento Magento Connector, you can build Magento-connected NodeJS applications and scripts. This article shows how to use NodeJS to connect to Magento 2 Adobe Commerce data to query, update, delete, and insert M2 data.

With built-in optimized data processing, the Nodejento Magento Connector offers unmatched performance for interacting with live Magento2 data using server Java Script I/O nonblocking language.

Connecting to Magento 2 Adobe Commerce Data

Connecting to Magento 2 data looks just like connecting to any relational data source. First, create a connection string using the required connection properties. For this article, you will pass the connection string as a parameter to the Sequlize constructor.

Follow the procedure below to install NodeJS and start accessing Magento 2 through JavaScript objects.

Install Required Modules

macOS

Download the macOS Installer directly from the nodejs.org website.

If you want to download the package with bash:

curl "https://nodejs.org/dist/latest/node-${VERSION:-$(wget -qO- https://nodejs.org/dist/latest/ | sed -nE 's|.*>node-(.*)\.pkg</a>.*|\1|p')}.pkg" > "$HOME/Downloads/node-latest.pkg" && sudo installer -store -pkg "$HOME/Downloads/node-latest.pkg" -target "/"

Alternatives

Using Homebrew:

brew install node

CentOS, Fedora and Red Hat Enterprise Linux

Node.js is available as a module called nodejs in CentOS/RHEL 8 and Fedora.

dnf module install nodejs:<stream>

where <stream> corresponds to the major version of Node.js. To see a list of available streams:

dnf module list nodejs

For example, to install Node.js 12:

dnf module install nodejs:12

For CentOS/RHEL 7 Node.js is available via Software Collections.

Clone NodeJento library:

git clone https://github.com/Genaker/nodejento.git

install required NPM modules:

  • sequelize

Sequelize is a promise-based Node.js ORM for Postgres, MySQL, MariaDB, SQLite and Microsoft SQL Server. It features solid transaction support, relations, eager and lazy loading, read replication and more.

  • mysql2
npm install

Model Magento Data in NodeJS

You can now connect with a connection string. Use the initModels function to create a Model for working with Adobe Commerce data.

var initModels = require("./Models/init-models"); 
const sequelize = new Sequelize(
'magento',
'root',
'',
{
host: '127.0.0.1',
dialect: 'mysql',
logging: console.log,
// stop the auto-pluralization performed by Sequelize using the freezeTableName: true option
freezeTableName: true
});
var magentoModels = initModels(sequelize);

Nodejento has a Mapping Class for Magento Data.

Nodejento has a mapping class for the table you wish to model in the ORM (in this article, we will model the Product table).

there is an example of the Product Sequlize ORM Model class :

const Sequelize = require('sequelize');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('CatalogProductEntity', {
entity_id: {
autoIncrement: true,
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false,
primaryKey: true,
comment: "Entity ID"
},
attribute_set_id: {
type: DataTypes.SMALLINT.UNSIGNED,
allowNull: false,
defaultValue: 0,
comment: "Attribute Set ID"
},
type_id: {
type: DataTypes.STRING(32),
allowNull: false,
defaultValue: "simple",
comment: "Type ID"
},
sku: {
type: DataTypes.STRING(64),
allowNull: true,
comment: "SKU"
},
has_options: {
type: DataTypes.SMALLINT,
allowNull: false,
defaultValue: 0,
comment: "Has Options"
},
required_options: {
type: DataTypes.SMALLINT.UNSIGNED,
allowNull: false,
defaultValue: 0,
comment: "Required Options"
},
created_at: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: Sequelize.Sequelize.fn('current_timestamp'),
comment: "Creation Time"
},
updated_at: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: Sequelize.Sequelize.fn('current_timestamp'),
comment: "Update Time"
}
}, {
sequelize,
tableName: 'catalog_product_entity',
hasTrigger: true,
timestamps: false,
...
});
};

Fetch Magento Data From the database

With the Sequlize ORM Model class prepared, you can query the data.

var magentoModels = initModels(sequelize);

var Product = await magentoModels.CatalogProductEntity.findOne({ where: {‘sku’: ‘24-MB01’}});

Insert Magento Data

To insert Magento data, define an instance of the mapped class and add it to the active session. Then, call the commit function on the session to push all added instances to the Magento database.

const Product = await magentoModels.CatalogProductEntity.create({ sku: “24-MB01”}); 
console.log(“Product's auto-generated ID:”, Product.entity_id);

Update Magento Data

To update Magento data, filter with “where” the desired record(s). Then, modify the values of the fields will push the modified record to Magento DB.

await magentoModels.CatalogProductEntity.update({ sku: "24-MB01-2" }, {   where: {     sku: "24-MB01"   } });

Delete Magento 2 Data

To delete Magento data, fetch the desired record(s) with a filter query. Then delete the record.

await magentoModels.CatalogProductEntity.destroy({   where: {    sku: "24-MB01-2"   } });

More Information:

Reach me if you have any questions (yegorshytikov@gmail.com).

Contributors are welcome!

--

--

Yegor Shytikov

True Stories about Magento 2. Melting down metal server infrastructure into cloud solutions.