Purpose
This snippets is a compiled LinkApi SDK commons command, check below
Usage
Type the commands below on the IDE (Visual Studio Code) e type tab
to select the snippet suggestion
Utils Commands
Parallel
parallel
//Result
await parallel(array, async (arrayItemName, uniqueKey) => {
try {
} catch (error) {
}
}, {
parallelExecutions: executionsNumber,
uniqueKeyPath: ' uniqueKeypath'
});
Logger Log
lglog
//Result
await Logger.log({
name: '',
status: 'SUCCESS',
finalLog: false,
uniqueKey:
});
Decorating a funcion
decorate
//Result
decorate(service, {
function: [handler]
});
Instanciating New Component
component
//Result
const [varName] = new Component('',{})
Requiring a service
rservice
//Result
const [serviceName] = require('../services/[serviceFileName]')
Requiring an util file
rutil
//Result
const [utilVarName] = require('../utils/[utilFilename]')
Instanciate New Service
service
//Result
const [serviceName] = new classService({
component: componentInstance
});
Component functions execution
Generic Function execution
fnexecute
//Result
const response = await this.component.request('method', 'path', {
urlParams: {},
queryString: {},
body: {}
});
Execute Post Function
fnexecutepost
//Result
const response = await this.component.request('POST', 'path', {
body: {
}
});
Execute Get Function
fnexecuteget
//Result
const response = await this.component.request('GET', 'path', {
queryString: {
}
});
Execute Find element by id function
fnexecutefind
//Result
const response = await this.component.request('GET', 'path/{id}', {
urlParams: {
"id": value
}
});
Dictionary commands
Getting a value from a dictionary group
dictionary-get
//Result
const response = await dictionary.get(keyName, groupName);
Setting a value to a dictionary group
dictionary-set
//Result
const response = await dictionary.set(keyName, data, groupName, options);
Commons structures helpers
Try Catch Structure
trycatch
//Result
try {
return { success: true , data: [] }
} catch (error) {
return { success: false, stack: error.stack, error }
}
SDK Parsers
Parse JSON to XML
lkp-json-xml
//Result
const { json } = require('@linkapi.solutions/nodejs-sdk/parser');
const jsonToParse = {
name: 'John'
};
const xml = await json.toXML(jsonToParse);
Parse JSON to CSV
lkp-json-csv
//Result
const { json } = require('@linkapi.solutions/nodejs-sdk/parser');
const jsonToParse = {
name: 'John'
};
const csv = await json.toCSV(jsonToParse);
Parse XML to JSON
lkp-xml-json
//Result
const { xml } = require('@linkapi.solutions/nodejs-sdk/parser');
const xmlToParse = `
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
`
const jsonParsed = await xml.toJSON(xmlToParse);
Parse CSV to JSON
lkp-csv-json
//Result
const { csv } = require('@linkapi.solutions/nodejs-sdk/parser');
const csvToParse = `
a,b,c
1,2,3
4,5,6
`;
const jsonParsed = await csv.toJSON(csvToParse);
Default constant values
Parse JSON to XML
SUCCESS
| ERROR
| REJECTED
| ALERT
| EMPTY
//Result
'SUCCESS'
'ERROR'
'REJECTED'
'ALERT'
'EMPTY'
Database
MongoDB
MongoDB Template Find
lkp-mongodb-template-find
//Result
const MongoDBService = require('@linkapi.solutions/nodejs-sdk/mongodb');
const findQuery = {};
const findOptions = {
skip: 0,
limit: 20,
sort: { codigo: -1 },
projection: { email: 1, codigo: 1 }
}
const collectionName = 'customers';
const connectionOptions = {
uri: 'mongodb://localhost:27017/databaseName',
// dbName: 'databaseName' // If you choose not to pass the database name in the connection string you can pass this parameter
};
const collection = new MongoDBService(collectionName, connectionOptions);
const result = await collection.find(findQuery, findOptions);
MongoDB Template FindOne
lkp-mongodb-template-find-one
//Result
const MongoDBService = require('@linkapi.solutions/nodejs-sdk/mongodb');
const findOneQuery = { email: 'product@domain' };
const findOneOptions = {
projection: { email: 1, codigo: 1 }
};
const collectionName = 'customers';
const connectionOptions = {
uri: 'mongodb://localhost:27017/databaseName',
// dbName: 'databaseName' // If you choose not to pass the database name in the connection string you can pass this parameter
};
const collection = new MongoDBService(collectionName, connectionOptions);
const result = await collection.findOne(findOneQuery, findOneOptions);
MongoDB Template Insert One
lkp-mongodb-template-insert-one
//Result
const MongoDBService = require('@linkapi.solutions/nodejs-sdk/mongodb');
const document = {
codigo: 7,
email: 'name@domain.com',
name: 'Name'
};
const collectionName = 'customers';
const connectionOptions = {
uri: 'mongodb://localhost:27017/databaseName',
// dbName: 'databaseName' // If you choose not to pass the database name in the connection string you can pass this parameter
};
const collection = new MongoDBService(collectionName, connectionOptions);
const result = await collection.insertOne(document);
MongoDB Template Insert Many
lkp-mongodb-template-insert-many
//Result
const MongoDBService = require('@linkapi.solutions/nodejs-sdk/mongodb');
const documents = [
{
codigo: 1,
email: 'customer@domain.com',
name: 'Customer'
},
{
codigo: 2,
email: 'customer2@domain.com',
name: 'Customer 2'
}
]
const collectionName = 'customers';
const connectionOptions = {
uri: 'mongodb://localhost:27017/databaseName',
// dbName: 'databaseName' // If you choose not to pass the database name in the connection string you can pass this parameter
};
const collection = new MongoDBService(collectionName, connectionOptions);
const result = await collection.insertMany(documents);
MongoDB Template Update One
lkp-mongodb-template-update-one
//Result
const MongoDBService = require('@linkapi.solutions/nodejs-sdk/mongodb');
const updateOneAction = { set: { customDomain: true, name: 'MyName' } }; // In this parameter you can inform any action parameter of the mongodb update operation
const updateOneOptions = {
// upsert: <boolean>,
// writeConcern: <document>,
// collation: <document>,
// arrayFilters: [ <filterdocument1>, ... ],
// hint: <document|string> // Available starting in MongoDB 4.2.1
};
const collectionName = 'customers';
const connectionOptions = {
uri: 'mongodb://localhost:27017/databaseName',
// dbName: 'databaseName' // If you choose not to pass the database name in the connection string you can pass this parameter
};
const collection = new MongoDBService(collectionName, connectionOptions);
const result = await collection.updateOne(updateOneQuery, updateOneAction, updateOneOptions);
MongoDB Template Update Many
lkp-mongodb-template-update-many
//Result
const MongoDBService = require('@linkapi.solutions/nodejs-sdk/mongodb');
const updateManyQuery = { email: /.*@domain.*/ };
const updateManyAction = { set: { customDomain: true } }; // In this parameter you can inform any action parameter of the mongodb update operation
const updateManyOptions = {
// upsert: <boolean>,
// writeConcern: <document>,
// collation: <document>,
// arrayFilters: [ <filterdocument1>, ... ],
// hint: <document|string> // Available starting in MongoDB 4.2.1
};
const collectionName = 'customers';
const connectionOptions = {
uri: 'mongodb://localhost:27017/databaseName',
// dbName: 'databaseName' // If you choose not to pass the database name in the connection string you can pass this parameter
};
const collection = new MongoDBService(collectionName, connectionOptions);
const result = await collection.updateMany(updateManyQuery, updateManyAction, updateManyOptions);
MongoDB Template Delete One
lkp-mongodb-template-delete-one
//Result
const MongoDBService = require('@linkapi.solutions/nodejs-sdk/mongodb');
const deleteOneQuery = { email: 'customer@domain.com' };
const collectionName = 'customers';
const connectionOptions = {
uri: 'mongodb://localhost:27017/databaseName',
// dbName: 'databaseName' // If you choose not to pass the database name in the connection string you can pass this parameter
};
const collection = new MongoDBService(collectionName, connectionOptions);
const result = await collection.deleteOne(deleteOneQuery);
MongoDB Template Delete Many
lkp-mongodb-template-delete-many
//Result
const MongoDBService = require('@linkapi.solutions/nodejs-sdk/mongodb');
const deleteManyQuery = { customDomain: true };
const collectionName = 'customers';
const connectionOptions = {
uri: 'mongodb://localhost:27017/databaseName',
// dbName: 'databaseName' // If you choose not to pass the database name in the connection string you can pass this parameter
};
const collection = new MongoDBService(collectionName, connectionOptions);
const result = await collection.deleteMany(deleteManyQuery);
MongoDB Template Aggregate
lkp-mongodb-template-aggregate
//Result
const MongoDBService = require('@linkapi.solutions/nodejs-sdk/mongodb');
const aggregatePipeline = [
{
match: {
customDomain: false
}
},
{
lookup: {
from: 'customeraddresses',
localField: 'codigo',
foreignField: 'customer',
as: 'addresses'
}
}
];
const collectionName = 'customers';
const connectionOptions = {
uri: 'mongodb://localhost:27017/databaseName',
// dbName: 'databaseName' // If you choose not to pass the database name in the connection string you can pass this parameter
};
const collection = new MongoDBService(collectionName, connectionOptions);
const result = await collection.aggregate(aggregatePipeline);
License
MIT