knex-utils
Advanced tools
Comparing version
@@ -66,3 +66,3 @@ "use strict"; | ||
const diff = calculateEntityListDiff(oldList, newList, params.idFields); | ||
const insertChunks = chunkUtils_1.chunk(diff.newEntries, chunkSize); | ||
const insertChunks = (0, chunkUtils_1.chunk)(diff.newEntries, chunkSize); | ||
for (const insertChunk of insertChunks) { | ||
@@ -76,3 +76,3 @@ await trx(params.table).insert(insertChunk); | ||
}); | ||
const deleteChunks = chunkUtils_1.chunk(deleteIds, chunkSize); | ||
const deleteChunks = (0, chunkUtils_1.chunk)(deleteIds, chunkSize); | ||
for (const deleteChunk of deleteChunks) { | ||
@@ -85,3 +85,3 @@ await trx(params.table).delete().whereIn(params.primaryKeyField, deleteChunk); | ||
const deleteCriteria = diff.removedEntries.map((entry) => { | ||
return objectUtils_1.pick(entry, params.idFields); | ||
return (0, objectUtils_1.pick)(entry, params.idFields); | ||
}); | ||
@@ -88,0 +88,0 @@ for (const entry of deleteCriteria) { |
export declare function copyWithoutUndefined<T extends Record<K, V>, K extends string | number | symbol, V>(originalValue: T): T; | ||
export declare function pick<T, K extends string | number | symbol>(source: T, propNames: readonly K[]): Pick<T, Exclude<keyof T, Exclude<keyof T, K>>>; | ||
export declare function pickWithoutUndefined<T, K extends string | number | symbol>(source: T, propNames: readonly K[]): Pick<T, Exclude<keyof T, Exclude<keyof T, K>>>; | ||
export declare function validateOnlyWhitelistedFields<T>(source: T, propNames: readonly string[]): void; | ||
export declare function validateOnlyWhitelistedFieldsSet(source: Record<string, any>, propNames: Set<string>): void; | ||
export declare function strictPickWithoutUndefined<T>(source: T, propNames: readonly string[]): Pick<T, Exclude<keyof T, Exclude<keyof T, string>>>; | ||
export declare function isEmptyObject(params: Record<string, any>): boolean; | ||
export declare function groupBy<T>(inputArray: T[], propName: string): Record<string, T[]>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.groupBy = exports.isEmptyObject = exports.pickWithoutUndefined = exports.pick = exports.copyWithoutUndefined = void 0; | ||
exports.groupBy = exports.isEmptyObject = exports.strictPickWithoutUndefined = exports.validateOnlyWhitelistedFieldsSet = exports.validateOnlyWhitelistedFields = exports.pickWithoutUndefined = exports.pick = exports.copyWithoutUndefined = void 0; | ||
function copyWithoutUndefined(originalValue) { | ||
@@ -42,2 +42,25 @@ return Object.keys(originalValue).reduce((acc, key) => { | ||
exports.pickWithoutUndefined = pickWithoutUndefined; | ||
function validateOnlyWhitelistedFields(source, propNames) { | ||
const keys = Object.keys(source); | ||
for (var x = 0; x < keys.length; x++) { | ||
if (propNames.indexOf(keys[x]) === -1) { | ||
throw new Error(`Unsupported field: ${keys[x]}`); | ||
} | ||
} | ||
} | ||
exports.validateOnlyWhitelistedFields = validateOnlyWhitelistedFields; | ||
function validateOnlyWhitelistedFieldsSet(source, propNames) { | ||
const keys = Object.keys(source); | ||
for (var x = 0; x < keys.length; x++) { | ||
if (!propNames.has(keys[x])) { | ||
throw new Error(`Unsupported field: ${keys[x]}`); | ||
} | ||
} | ||
} | ||
exports.validateOnlyWhitelistedFieldsSet = validateOnlyWhitelistedFieldsSet; | ||
function strictPickWithoutUndefined(source, propNames) { | ||
validateOnlyWhitelistedFields(source, propNames); | ||
return pickWithoutUndefined(source, propNames); | ||
} | ||
exports.strictPickWithoutUndefined = strictPickWithoutUndefined; | ||
function isEmptyObject(params) { | ||
@@ -44,0 +67,0 @@ for (const key in params) { |
{ | ||
"name": "knex-utils", | ||
"version": "5.4.0", | ||
"version": "5.5.0", | ||
"license": "MIT", | ||
@@ -22,3 +22,3 @@ "maintainers": [ | ||
"test:ci": "npm run lint && npm run test:coverage", | ||
"lint": "eslint --format codeframe \"lib/**/*.ts\" \"test/**/*.ts\"", | ||
"lint": "eslint \"lib/**/*.ts\" \"test/**/*.ts\"", | ||
"prettier": "prettier --write \"{lib,test}/**/*.{js,ts}\" index.ts", | ||
@@ -31,19 +31,19 @@ "prepublishOnly": "npm run build" | ||
"devDependencies": { | ||
"@types/jest": "^27.0.1", | ||
"@types/node": "^16.7.1", | ||
"@typescript-eslint/eslint-plugin": "^4.29.2", | ||
"@typescript-eslint/parser": "^4.29.2", | ||
"eslint": "^7.32.0", | ||
"@types/jest": "^27.0.3", | ||
"@types/node": "^16.11.11", | ||
"@typescript-eslint/eslint-plugin": "^5.5.0", | ||
"@typescript-eslint/parser": "^5.5.0", | ||
"eslint": "^8.4.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-prettier": "^3.4.1", | ||
"jest": "^27.0.6", | ||
"knex": "^0.95.10", | ||
"jest": "^27.4.3", | ||
"knex": "^0.95.14", | ||
"mysql": "^2.18.1", | ||
"mysql2": "^2.3.0", | ||
"mysql2": "^2.3.3", | ||
"pg": "^8.7.1", | ||
"prettier": "^2.3.2", | ||
"prettier": "^2.5.1", | ||
"sqlite3": "^5.0.2", | ||
"tedious": "^11.4.0", | ||
"ts-jest": "^27.0.5", | ||
"typescript": "4.3.5" | ||
"ts-jest": "^27.1.0", | ||
"typescript": "4.5.2" | ||
}, | ||
@@ -50,0 +50,0 @@ "engines": { |
@@ -89,2 +89,6 @@ # knex-utils | ||
* `strictPickWithoutUndefined(source: T, propNames: K[]): Pick<T, Exclude<keyof T, Exclude<keyof T, K>>>` - same as `pick`, but skips properties with value `undefined`. Throws an error if `source` has any fields not included in `propNames`. | ||
* `validateOnlyWhitelistedFieldsSet(source: Record<string, any>, propNames: Set<string>)` - throws an error if `source` has any fields not included in `propnames` | ||
### DB relation difference operations | ||
@@ -91,0 +95,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
32867
8.94%334
8.44%131
3.15%