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

afssequelize

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

afssequelize - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

lib/generator.js

8

index.js

@@ -5,2 +5,5 @@ 'use strict';

const Translatable = require('./lib/translatable');
const LanguageDAO = require('./lib/language-dao');
const Language = require('./lib/language-model');
const generator = require('./lib/generator');

@@ -10,2 +13,7 @@ module.exports = {

Translatable,
LanguageDAO,
Language,
generateDb: generator.generateDb,
generateConfig: generator.generateConfig,
Sequelize: generator.Sequelize,
};

93

lib/translatable.js

@@ -33,3 +33,3 @@ 'use strict';

createTextTx(input, transaction) {
async createTextTx(input, transaction) {
const Table = this.db[this.tableName];

@@ -40,11 +40,9 @@ const { parentIdField } = this;

where[parentIdField] = input.id;
return Table.destroy({ where, transaction })
.then(() => {
const record = this.createRecord(input);
return Table.create(record, { transaction })
.then(() => input);
});
await Table.destroy({ where, transaction })
const record = this.createRecord(input);
await Table.create(record, { transaction })
return input
}
deleteTextTx(parentId, transaction) {
async deleteTextTx(parentId, transaction) {
const Table = this.db[this.tableName];

@@ -58,3 +56,3 @@ const { parentIdField } = this;

createMultipleTextsTx(inputs, inputLanguage, transaction) {
async createMultipleTextsTx(inputs, inputLanguage, transaction) {
const Table = this.db[this.tableName];

@@ -65,18 +63,16 @@ const { parentIdField } = this;

const where = { language, [parentIdField]: { [Op.in]: ids } };
return Table.destroy({ where, transaction })
.then(() => {
const records = inputs.map((input) => this.createRecord(input, language));
return Table.bulkCreate(records, { transaction });
});
await Table.destroy({ where, transaction })
const records = inputs.map((input) => this.createRecord(input, language));
return Table.bulkCreate(records, { transaction });
}
createText(input) {
async createText(input) {
return this.transaction((transaction) => this.createTextTx(input, transaction));
}
createMultipleTexts(input) {
async createMultipleTexts(input) {
return this.transaction((transaction) => this.createMultipleTextsTx(input, transaction));
}
getText(parentId, language = 'en') {
async getText(parentId, language = 'en') {
const Table = this.db[this.tableName];

@@ -86,10 +82,8 @@ const where = { language };

const query = { where, raw: true, attributes: this.textFields };
return Table.findOne(query)
.then((result) => {
if ((language === 'en') || result) {
return result;
}
query.where.language = 'en';
return Table.findOne(query);
});
const result = await Table.findOne(query);
if ((language === 'en') || result) {
return result;
}
query.where.language = 'en';
return Table.findOne(query);
}

@@ -111,8 +105,9 @@

updateText(parent, language) {
return this.getText(parent.id, language)
.then((result) => this.updateTextFields(parent, result));
async updateText(parent, language) {
const result = await this.getText(parent.id, language);
this.updateTextFields(parent, result);
return parent;
}
getAllTexts(ids, language = 'en') {
async getAllTexts(ids, language = 'en') {
const Table = this.db[this.tableName];

@@ -127,28 +122,24 @@ const { parentIdField } = this;

_.set(options, `where.${parentIdField}`, { [Op.in]: ids });
return Table.findAll(options)
.then((records) => {
if (language === 'en') {
return _.keyBy(records, parentIdField);
}
const enRecords = _.remove(records, (r) => r.language === 'en');
const map = _.keyBy(records, parentIdField);
enRecords.forEach((record) => {
const parentId = record[parentIdField];
if (!map[parentId]) {
map[parentId] = record;
records.push(record);
}
});
return map;
});
const records = await Table.findAll(options);
if (language === 'en') {
return _.keyBy(records, parentIdField);
}
const enRecords = _.remove(records, (r) => r.language === 'en');
const map = _.keyBy(records, parentIdField);
enRecords.forEach((record) => {
const parentId = record[parentIdField];
if (!map[parentId]) {
map[parentId] = record;
records.push(record);
}
});
return map;
}
updateAllTexts(parents, language, idField = 'id') {
async updateAllTexts(parents, language, idField = 'id') {
const ids = _.map(parents, idField);
return this.getAllTexts(ids, language)
.then((map) => {
parents.forEach((parent) => this.updateTextFields(parent, map[parent[idField]]));
return parents;
});
const map = await this.getAllTexts(ids, language);
parents.forEach((parent) => this.updateTextFields(parent, map[parent[idField]]));
return parents;
}
};
{
"name": "afssequelize",
"version": "0.0.1",
"version": "0.0.2",
"description": "sequelize utilities",

@@ -24,4 +24,5 @@ "main": "index.js",

"lodash": "^4.17.15",
"pg": "^7.17.1",
"sequelize": "^5.21.3"
}
}
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