leoric 2.9.1 → 2.9.2

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leoric",
3
- "version": "2.9.1",
3
+ "version": "2.9.2",
4
4
  "description": "JavaScript Object-relational mapping alchemy",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -8,7 +8,7 @@ import { AbstractBone } from '../types/abstract_bone';
8
8
  import { Spell } from '../spell';
9
9
 
10
10
  type WhereConditions<T extends typeof SequelizeBone> = {
11
- [Property in keyof Extract<InstanceType<T>, Literal>]?: Literal | Literal[] | OperatorCondition;
11
+ [Property in BoneColumns<T>]?: Literal | Literal[] | OperatorCondition;
12
12
  } | {
13
13
  [key in '$and' | '$or']?: WhereConditions<T>[];
14
14
  }
@@ -41,9 +41,7 @@ interface SequelizeConditions<T extends typeof SequelizeBone> extends BaseSequel
41
41
  }
42
42
 
43
43
  interface FindOrCreateOptions<T extends typeof SequelizeBone> extends BaseSequelizeConditions<T> {
44
- defaults?: {
45
- [Property in keyof Extract<InstanceType<T>, Literal>]?: Literal
46
- }
44
+ defaults?: BoneCreateValues<T>;
47
45
  }
48
46
 
49
47
  interface FindOrBuildOptions<T extends typeof SequelizeBone> extends FindOrCreateOptions<T> {
@@ -130,19 +128,19 @@ export class SequelizeBone extends AbstractBone {
130
128
  */
131
129
  static bulkBuild<T extends typeof SequelizeBone>(this:T, valueSets: Array<BoneCreateValues<T>>, options?: BoneOptions): Array<InstanceType<T>>;
132
130
 
131
+ static count<T extends typeof SequelizeBone>(this: T, conditions?: SequelizeConditions<T>): Spell<T, ResultSet<T> | number>;
133
132
  static count<T extends typeof SequelizeBone>(this: T, name?: BoneColumns<T>): Spell<T, ResultSet<T> | number>;
134
133
  static count<T extends typeof SequelizeBone>(this: T, name?: Raw | '*'): Spell<T, ResultSet<T> | number>;
135
- static count<T extends typeof SequelizeBone>(this: T, conditions?: SequelizeConditions<T>): Spell<T, ResultSet<T> | number>;
136
134
 
137
135
  static decrement<T extends typeof SequelizeBone>(
138
136
  this: T,
139
- fields: { [Property in keyof Extract<InstanceType<T>, Literal>]?: number } | string | Array<BoneColumns<T> | string> ,
137
+ fields: { [Property in BoneColumns<T>]?: number } | string | Array<BoneColumns<T> | string> ,
140
138
  options?: SequelizeConditions<T>
141
139
  ): Spell<T, QueryResult>;
142
140
 
143
141
  static increment<T extends typeof SequelizeBone>(
144
142
  this: T,
145
- fields: { [Property in keyof Extract<InstanceType<T>, Literal>]?: number } | string | Array<BoneColumns<T> | string> ,
143
+ fields: { [Property in BoneColumns<T>]?: number } | string | Array<BoneColumns<T> | string> ,
146
144
  options?: SequelizeConditions<T>
147
145
  ): Spell<T, QueryResult>;
148
146
 
@@ -161,7 +159,7 @@ export class SequelizeBone extends AbstractBone {
161
159
  static findAll<T extends typeof SequelizeBone>(this: T, options?: SequelizeConditions<T>): Spell<T, Collection<InstanceType<T>>>;
162
160
  static find<T extends typeof SequelizeBone>(this: T, options?: SequelizeConditions<T>): Spell<T, InstanceType<T> | null>;
163
161
  static findAndCountAll<T extends typeof SequelizeBone>(this: T, options?: SequelizeConditions<T>): Promise<{
164
- rows: Array<typeof SequelizeBone>,
162
+ rows: Array<InstanceType<T>>,
165
163
  count: number,
166
164
  }>
167
165
 
package/src/collection.js CHANGED
@@ -98,16 +98,18 @@ function dispatch(spell, rows, fields) {
98
98
 
99
99
  const results = new Collection();
100
100
  for (const row of rows) {
101
- const result = {};
102
- for (const prop in row) {
101
+ const result = Object.keys(row).reduce((res, prop) => {
103
102
  const data = row[prop];
104
103
  const qualifier = prop === table ? tableAlias : prop;
105
104
  if (qualifier === '' || qualifier === tableAlias) {
106
- Object.assign(result, data);
105
+ Object.assign(res, data);
107
106
  } else {
108
- if (Object.values(data).some(value => value != null)) result[prop] = data;
107
+ if (Object.values(data).some(value => value != null)) {
108
+ res[prop] = data;
109
+ }
109
110
  }
110
- }
111
+ return res;
112
+ }, {});
111
113
  let current;
112
114
  if (joined && result[primaryColumn] != null) {
113
115
  current = results.find(r => r[primaryKey] == result[primaryColumn]);
package/src/spell.d.ts CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  Literal, command, Raw, Connection,
3
3
  ResultSet, QueryResult,
4
4
  QueryOptions, SetOptions, WithOptions,
5
- Collection, WhereConditions, OrderOptions, BoneColumns,
5
+ Collection, WhereConditions, OrderOptions, BoneColumns, OnConditions,
6
6
  } from './types/common';
7
7
  import { AbstractBone } from './types/abstract_bone';
8
8
  import { Hint, IndexHint, CommonHintsArgs, HintInterface } from './hint';
@@ -92,6 +92,9 @@ export class Spellbook {
92
92
  export class Spell<T extends typeof AbstractBone, U = InstanceType<T> | Collection<InstanceType<T>> | ResultSet<T> | number | null> extends Promise<U> {
93
93
  constructor(Model: T, opts: SpellOptions);
94
94
 
95
+ Model: T;
96
+ connection: Connection;
97
+
95
98
  command: string;
96
99
  scopes: Function[];
97
100
 
@@ -107,7 +110,7 @@ export class Spell<T extends typeof AbstractBone, U = InstanceType<T> | Collecti
107
110
  with(...qualifiers: string[]): Spell<T, U>;
108
111
 
109
112
  join<V extends typeof AbstractBone>(Model: V, onConditions: string, ...values: Literal[]): Spell<T, U>;
110
- join<V extends typeof AbstractBone>(Model: V, onConditions: WhereConditions<T>): Spell<T, U>;
113
+ join<V extends typeof AbstractBone>(Model: V, onConditions: OnConditions<T>): Spell<T, U>;
111
114
 
112
115
  $where(conditions: WhereConditions<T>): this;
113
116
  $where(conditions: string, ...values: Literal[]): this;
@@ -165,9 +168,9 @@ export class Spell<T extends typeof AbstractBone, U = InstanceType<T> | Collecti
165
168
  toString(): string;
166
169
 
167
170
  all: Spell<T, U>;
168
- first: Spell<T, InstanceType<T>>;
169
- last: Spell<T, InstanceType<T>>;
170
- get(index: number): Spell<T, InstanceType<T>>;
171
+ first: Spell<T, InstanceType<T> | null>;
172
+ last: Spell<T, InstanceType<T> | null>;
173
+ get(index: number): Spell<T, InstanceType<T> | null>;
171
174
 
172
175
  unscoped: Spell<T, U>;
173
176
  unparanoid: Spell<T, U>;
@@ -3,7 +3,7 @@ import {
3
3
  Pool, Literal, WhereConditions,
4
4
  Collection, ResultSet, OrderOptions,
5
5
  QueryOptions, AttributeMeta, AssociateOptions, Values, Connection, BulkCreateOptions, BoneCreateValues,
6
- GeneratorReturnType, BoneColumns, InstanceColumns, Raw,
6
+ GeneratorReturnType, BoneColumns, InstanceColumns, Raw, OnConditions,
7
7
  } from './common';
8
8
  import { AbstractDriver } from '../drivers';
9
9
  import { Spell } from '../spell';
@@ -110,7 +110,7 @@ export class AbstractBone {
110
110
  static renameAttribute<T extends typeof AbstractBone>(this: T, originalName: string, newName: string): void;
111
111
 
112
112
  static alias<T extends typeof AbstractBone>(this: T, name: BoneColumns<T>): string;
113
- static alias<T extends typeof AbstractBone>(this: T, data: Record<BoneColumns<T>, Literal>): Record<string, Literal>;
113
+ static alias<T extends typeof AbstractBone>(this: T, data: { [key in BoneColumns<T>]: Literal }): Record<string, Literal>;
114
114
  static unalias<T extends typeof AbstractBone>(this: T, name: BoneColumns<T>): string;
115
115
 
116
116
  // after alias/unalias
@@ -127,7 +127,7 @@ export class AbstractBone {
127
127
  * @example
128
128
  * Bone.create({ foo: 1, bar: 'baz' })
129
129
  */
130
- static create<T extends typeof AbstractBone>(this: T, values: BoneCreateValues<T> & Partial<Record<BoneColumns<T>, Literal>>, options?: QueryOptions): Promise<InstanceType<T>>;
130
+ static create<T extends typeof AbstractBone>(this: T, values: BoneCreateValues<T>, options?: QueryOptions): Promise<InstanceType<T>>;
131
131
 
132
132
  /**
133
133
  * INSERT or UPDATE rows
@@ -192,8 +192,8 @@ export class AbstractBone {
192
192
  * @example
193
193
  * Bone.join(Muscle, 'bones.id == muscles.boneId')
194
194
  */
195
- static join<T extends typeof AbstractBone>(this: T, Model: AbstractBone, onConditions: WhereConditions<T>): Spell<T, Collection<InstanceType<T>>>;
196
- static join<T extends typeof AbstractBone>(this: T, Model: AbstractBone, onConditions: string, ...values: Literal[]): Spell<T, Collection<InstanceType<T>>>;
195
+ static join<T extends typeof AbstractBone, U extends typeof AbstractBone>(this: T, Model: U, onConditions: OnConditions<T>): Spell<T, Collection<InstanceType<T>>>;
196
+ static join<T extends typeof AbstractBone, U extends typeof AbstractBone>(this: T, Model: U, onConditions: string, ...values: Literal[]): Spell<T, Collection<InstanceType<T>>>;
197
197
 
198
198
  /**
199
199
  * Set WHERE conditions
@@ -30,6 +30,10 @@ export interface QueryResult {
30
30
  fields?: Array<{ table: string, name: string }>,
31
31
  }
32
32
 
33
+ interface TransactionMethodOptions {
34
+ Model: typeof AbstractBone;
35
+ }
36
+
33
37
  export interface Connection {
34
38
  /**
35
39
  * MySQL
@@ -44,6 +48,13 @@ export interface Connection {
44
48
  values?: Array<Literal | Literal[]>,
45
49
  callback?: (err: Error, result: QueryResult) => void,
46
50
  ): void
51
+
52
+ begin(opts: TransactionMethodOptions): Promise<void>;
53
+
54
+ commit(opts: TransactionMethodOptions): Promise<void>;
55
+
56
+ rollback(opts: TransactionMethodOptions): Promise<void>;
57
+
47
58
  }
48
59
 
49
60
  export declare class Pool {
@@ -65,7 +76,7 @@ export interface QueryOptions {
65
76
  }
66
77
 
67
78
  export type BulkCreateOptions = QueryOptions & {
68
- updateOnDuplicate?: string[];
79
+ updateOnDuplicate?: string[] | true;
69
80
  fields?: string[];
70
81
  }
71
82
 
@@ -147,7 +158,7 @@ export class Raw {
147
158
  }
148
159
 
149
160
  export type SetOptions<T extends typeof AbstractBone> = {
150
- [Property in keyof Extract<InstanceType<T>, Literal>]: Literal
161
+ [Property in BoneColumns<T>]: Literal
151
162
  } | {
152
163
  [key: string]: Literal
153
164
  };
@@ -156,10 +167,12 @@ export type WithOptions = {
156
167
  [qualifier: string]: { select: string | string[], throughRelation?: string }
157
168
  }
158
169
 
170
+ type OrderSortType = 'desc' | 'asc' | Uppercase<'desc' | 'asc'>;
171
+
159
172
  type OrderOptions<T extends typeof AbstractBone> = {
160
- [key in keyof Extract<InstanceType<T>, Literal>]?: 'desc' | 'asc'
161
- } | [ BoneColumns<T>, 'desc' | 'asc' ]
162
- | Array<BoneColumns<T> | [ BoneColumns<T>, 'desc' | 'asc' ] | Raw | string | Array<Raw | string>>
173
+ [Property in Extract<BoneColumns<T>, Literal>]?: OrderSortType
174
+ } | [ BoneColumns<T>, OrderSortType ]
175
+ | Array<BoneColumns<T> | [ BoneColumns<T>, OrderSortType ] | Raw | string | Array<Raw | string>>
163
176
  | string | Raw;
164
177
 
165
178
  export class Collection<T extends AbstractBone> extends Array<T> {
@@ -169,7 +182,13 @@ export class Collection<T extends AbstractBone> extends Array<T> {
169
182
  }
170
183
 
171
184
  export type WhereConditions<T extends typeof AbstractBone> = {
172
- [Property in keyof Extract<InstanceType<T>, Literal>]?: Literal | Literal[] | OperatorCondition;
185
+ [Property in BoneColumns<T>]?: Literal | Literal[] | OperatorCondition;
186
+ } | {
187
+ [key in '$and' | '$or']?: WhereConditions<T>[];
188
+ }
189
+
190
+ export type OnConditions<T extends typeof AbstractBone> = {
191
+ [Property in BoneColumns<T>]?: Literal | Literal[] | OperatorCondition ;
173
192
  } | {
174
193
  [key in '$and' | '$or']?: WhereConditions<T>[];
175
194
  }