leoric 2.7.2 → 2.8.1

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/index.js CHANGED
@@ -52,6 +52,7 @@ Object.assign(Realm, {
52
52
  connect,
53
53
  disconnect,
54
54
  Bone,
55
+ SequelizeBone: sequelize(Bone),
55
56
  Collection,
56
57
  DataTypes,
57
58
  Logger,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "leoric",
3
- "version": "2.7.2",
3
+ "version": "2.8.1",
4
4
  "description": "JavaScript Object-relational mapping alchemy",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -0,0 +1,190 @@
1
+ import {
2
+ Attributes, Literal, OperatorCondition,
3
+ BoneOptions, ResultSet, Raw,
4
+ SetOptions, BeforeHooksType, AfterHooksType,
5
+ QueryOptions, OrderOptions, QueryResult
6
+ } from '../types/common';
7
+ import { AbstractBone } from '../types/abstract_bone';
8
+ import { Spell } from '../spell';
9
+
10
+ type WhereConditions<T extends typeof SequelizeBone> = {
11
+ [Property in keyof Extract<InstanceType<T>, Literal>]?: Literal | Literal[] | OperatorCondition;
12
+ } | {
13
+ [key in '$and' | '$or']?: WhereConditions<T>[];
14
+ }
15
+
16
+ interface SequelizeDestroyOptions extends QueryOptions {
17
+ force?: boolean;
18
+ }
19
+
20
+ interface BaseSequelizeConditions<T extends typeof SequelizeBone> extends QueryOptions {
21
+ where?: WhereConditions<T>;
22
+ order?: OrderOptions<T>;
23
+ limit?: number;
24
+ attributes?: string | Raw | Array<[keyof Extract<InstanceType<T>, Literal>] | string | Raw> | [keyof Extract<InstanceType<T>, Literal>];
25
+ offset?: number;
26
+ }
27
+
28
+ interface SequelizeConditions<T extends typeof SequelizeBone> extends BaseSequelizeConditions<T> {
29
+ group?: string | string[] | Raw;
30
+ having?: WhereConditions<T> | string | { [key:string]: Literal | Literal[] } | Raw;
31
+ include?: string | Raw;
32
+ }
33
+
34
+ interface FindOrCreateOptions<T extends typeof SequelizeBone> extends BaseSequelizeConditions<T> {
35
+ defaults?: {
36
+ [Property in keyof Extract<InstanceType<T>, Literal>]?: Literal
37
+ }
38
+ }
39
+
40
+ interface FindOrBuildOptions<T extends typeof SequelizeBone> extends FindOrCreateOptions<T> {
41
+ raw?: boolean;
42
+ isNewRecord?: boolean;
43
+ validate?: boolean;
44
+ }
45
+
46
+ interface DestroyOptions<T extends typeof SequelizeBone> extends SequelizeConditions<T> {
47
+ force?: boolean;
48
+ }
49
+
50
+ type ScopeOptions = {
51
+ override?: boolean
52
+ }
53
+
54
+ type Values<T extends typeof SequelizeBone> = {
55
+ [Property in keyof Extract<InstanceType<T>, Literal>]?: Literal;
56
+ }
57
+
58
+ type aggregators = 'count' | 'COUNT' | 'average' | 'AVERAGE' | 'minimum' | 'MINIMUM' | 'maximum' | 'MAXIMUM' | 'sum' | 'SUM';
59
+
60
+ export class Collection<T extends SequelizeBone> extends Array<T> {
61
+ save(): Promise<void>;
62
+ toJSON(): Object[];
63
+ toObject(): Object[];
64
+ }
65
+
66
+ export class SequelizeBone extends AbstractBone {
67
+
68
+ static get sequelize(): boolean;
69
+
70
+ static get Instance(): SequelizeBone;
71
+
72
+ static get rawAttributes(): Attributes;
73
+
74
+ static getTableName(): boolean;
75
+
76
+ static removeAttribute(name: string): void;
77
+
78
+ /**
79
+ *
80
+ * @static
81
+ * @param {string} name before/after create|destroy|upsert|remove|update
82
+ * @param {string | Function} fnNameOrFun function name or function
83
+ * @param {Function} func hook function
84
+ */
85
+ static addHook(
86
+ name: BeforeHooksType | AfterHooksType | 'beforeDestroy' | 'afterDestroy' | 'beforeBulkDestroy' | 'afterBulkDestroy' | 'beforeBulkUpdate' | 'afterBulkUpdate',
87
+ fnNameOrFun: string | Function,
88
+ func?: Function,
89
+ ): void;
90
+
91
+ /**
92
+ * add scope see https://sequelize.org/master/class/lib/model.js~Model.html#static-method-addScope
93
+ * @deprecated scope is not recommended to use
94
+ * @param name
95
+ * @param scope
96
+ * @param opts
97
+ */
98
+ static addScope<T extends typeof SequelizeBone>(this: T, name: string, scope: ((...args: any[]) => SequelizeConditions<T>) | SequelizeConditions<T>, opts?: ScopeOptions): void;
99
+
100
+ /**
101
+ * @deprecated scope is not recommended to use
102
+ * @param name
103
+ * @param args
104
+ */
105
+ static scope<T extends typeof SequelizeBone>(this: T, name?: (string | ((...args: any[]) => SequelizeConditions<T>) | SequelizeConditions<T> | Array<SequelizeConditions<T>>), ...args: any[]): T;
106
+
107
+ static unscoped(): Spell<typeof SequelizeBone>;
108
+
109
+ /**
110
+ * @deprecated scope is not recommended to use
111
+ * @param name
112
+ * @param args
113
+ */
114
+ static setScope<T extends typeof SequelizeBone>(this: T, name: (string | ((...args: any[]) => SequelizeConditions<T>) | SequelizeConditions<T> | Array<SequelizeConditions<T>>), ...args: any[]): void;
115
+
116
+ static aggregate<T extends typeof SequelizeBone>(this: T, name: string, func: aggregators, options?: SequelizeConditions<T>): Spell<T, number>;
117
+
118
+ static build<T extends typeof SequelizeBone>(this: T, values: Values<T>, options?: BoneOptions): InstanceType<T>;
119
+
120
+ /**
121
+ * see https://github.com/sequelize/sequelize/blob/a729c4df41fa3a58fbecaf879265d2fb73d80e5f/src/model.js#L2299
122
+ * @param valueSets
123
+ * @param options
124
+ */
125
+ static bulkBuild<T extends typeof SequelizeBone>(this:T, valueSets: Array<Values<T>>, options?: BoneOptions): Array<InstanceType<T>>;
126
+
127
+ static count<T extends typeof SequelizeBone>(this: T, name?: string): Spell<T, ResultSet | number>;
128
+ static count<T extends typeof SequelizeBone>(this: T, conditions?: SequelizeConditions<T>): Spell<T, ResultSet | number>;
129
+
130
+ static decrement<T extends typeof SequelizeBone>(
131
+ this: T,
132
+ fields: string | Array<string> | { [Property in keyof Extract<InstanceType<T>, Literal>]?: number },
133
+ options?: SequelizeConditions<T>
134
+ ): Spell<T, QueryResult>;
135
+
136
+ static increment<T extends typeof SequelizeBone>(
137
+ this: T,
138
+ fields: string | Array<string> | { [Property in keyof Extract<InstanceType<T>, Literal>]?: number },
139
+ options?: SequelizeConditions<T>
140
+ ): Spell<T, QueryResult>;
141
+
142
+ static max<T extends typeof SequelizeBone>(this: T, filed: string, options?: SequelizeConditions<T>): Promise<Literal>;
143
+ static min<T extends typeof SequelizeBone>(this: T, filed: string, options?: SequelizeConditions<T>): Promise<Literal>;
144
+ static sum<T extends typeof SequelizeBone>(this: T, filed: string, options?: SequelizeConditions<T>): Promise<Literal>;
145
+
146
+ static destroy<T extends typeof SequelizeBone>(this: T, options?: DestroyOptions<T>): Promise<Array<number> | number>;
147
+ static bulkDestroy<T extends typeof SequelizeBone>(this: T, options?: DestroyOptions<T>): Spell<T, number>;
148
+
149
+ static findAll<T extends typeof SequelizeBone>(this: T, options?: SequelizeConditions<T>): Spell<T, Collection<InstanceType<T>>>;
150
+ static find<T extends typeof SequelizeBone>(this: T, options?: SequelizeConditions<T>): Spell<T, InstanceType<T> | null>;
151
+ static findAndCountAll<T extends typeof SequelizeBone>(this: T, options?: SequelizeConditions<T>): Promise<{
152
+ rows: Array<typeof SequelizeBone>,
153
+ count: number,
154
+ }>
155
+
156
+ static findByPk<T extends typeof SequelizeBone>(this:T, pk: number | bigint | string, options?: Pick<SequelizeConditions<T>, 'paranoid' | 'connection' | 'transaction' |'hint' | 'hints'>): Spell<T, InstanceType<T>>;
157
+
158
+ static findOne<T extends typeof SequelizeBone>(this: T, whereConditions: string, ...values: Literal[]): Spell<T, InstanceType<T>>;
159
+ static findOne<T extends typeof SequelizeBone>(this: T, primaryKey: number | number[] | bigint): Spell<T, InstanceType<T>>;
160
+ static findOne<T extends typeof SequelizeBone>(this: T, options?: SequelizeConditions<T>): Spell<T, InstanceType<T>>;
161
+
162
+ static findCreateFind<T extends typeof SequelizeBone>(this: T, options: FindOrCreateOptions<T>): Promise<InstanceType<T>>;
163
+ static findOrBuild<T extends typeof SequelizeBone>(this: T, options: FindOrBuildOptions<T>): Promise<[InstanceType<T>, boolean]>;
164
+ static findOrCreate<T extends typeof SequelizeBone>(this: T, options: FindOrBuildOptions<T>): Promise<[InstanceType<T>, boolean]>;
165
+
166
+ static restore<T extends typeof SequelizeBone>(this: T, options: BaseSequelizeConditions<T>): Spell<T, number>;
167
+
168
+ static update<T extends typeof SequelizeBone>(this: T, values: SetOptions<T>, options: BaseSequelizeConditions<T>): Promise<number>;
169
+ static bulkUpdate<T extends typeof SequelizeBone>(this: T, values: SetOptions<T>, options: BaseSequelizeConditions<T>): Spell<T, number>;
170
+
171
+ /**
172
+ * An alias of instance constructor. Some legacy code access model name from instance with `this.Model.name`.
173
+ */
174
+ get Model(): typeof SequelizeBone;
175
+ get dataValues(): { [key: string]: Literal };
176
+
177
+ where(): { [key: string]: number | bigint | string };
178
+ set(key: string, value: Literal | Literal[]): void;
179
+ get(key?: string): Literal | { [key: string]: Literal };
180
+ setDataValue(key: string, value: Literal | Literal[]): void;
181
+ getDataValue(key?: string): Literal | { [key: string]: Literal };
182
+ previous(key?: string): Literal | Literal[] | { [key: string]: Literal | Literal[] };
183
+ isSoftDeleted(): boolean;
184
+
185
+ increment(field: string | string[] | { [Property in keyof Extract<this, Literal>]?: number }, options?: QueryOptions): Spell<typeof SequelizeBone, QueryResult>;
186
+ decrement(field: string | string[] | { [Property in keyof Extract<this, Literal>]?: number }, options?: QueryOptions): Spell<typeof SequelizeBone, QueryResult>;
187
+ destroy(options?: SequelizeDestroyOptions): Promise<this| number>;
188
+ }
189
+
190
+ export const sequelize: (Bone: AbstractBone) => typeof SequelizeBone;
@@ -19,7 +19,7 @@ function translateOptions(spell, options) {
19
19
  if (having) spell.$having(having);
20
20
 
21
21
  if (order) {
22
- if (typeof order === 'string' || order instanceof Raw) {
22
+ if (typeof order === 'string' || order instanceof Raw || isPlainObject(order)) {
23
23
  spell.$order(order);
24
24
  } else if (Array.isArray(order) && order.length) {
25
25
  if (order.some(item => Array.isArray(item))) {
@@ -45,15 +45,7 @@ function translateOptions(spell, options) {
45
45
  }
46
46
 
47
47
  const setScopeToSpell = (scope) => (spell) => {
48
- if (scope.where) {
49
- spell.$where(scope.where);
50
- }
51
- if (scope.order) {
52
- spell.$order(scope.order);
53
- }
54
- if (scope.limit) {
55
- spell.$limit(scope.limit);
56
- }
48
+ translateOptions(spell, scope);
57
49
  };
58
50
 
59
51
  /**
@@ -78,7 +70,7 @@ function mergeScope(scopes) {
78
70
  }
79
71
  }
80
72
  return merged;
81
- };
73
+ }
82
74
 
83
75
  /**
84
76
  * parse scope
@@ -114,7 +106,7 @@ function filterOptions(options = {}) {
114
106
 
115
107
  // https://sequelize.org/master/class/lib/model.js~Model.html
116
108
  // https://sequelize.org/master/manual/model-querying-finders.html
117
- module.exports = Bone => {
109
+ module.exports = function sequelize(Bone) {
118
110
  return class Spine extends Bone {
119
111
 
120
112
  /*
@@ -139,7 +131,7 @@ module.exports = Bone => {
139
131
 
140
132
  /**
141
133
  * add scope see https://sequelize.org/master/class/lib/model.js~Model.html#static-method-addScope
142
- *
134
+ * @deprecated scope is not recommended to use
143
135
  * @static
144
136
  * @param {string} name
145
137
  * @param {Object|Function} scope
@@ -156,20 +148,31 @@ module.exports = Bone => {
156
148
  }
157
149
  }
158
150
 
151
+ /**
152
+ * @deprecated scope is not recommended to use
153
+ * @param {string} name
154
+ * @param {...any} args
155
+ * @returns
156
+ */
159
157
  static scope(name, ...args) {
160
158
  const parentName = this.name;
161
159
  class ScopeClass extends this {
162
160
  static name = parentName;
163
- };
161
+ }
164
162
  ScopeClass.setScope(name, ...args);
165
163
  return ScopeClass;
166
164
  }
167
165
 
166
+ static get unscoped() {
167
+ return this.scope();
168
+ }
169
+
168
170
  static unscoped() {
169
171
  return this.scope();
170
172
  }
171
173
 
172
174
  /**
175
+ * @deprecated scope is not recommended to use
173
176
  * @static
174
177
  * @param {function|object|array} name
175
178
  */
@@ -274,12 +277,13 @@ module.exports = Bone => {
274
277
  // EXISTS
275
278
  // static bulkCreate() {}
276
279
 
277
- static async count(options = {}) {
280
+ static count(options = {}) {
281
+ if (typeof options === 'string') return super.find().$count(options);
278
282
  const { where, col, group, paranoid } = options;
279
283
  let spell = super.find(where, filterOptions(options));
280
284
  if (Array.isArray(group)) spell.$group(...group);
281
285
  if (paranoid === false) spell = spell.unparanoid;
282
- return await spell.$count(col);
286
+ return spell.$count(col);
283
287
  }
284
288
 
285
289
  // EXISTS
@@ -562,7 +566,7 @@ module.exports = Bone => {
562
566
  // EXISTS
563
567
  // get isNewRecord() {}
564
568
 
565
- async decrement(fields, options = {}) {
569
+ decrement(fields, options = {}) {
566
570
  const Model = this.constructor;
567
571
  const { primaryKey } = Model;
568
572
  if (this[primaryKey] == null) {
package/src/bone.d.ts ADDED
@@ -0,0 +1,53 @@
1
+ import { Spell } from './spell';
2
+ import { AbstractBone } from './types/abstract_bone';
3
+ import { Collection, Literal, QueryOptions, ResultSet, WhereConditions } from './types/common';
4
+
5
+ export default class Bone extends AbstractBone {
6
+
7
+ /**
8
+ * SELECT rows
9
+ * @example
10
+ * Bone.find('foo = ?', 1)
11
+ * Bone.find({ foo: { $eq: 1 } })
12
+ */
13
+ static find<T extends typeof Bone>(this: T, whereConditions: WhereConditions<T>): Spell<T, Collection<InstanceType<T>>>;
14
+ static find<T extends typeof Bone>(this: T, whereConditions: string, ...values: Literal[]): Spell<T, Collection<InstanceType<T>>>;
15
+ static find<T extends typeof Bone>(this: T, primaryKey: number | number[] | bigint): Spell<T, Collection<InstanceType<T>>>;
16
+ static find<T extends typeof Bone>(this: T, ): Spell<T, Collection<InstanceType<T>>>;
17
+
18
+ /**
19
+ * SELECT rows LIMIT 1. Besides limiting the results to one rows, the type of the return value is different from {@link Bone.find} too. If no results were found, {@link Bone.findOne} returns null. If results were found, it returns the found record instead of wrapping them as a collection.
20
+ * @example
21
+ * Bone.findOne('foo = ?', 1)
22
+ * Bone.findOne({ foo: { $eq: 1 } })
23
+ */
24
+ static findOne<T extends typeof Bone>(this: T, whereConditions: WhereConditions<T>): Spell<T, InstanceType<T> | null>;
25
+ static findOne<T extends typeof Bone>(this: T, whereConditions: string, ...values: Literal[]): Spell<T, InstanceType<T> | null>;
26
+ static findOne<T extends typeof Bone>(this: T, primaryKey: number | number[] | bigint): Spell<T, InstanceType<T> | null>;
27
+ static findOne<T extends typeof Bone>(this: T, ): Spell<T, InstanceType<T> | null>;
28
+
29
+ static sum<T extends typeof Bone>(this: T, name?: string): Spell<T, ResultSet | number>;
30
+
31
+ /**
32
+ * restore rows
33
+ * @example
34
+ * Bone.restore({ title: 'aaa' })
35
+ * Bone.restore({ title: 'aaa' }, { hooks: false })
36
+ * @param conditions query conditions
37
+ * @param opts query options
38
+ */
39
+ static restore<T extends typeof Bone>(this: T, conditions: Object, opts?: QueryOptions): Spell<T, number>;
40
+
41
+ /**
42
+ * UPDATE rows.
43
+ */
44
+ static update<T extends typeof Bone>(this: T, whereConditions: WhereConditions<T>, values?: Object, opts?: QueryOptions): Spell<T, number>;
45
+
46
+ /**
47
+ * Discard all the applied scopes.
48
+ * @example
49
+ * Bone.all.unscoped // includes soft deleted rows
50
+ */
51
+ static unscoped: Spell<typeof Bone>;
52
+
53
+ }
package/src/bone.js CHANGED
@@ -1291,8 +1291,8 @@ class Bone {
1291
1291
  }
1292
1292
 
1293
1293
  for (const name in attributes) {
1294
- const { columnName } = attributes[name];
1295
- if (!(columnName in row)) {
1294
+ const attribute = attributes[name];
1295
+ if (!(attribute.columnName in row) && !attribute.virtual) {
1296
1296
  instance._getRawUnset().add(name);
1297
1297
  }
1298
1298
  }
@@ -1 +1 @@
1
- {"version":3,"file":"decorators.js","sourceRoot":"","sources":["decorators.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4BAA0B;AAG1B,2DAA8D;AAC9D,2CAAqD;AAWrD,SAAS,QAAQ,CAAC,MAAM;IACtB,MAAM,EACJ,MAAM,EAAE,OAAO,EACf,IAAI,EACJ,MAAM,EACN,OAAO,GACR,GAAG,oBAAS,CAAC;IAEd,QAAQ,MAAM,EAAE;QACd,KAAK,MAAM;YACT,OAAO,MA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 @@ function findType(tsType) {
37
37
  }
38
38
  }
39
39
 
40
- export function Column(options?: ColumnOption | DATA_TYPE<DataType>) {
40
+ export function Column(options?: ColumnOption | DATA_TYPE<DataType> | DataType) {
41
41
  return function(target: Bone, propertyKey: string) {
42
42
  if (options == null) {
43
43
  options = {};
@@ -54,7 +54,7 @@ export function Column(options?: ColumnOption | DATA_TYPE<DataType>) {
54
54
  if (!('type' in options)) throw new Error(`unknown column options ${options}`);
55
55
 
56
56
  // target refers to model prototype, an internal instance of `Bone {}`
57
- const model = target.constructor;
57
+ const model = target.constructor as any;
58
58
  const { attributes = (model.attributes = {}) } = model;
59
59
  const { name: columnName, ...restOptions } = options;
60
60
  attributes[propertyKey] = { ...restOptions, columnName };
@@ -0,0 +1,188 @@
1
+ import { Spellbook, SpellMeta, Spell } from '../spell';
2
+ import { DataType, AbstractDataType } from '../data_types';
3
+ import { Attribute, Pool, ResultSet, Literal, QueryResult, AttributeMeta, ColumnMeta } from '../types/common';
4
+ import { AbstractBone } from '../types/abstract_bone';
5
+
6
+ export interface ConnectOptions {
7
+ client?: 'mysql' | 'mysql2' | 'pg' | 'sqlite3' | '@journeyapps/sqlcipher';
8
+ dialect?: 'mysql' | 'postgres' | 'sqlite';
9
+ host?: string;
10
+ port?: number | string;
11
+ user?: string;
12
+ password?: string;
13
+ database: string;
14
+ charset?: string;
15
+ models?: string | (typeof AbstractBone)[];
16
+ subclass?: boolean;
17
+ driver?: typeof AbstractDriver;
18
+ }
19
+
20
+ export class AbstractDriver {
21
+
22
+ static Spellbook: typeof Spellbook;
23
+ static DataType: typeof DataType;
24
+ static Attribute: typeof Attribute;
25
+
26
+ /**
27
+ * The type of driver, currently there are mysql, sqlite, and postgres
28
+ */
29
+ type: string;
30
+
31
+ /**
32
+ * The database current driver is using.
33
+ */
34
+ database: string;
35
+
36
+ /**
37
+ * The connection pool of the driver.
38
+ */
39
+ pool: Pool;
40
+
41
+ /**
42
+ * The SQL dialect
43
+ */
44
+ dialect: string;
45
+
46
+ spellbook: Spellbook;
47
+
48
+ DataType: DataType;
49
+
50
+ Attribute: Attribute;
51
+
52
+ constructor(options: ConnectOptions);
53
+
54
+ escape: (v: string) => string;
55
+ escapeId: (v: string) => string;
56
+
57
+ /**
58
+ * Grab a connection and query the database
59
+ */
60
+ query(sql: string | { sql: string, nestTables?: boolean}, values?: Array<Literal | Literal[]>, opts?: SpellMeta): Promise<QueryResult>;
61
+
62
+ /**
63
+ * disconnect manually
64
+ * @param callback
65
+ */
66
+ disconnect(callback?: Function): Promise<boolean | void>;
67
+
68
+ /**
69
+ * query with spell
70
+ * @param spell
71
+ */
72
+ cast(spell: Spell<typeof AbstractBone, ResultSet | number | null>): Promise<QueryResult>;
73
+
74
+ /**
75
+ * format spell
76
+ * @param spell SpellMeta
77
+ */
78
+ format(spell: SpellMeta): any;
79
+
80
+ /**
81
+ * create table
82
+ * @param tabe table name
83
+ * @param attributes attributes
84
+ */
85
+ createTable(tabe: string, attributes: { [key: string]: AbstractDataType<DataType> | AttributeMeta }): Promise<void>;
86
+
87
+ /**
88
+ * alter table
89
+ * @param tabe table name
90
+ * @param attributes alter attributes
91
+ */
92
+ alterTable(tabe: string, attributes: { [key: string]: AbstractDataType<DataType> | AttributeMeta }): Promise<void>;
93
+
94
+ /**
95
+ * describe table
96
+ * @param table table name
97
+ */
98
+ describeTable(table: string): Promise<{ [key: string]: ColumnMeta }>;
99
+
100
+ /**
101
+ * query table schemas
102
+ * @param database database name
103
+ * @param table table name or table name array
104
+ */
105
+ querySchemaInfo(database: string, table: string | string[]): Promise<{ [key: string] : { [key: string]: ColumnMeta }[]}>;
106
+
107
+ /**
108
+ * add column to table
109
+ * @param table table name
110
+ * @param name column name
111
+ * @param params column meta info
112
+ */
113
+ addColumn(table: string, name: string, params: ColumnMeta): Promise<void>;
114
+
115
+ /**
116
+ * change column meta in table
117
+ * @param table table name
118
+ * @param name column name
119
+ * @param params column meta info
120
+ */
121
+ changeColumn(table: string, name: string, params: ColumnMeta): Promise<void>;
122
+
123
+ /**
124
+ * remove column in table
125
+ * @param table table name
126
+ * @param name column name
127
+ */
128
+ removeColumn(table: string, name: string): Promise<void>;
129
+
130
+ /**
131
+ * rename column in table
132
+ * @param table table name
133
+ * @param name column name
134
+ * @param newName new column name
135
+ */
136
+ renameColumn(table: string, name: string, newName: string): Promise<void>;
137
+
138
+ /**
139
+ * rename table
140
+ * @param table table name
141
+ * @param newTable new table name
142
+ */
143
+ renameTable(table: string, newTable: string): Promise<void>;
144
+
145
+ /**
146
+ * drop table
147
+ * @param table table name
148
+ */
149
+ dropTable(table: string): Promise<void>;
150
+
151
+ /**
152
+ * truncate table
153
+ * @param table table name
154
+ */
155
+ truncateTable(table: string): Promise<void>;
156
+
157
+ /**
158
+ * add index in table
159
+ * @param table table name
160
+ * @param attributes attributes name
161
+ * @param opts
162
+ */
163
+ addIndex(table: string, attributes: string[], opts?: { unique?: boolean, type?: string }): Promise<void>;
164
+
165
+ /**
166
+ * remove index in table
167
+ * @param table string
168
+ * @param attributes attributes name
169
+ * @param opts
170
+ */
171
+ removeIndex(table: string, attributes: string[], opts?: { unique?: boolean, type?: string }): Promise<void>;
172
+
173
+ }
174
+
175
+ export class MysqlDriver extends AbstractDriver {
176
+ type: 'mysql';
177
+ dialect: 'mysql';
178
+ }
179
+
180
+ export class PostgresDriver extends AbstractDriver {
181
+ type: 'postgres';
182
+ dialect: 'postgres';
183
+ }
184
+
185
+ export class SqliteDriver extends AbstractDriver {
186
+ type: 'sqlite';
187
+ dialect: 'sqlite';
188
+ }
package/src/expr.js CHANGED
@@ -347,7 +347,8 @@ function parseExprList(str, ...values) {
347
347
  const results = [];
348
348
  while (chr) {
349
349
  results.push(expr());
350
- if (chr === ',') next();
350
+ if (chr && chr !== ',') throw new Error(`Unexpected token ${chr}`);
351
+ next();
351
352
  }
352
353
  return results;
353
354
  }