Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save bunch #89

Merged
merged 9 commits into from
Nov 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
86 changes: 73 additions & 13 deletions models/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Table {
this.table_name = tableName;
this.knex = givenKnex || knex;
if (!this.knex) throw new Error('Knex param missing.');
this.INSERT_LIMIT_ARRAY = 10000;
}

toString() {
Expand Down Expand Up @@ -138,18 +139,65 @@ class Table {
// SAVE
// ################################################

save(originalEntry) {
const errorString = 'Something went wrong';
const f = async () => {
const entry = Utils.cloneJSON(originalEntry);
const isSave = true;
const parsed = await this.parseAttributesForUpsert(entry, isSave);
const query = this.saveQuery(parsed);
const saved = await Table.fetchQuery(query);
if (!saved || saved.length === 0) throw new Error(errorString);
return saved[0];
};
return f();
// save(originalEntry) {
// const errorString = 'Something went wrong';
// const f = async () => {
// const entry = Utils.cloneJSON(originalEntry);
// const isSave = true;
// const parsed = await this.parseAttributesForUpsert(entry, isSave);
// const query = this.saveQuery(parsed);
// const saved = await Table.fetchQuery(query);
// if (!saved || saved.length === 0) throw new Error(errorString);
// return saved[0];
// };
// return f();
// }

async save(input) {
const copy = Utils.cloneJSON(input);
if (Array.isArray(copy)) return this.saveBunch(copy);
const parsed = Table.parseForSave(copy);
const query = this.saveQuery(parsed);
const [saved] = await Table.fetchQuery(query);
return saved;
}

static parseForSaveArray(array) {
array = array || [];
const parsed = [];
for (let i = 0; i < array.length; i++) {
const element = array[i];
parsed.push(Table.parseForSave(element));
}
return parsed;
}

// getSubArray(i, array) {
// const copy = [...array];
// const initial = this.INSERT_LIMIT_ARRAY * i;
// // const final = i < iterations - 1 ? initial + this.INSERT_LIMIT_ARRAY : array.length;
// const final = Math.min(initial + this.INSERT_LIMIT_ARRAY, copy.length);
// const result = copy.splice(initial, final);
// return result;
// }

getSubArray(i, iterations, array) {
const initial = this.INSERT_LIMIT_ARRAY * i;
const final = i < iterations - 1 ? initial + this.INSERT_LIMIT_ARRAY : array.length;
const result = array.slice(initial, final);
return result;
}

saveBunch(array) {
const parsed = Table.parseForSaveArray(array);
const promises = [];
const iterations = parsed.length / this.INSERT_LIMIT_ARRAY;
for (let i = 0; i < iterations; i++) {
const subArray = this.getSubArray(i, iterations, parsed);
const query = this.saveQuery(subArray);
promises.push(Table.fetchQuery(query));
}
return Utils.Promise.doAll(promises);
}

saveQuery(entry) {
Expand Down Expand Up @@ -673,10 +721,17 @@ class Table {
return f();
}

static parseForSave(entry) {
Table.removeUnSetableAttributes(entry);
Table.addTimestamps(entry, true);
return entry;
}

static makeError(err) {
const keys400 = Object.keys(ERROR_400);
if (ERROR_400_BY_CODE[err.code]) return Message.new(400, ERROR_400_BY_CODE[err.code], err);
if (keys400.indexOf(err.routine) > -1) {
return Message.new(400, ERROR_400[err], err);
return Message.new(400, ERROR_400[err.routine], err);
}
return Message.new(500, 'Internal error', err);
}
Expand Down Expand Up @@ -730,6 +785,11 @@ const ERROR_400 = {

};

const ERROR_400_BY_CODE = {
42703: 'Intentando de agregar columna inexistent',

};

const OPTIONS_KEYS = ['startDate', 'endDate', 'groupBy', 'orderBy', 'limit', 'offset', 'rawSelect', 'clearSelect', 'rawWhere'];


Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"homepage": "https://github.com/afontainec/chinchay#readme",
"dependencies": {
"codemaster": "0.0.8",
"codemaster": "0.0.29",
"commander": "^2.19.0",
"express": "^4.16.4",
"knex": "^0.19.5",
Expand Down
32 changes: 32 additions & 0 deletions test/models/table/save/parseForSave.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';

// Require the dev-dependencies
const { assert } = require('chai');
const Table = require('../../../../models/table');


// Our parent block
describe('TABLE GATEWAY: parse for savesave', () => { // eslint-disable-line

it('Happy path', async () => { // eslint-disable-line
const entry = {
name: 'name',
price: 120,
id: 100,
created_at: 'hola',
updated_at: 'hi',
};
const parsed = Table.parseForSave(entry);
assert.equal(parsed.name, 'name');
assert.equal(parsed.price, 120);
assert.isUndefined(parsed.id);
assert.instanceOf(parsed.created_at, Date);
assert.instanceOf(parsed.updated_at, Date);
});

it('Entry is undef', async () => { // eslint-disable-line
const parsed = Table.parseForSave();
assert.isUndefined(parsed);
});
});
29 changes: 28 additions & 1 deletion test/models/table/save/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,35 @@ describe('TABLE GATEWAY: save', () => { // eslint-disable-line
};
Coffee.save(entry).then(() => {
done(new Error('Should not get here'));
}).catch(() => {
}).catch((err) => {
assert.equal(err.code, 400);
assert.equal(err.message, 'Intentando de agregar columna inexistent');
done();
});
});
});

describe('TABLE save: when a input is an Array', () => { // eslint-disable-line

const arrayToTest = [];
const len = 40000;

before(async () => { // eslint-disable-line
await knex('coffee').del();
for (let i = 0; i < len; i++) {
arrayToTest.push({
name: `${i}`,
price: i,
});
}
});

it('save a array', async () => { // eslint-disable-line
await Coffee.saveBunch([...arrayToTest]);
const saved = await knex('coffee').select('*').orderBy('price');
for (let i = 0; i < saved.length; i++) {
delete saved[i].id;
}
assert.deepEqual(arrayToTest, saved);
});
});
36 changes: 36 additions & 0 deletions test/models/table/saveBunch/getsubArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';

// Require the dev-dependencies
const { assert } = require('chai');
// const knex = require('../../../../knex');
// const Table = require('../../../../models/table');
const Coffee = require('../../../../models/coffee-example');


const arrayToTest = [];
const len = 40000;

// Our parent block
describe('MODEL TABLE: getSubArray unit', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
for (let i = 0; i < len; i++) {
arrayToTest.push({ id: 1 });
}
});

it('hapry path', async () => { // eslint-disable-line
const iterations = len / Coffee.INSERT_LIMIT_ARRAY;
const subArrayArray = [];
for (let i = 0; i < iterations; i++) {
subArrayArray.push(Coffee.getSubArray(i, iterations, arrayToTest));
}
const outcome = [];
for (let i = 0; i < subArrayArray.length; i++) {
for (let j = 0; j < subArrayArray[i].length; j++) {
outcome.push(subArrayArray[i][j]);
}
}
assert.deepEqual(arrayToTest, outcome);
});
});
47 changes: 47 additions & 0 deletions test/models/table/saveBunch/parseDorSaveArray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';

// Require the dev-dependencies
const { assert } = require('chai');
// const knex = require('../../../../knex');
const Table = require('../../../../models/table');
// const Coffee = require('../../../../models/coffee-example');


const arrayToTest = [
{
id: 1,
prop: 'prop',
created_at: new Date(),
updated_at: new Date(),
}, {
id: 2,
prop: 'prop',
created_at: new Date(),
updated_at: new Date(),
}, {
id: 3,
prop: 'prop',
created_at: new Date(),
updated_at: new Date(),
},
];


// Our parent block
describe('MODEL TABLE: parseForSaveArray integration test', () => { // eslint-disable-line

it('hapry path', async () => { // eslint-disable-line
const parsed = Table.parseForSaveArray(arrayToTest);
for (let i = 0; i < parsed.length; i++) {
assert.isUndefined(parsed[i].id);
assert.isDefined(parsed[i].created_at);
assert.isDefined(parsed[i].updated_at);
assert.equal(parsed[i].prop, 'prop');
}
});
it('when undefined input', async () => { // eslint-disable-line
const parsed = Table.parseForSaveArray();
assert.deepEqual(parsed, []);
});
});
33 changes: 33 additions & 0 deletions test/models/table/saveBunch/saveBunch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';

// Require the dev-dependencies
const { assert } = require('chai');
const knex = require('../../../../knex');
const Coffee = require('../../../../models/coffee-example');


const arrayToTest = [];
const len = 40000;

// Our parent block
describe('TABLE saveBunch: integration', () => { // eslint-disable-line
before(async () => { // eslint-disable-line
await knex('coffee').del();
for (let i = 0; i < len; i++) {
arrayToTest.push({
name: `${i}`,
price: i,
});
}
});

it('save a array', async () => { // eslint-disable-line
await Coffee.saveBunch([...arrayToTest]);
const saved = await knex('coffee').select('*').orderBy('price');
for (let i = 0; i < saved.length; i++) {
delete saved[i].id;
}
assert.deepEqual(arrayToTest, saved);
});
});