Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 | 1x 41x 41x 40x 1x 40x 40x 23x 23x 23x 23x 23x 23x 23x 552x 552x 552x 552x 552x 23x 23x 23x 40x 5x 5x 5x 5x 5x 5x 5x 5x 120x 120x 12x 7x 12x 12x 12x 12x 5x 40x 6x 6x 6x 6x 6x 6x 6x 144x 144x 13x 7x 13x 13x 13x 6x 6x 6x 40x 10x 10x 10x 10x 10x 10x 10x 240x 240x 14x 4x 14x 14x 14x 10x 40x 2x 2x 2x 2x 2x 2x 2x 48x 48x 5x 3x 5x 5x 5x 2x 40x 3x 3x 3x 3x 6x 6x 6x 3x 40x 5x 5x 5x 40x 6x 6x 6x 40x 2x 2x 2x 2x 40x 2x 2x 2x 2x 40x 1x 1x 1x 1x 40x 1x 1x 1x 1x 40x 1x 1x 1x 1x 40x 2x 2x 2x 2x 40x 2x 2x 2x 2x 40x 2x 2x 2x 2x 40x | import { createCore, IModel, ICollection, IEntities } from './core';
import { ICoreIntegratedDriver } from './driver-integrations/index';
import { createForPGP } from './driver-integrations/pgp';
export interface ICreateOptions {
entities: IEntities<IModel>;
db: any;
logError?: (err: Error) => never;
}
export interface IPureORM extends ICoreIntegratedDriver {
/* ------------------------------------------------------------------------*/
/* Built-in basic CRUD functions ------------------------------------------*/
/* ------------------------------------------------------------------------*/
/* These are just provided because they are so common and straight-forward.
* While the goal of this library is foster writing SQL in your data access
* layer (which returns pure business objects) some CRUD operations are so
* common they are included in the ORM. Feel free to completely disregard
* if you want to write these in your data access layer yourself.
*/
getMatching: <T extends IModel>(model: T) => Promise<T>;
getOneOrNoneMatching: <T extends IModel>(model: T) => Promise<T | void>;
getAnyMatching: <T extends ICollection<IModel>>(
model: IModel
) => Promise<T | void>;
getAllMatching: <T extends ICollection<IModel>>(model: IModel) => Promise<T>;
create: <T extends IModel>(model: T) => Promise<T>;
update: <T extends IModel>(model: T, options?: { on: string }) => Promise<T>;
delete: <T extends IModel>(model: T) => Promise<void>;
deleteMatching: <T extends IModel>(model: T) => Promise<void>;
/* ------------------------------------------------------------------------*/
/* Helper Utility Functions -----------------------------------------------*/
/* ------------------------------------------------------------------------*/
getSqlInsertParts: (model: IModel) => {
columns: string;
values: Array<string>;
valuesVar: Array<string>;
};
getSqlUpdateParts: (
model: IModel,
on?: string
) => { clause: string; idVar: string; values: Array<string> };
getMatchingParts: (model: IModel) => {
whereClause: string;
values: Array<string>;
};
getMatchingPartsObject: (model: IModel) => {
whereClause: string;
values: Array<string>;
};
getNewWith: (model: IModel, sqlColumns: any, values: any) => IModel;
getValueBySqlColumn: (model: IModel, sqlColumn: string) => string;
getSqlColumnForPropertyName: (model: IModel, propertyName: string) => string;
}
export const create = ({
entities: externalEntities,
db,
logError
}: ICreateOptions): IPureORM => {
const core = createCore({ entities: externalEntities });
let orm: ICoreIntegratedDriver;
if (db.$config.pgp) {
orm = createForPGP({ core, db, logError });
} else {
throw new Error(
`You're database driver is not yet supported. You can make a PR to add it, or use the \`createCore\` export which doesn't try to abstract over the database driver, and instead you pass the results of the database driver queries to it.`
);
}
/* ------------------------------------------------------------------------*/
/* Helper Utilities for CRUD functions ------------------------------------*/
/* ------------------------------------------------------------------------*/
interface IOrmHelperPlan {
quotedColumns: Array<string>;
updateClausePrefixes: Array<string>;
wherePositionalPrefixes: Array<string>;
whereNamedPrefixes: Array<string>;
}
const helperPlanByEntity = new Map<any, IOrmHelperPlan>();
const getHelperPlan = (entity: any): IOrmHelperPlan => {
let plan = helperPlanByEntity.get(entity);
Eif (!plan) {
const quotedColumns = new Array(entity.columnNames.length);
const updateClausePrefixes = new Array(entity.columnNames.length);
const wherePositionalPrefixes = new Array(entity.columnNames.length);
const whereNamedPrefixes = new Array(entity.columnNames.length);
for (let i = 0; i < entity.columnNames.length; i++) {
const column = entity.columnNames[i];
quotedColumns[i] = `"${column}"`;
updateClausePrefixes[i] = `"${column}" = $`;
wherePositionalPrefixes[i] = `"${entity.tableName}"."${column}" = $`;
whereNamedPrefixes[i] = `"${entity.tableName}"."${column}" = $(`;
}
plan = {
quotedColumns,
updateClausePrefixes,
wherePositionalPrefixes,
whereNamedPrefixes
};
helperPlanByEntity.set(entity, plan);
}
return plan;
};
const getSqlInsertParts = (
model: IModel
): { columns: string; values: Array<string>; valuesVar: Array<string> } => {
const entity = orm.getEntityByModel(model);
const { columnNames, propertyNames } = entity;
const helperPlan = getHelperPlan(entity);
let columns = '';
const values: Array<any> = [];
const valuesVar: Array<string> = [];
let paramIndex = 1;
for (let i = 0; i < columnNames.length; i++) {
const val = model[propertyNames[i] as keyof typeof model];
if (val !== void 0) {
if (columns) {
columns += ', ';
}
columns += helperPlan.quotedColumns[i];
values.push(val);
valuesVar.push(`$${paramIndex}`);
paramIndex++;
}
}
return { columns, values, valuesVar };
};
const getSqlUpdateParts = (
model: IModel,
on = 'id'
): { clause: string; idVar: string; values: Array<string> } => {
const entity = orm.getEntityByModel(model);
const { columnNames, propertyNames } = entity;
const helperPlan = getHelperPlan(entity);
let clause = '';
const values: Array<any> = [];
let paramIndex = 1;
for (let i = 0; i < columnNames.length; i++) {
const val = model[propertyNames[i] as keyof typeof model];
if (val !== void 0) {
if (clause) {
clause += ', ';
}
clause += helperPlan.updateClausePrefixes[i] + paramIndex;
values.push(val);
paramIndex++;
}
}
const idVar = `$${paramIndex}`;
values.push(model[on as keyof typeof model]);
return { clause, idVar, values };
};
const getMatchingParts = (
model: IModel
): { whereClause: string; values: Array<string> } => {
const entity = orm.getEntityByModel(model);
const { propertyNames, columnNames } = entity;
const helperPlan = getHelperPlan(entity);
const values: Array<any> = [];
let paramIndex = 1;
let whereClause = '';
for (let i = 0; i < propertyNames.length; i++) {
const val = model[propertyNames[i] as keyof typeof model];
if (val != null) {
if (whereClause) {
whereClause += ' AND ';
}
whereClause += helperPlan.wherePositionalPrefixes[i] + paramIndex;
values.push(val);
paramIndex++;
}
}
return { whereClause, values };
};
// This one returns an object, which allows it to be more versatile.
// To-do: make this one even better and use it instead of the one above.
const getMatchingPartsObject = (
model: IModel
): { whereClause: string; values: Array<string> } => {
const entity = orm.getEntityByModel(model);
const { propertyNames, columnNames } = entity;
const helperPlan = getHelperPlan(entity);
const values: any = {};
let paramIndex = 1;
let whereClause = '';
for (let i = 0; i < propertyNames.length; i++) {
const val = model[propertyNames[i] as keyof typeof model];
if (val != null) {
if (whereClause) {
whereClause += ' AND ';
}
whereClause += helperPlan.whereNamedPrefixes[i] + paramIndex + ')';
values[paramIndex] = val;
paramIndex++;
}
}
return { whereClause, values };
};
const getNewWith = (model: IModel, sqlColumns: any, values: any): IModel => {
const Constructor = model.constructor as any;
const entity = orm.getEntityByModel(model);
const modelData: any = {};
for (let i = 0; i < sqlColumns.length; i++) {
const propertyName = entity.columnToPropertyMap.get(sqlColumns[i]);
Eif (propertyName) {
modelData[propertyName] = values[i];
}
}
return new Constructor(modelData);
};
const getValueBySqlColumn = (model: IModel, sqlColumn: string): string => {
const entity = orm.getEntityByModel(model);
const propertyName = entity.columnToPropertyMap.get(sqlColumn);
return propertyName
? model[propertyName as keyof typeof model]
: (undefined as any);
};
const getSqlColumnForPropertyName = (
model: IModel,
propertyName: string
): string => {
const entity = orm.getEntityByModel(model);
const column = entity.propertyToColumnMap.get(propertyName);
return column as string;
};
/* ------------------------------------------------------------------------*/
/* Built-in basic CRUD functions ------------------------------------------*/
/* ------------------------------------------------------------------------*/
// Standard create
const create = <T extends IModel>(model: T): Promise<T> => {
const entity = orm.getEntityByModel(model);
const { columns, values, valuesVar } = getSqlInsertParts(model);
const query = `
INSERT INTO "${entity.tableName}" ( ${columns} )
VALUES ( ${valuesVar} )
RETURNING ${entity.selectColumnsClause};
`;
return orm.one<T>(query, values);
};
// Standard update
const update = <T extends IModel>(
model: T,
{ on = 'id' } = {}
): Promise<T> => {
const entity = orm.getEntityByModel(model);
const { clause, idVar, values } = getSqlUpdateParts(model, on);
const query = `
UPDATE "${entity.tableName}"
SET ${clause}
WHERE "${entity.tableName}".${getSqlColumnForPropertyName(
model,
on
)} = ${idVar}
RETURNING ${entity.selectColumnsClause};
`;
return orm.one<T>(query, values);
};
// Standard delete
const _delete = <T extends IModel>(model: T): Promise<void> => {
const entity = orm.getEntityByModel(model);
const id = (model as any).id;
const query = `
DELETE FROM "${entity.tableName}"
WHERE "${entity.tableName}".id = $(id)
`;
return orm.none(query, { id });
};
const deleteMatching = <T extends IModel>(model: T): Promise<void> => {
const entity = orm.getEntityByModel(model);
const { whereClause, values } = getMatchingParts(model);
const query = `
DELETE FROM "${entity.tableName}"
WHERE ${whereClause};
`;
return orm.none(query, values);
};
const getMatching = <T extends IModel>(model: T): Promise<T> => {
const entity = orm.getEntityByModel(model);
const { whereClause, values } = getMatchingParts(model);
const query = `
SELECT ${entity.selectColumnsClause}
FROM "${entity.tableName}"
WHERE ${whereClause};
`;
return orm.one<T>(query, values);
};
const getOneOrNoneMatching = <T extends IModel>(
model: T
): Promise<T | void> => {
const entity = orm.getEntityByModel(model);
const { whereClause, values } = getMatchingParts(model);
const query = `
SELECT ${entity.selectColumnsClause}
FROM "${entity.tableName}"
WHERE ${whereClause};
`;
return orm.oneOrNone<T>(query, values);
};
const getAnyMatching = <T extends ICollection<IModel>>(
model: IModel
): Promise<T | void> => {
const entity = orm.getEntityByModel(model);
const { whereClause, values } = getMatchingParts(model);
const query = `
SELECT ${entity.selectColumnsClause}
FROM "${entity.tableName}"
WHERE ${whereClause};
`;
return orm.any<T>(query, values);
};
const getAllMatching = <T extends ICollection<IModel>>(
model: IModel
): Promise<T> => {
const entity = orm.getEntityByModel(model);
const { whereClause, values } = getMatchingParts(model);
const query = `
SELECT ${entity.selectColumnsClause}
FROM "${entity.tableName}"
WHERE ${whereClause};
`;
return orm.many<T>(query, values);
};
return Object.assign({}, orm, {
// Built-in basic CRUD functions
create,
update,
delete: _delete,
deleteMatching,
getMatching,
getOneOrNoneMatching,
getAnyMatching,
getAllMatching,
// Helper Utility functions
getSqlInsertParts,
getSqlUpdateParts,
getMatchingParts,
getMatchingPartsObject,
getNewWith,
getValueBySqlColumn,
getSqlColumnForPropertyName
});
};
|