orchid-orm 1.3.16 → 1.4.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/dist/index.d.ts +39 -32
  3. package/dist/index.esm.js +102 -100
  4. package/dist/index.esm.js.map +1 -1
  5. package/dist/index.js +102 -100
  6. package/dist/index.js.map +1 -1
  7. package/jest-setup.ts +4 -0
  8. package/package.json +8 -4
  9. package/src/appCodeUpdater/appCodeUpdater.ts +19 -0
  10. package/src/appCodeUpdater/fileChanges.ts +41 -0
  11. package/src/appCodeUpdater/testUtils.ts +31 -0
  12. package/src/appCodeUpdater/tsUtils.ts +137 -0
  13. package/src/appCodeUpdater/updateMainFile.test.ts +230 -0
  14. package/src/appCodeUpdater/updateMainFile.ts +163 -0
  15. package/src/appCodeUpdater/updateTableFile.ts +19 -0
  16. package/src/index.ts +5 -1
  17. package/src/orm.test.ts +13 -13
  18. package/src/orm.ts +21 -21
  19. package/src/relations/belongsTo.test.ts +1 -1
  20. package/src/relations/belongsTo.ts +2 -2
  21. package/src/relations/hasAndBelongsToMany.test.ts +1 -1
  22. package/src/relations/hasAndBelongsToMany.ts +9 -9
  23. package/src/relations/hasMany.test.ts +16 -10
  24. package/src/relations/hasMany.ts +6 -6
  25. package/src/relations/hasOne.test.ts +10 -10
  26. package/src/relations/hasOne.ts +6 -6
  27. package/src/relations/relations.ts +73 -71
  28. package/src/relations/utils.ts +3 -3
  29. package/src/repo.test.ts +29 -29
  30. package/src/repo.ts +6 -6
  31. package/src/{model.test.ts → table.test.ts} +15 -15
  32. package/src/{model.ts → table.ts} +17 -17
  33. package/src/test-utils/test-db.ts +15 -15
  34. package/src/test-utils/{test-models.ts → test-tables.ts} +42 -42
  35. package/src/transaction.test.ts +1 -1
  36. package/src/transaction.ts +4 -4
  37. package/src/utils.ts +9 -0
  38. package/tsconfig.json +1 -0
package/dist/index.js CHANGED
@@ -4,8 +4,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var pqb = require('pqb');
6
6
 
7
- const createModel = (options) => {
8
- return class Model {
7
+ const createBaseTable = (options) => {
8
+ return class BaseTable {
9
9
  constructor() {
10
10
  this.setColumns = (fn) => {
11
11
  const shape = pqb.getColumnTypes(options.columnTypes, fn);
@@ -243,11 +243,11 @@ const nestedUpdate$3 = ({ query, primaryKey, foreignKey }) => {
243
243
  };
244
244
  };
245
245
 
246
- const getThroughRelation = (model, through) => {
247
- return model.relations[through];
246
+ const getThroughRelation = (table, through) => {
247
+ return table.relations[through];
248
248
  };
249
249
  const getSourceRelation = (throughRelation, source) => {
250
- return throughRelation.model.relations[source];
250
+ return throughRelation.table.relations[source];
251
251
  };
252
252
  const hasRelationHandleCreate = (q, ctx, item, rowIndex, key, primaryKey, nestedInsert) => {
253
253
  const value = item[key];
@@ -345,7 +345,7 @@ class HasOneVirtualColumn extends pqb.VirtualColumn {
345
345
  );
346
346
  }
347
347
  }
348
- const makeHasOneMethod = (model, relation, relationName, query) => {
348
+ const makeHasOneMethod = (table, relation, relationName, query) => {
349
349
  if (relation.options.required) {
350
350
  query._take();
351
351
  } else {
@@ -353,14 +353,14 @@ const makeHasOneMethod = (model, relation, relationName, query) => {
353
353
  }
354
354
  if ("through" in relation.options) {
355
355
  const { through, source } = relation.options;
356
- const throughRelation = getThroughRelation(model, through);
356
+ const throughRelation = getThroughRelation(table, through);
357
357
  const sourceRelation = getSourceRelation(throughRelation, source);
358
358
  const sourceQuery = sourceRelation.joinQuery(throughRelation.query, sourceRelation.query).as(relationName);
359
359
  const whereExistsCallback = () => sourceQuery;
360
360
  return {
361
361
  returns: "one",
362
362
  method: (params) => {
363
- const throughQuery = model[through](params);
363
+ const throughQuery = table[through](params);
364
364
  return query.whereExists(
365
365
  throughQuery,
366
366
  whereExistsCallback
@@ -542,10 +542,10 @@ class HasManyVirtualColumn extends pqb.VirtualColumn {
542
542
  );
543
543
  }
544
544
  }
545
- const makeHasManyMethod = (model, relation, relationName, query) => {
545
+ const makeHasManyMethod = (table, relation, relationName, query) => {
546
546
  if ("through" in relation.options) {
547
547
  const { through, source } = relation.options;
548
- const throughRelation = getThroughRelation(model, through);
548
+ const throughRelation = getThroughRelation(table, through);
549
549
  const sourceRelation = getSourceRelation(throughRelation, source);
550
550
  const sourceRelationQuery = sourceRelation.query.as(relationName);
551
551
  const sourceQuery = sourceRelation.joinQuery(
@@ -556,7 +556,7 @@ const makeHasManyMethod = (model, relation, relationName, query) => {
556
556
  return {
557
557
  returns: "many",
558
558
  method: (params) => {
559
- const throughQuery = model[through](params);
559
+ const throughQuery = table[through](params);
560
560
  return query.whereExists(
561
561
  throughQuery,
562
562
  whereExistsCallback
@@ -788,7 +788,7 @@ class HasAndBelongsToManyVirtualColumn extends pqb.VirtualColumn {
788
788
  );
789
789
  }
790
790
  }
791
- const makeHasAndBelongsToManyMethod = (model, qb, relation, relationName, query) => {
791
+ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query) => {
792
792
  const {
793
793
  primaryKey: pk,
794
794
  foreignKey: fk,
@@ -799,14 +799,14 @@ const makeHasAndBelongsToManyMethod = (model, qb, relation, relationName, query)
799
799
  const foreignKeyFull = `${joinTable}.${fk}`;
800
800
  const associationForeignKeyFull = `${joinTable}.${afk}`;
801
801
  const associationPrimaryKeyFull = `${pqb.getQueryAs(query)}.${apk}`;
802
- const __model = Object.create(qb.__model);
803
- __model.__model = __model;
804
- __model.table = joinTable;
805
- __model.shape = {
806
- [fk]: model.shape[pk],
802
+ const __table = Object.create(qb.__table);
803
+ __table.__table = __table;
804
+ __table.table = joinTable;
805
+ __table.shape = {
806
+ [fk]: table.shape[pk],
807
807
  [afk]: query.shape[apk]
808
808
  };
809
- const subQuery = Object.create(__model);
809
+ const subQuery = Object.create(__table);
810
810
  subQuery.query = __spreadValues$2({}, subQuery.query);
811
811
  const state = {
812
812
  relatedTableQuery: query,
@@ -1056,38 +1056,40 @@ const nestedUpdate = (state) => {
1056
1056
  };
1057
1057
  };
1058
1058
 
1059
- const applyRelations = (qb, models, result) => {
1060
- const modelsEntries = Object.entries(models);
1059
+ const applyRelations = (qb, tables, result) => {
1060
+ const tableEntries = Object.entries(tables);
1061
1061
  const delayedRelations = /* @__PURE__ */ new Map();
1062
- for (const modelName in models) {
1063
- const model = models[modelName];
1064
- if (!("relations" in model) || typeof model.relations !== "object")
1062
+ for (const name in tables) {
1063
+ const table = tables[name];
1064
+ if (!("relations" in table) || typeof table.relations !== "object")
1065
1065
  continue;
1066
- const dbModel = result[modelName];
1067
- for (const relationName in model.relations) {
1068
- const relation = model.relations[relationName];
1069
- const otherModelClass = relation.fn();
1070
- const otherModel = modelsEntries.find(
1071
- (pair) => pair[1] instanceof otherModelClass
1066
+ const dbTable = result[name];
1067
+ for (const relationName in table.relations) {
1068
+ const relation = table.relations[relationName];
1069
+ const otherTableClass = relation.fn();
1070
+ const otherTable = tableEntries.find(
1071
+ (pair) => pair[1] instanceof otherTableClass
1072
1072
  );
1073
- if (!otherModel) {
1074
- throw new Error(`Cannot find model for class ${otherModelClass.name}`);
1073
+ if (!otherTable) {
1074
+ throw new Error(
1075
+ `Cannot find table class for class ${otherTableClass.name}`
1076
+ );
1075
1077
  }
1076
- const otherModelName = otherModel[0];
1077
- const otherDbModel = result[otherModelName];
1078
- if (!otherDbModel)
1079
- throw new Error(`Cannot find model by name ${otherModelName}`);
1078
+ const otherTableName = otherTable[0];
1079
+ const otherDbTable = result[otherTableName];
1080
+ if (!otherDbTable)
1081
+ throw new Error(`Cannot find table class by name ${otherTableName}`);
1080
1082
  const data = {
1081
1083
  relationName,
1082
1084
  relation,
1083
- dbModel,
1084
- otherDbModel
1085
+ dbTable,
1086
+ otherDbTable
1085
1087
  };
1086
1088
  const options = relation.options;
1087
1089
  if (typeof options.through === "string" && typeof options.source === "string") {
1088
- const throughRelation = getThroughRelation(dbModel, options.through);
1090
+ const throughRelation = getThroughRelation(dbTable, options.through);
1089
1091
  if (!throughRelation) {
1090
- delayRelation(delayedRelations, dbModel, options.through, data);
1092
+ delayRelation(delayedRelations, dbTable, options.through, data);
1091
1093
  continue;
1092
1094
  }
1093
1095
  const sourceRelation = getSourceRelation(
@@ -1097,7 +1099,7 @@ const applyRelations = (qb, models, result) => {
1097
1099
  if (!sourceRelation) {
1098
1100
  delayRelation(
1099
1101
  delayedRelations,
1100
- throughRelation.model,
1102
+ throughRelation.table,
1101
1103
  options.source,
1102
1104
  data
1103
1105
  );
@@ -1112,42 +1114,42 @@ const applyRelations = (qb, models, result) => {
1112
1114
  for (const key in value) {
1113
1115
  for (const item of value[key]) {
1114
1116
  const { relation } = item;
1115
- if (item.dbModel.relations[item.relationName])
1117
+ if (item.dbTable.relations[item.relationName])
1116
1118
  continue;
1117
- const as = item.dbModel.definedAs;
1119
+ const as = item.dbTable.definedAs;
1118
1120
  let message = `Cannot define a \`${item.relationName}\` relation on \`${as}\``;
1119
- const model = result[as];
1121
+ const table = result[as];
1120
1122
  const { through, source } = relation.options;
1121
- const throughRel = model.relations[through];
1123
+ const throughRel = table.relations[through];
1122
1124
  if (through && !throughRel) {
1123
1125
  message += `: cannot find \`${through}\` relation required by the \`through\` option`;
1124
- } else if (source && throughRel && !throughRel.model.relations[source]) {
1125
- message += `: cannot find \`${source}\` relation in \`${throughRel.model.definedAs}\` required by the \`source\` option`;
1126
+ } else if (source && throughRel && !throughRel.table.relations[source]) {
1127
+ message += `: cannot find \`${source}\` relation in \`${throughRel.table.definedAs}\` required by the \`source\` option`;
1126
1128
  }
1127
1129
  throw new Error(message);
1128
1130
  }
1129
1131
  }
1130
1132
  }
1131
1133
  };
1132
- const delayRelation = (delayedRelations, model, relationName, data) => {
1133
- let modelRelations = delayedRelations.get(model);
1134
- if (!modelRelations) {
1135
- modelRelations = {};
1136
- delayedRelations.set(model, modelRelations);
1134
+ const delayRelation = (delayedRelations, table, relationName, data) => {
1135
+ let tableRelations = delayedRelations.get(table);
1136
+ if (!tableRelations) {
1137
+ tableRelations = {};
1138
+ delayedRelations.set(table, tableRelations);
1137
1139
  }
1138
- if (modelRelations[relationName]) {
1139
- modelRelations[relationName].push(data);
1140
+ if (tableRelations[relationName]) {
1141
+ tableRelations[relationName].push(data);
1140
1142
  } else {
1141
- modelRelations[relationName] = [data];
1143
+ tableRelations[relationName] = [data];
1142
1144
  }
1143
1145
  };
1144
- const applyRelation = (qb, { relationName, relation, dbModel, otherDbModel }, delayedRelations) => {
1146
+ const applyRelation = (qb, { relationName, relation, dbTable, otherDbTable }, delayedRelations) => {
1145
1147
  var _a;
1146
- const query = (relation.options.scope ? relation.options.scope(otherDbModel) : otherDbModel).as(relationName);
1148
+ const query = (relation.options.scope ? relation.options.scope(otherDbTable) : otherDbTable).as(relationName);
1147
1149
  const definedAs = query.definedAs;
1148
1150
  if (!definedAs) {
1149
1151
  throw new Error(
1150
- `Model for table ${query.table} is not attached to db instance`
1152
+ `Table class for table ${query.table} is not attached to db instance`
1151
1153
  );
1152
1154
  }
1153
1155
  const { type } = relation;
@@ -1155,12 +1157,12 @@ const applyRelation = (qb, { relationName, relation, dbModel, otherDbModel }, de
1155
1157
  if (type === "belongsTo") {
1156
1158
  data = makeBelongsToMethod(relation, relationName, query);
1157
1159
  } else if (type === "hasOne") {
1158
- data = makeHasOneMethod(dbModel, relation, relationName, query);
1160
+ data = makeHasOneMethod(dbTable, relation, relationName, query);
1159
1161
  } else if (type === "hasMany") {
1160
- data = makeHasManyMethod(dbModel, relation, relationName, query);
1162
+ data = makeHasManyMethod(dbTable, relation, relationName, query);
1161
1163
  } else if (type === "hasAndBelongsToMany") {
1162
1164
  data = makeHasAndBelongsToManyMethod(
1163
- dbModel,
1165
+ dbTable,
1164
1166
  qb,
1165
1167
  relation,
1166
1168
  relationName,
@@ -1173,38 +1175,38 @@ const applyRelation = (qb, { relationName, relation, dbModel, otherDbModel }, de
1173
1175
  query._take();
1174
1176
  }
1175
1177
  if (data.virtualColumn) {
1176
- dbModel.shape[relationName] = data.virtualColumn;
1178
+ dbTable.shape[relationName] = data.virtualColumn;
1177
1179
  }
1178
- makeRelationQuery(dbModel, definedAs, relationName, data);
1179
- dbModel.relations[relationName] = {
1180
+ makeRelationQuery(dbTable, definedAs, relationName, data);
1181
+ dbTable.relations[relationName] = {
1180
1182
  type,
1181
1183
  key: relationName,
1182
- model: otherDbModel,
1184
+ table: otherDbTable,
1183
1185
  query,
1184
1186
  joinQuery: data.joinQuery,
1185
1187
  primaryKey: data.primaryKey,
1186
1188
  options: relation.options
1187
1189
  };
1188
- const modelRelations = delayedRelations.get(dbModel);
1189
- if (!modelRelations)
1190
+ const tableRelations = delayedRelations.get(dbTable);
1191
+ if (!tableRelations)
1190
1192
  return;
1191
- (_a = modelRelations[relationName]) == null ? void 0 : _a.forEach((data2) => {
1193
+ (_a = tableRelations[relationName]) == null ? void 0 : _a.forEach((data2) => {
1192
1194
  applyRelation(qb, data2, delayedRelations);
1193
1195
  });
1194
1196
  };
1195
- const makeRelationQuery = (model, definedAs, relationName, data) => {
1196
- Object.defineProperty(model, relationName, {
1197
+ const makeRelationQuery = (table, definedAs, relationName, data) => {
1198
+ Object.defineProperty(table, relationName, {
1197
1199
  get() {
1198
1200
  var _a;
1199
- const toModel = this.db[definedAs].as(relationName);
1201
+ const toTable = this.db[definedAs].as(relationName);
1200
1202
  if (data.returns === "one") {
1201
- toModel._take();
1203
+ toTable._take();
1202
1204
  }
1203
- const query = this.isSubQuery ? toModel : toModel._whereExists(data.reverseJoin(this, toModel), (q) => q);
1205
+ const query = this.isSubQuery ? toTable : toTable._whereExists(data.reverseJoin(this, toTable), (q) => q);
1204
1206
  query.query[pqb.relationQueryKey] = {
1205
1207
  relationName,
1206
1208
  sourceQuery: this,
1207
- relationQuery: toModel,
1209
+ relationQuery: toTable,
1208
1210
  joinQuery: data.joinQuery
1209
1211
  };
1210
1212
  const setQuery = (_a = data.modifyRelatedQuery) == null ? void 0 : _a.call(data, query);
@@ -1227,10 +1229,10 @@ function transaction(fn) {
1227
1229
  for (const key in this) {
1228
1230
  const value = this[key];
1229
1231
  if (value instanceof pqb.Db) {
1230
- const model = value.transacting(q);
1231
- model.__model = model;
1232
- model.db = orm;
1233
- orm[key] = model;
1232
+ const table = value.transacting(q);
1233
+ table.__table = table;
1234
+ table.db = orm;
1235
+ orm[key] = table;
1234
1236
  } else {
1235
1237
  orm[key] = value;
1236
1238
  }
@@ -1270,7 +1272,7 @@ var __objRest = (source, exclude) => {
1270
1272
  }
1271
1273
  return target;
1272
1274
  };
1273
- const orchidORM = (_a, models) => {
1275
+ const orchidORM = (_a, tables) => {
1274
1276
  var _b = _a, {
1275
1277
  log,
1276
1278
  logger,
@@ -1304,31 +1306,31 @@ const orchidORM = (_a, models) => {
1304
1306
  $queryBuilder: qb,
1305
1307
  $close: () => adapter.close()
1306
1308
  };
1307
- const modelInstances = {};
1308
- for (const key in models) {
1309
+ const tableInstances = {};
1310
+ for (const key in tables) {
1309
1311
  if (key[0] === "$") {
1310
- throw new Error(`Model name must not start with $`);
1312
+ throw new Error(`Table class name must not start with $`);
1311
1313
  }
1312
- const model = new models[key]();
1313
- modelInstances[key] = model;
1314
+ const table = new tables[key]();
1315
+ tableInstances[key] = table;
1314
1316
  const options2 = __spreadProps(__spreadValues$1({}, commonOptions), {
1315
- schema: model.schema
1317
+ schema: table.schema
1316
1318
  });
1317
- if (model.noPrimaryKey)
1319
+ if (table.noPrimaryKey)
1318
1320
  options2.noPrimaryKey = "ignore";
1319
- const dbModel = new pqb.Db(
1321
+ const dbTable = new pqb.Db(
1320
1322
  adapter,
1321
1323
  qb,
1322
- model.table,
1323
- model.columns.shape,
1324
- model.columnTypes,
1324
+ table.table,
1325
+ table.columns.shape,
1326
+ table.columnTypes,
1325
1327
  options2
1326
1328
  );
1327
- dbModel.definedAs = key;
1328
- dbModel.db = result;
1329
- result[key] = dbModel;
1329
+ dbTable.definedAs = key;
1330
+ dbTable.db = result;
1331
+ result[key] = dbTable;
1330
1332
  }
1331
- applyRelations(qb, modelInstances, result);
1333
+ applyRelations(qb, tableInstances, result);
1332
1334
  return result;
1333
1335
  };
1334
1336
 
@@ -1348,26 +1350,26 @@ var __spreadValues = (a, b) => {
1348
1350
  }
1349
1351
  return a;
1350
1352
  };
1351
- const createRepo = (model, methods) => {
1353
+ const createRepo = (table, methods) => {
1352
1354
  const queryMethods = __spreadValues(__spreadValues(__spreadValues(__spreadValues({}, methods.queryMethods), methods.queryOneMethods), methods.queryWithWhereMethods), methods.queryOneWithWhereMethods);
1353
1355
  const plainMethods = methods.methods;
1354
1356
  const repo = (q2) => {
1355
- const proto = Object.create(q2.__model);
1356
- proto.__model = proto;
1357
+ const proto = Object.create(q2.__table);
1358
+ proto.__table = proto;
1357
1359
  const result = Object.create(proto);
1358
1360
  result.query = pqb.getClonedQueryData(q2.query);
1359
1361
  if (plainMethods) {
1360
- Object.assign(proto.__model, plainMethods);
1362
+ Object.assign(proto.__table, plainMethods);
1361
1363
  }
1362
1364
  for (const key in queryMethods) {
1363
1365
  const method = queryMethods[key];
1364
- proto.__model[key] = function(...args) {
1366
+ proto.__table[key] = function(...args) {
1365
1367
  return method(this, ...args);
1366
1368
  };
1367
1369
  }
1368
1370
  return result;
1369
1371
  };
1370
- const q = repo(model);
1372
+ const q = repo(table);
1371
1373
  return new Proxy(repo, {
1372
1374
  get(_, key) {
1373
1375
  return q[key];
@@ -1375,7 +1377,7 @@ const createRepo = (model, methods) => {
1375
1377
  });
1376
1378
  };
1377
1379
 
1378
- exports.createModel = createModel;
1380
+ exports.createBaseTable = createBaseTable;
1379
1381
  exports.createRepo = createRepo;
1380
1382
  exports.orchidORM = orchidORM;
1381
1383
  //# sourceMappingURL=index.js.map