kysely-mapper
Advanced tools
Comparing version 0.4.3 to 0.4.4
@@ -70,3 +70,3 @@ "use strict"; | ||
? [] | ||
: settings.selectedColumns.includes('*') | ||
: settings.selectedColumns[0] === '*' | ||
? [] | ||
@@ -73,0 +73,0 @@ : settings.selectedColumns; |
@@ -34,3 +34,3 @@ import { Insertable, Selectable, Updateable } from 'kysely'; | ||
*/ | ||
insertTransform(obj: Selectable<DB[TB]>): Insertable<DB[TB]>; | ||
insertTransform(obj: Selectable<DB[TB]>, _columns: Readonly<(keyof Insertable<DB[TB]> & string)[]> | ['*']): Insertable<DB[TB]>; | ||
/** | ||
@@ -48,3 +48,3 @@ * Transforms the returns of an insert query into the the object returned | ||
*/ | ||
updateTransform(source: Selectable<DB[TB]>): Updateable<DB[TB]>; | ||
updateTransform(source: Selectable<DB[TB]>, _columns: Readonly<(keyof Updateable<DB[TB]> & string)[]> | ['*']): Updateable<DB[TB]>; | ||
/** | ||
@@ -51,0 +51,0 @@ * Transforms the returns of an update query into the the object returned |
@@ -35,3 +35,3 @@ "use strict"; | ||
*/ | ||
insertTransform(obj) { | ||
insertTransform(obj, _columns) { | ||
const insertedValues = Object.assign({}, obj); | ||
@@ -63,3 +63,3 @@ this.keyColumns.forEach((column) => { | ||
*/ | ||
updateTransform(source) { | ||
updateTransform(source, _columns) { | ||
return source; | ||
@@ -66,0 +66,0 @@ } |
@@ -40,6 +40,7 @@ import { Insertable, Selectable, Selection, Updateable } from 'kysely'; | ||
/** | ||
* Transformation to apply to inserted objects before insertion. | ||
* `source` is the object provided for insertion. | ||
* Transformation to apply to inserted objects before insertion. `source` | ||
* is the object provided for insertion. Only the columns in `columns` will | ||
* actually be inserted, with `[*]` indicating all columns. | ||
*/ | ||
insertTransform?: (source: InsertedObject) => Insertable<DB[TB]>; | ||
insertTransform?: (source: InsertedObject, columns: Readonly<(keyof Insertable<DB[TB]> & string)[]> | ['*']) => Insertable<DB[TB]>; | ||
/** | ||
@@ -65,4 +66,6 @@ * Transformation to apply to column values returned from inserts before | ||
* is the object containing the values which which to update the table row. | ||
* Only the columns in `columns` will actually be updated, with `[*]` | ||
* indicating all columns. | ||
*/ | ||
updateTransform?: (source: UpdatingObject) => Updateable<DB[TB]>; | ||
updateTransform?: (source: UpdatingObject, columns: Readonly<(keyof Updateable<DB[TB]> & string)[]> | ['*']) => Updateable<DB[TB]>; | ||
/** | ||
@@ -69,0 +72,0 @@ * Transformation to apply to column values returned from updates before |
@@ -9,2 +9,3 @@ import { Kysely, InsertQueryBuilder, Insertable } from 'kysely'; | ||
export declare class CompilingMappingInsertQuery<DB, TB extends keyof DB & string, QB extends InsertQueryBuilder<DB, TB, any>, InsertedObject, InsertReturnColumns extends Readonly<SelectionColumn<DB, TB>[]> | ['*'], InsertReturn> extends CompilingValuesQuery<DB, TB, QB, InsertReturnColumns, {}, Insertable<DB[TB]>> { | ||
protected readonly columnsToInsert: Readonly<(keyof Insertable<DB[TB]> & string)[]>; | ||
protected readonly transforms: Readonly<InsertTransforms<DB, TB, InsertedObject, InsertReturnColumns, InsertReturn>>; | ||
@@ -11,0 +12,0 @@ constructor(db: Kysely<DB>, qb: QB, columnsToInsert: Readonly<(keyof Insertable<DB[TB]> & string)[]>, transforms: Readonly<InsertTransforms<DB, TB, InsertedObject, InsertReturnColumns, InsertReturn>>, returnColumns: Readonly<InsertReturnColumns>); |
@@ -20,2 +20,3 @@ "use strict"; | ||
super(db, returnColumns); | ||
this.columnsToInsert = columnsToInsert; | ||
this.transforms = transforms; | ||
@@ -62,3 +63,3 @@ const parameterizedValues = this.getParameterizedObject(columnsToInsert); | ||
? obj | ||
: this.transforms.insertTransform(obj); | ||
: this.transforms.insertTransform(obj, this.columnsToInsert); | ||
} | ||
@@ -65,0 +66,0 @@ } |
@@ -10,2 +10,3 @@ import { Kysely, UpdateQueryBuilder, UpdateResult, Updateable } from 'kysely'; | ||
export declare class CompilingMappingUpdateQuery<DB, TB extends keyof DB & string, QB extends UpdateQueryBuilder<DB, TB, TB, UpdateResult>, UpdatingObject, UpdateReturnColumns extends Readonly<SelectionColumn<DB, TB>[]> | ['*'], ReturnCount, UpdateReturn, Parameters extends ParametersObject<Parameters>> extends CompilingValuesQuery<DB, TB, QB, UpdateReturnColumns, Parameters, Updateable<DB[TB]>> { | ||
protected readonly columnsToUpdate: Readonly<(keyof Updateable<DB[TB]> & string)[]>; | ||
protected readonly transforms: Readonly<CountTransform<ReturnCount> & UpdateTransforms<DB, TB, UpdatingObject, UpdateReturnColumns, UpdateReturn>>; | ||
@@ -12,0 +13,0 @@ constructor(db: Kysely<DB>, qb: QB, columnsToUpdate: Readonly<(keyof Updateable<DB[TB]> & string)[]>, transforms: Readonly<CountTransform<ReturnCount> & UpdateTransforms<DB, TB, UpdatingObject, UpdateReturnColumns, UpdateReturn>>, returnColumns: Readonly<UpdateReturnColumns>); |
@@ -20,2 +20,3 @@ "use strict"; | ||
super(db, returnColumns); | ||
this.columnsToUpdate = columnsToUpdate; | ||
this.transforms = transforms; | ||
@@ -91,3 +92,3 @@ const parameterizedValues = this.getParameterizedObject(columnsToUpdate); | ||
? obj | ||
: this.transforms.updateTransform(obj); | ||
: this.transforms.updateTransform(obj, this.columnsToUpdate); | ||
} | ||
@@ -94,0 +95,0 @@ applyUpdateReturnTransform(source, returns) { |
@@ -47,2 +47,8 @@ import { Kysely, InsertQueryBuilder, InsertResult, Insertable } from 'kysely'; | ||
/** | ||
* Returns an array of the columns to be inserted, with | ||
* `['*']` indicating that all columns will be inserted. | ||
* @returns An array of the columns to be inserted. | ||
*/ | ||
protected getInsertColumns(): Readonly<(keyof Insertable<DB[TB]> & string)[]> | ['*']; | ||
/** | ||
* Returns a query builder for inserting rows into the table and | ||
@@ -49,0 +55,0 @@ * returning values, caching the query builder for future use. |
@@ -80,2 +80,10 @@ "use strict"; | ||
/** | ||
* Returns an array of the columns to be inserted, with | ||
* `['*']` indicating that all columns will be inserted. | ||
* @returns An array of the columns to be inserted. | ||
*/ | ||
getInsertColumns() { | ||
return ['*']; | ||
} | ||
/** | ||
* Returns a query builder for inserting rows into the table and | ||
@@ -101,6 +109,7 @@ * returning values, caching the query builder for future use. | ||
loadInsertedObjects(qb, objOrObjs) { | ||
const insertColumns = this.getInsertColumns(); | ||
if (Array.isArray(objOrObjs)) { | ||
const transformedObjs = this.transforms.insertTransform === undefined | ||
? objOrObjs | ||
: objOrObjs.map(this.transforms.insertTransform); | ||
: objOrObjs.map((obj) => this.transforms.insertTransform(obj, insertColumns)); | ||
// TS requires separate calls to values() for different arg types. | ||
@@ -111,3 +120,3 @@ return this.setColumnValues(qb, transformedObjs); | ||
? objOrObjs | ||
: this.transforms.insertTransform(objOrObjs); | ||
: this.transforms.insertTransform(objOrObjs, insertColumns); | ||
// TS requires separate calls to values() for different arg types. | ||
@@ -114,0 +123,0 @@ return this.setColumnValues(qb, transformedObj); |
@@ -23,2 +23,3 @@ import { Kysely, InsertQueryBuilder, InsertResult, Insertable } from 'kysely'; | ||
compile(): CompilingMappingInsertQuery<DB, TB, QB, InsertedObject, InsertReturnColumns, InsertReturn>; | ||
protected getInsertColumns(): Readonly<(keyof Insertable<DB[TB]> & string)[]> | ['*']; | ||
protected setColumnValues(qb: InsertQueryBuilder<DB, TB, InsertResult>, objOrObjs: Insertable<DB[TB]> | Insertable<DB[TB]>[]): InsertQueryBuilder<DB, TB, InsertResult>; | ||
@@ -25,0 +26,0 @@ protected toInsertableObject(obj: Insertable<DB[TB]>): Insertable<DB[TB]>; |
@@ -27,2 +27,5 @@ "use strict"; | ||
} | ||
getInsertColumns() { | ||
return this.columnsToInsert; | ||
} | ||
setColumnValues(qb, objOrObjs) { | ||
@@ -29,0 +32,0 @@ if (Array.isArray(objOrObjs)) { |
@@ -25,2 +25,3 @@ import { Kysely, UpdateQueryBuilder, UpdateResult, Updateable } from 'kysely'; | ||
compile<Parameters extends ParametersObject<Parameters> = {}>(): CompilingMappingUpdateQuery<DB, TB, QB, UpdatingObject, UpdateReturnColumns, ReturnCount, UpdateReturn, Parameters>; | ||
protected getUpdateColumns(): Readonly<(keyof Updateable<DB[TB]> & string)[]> | ['*']; | ||
protected setColumnValues(qb: UpdateQueryBuilder<DB, TB, TB, UpdateResult>, obj: Updateable<DB[TB]>): UpdateQueryBuilder<DB, TB, TB, UpdateResult>; | ||
@@ -27,0 +28,0 @@ protected toUpdateableObject(obj: Updateable<DB[TB]>): Updateable<DB[TB]>; |
@@ -27,2 +27,5 @@ "use strict"; | ||
} | ||
getUpdateColumns() { | ||
return this.columnsToUpdate; | ||
} | ||
setColumnValues(qb, obj) { | ||
@@ -29,0 +32,0 @@ return qb.set(this.toUpdateableObject(obj)); |
@@ -55,2 +55,8 @@ import { Kysely, UpdateQueryBuilder, UpdateResult, Updateable } from 'kysely'; | ||
/** | ||
* Returns an array of the columns to be updated, with | ||
* `['*']` indicating that all columns will be updated. | ||
* @returns An array of the columns to be updated. | ||
*/ | ||
protected getUpdateColumns(): Readonly<(keyof Updateable<DB[TB]> & string)[]> | ['*']; | ||
/** | ||
* Returns a query builder for updating rows in the table and | ||
@@ -57,0 +63,0 @@ * returning values, caching the query builder for future use. |
@@ -97,2 +97,10 @@ "use strict"; | ||
/** | ||
* Returns an array of the columns to be updated, with | ||
* `['*']` indicating that all columns will be updated. | ||
* @returns An array of the columns to be updated. | ||
*/ | ||
getUpdateColumns() { | ||
return ['*']; | ||
} | ||
/** | ||
* Returns a query builder for updating rows in the table and | ||
@@ -118,5 +126,6 @@ * returning values, caching the query builder for future use. | ||
loadUpdatingObject(qb, obj) { | ||
const updateColumns = this.getUpdateColumns(); | ||
const transformedObj = this.transforms.updateTransform === undefined | ||
? obj | ||
: this.transforms.updateTransform(obj); | ||
: this.transforms.updateTransform(obj, updateColumns); | ||
return this.setColumnValues(qb, transformedObj); | ||
@@ -123,0 +132,0 @@ } |
@@ -62,3 +62,17 @@ "use strict"; | ||
})); | ||
it('provides insertTransform with column subset', () => __awaiter(void 0, void 0, void 0, function* () { | ||
expect.assertions(1); | ||
const mapper = userMapperReturningID.withTransforms({ | ||
insertTransform: (source, columns) => { | ||
expect(columns).toEqual(['name', 'handle']); | ||
return source; | ||
}, | ||
}); | ||
yield mapper.insert().columns(['name', 'handle']).run({ | ||
name: 'John Doe', | ||
handle: 'johndoe', | ||
email: 'jdoe@abc.def', | ||
}); | ||
})); | ||
}); | ||
//# sourceMappingURL=insert-columns.test.js.map |
@@ -82,2 +82,8 @@ "use strict"; | ||
it('compiles an insert query with transformation', () => __awaiter(void 0, void 0, void 0, function* () { | ||
expect.assertions(7); | ||
const columnSubset = [ | ||
'name', | ||
'handle', | ||
'email', | ||
]; | ||
const transformMapper = new table_mapper_1.TableMapper(db, 'users', { | ||
@@ -90,7 +96,10 @@ insertReturnColumns: ['id'], | ||
}, | ||
insertTransform: (source) => ({ | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}), | ||
insertTransform: (source, columns) => { | ||
expect(columns).toEqual(columnSubset); | ||
return { | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}; | ||
}, | ||
insertReturnTransform: (source, returns) => new test_types_1.User(returns.id, source.firstName, source.lastName, source.handle, source.email), | ||
@@ -104,3 +113,3 @@ countTransform: (count) => Number(count), | ||
.insert() | ||
.columns(['name', 'handle', 'email']) | ||
.columns(columnSubset) | ||
.compile(); | ||
@@ -107,0 +116,0 @@ // test returnOne() |
@@ -92,11 +92,15 @@ "use strict"; | ||
})); | ||
it('transforms insertion and insertion return', () => __awaiter(void 0, void 0, void 0, function* () { | ||
it("transforms insertion and insertion return, columns is ['*']", () => __awaiter(void 0, void 0, void 0, function* () { | ||
expect.assertions(5); | ||
const insertAndReturnTransformMapper = new table_mapper_1.TableMapper(db, 'users', { | ||
insertReturnColumns: ['id'], | ||
}).withTransforms({ | ||
insertTransform: (source) => ({ | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}), | ||
insertTransform: (source, columns) => { | ||
expect(columns).toEqual(['*']); | ||
return { | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}; | ||
}, | ||
insertReturnTransform: (source, returns) => new test_types_1.ReturnedUser(returns.id, source.firstName, source.lastName, source.handle, source.email), | ||
@@ -103,0 +107,0 @@ countTransform: (count) => Number(count), |
@@ -15,2 +15,3 @@ "use strict"; | ||
const test_objects_1 = require("./utils/test-objects"); | ||
const table_mapper_1 = require("../mappers/table-mapper"); | ||
let db; | ||
@@ -69,3 +70,17 @@ let userMapperReturningID; | ||
})); | ||
it('provides updateTransform with column subset', () => __awaiter(void 0, void 0, void 0, function* () { | ||
expect.assertions(1); | ||
const mapper = new table_mapper_1.TableMapper(db, 'users').withTransforms({ | ||
updateTransform: (source, columns) => { | ||
expect(columns).toEqual(['name', 'handle']); | ||
return source; | ||
}, | ||
}); | ||
yield mapper.update().columns(['name', 'handle']).run({ | ||
name: 'John Doe', | ||
handle: 'johndoe', | ||
email: 'jdoe@abc.def', | ||
}); | ||
})); | ||
}); | ||
//# sourceMappingURL=update-columns.test.js.map |
@@ -208,2 +208,4 @@ "use strict"; | ||
it('compiles an update query with transformation', () => __awaiter(void 0, void 0, void 0, function* () { | ||
expect.assertions(12); | ||
const columnSubset = ['name']; | ||
const transformMapper = new table_mapper_1.TableMapper(db, 'users', { | ||
@@ -226,7 +228,10 @@ insertReturnColumns: ['*'], | ||
}, | ||
updateTransform: (source) => ({ | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}), | ||
updateTransform: (source, columns) => { | ||
expect(columns).toEqual(columnSubset); | ||
return { | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}; | ||
}, | ||
updateReturnTransform: (_source, returns) => { | ||
@@ -246,3 +251,3 @@ const names = returns.name.split(' '); | ||
.update({ id: insertReturns[2].id }) | ||
.columns(['name']) | ||
.columns(columnSubset) | ||
.compile(); | ||
@@ -249,0 +254,0 @@ // test returnOne() |
@@ -113,3 +113,4 @@ "use strict"; | ||
})); | ||
it('transforms update and update return', () => __awaiter(void 0, void 0, void 0, function* () { | ||
it("transforms update and update return, columns is ['*']", () => __awaiter(void 0, void 0, void 0, function* () { | ||
expect.assertions(2); | ||
const updateAndReturnTransformMapper = new table_mapper_1.TableMapper(db, 'users', { | ||
@@ -119,7 +120,10 @@ insertReturnColumns: ['id'], | ||
}).withTransforms({ | ||
updateTransform: (source) => ({ | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}), | ||
updateTransform: (source, columns) => { | ||
expect(columns).toEqual(['*']); | ||
return { | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}; | ||
}, | ||
updateReturnTransform: (source, returns) => new test_types_1.ReturnedUser(returns.id, source.firstName, source.lastName, source.handle, source.email), | ||
@@ -126,0 +130,0 @@ }); |
@@ -67,3 +67,3 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) { | ||
? [] | ||
: settings.selectedColumns.includes('*') | ||
: settings.selectedColumns[0] === '*' | ||
? [] | ||
@@ -70,0 +70,0 @@ : settings.selectedColumns; |
@@ -34,3 +34,3 @@ import { Insertable, Selectable, Updateable } from 'kysely'; | ||
*/ | ||
insertTransform(obj: Selectable<DB[TB]>): Insertable<DB[TB]>; | ||
insertTransform(obj: Selectable<DB[TB]>, _columns: Readonly<(keyof Insertable<DB[TB]> & string)[]> | ['*']): Insertable<DB[TB]>; | ||
/** | ||
@@ -48,3 +48,3 @@ * Transforms the returns of an insert query into the the object returned | ||
*/ | ||
updateTransform(source: Selectable<DB[TB]>): Updateable<DB[TB]>; | ||
updateTransform(source: Selectable<DB[TB]>, _columns: Readonly<(keyof Updateable<DB[TB]> & string)[]> | ['*']): Updateable<DB[TB]>; | ||
/** | ||
@@ -51,0 +51,0 @@ * Transforms the returns of an update query into the the object returned |
@@ -32,3 +32,3 @@ /** | ||
*/ | ||
insertTransform(obj) { | ||
insertTransform(obj, _columns) { | ||
const insertedValues = Object.assign({}, obj); | ||
@@ -60,3 +60,3 @@ this.keyColumns.forEach((column) => { | ||
*/ | ||
updateTransform(source) { | ||
updateTransform(source, _columns) { | ||
return source; | ||
@@ -63,0 +63,0 @@ } |
@@ -40,6 +40,7 @@ import { Insertable, Selectable, Selection, Updateable } from 'kysely'; | ||
/** | ||
* Transformation to apply to inserted objects before insertion. | ||
* `source` is the object provided for insertion. | ||
* Transformation to apply to inserted objects before insertion. `source` | ||
* is the object provided for insertion. Only the columns in `columns` will | ||
* actually be inserted, with `[*]` indicating all columns. | ||
*/ | ||
insertTransform?: (source: InsertedObject) => Insertable<DB[TB]>; | ||
insertTransform?: (source: InsertedObject, columns: Readonly<(keyof Insertable<DB[TB]> & string)[]> | ['*']) => Insertable<DB[TB]>; | ||
/** | ||
@@ -65,4 +66,6 @@ * Transformation to apply to column values returned from inserts before | ||
* is the object containing the values which which to update the table row. | ||
* Only the columns in `columns` will actually be updated, with `[*]` | ||
* indicating all columns. | ||
*/ | ||
updateTransform?: (source: UpdatingObject) => Updateable<DB[TB]>; | ||
updateTransform?: (source: UpdatingObject, columns: Readonly<(keyof Updateable<DB[TB]> & string)[]> | ['*']) => Updateable<DB[TB]>; | ||
/** | ||
@@ -69,0 +72,0 @@ * Transformation to apply to column values returned from updates before |
@@ -9,2 +9,3 @@ import { Kysely, InsertQueryBuilder, Insertable } from 'kysely'; | ||
export declare class CompilingMappingInsertQuery<DB, TB extends keyof DB & string, QB extends InsertQueryBuilder<DB, TB, any>, InsertedObject, InsertReturnColumns extends Readonly<SelectionColumn<DB, TB>[]> | ['*'], InsertReturn> extends CompilingValuesQuery<DB, TB, QB, InsertReturnColumns, {}, Insertable<DB[TB]>> { | ||
protected readonly columnsToInsert: Readonly<(keyof Insertable<DB[TB]> & string)[]>; | ||
protected readonly transforms: Readonly<InsertTransforms<DB, TB, InsertedObject, InsertReturnColumns, InsertReturn>>; | ||
@@ -11,0 +12,0 @@ constructor(db: Kysely<DB>, qb: QB, columnsToInsert: Readonly<(keyof Insertable<DB[TB]> & string)[]>, transforms: Readonly<InsertTransforms<DB, TB, InsertedObject, InsertReturnColumns, InsertReturn>>, returnColumns: Readonly<InsertReturnColumns>); |
@@ -17,2 +17,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
super(db, returnColumns); | ||
this.columnsToInsert = columnsToInsert; | ||
this.transforms = transforms; | ||
@@ -59,5 +60,5 @@ const parameterizedValues = this.getParameterizedObject(columnsToInsert); | ||
? obj | ||
: this.transforms.insertTransform(obj); | ||
: this.transforms.insertTransform(obj, this.columnsToInsert); | ||
} | ||
} | ||
//# sourceMappingURL=compiling-insert-query.js.map |
@@ -10,2 +10,3 @@ import { Kysely, UpdateQueryBuilder, UpdateResult, Updateable } from 'kysely'; | ||
export declare class CompilingMappingUpdateQuery<DB, TB extends keyof DB & string, QB extends UpdateQueryBuilder<DB, TB, TB, UpdateResult>, UpdatingObject, UpdateReturnColumns extends Readonly<SelectionColumn<DB, TB>[]> | ['*'], ReturnCount, UpdateReturn, Parameters extends ParametersObject<Parameters>> extends CompilingValuesQuery<DB, TB, QB, UpdateReturnColumns, Parameters, Updateable<DB[TB]>> { | ||
protected readonly columnsToUpdate: Readonly<(keyof Updateable<DB[TB]> & string)[]>; | ||
protected readonly transforms: Readonly<CountTransform<ReturnCount> & UpdateTransforms<DB, TB, UpdatingObject, UpdateReturnColumns, UpdateReturn>>; | ||
@@ -12,0 +13,0 @@ constructor(db: Kysely<DB>, qb: QB, columnsToUpdate: Readonly<(keyof Updateable<DB[TB]> & string)[]>, transforms: Readonly<CountTransform<ReturnCount> & UpdateTransforms<DB, TB, UpdatingObject, UpdateReturnColumns, UpdateReturn>>, returnColumns: Readonly<UpdateReturnColumns>); |
@@ -17,2 +17,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
super(db, returnColumns); | ||
this.columnsToUpdate = columnsToUpdate; | ||
this.transforms = transforms; | ||
@@ -88,3 +89,3 @@ const parameterizedValues = this.getParameterizedObject(columnsToUpdate); | ||
? obj | ||
: this.transforms.updateTransform(obj); | ||
: this.transforms.updateTransform(obj, this.columnsToUpdate); | ||
} | ||
@@ -91,0 +92,0 @@ applyUpdateReturnTransform(source, returns) { |
@@ -47,2 +47,8 @@ import { Kysely, InsertQueryBuilder, InsertResult, Insertable } from 'kysely'; | ||
/** | ||
* Returns an array of the columns to be inserted, with | ||
* `['*']` indicating that all columns will be inserted. | ||
* @returns An array of the columns to be inserted. | ||
*/ | ||
protected getInsertColumns(): Readonly<(keyof Insertable<DB[TB]> & string)[]> | ['*']; | ||
/** | ||
* Returns a query builder for inserting rows into the table and | ||
@@ -49,0 +55,0 @@ * returning values, caching the query builder for future use. |
@@ -77,2 +77,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
/** | ||
* Returns an array of the columns to be inserted, with | ||
* `['*']` indicating that all columns will be inserted. | ||
* @returns An array of the columns to be inserted. | ||
*/ | ||
getInsertColumns() { | ||
return ['*']; | ||
} | ||
/** | ||
* Returns a query builder for inserting rows into the table and | ||
@@ -98,6 +106,7 @@ * returning values, caching the query builder for future use. | ||
loadInsertedObjects(qb, objOrObjs) { | ||
const insertColumns = this.getInsertColumns(); | ||
if (Array.isArray(objOrObjs)) { | ||
const transformedObjs = this.transforms.insertTransform === undefined | ||
? objOrObjs | ||
: objOrObjs.map(this.transforms.insertTransform); | ||
: objOrObjs.map((obj) => this.transforms.insertTransform(obj, insertColumns)); | ||
// TS requires separate calls to values() for different arg types. | ||
@@ -108,3 +117,3 @@ return this.setColumnValues(qb, transformedObjs); | ||
? objOrObjs | ||
: this.transforms.insertTransform(objOrObjs); | ||
: this.transforms.insertTransform(objOrObjs, insertColumns); | ||
// TS requires separate calls to values() for different arg types. | ||
@@ -111,0 +120,0 @@ return this.setColumnValues(qb, transformedObj); |
@@ -23,2 +23,3 @@ import { Kysely, InsertQueryBuilder, InsertResult, Insertable } from 'kysely'; | ||
compile(): CompilingMappingInsertQuery<DB, TB, QB, InsertedObject, InsertReturnColumns, InsertReturn>; | ||
protected getInsertColumns(): Readonly<(keyof Insertable<DB[TB]> & string)[]> | ['*']; | ||
protected setColumnValues(qb: InsertQueryBuilder<DB, TB, InsertResult>, objOrObjs: Insertable<DB[TB]> | Insertable<DB[TB]>[]): InsertQueryBuilder<DB, TB, InsertResult>; | ||
@@ -25,0 +26,0 @@ protected toInsertableObject(obj: Insertable<DB[TB]>): Insertable<DB[TB]>; |
@@ -24,2 +24,5 @@ import { MappingInsertQuery } from './insert-query'; | ||
} | ||
getInsertColumns() { | ||
return this.columnsToInsert; | ||
} | ||
setColumnValues(qb, objOrObjs) { | ||
@@ -26,0 +29,0 @@ if (Array.isArray(objOrObjs)) { |
@@ -25,2 +25,3 @@ import { Kysely, UpdateQueryBuilder, UpdateResult, Updateable } from 'kysely'; | ||
compile<Parameters extends ParametersObject<Parameters> = {}>(): CompilingMappingUpdateQuery<DB, TB, QB, UpdatingObject, UpdateReturnColumns, ReturnCount, UpdateReturn, Parameters>; | ||
protected getUpdateColumns(): Readonly<(keyof Updateable<DB[TB]> & string)[]> | ['*']; | ||
protected setColumnValues(qb: UpdateQueryBuilder<DB, TB, TB, UpdateResult>, obj: Updateable<DB[TB]>): UpdateQueryBuilder<DB, TB, TB, UpdateResult>; | ||
@@ -27,0 +28,0 @@ protected toUpdateableObject(obj: Updateable<DB[TB]>): Updateable<DB[TB]>; |
@@ -24,2 +24,5 @@ import { MappingUpdateQuery } from './update-query'; | ||
} | ||
getUpdateColumns() { | ||
return this.columnsToUpdate; | ||
} | ||
setColumnValues(qb, obj) { | ||
@@ -26,0 +29,0 @@ return qb.set(this.toUpdateableObject(obj)); |
@@ -55,2 +55,8 @@ import { Kysely, UpdateQueryBuilder, UpdateResult, Updateable } from 'kysely'; | ||
/** | ||
* Returns an array of the columns to be updated, with | ||
* `['*']` indicating that all columns will be updated. | ||
* @returns An array of the columns to be updated. | ||
*/ | ||
protected getUpdateColumns(): Readonly<(keyof Updateable<DB[TB]> & string)[]> | ['*']; | ||
/** | ||
* Returns a query builder for updating rows in the table and | ||
@@ -57,0 +63,0 @@ * returning values, caching the query builder for future use. |
@@ -94,2 +94,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
/** | ||
* Returns an array of the columns to be updated, with | ||
* `['*']` indicating that all columns will be updated. | ||
* @returns An array of the columns to be updated. | ||
*/ | ||
getUpdateColumns() { | ||
return ['*']; | ||
} | ||
/** | ||
* Returns a query builder for updating rows in the table and | ||
@@ -115,5 +123,6 @@ * returning values, caching the query builder for future use. | ||
loadUpdatingObject(qb, obj) { | ||
const updateColumns = this.getUpdateColumns(); | ||
const transformedObj = this.transforms.updateTransform === undefined | ||
? obj | ||
: this.transforms.updateTransform(obj); | ||
: this.transforms.updateTransform(obj, updateColumns); | ||
return this.setColumnValues(qb, transformedObj); | ||
@@ -120,0 +129,0 @@ } |
@@ -60,3 +60,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
})); | ||
it('provides insertTransform with column subset', () => __awaiter(void 0, void 0, void 0, function* () { | ||
expect.assertions(1); | ||
const mapper = userMapperReturningID.withTransforms({ | ||
insertTransform: (source, columns) => { | ||
expect(columns).toEqual(['name', 'handle']); | ||
return source; | ||
}, | ||
}); | ||
yield mapper.insert().columns(['name', 'handle']).run({ | ||
name: 'John Doe', | ||
handle: 'johndoe', | ||
email: 'jdoe@abc.def', | ||
}); | ||
})); | ||
}); | ||
//# sourceMappingURL=insert-columns.test.js.map |
@@ -80,2 +80,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
it('compiles an insert query with transformation', () => __awaiter(void 0, void 0, void 0, function* () { | ||
expect.assertions(7); | ||
const columnSubset = [ | ||
'name', | ||
'handle', | ||
'email', | ||
]; | ||
const transformMapper = new TableMapper(db, 'users', { | ||
@@ -88,7 +94,10 @@ insertReturnColumns: ['id'], | ||
}, | ||
insertTransform: (source) => ({ | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}), | ||
insertTransform: (source, columns) => { | ||
expect(columns).toEqual(columnSubset); | ||
return { | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}; | ||
}, | ||
insertReturnTransform: (source, returns) => new User(returns.id, source.firstName, source.lastName, source.handle, source.email), | ||
@@ -102,3 +111,3 @@ countTransform: (count) => Number(count), | ||
.insert() | ||
.columns(['name', 'handle', 'email']) | ||
.columns(columnSubset) | ||
.compile(); | ||
@@ -105,0 +114,0 @@ // test returnOne() |
@@ -90,11 +90,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
})); | ||
it('transforms insertion and insertion return', () => __awaiter(void 0, void 0, void 0, function* () { | ||
it("transforms insertion and insertion return, columns is ['*']", () => __awaiter(void 0, void 0, void 0, function* () { | ||
expect.assertions(5); | ||
const insertAndReturnTransformMapper = new TableMapper(db, 'users', { | ||
insertReturnColumns: ['id'], | ||
}).withTransforms({ | ||
insertTransform: (source) => ({ | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}), | ||
insertTransform: (source, columns) => { | ||
expect(columns).toEqual(['*']); | ||
return { | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}; | ||
}, | ||
insertReturnTransform: (source, returns) => new ReturnedUser(returns.id, source.firstName, source.lastName, source.handle, source.email), | ||
@@ -101,0 +105,0 @@ countTransform: (count) => Number(count), |
@@ -13,2 +13,3 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { USERS } from './utils/test-objects'; | ||
import { TableMapper } from '../mappers/table-mapper'; | ||
let db; | ||
@@ -67,3 +68,17 @@ let userMapperReturningID; | ||
})); | ||
it('provides updateTransform with column subset', () => __awaiter(void 0, void 0, void 0, function* () { | ||
expect.assertions(1); | ||
const mapper = new TableMapper(db, 'users').withTransforms({ | ||
updateTransform: (source, columns) => { | ||
expect(columns).toEqual(['name', 'handle']); | ||
return source; | ||
}, | ||
}); | ||
yield mapper.update().columns(['name', 'handle']).run({ | ||
name: 'John Doe', | ||
handle: 'johndoe', | ||
email: 'jdoe@abc.def', | ||
}); | ||
})); | ||
}); | ||
//# sourceMappingURL=update-columns.test.js.map |
@@ -206,2 +206,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
it('compiles an update query with transformation', () => __awaiter(void 0, void 0, void 0, function* () { | ||
expect.assertions(12); | ||
const columnSubset = ['name']; | ||
const transformMapper = new TableMapper(db, 'users', { | ||
@@ -224,7 +226,10 @@ insertReturnColumns: ['*'], | ||
}, | ||
updateTransform: (source) => ({ | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}), | ||
updateTransform: (source, columns) => { | ||
expect(columns).toEqual(columnSubset); | ||
return { | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}; | ||
}, | ||
updateReturnTransform: (_source, returns) => { | ||
@@ -244,3 +249,3 @@ const names = returns.name.split(' '); | ||
.update({ id: insertReturns[2].id }) | ||
.columns(['name']) | ||
.columns(columnSubset) | ||
.compile(); | ||
@@ -247,0 +252,0 @@ // test returnOne() |
@@ -111,3 +111,4 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
})); | ||
it('transforms update and update return', () => __awaiter(void 0, void 0, void 0, function* () { | ||
it("transforms update and update return, columns is ['*']", () => __awaiter(void 0, void 0, void 0, function* () { | ||
expect.assertions(2); | ||
const updateAndReturnTransformMapper = new TableMapper(db, 'users', { | ||
@@ -117,7 +118,10 @@ insertReturnColumns: ['id'], | ||
}).withTransforms({ | ||
updateTransform: (source) => ({ | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}), | ||
updateTransform: (source, columns) => { | ||
expect(columns).toEqual(['*']); | ||
return { | ||
name: `${source.firstName} ${source.lastName}`, | ||
handle: source.handle, | ||
email: source.email, | ||
}; | ||
}, | ||
updateReturnTransform: (source, returns) => new ReturnedUser(returns.id, source.firstName, source.lastName, source.handle, source.email), | ||
@@ -124,0 +128,0 @@ }); |
{ | ||
"name": "kysely-mapper", | ||
"version": "0.4.3", | ||
"version": "0.4.4", | ||
"author": "Joseph T. Lapp <arachnojoe@gmail.com>", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -7,3 +7,3 @@ # kysely-mapper | ||
This utility helps eliminate the boilerplate associated with mapping between database tables and objects across multiple queries. | ||
This utility eliminates the boilerplate associated with mapping between database tables and objects across multiple queries. | ||
@@ -390,4 +390,15 @@ Unconfigured, the utility does no mapping and only serves as a shorthand for accessing tables using column names as fields. When configured, it provides nearly complete control over how objects map to and from individual tables. Mappings can be tailored per table and can vary in degree of ORM functionality. | ||
Mind you, `updateTransform` will still run in its entirety, but only the specified subset of its return values will be used in the update. The `columns()` method is also available on insertion for fine control over when to use database defaults, with the same caveat applying to `insertTransform`. | ||
Whatever `updateTransform` does, only the specified subset of its return values will be used in the update. The `columns()` method is also available on insertion for fine control over when to use database defaults, with the same caveat applying to `insertTransform`. | ||
You might want to decline to return values that are unneeded and expensive to compute. Both `updateTransform` and `insertTransform` receive a second parameter indicating the columns that will be updated or inserted, with `[*]` indicating all columns. Here's an example: | ||
```ts | ||
updateTransform: (source: User, columns) => ({ | ||
name: columns.includes('name') | ||
? `${source.firstName} ${source.lastName}` | ||
: undefined, | ||
birth_year: source.birthYear, | ||
}); | ||
``` | ||
For greater flexibility, we could have had the update source be a union of types: | ||
@@ -530,3 +541,3 @@ | ||
We can do something similar when using compiling queries. There's no need to keep the table mapper around if all queries are compiling, as we only need the compiled queries: | ||
We can do something similar when using compiling queries. There's no need to keep the table mapper around if all queries are compiling: | ||
@@ -657,5 +668,5 @@ ```ts | ||
| --- | --- | | ||
| `insertTransform` | (source-object) => table columns object<br/> Transforms the source object into the table column-values to insert. The default assumes the source object contains only table columns. | | ||
| `insertTransform` | (source-object, columns) => table columns object<br/> Transforms the source object into the table column-values to insert. Only the columns in `columns` will actually be inserted, with `[*]` indicating all columns. The default assumes the source object contains only table columns. | | ||
| `insertReturnTransform` | (source-object, returns) => insert return<br/> Transforms the source object and the returned column-values into the value to return from the insert query. The default returns an object containing the returned columns, unless there are no `insertReturnColumns`, in which case the return type is `void`. | | ||
| `updateTransform` | (source-object) => table columns object<br/> Transforms the source object into the table column-values to update. The default assumes the source object contains only table columns. | | ||
| `updateTransform` | (source-object, columns) => table columns object<br/> Transforms the source object into the table column-values to update. Only the columns in `columns` will actually be updated, with `[*]` indicating all columns. The default assumes the source object contains only table columns. | | ||
| `updateReturnTransform` | (source-object, returns) => update return<br/> Transforms the source object and the returned column-values into the value to return from the udpate query. The default returns an object containing the returned columns, unless there are no `updateReturnColumns`, in which case the return type is `void`. | | ||
@@ -662,0 +673,0 @@ | `selectTransform` | (selected-row) => selected object<br/> Transforms a selected row of column-values into the object to return from the select query. The default returns the selected row. | |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1047351
11256
719