@stacksjs/orm 0.69.2 → 0.70.0

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.
@@ -1,43 +1,55 @@
1
- declare interface WhereCondition {
1
+ export declare type Operator = '=' | '<' | '>' | '<=' | '>=' | '<>' | '!=' | 'like' | 'not like' | 'in' | 'not in' | 'between' | 'not between' | 'is' | 'is not'
2
+
3
+ interface WhereCondition<T, V = any> {
2
4
  type: 'and' | 'or'
3
5
  method: 'where' | 'whereIn' | 'whereNull' | 'whereNotNull' | 'whereBetween' | 'whereExists'
4
- column: string
5
- operator?: string
6
- value?: any
7
- values?: any[]
8
- callback?: (query: SubqueryBuilder) => void
6
+ column: keyof T
7
+ operator?: Operator
8
+ value?: V
9
+ values?: V[] | [V, V]
10
+ range?: [V, V]
11
+ callback?: (query: SubqueryBuilder<T>) => void
9
12
  }
10
- export declare class SubqueryBuilder {
11
- private conditions: WhereCondition[] = []
12
13
 
13
- where(...args: (string | number | boolean | undefined | null)[]): void {
14
- this.addCondition('and', 'where', ...args)
14
+ export class SubqueryBuilder<T> {
15
+ private conditions: WhereCondition<T>[] = []
16
+
17
+ where<V>(column: keyof T, ...args: [V] | [Operator, V]): void {
18
+ const [operatorOrValue, value] = args
19
+ const operator = value === undefined ? '=' : operatorOrValue as Operator
20
+ const actualValue: V = value === undefined ? operatorOrValue as V : value
21
+
22
+ this.addCondition('and', 'where', column, operator, actualValue)
15
23
  }
16
24
 
17
- orWhere(...args: (string | number | boolean | undefined | null)[]): void {
18
- this.addCondition('or', 'where', ...args)
25
+ orWhere<V>(column: keyof T, ...args: [V] | [Operator, V]): void {
26
+ const [operatorOrValue, value] = args
27
+ const operator = value === undefined ? '=' : operatorOrValue as Operator
28
+ const actualValue: V = value === undefined ? operatorOrValue as V : value
29
+
30
+ this.addCondition('or', 'where', column, operator, actualValue)
19
31
  }
20
32
 
21
- whereIn(column: string, values: any[]): void {
33
+ whereIn<V>(column: keyof T, values: V[]): void {
22
34
  this.conditions.push({
23
35
  type: 'and',
24
36
  method: 'whereIn',
25
37
  column,
26
38
  values,
27
- })
39
+ } as WhereCondition<T, V>)
28
40
  }
29
41
 
30
- whereNotIn(column: string, values: any[]): void {
42
+ whereNotIn<V>(column: keyof T, values: V[]): void {
31
43
  this.conditions.push({
32
44
  type: 'and',
33
45
  method: 'whereIn',
34
46
  column,
35
47
  values,
36
- operator: 'not',
37
- })
48
+ operator: 'not in',
49
+ } as WhereCondition<T, V>)
38
50
  }
39
51
 
40
- whereNull(column: string): void {
52
+ whereNull(column: keyof T): void {
41
53
  this.conditions.push({
42
54
  type: 'and',
43
55
  method: 'whereNull',
@@ -45,7 +57,7 @@ export declare class SubqueryBuilder {
45
57
  })
46
58
  }
47
59
 
48
- whereNotNull(column: string): void {
60
+ whereNotNull(column: keyof T): void {
49
61
  this.conditions.push({
50
62
  type: 'and',
51
63
  method: 'whereNotNull',
@@ -53,44 +65,29 @@ export declare class SubqueryBuilder {
53
65
  })
54
66
  }
55
67
 
56
- whereBetween(column: string, range: [any, any]): void {
68
+ whereBetween<V>(column: keyof T, range: [V, V]): void {
57
69
  this.conditions.push({
58
70
  type: 'and',
59
71
  method: 'whereBetween',
60
72
  column,
61
- values: range,
73
+ range,
62
74
  })
63
75
  }
64
76
 
65
- whereExists(callback: (query: SubqueryBuilder) => void): void {
77
+ whereExists(callback: (query: SubqueryBuilder<T>) => void): void {
66
78
  this.conditions.push({
67
79
  type: 'and',
68
80
  method: 'whereExists',
69
- column: '',
81
+ column: '' as keyof T,
70
82
  callback,
71
83
  })
72
84
  }
73
85
 
74
- private addCondition(type: 'and' | 'or', method: 'where', ...args: (string | number | boolean | undefined | null)[]): void {
75
- let column: any
76
- let operator: any
77
- let value: any
78
-
79
- if (args.length === 2) {
80
- [column, value] = args
81
- operator = '='
82
- }
83
- else if (args.length === 3) {
84
- [column, operator, value] = args
85
- }
86
- else {
87
- throw new HttpError(500, 'Invalid number of arguments')
88
- }
89
-
86
+ private addCondition<V>(type: 'and' | 'or', method: 'where', column: keyof T, operator: Operator, value: V): void {
90
87
  this.conditions.push({ type, method, column, operator, value })
91
88
  }
92
89
 
93
- getConditions(): WhereCondition[] {
90
+ getConditions(): WhereCondition<T>[] {
94
91
  return this.conditions
95
92
  }
96
93
  }
package/dist/utils.d.ts CHANGED
@@ -8,7 +8,7 @@ export async function modelTableName(model: Model | ModelPath): Promise<string>
8
8
  return model.table ?? snakeCase(plural(model?.name || ''))
9
9
  }
10
10
  export declare function getModelName(model: Model, modelPath: string): string;
11
- export declare function getTableName(model: Model, modelPath: string): string;
11
+ export declare function getTableName(model: Model, modelPath: string): TableNames;
12
12
  export declare function getPivotTableName(formattedModelName: string, modelRelationTable: string): string;
13
13
  export declare function hasRelations(obj: any, key: string): boolean;
14
14
  export declare function getRelationType(relation: string): string;
@@ -17,4 +17,6 @@ export declare function getHiddenAttributes(attributes: AttributesElements | und
17
17
  export declare function getGuardedAttributes(model: Model): string[];
18
18
  export declare function getFillableAttributes(model: Model, otherModelRelations: RelationConfig[]): string[];
19
19
  declare function parseRule(rule: string): FieldArrayElement | null;
20
- export declare function mapEntity(attribute: ModelElement): string | undefined;
20
+ export declare function mapEntity(attribute: ModelElement): string | undefined;
21
+ export declare function findCoreModel(modelName: string): string;
22
+ export declare function findUserModel(modelName: string): string;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/orm",
3
3
  "type": "module",
4
- "version": "0.69.2",
4
+ "version": "0.70.0",
5
5
  "description": "The Stacks ORM integration",
6
6
  "author": "Chris Breuer",
7
7
  "contributors": ["Chris Breuer <chris@stacksjs.org>"],
@@ -34,10 +34,10 @@
34
34
  "prepublishOnly": "bun run build"
35
35
  },
36
36
  "devDependencies": {
37
- "@stacksjs/build": "0.69.2",
38
- "@stacksjs/config": "0.69.2",
39
- "@stacksjs/development": "0.69.2",
40
- "@stacksjs/query-builder": "0.69.2",
41
- "@stacksjs/types": "0.69.2"
37
+ "@stacksjs/build": "0.70.0",
38
+ "@stacksjs/config": "0.70.0",
39
+ "@stacksjs/development": "0.70.0",
40
+ "@stacksjs/query-builder": "0.70.0",
41
+ "@stacksjs/types": "0.70.0"
42
42
  }
43
43
  }