@loomcore/api 0.1.101 → 0.1.103

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.
@@ -12,7 +12,7 @@ export declare class MigrationRunner {
12
12
  private getMigrator;
13
13
  private loadFileMigrations;
14
14
  private wipeDatabase;
15
- run(command?: 'up' | 'down' | 'reset', target?: string): Promise<void>;
15
+ run(command?: 'up' | 'down' | 'reset' | 'create', target?: string): Promise<void>;
16
16
  private closeConnection;
17
17
  create(name: string): Promise<void>;
18
18
  }
@@ -183,6 +183,13 @@ export class MigrationRunner {
183
183
  }
184
184
  async run(command = 'up', target) {
185
185
  try {
186
+ if (command === 'create') {
187
+ if (!target) {
188
+ throw new Error('Migration name is required for create. Example: npm run migrate create add-users-table');
189
+ }
190
+ await this.create(target);
191
+ return;
192
+ }
186
193
  if (command === 'reset') {
187
194
  if (!this.dbMigrationConfig) {
188
195
  throw new Error('Reset configuration not found');
@@ -15,28 +15,45 @@ function parseJsonValue(value) {
15
15
  }
16
16
  return value;
17
17
  }
18
- function getJoinAliases(operations) {
18
+ function getJoinAliasesAndParents(operations) {
19
19
  const aliases = new Set();
20
+ const parentByAlias = new Map();
20
21
  for (const op of operations) {
21
22
  if (op instanceof LeftJoin || op instanceof InnerJoin || op instanceof LeftJoinMany) {
22
23
  aliases.add(op.as);
24
+ const parent = op.localField.includes('.') ? op.localField.split('.')[0] : null;
25
+ parentByAlias.set(op.as, parent);
23
26
  }
24
27
  }
25
- return aliases;
28
+ return { aliases, parentByAlias };
26
29
  }
27
30
  const PREFIX_SEP = '__';
31
+ function setJoinValue(joinData, alias, value, parentByAlias) {
32
+ const parent = parentByAlias.get(alias) ?? null;
33
+ if (parent) {
34
+ let parentObj = joinData[parent];
35
+ if (parentObj == null || typeof parentObj !== 'object' || Array.isArray(parentObj)) {
36
+ parentObj = {};
37
+ joinData[parent] = parentObj;
38
+ }
39
+ parentObj[alias] = value;
40
+ }
41
+ else {
42
+ joinData[alias] = value;
43
+ }
44
+ }
28
45
  export function transformJoinResults(rows, operations) {
29
- const joinAliases = getJoinAliases(operations);
46
+ const { aliases: joinAliases, parentByAlias } = getJoinAliasesAndParents(operations);
30
47
  if (joinAliases.size === 0) {
31
48
  return rows;
32
49
  }
33
50
  return rows.map((row) => {
34
51
  const transformed = {};
35
- const joinData = {};
52
+ const flatJoinValues = {};
36
53
  const prefixedByAlias = {};
37
54
  for (const key of Object.keys(row)) {
38
55
  if (joinAliases.has(key)) {
39
- joinData[key] = parseJsonValue(row[key]);
56
+ flatJoinValues[key] = parseJsonValue(row[key]);
40
57
  }
41
58
  else if (key.includes(PREFIX_SEP)) {
42
59
  const i = key.indexOf(PREFIX_SEP);
@@ -58,10 +75,20 @@ export function transformJoinResults(rows, operations) {
58
75
  for (const alias of Object.keys(prefixedByAlias)) {
59
76
  const obj = prefixedByAlias[alias];
60
77
  const hasAny = Object.values(obj).some(v => v !== null && v !== undefined);
61
- if (!(alias in joinData)) {
62
- joinData[alias] = hasAny ? obj : null;
78
+ if (!(alias in flatJoinValues)) {
79
+ flatJoinValues[alias] = hasAny ? obj : null;
63
80
  }
64
81
  }
82
+ const joinData = {};
83
+ for (const op of operations) {
84
+ if (!(op instanceof LeftJoin || op instanceof InnerJoin || op instanceof LeftJoinMany))
85
+ continue;
86
+ const alias = op.as;
87
+ const value = flatJoinValues[alias];
88
+ if (value === undefined)
89
+ continue;
90
+ setJoinValue(joinData, alias, value, parentByAlias);
91
+ }
65
92
  if (Object.keys(joinData).length > 0) {
66
93
  transformed._joinData = joinData;
67
94
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/api",
3
- "version": "0.1.101",
3
+ "version": "0.1.103",
4
4
  "private": false,
5
5
  "description": "Loom Core Api - An opinionated Node.js api using Typescript, Express, and MongoDb or PostgreSQL",
6
6
  "scripts": {