leoric 2.6.3 → 2.7.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/History.md CHANGED
@@ -1,3 +1,40 @@
1
+ 2.7.2 / 2022-08-24
2
+ ==================
3
+
4
+ ## What's Changed
5
+ * Update associations.md by @dxhuii in https://github.com/cyjake/leoric/pull/331
6
+ * refactor: refactor type definitions and fix unique not work in columnOptions by @JimmyDaddy in https://github.com/cyjake/leoric/pull/332
7
+ * fix: declare more exported functions such as isBone and heresql by @cyjake in https://github.com/cyjake/leoric/pull/333
8
+ * fix: INTEGER like data type and STRING extra options didn't work in polymorphism, fix decorators ColumnOptions.type to support invokable by @JimmyDaddy in https://github.com/cyjake/leoric/pull/334
9
+
10
+ ## New Contributors
11
+ * @dxhuii made their first contribution in https://github.com/cyjake/leoric/pull/331
12
+
13
+ **Full Changelog**: https://github.com/cyjake/leoric/compare/v2.7.1...v2.7.2
14
+
15
+ 2.7.1 / 2022-08-24
16
+ ==================
17
+
18
+ ## What's Changed
19
+ * fix: projects might have strictPropertyInitialization set to true by @cyjake in https://github.com/cyjake/leoric/pull/329
20
+ * fix: types for validate in `Column` decorator by @vagusX in https://github.com/cyjake/leoric/pull/330
21
+
22
+ ## New Contributors
23
+ * @vagusX made their first contribution in https://github.com/cyjake/leoric/pull/330
24
+
25
+ **Full Changelog**: https://github.com/cyjake/leoric/compare/v2.7.0...v2.7.1
26
+
27
+ 2.7.0 / 2022-08-24
28
+ ==================
29
+
30
+ ## What's Changed
31
+ * fix: glue code for opts.dialectModulePath by @cyjake in https://github.com/cyjake/leoric/pull/326
32
+ * fix: primaryKey in upsert values should be validate in sqlite and postgres by @JimmyDaddy in https://github.com/cyjake/leoric/pull/328
33
+ * feat: change DataTypes to ts and complete decorators type definitions by @JimmyDaddy in https://github.com/cyjake/leoric/pull/319
34
+
35
+
36
+ **Full Changelog**: https://github.com/cyjake/leoric/compare/v2.6.3...v2.7.0
37
+
1
38
  2.6.3 / 2022-08-04
2
39
  ==================
3
40
 
@@ -1,22 +1,23 @@
1
- import DataType from './data_types';
2
- import { Hint, IndexHint } from './hint';
3
-
4
- export { DataType as DataTypes };
5
- export * from '../src/decorators';
6
-
7
- export type command = 'select' | 'insert' | 'bulkInsert' | 'update' | 'delete' | 'upsert';
8
- export type Literal = null | undefined | boolean | number | bigint | string | Date | object | ArrayBuffer;
1
+ import DataTypes, { DataType, AbstractDataType, LENGTH_VARIANTS } from './src/data_types';
2
+ import { Hint, IndexHint } from './src/types/hint';
3
+ import {
4
+ Literal, Validator, ColumnBase,
5
+ QueryResult, Connection, QueryOptions,
6
+ AssociateOptions, command, ResultSet
7
+ } from './src/types/common';
8
+
9
+ export {
10
+ LENGTH_VARIANTS as LENGTH_VARIANTS,
11
+ DataTypes, Literal, Validator, Connection,
12
+ Hint, IndexHint,
13
+ };
14
+ export * from './src/decorators';
9
15
 
10
16
  export class Raw {
11
17
  value: string;
12
18
  type: 'raw';
13
19
  }
14
20
 
15
-
16
- type DataTypes<T> = {
17
- [Property in keyof T as Exclude<Property, "toSqlString">]: T[Property]
18
- }
19
-
20
21
  type RawQueryResult = typeof Bone | ResultSet | boolean | number;
21
22
 
22
23
  interface ExprIdentifier {
@@ -145,7 +146,6 @@ export class Spell<T extends typeof Bone, U = InstanceType<T> | Collection<Insta
145
146
  toString(): string;
146
147
  }
147
148
 
148
-
149
149
  type OperatorCondition = {
150
150
  [key in '$eq' | '$ne']?: Literal;
151
151
  } & {
@@ -172,65 +172,25 @@ type InstanceValues<T> = {
172
172
  [Property in keyof Extract<T, Literal>]?: Extract<T, Literal>[Property]
173
173
  }
174
174
 
175
- export interface ColumnMeta {
176
- columnName?: string;
177
- columnType?: string;
178
- allowNull?: boolean;
179
- defaultValue?: Literal;
180
- primaryKey?: boolean;
181
- unique?: boolean;
175
+ export interface ColumnMeta extends ColumnBase {
182
176
  dataType?: string;
183
- comment?: string;
184
177
  datetimePrecision?: string;
185
178
  }
186
179
 
187
- declare type validator = Literal | Function | Array<Literal | Literal[]>;
188
-
189
180
  export interface AttributeMeta extends ColumnMeta {
190
181
  jsType?: Literal;
191
- type: DataType;
182
+ type: AbstractDataType<DataType>;
192
183
  virtual?: boolean,
193
- toSqlString: () => string;
194
- validate: {
195
- [key: string]: validator;
184
+ toSqlString?: () => string;
185
+ validate?: {
186
+ [key: string]: Validator;
196
187
  }
197
188
  }
198
189
 
199
- interface RelateOptions {
200
- className?: string;
201
- foreignKey?: string;
202
- }
203
-
204
- interface QueryOptions {
205
- validate?: boolean;
206
- individualHooks?: boolean;
207
- hooks?: boolean;
208
- paranoid?: boolean;
209
- silent?: boolean;
210
- connection?: Connection;
211
- }
212
-
213
190
  interface TransactionOptions {
214
191
  connection: Connection;
215
192
  }
216
193
 
217
- interface QueryResult {
218
- insertId?: number;
219
- affectedRows?: number;
220
- rows?: Array<Record<string, Literal>>,
221
- fields?: Array<{ table: string, name: string }>,
222
- }
223
-
224
- export interface Connection {
225
- /**
226
- * MySQL
227
- */
228
- query(
229
- query: string,
230
- values: Array<Literal | Literal[]>,
231
- ): Promise<QueryResult>;
232
- }
233
-
234
194
  declare class Pool {
235
195
  getConnection(): Connection;
236
196
  }
@@ -347,14 +307,14 @@ declare class AbstractDriver {
347
307
  * @param tabe table name
348
308
  * @param attributes attributes
349
309
  */
350
- createTable(tabe: string, attributes: { [key: string]: DataTypes<DataType> | AttributeMeta }): Promise<void>;
310
+ createTable(tabe: string, attributes: { [key: string]: AbstractDataType<DataType> | AttributeMeta }): Promise<void>;
351
311
 
352
312
  /**
353
313
  * alter table
354
314
  * @param tabe table name
355
315
  * @param attributes alter attributes
356
316
  */
357
- alterTable(tabe: string, attributes: { [key: string]: DataTypes<DataType> | AttributeMeta }): Promise<void>;
317
+ alterTable(tabe: string, attributes: { [key: string]: AbstractDataType<DataType> | AttributeMeta }): Promise<void>;
358
318
 
359
319
  /**
360
320
  * describe table
@@ -452,10 +412,6 @@ export class SqliteDriver extends AbstractDriver {
452
412
  dialect: 'sqlite';
453
413
  }
454
414
 
455
- type ResultSet = {
456
- [key: string]: Literal
457
- };
458
-
459
415
  export class Collection<T extends Bone> extends Array<T> {
460
416
  save(): Promise<void>;
461
417
  toJSON(): Object[];
@@ -463,7 +419,7 @@ export class Collection<T extends Bone> extends Array<T> {
463
419
  }
464
420
 
465
421
  export class Bone {
466
- static DataTypes: typeof DataType;
422
+ static DataTypes: typeof DataTypes;
467
423
 
468
424
  /**
469
425
  * get the connection pool of the driver
@@ -503,7 +459,7 @@ export class Bone {
503
459
  /**
504
460
  * The attribute definitions of the model.
505
461
  */
506
- static attributes: { [key: string]: DataTypes<DataType> | AttributeMeta };
462
+ static attributes: { [key: string]: AbstractDataType<DataType> | AttributeMeta };
507
463
 
508
464
  /**
509
465
  * The schema info of current model.
@@ -548,9 +504,9 @@ export class Bone {
548
504
  static alias(data: Record<string, Literal>): Record<string, Literal>;
549
505
  static unalias(name: string): string;
550
506
 
551
- static hasOne(name: string, opts?: RelateOptions): void;
552
- static hasMany(name: string, opts?: RelateOptions): void;
553
- static belongsTo(name: string, opts?: RelateOptions): void;
507
+ static hasOne(name: string, opts?: AssociateOptions): void;
508
+ static hasMany(name: string, opts?: AssociateOptions): void;
509
+ static belongsTo(name: string, opts?: AssociateOptions): void;
554
510
 
555
511
  /**
556
512
  * INSERT rows
@@ -881,7 +837,7 @@ interface RawQueryOptions {
881
837
 
882
838
  export default class Realm {
883
839
  Bone: typeof Bone;
884
- DataTypes: typeof DataType;
840
+ DataTypes: typeof DataTypes;
885
841
  driver: AbstractDriver;
886
842
  models: Record<string, Bone>;
887
843
  connected?: boolean;
@@ -898,7 +854,7 @@ export default class Realm {
898
854
 
899
855
  define(
900
856
  name: string,
901
- attributes: Record<string, DataTypes<DataType> | AttributeMeta>,
857
+ attributes: Record<string, AbstractDataType<DataType> | AttributeMeta>,
902
858
  options?: InitOptions,
903
859
  descriptors?: Record<string, Function>,
904
860
  ): typeof Bone;
@@ -927,7 +883,21 @@ export default class Realm {
927
883
  */
928
884
  export function connect(opts: ConnectOptions): Promise<Realm>;
929
885
  export function disconnect(realm: Realm, callback?: Function): Promise<boolean | void>;
930
- export {
931
- Hint,
932
- IndexHint,
933
- }
886
+
887
+ /**
888
+ * Check if cls is subclass of Bone
889
+ * @param cls
890
+ */
891
+ export function isBone(cls: any): boolean;
892
+
893
+ /**
894
+ * Concatenate multiline SQL into oneline
895
+ * @example
896
+ * heresql(`
897
+ * SELECT *
898
+ * FROM users
899
+ * WHERE age >= 35
900
+ * `)
901
+ * @param text
902
+ */
903
+ export function heresql(text): string;
package/index.js CHANGED
@@ -5,7 +5,7 @@ const Logger = require('./src/drivers/abstract/logger');
5
5
  const Spell = require('./src/spell');
6
6
  const Bone = require('./src/bone');
7
7
  const Collection = require('./src/collection');
8
- const { invokable: DataTypes } = require('./src/data_types');
8
+ const { invokable: DataTypes, LENGTH_VARIANTS } = require('./src/data_types');
9
9
  const migrations = require('./src/migrations');
10
10
  const sequelize = require('./src/adapters/sequelize');
11
11
  const { heresql } = require('./src/utils/string');
@@ -65,6 +65,7 @@ Object.assign(Realm, {
65
65
  SqliteDriver,
66
66
  AbstractDriver,
67
67
  Raw,
68
+ LENGTH_VARIANTS,
68
69
  isBone,
69
70
  });
70
71
 
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "leoric",
3
- "version": "2.6.3",
3
+ "version": "2.7.2",
4
4
  "description": "JavaScript Object-relational mapping alchemy",
5
5
  "main": "index.js",
6
- "types": "types/index.d.ts",
6
+ "types": "index.d.ts",
7
7
  "files": [
8
- "index.js",
9
8
  "src",
10
- "types"
9
+ "index.js",
10
+ "index.d.ts"
11
11
  ],
12
12
  "scripts": {
13
13
  "jsdoc": "rm -rf docs/api && jsdoc -c .jsdoc.json -d docs/api -t node_modules/@cara/minami",
@@ -67,6 +67,7 @@
67
67
  "@types/node": "^16.10.1",
68
68
  "dayjs": "^1.10.3",
69
69
  "eslint": "^7.20.0",
70
+ "eslint-plugin-no-only-tests": "^3.0.0",
70
71
  "expect.js": "^0.3.1",
71
72
  "jsdoc": "^3.6.3",
72
73
  "mocha": "^8.2.1",
@@ -65,7 +65,7 @@ const setScopeToSpell = (scope) => (spell) => {
65
65
  * @returns {Object}
66
66
  */
67
67
  function mergeScope(scopes) {
68
- let merged = {};
68
+ const merged = {};
69
69
  for (const scope of scopes) {
70
70
  if (scope.where) {
71
71
  merged.where = Object.assign({}, merged.where, scope.where);
@@ -227,7 +227,7 @@ module.exports = Bone => {
227
227
  }
228
228
 
229
229
  const { where } = options;
230
- let spell = this._find(where, options)[`$${func}`](name);
230
+ const spell = this._find(where, options)[`$${func}`](name);
231
231
  if (options.paranoid === false) return spell.unparanoid;
232
232
  return spell;
233
233
  }
@@ -262,9 +262,9 @@ module.exports = Bone => {
262
262
 
263
263
  /**
264
264
  * see https://github.com/sequelize/sequelize/blob/a729c4df41fa3a58fbecaf879265d2fb73d80e5f/src/model.js#L2299
265
- * @param {Array<Object>} valueSets
266
- * @param {Object} options
267
- * @returns
265
+ * @param {Array<Object>} valueSets
266
+ * @param {Object} options
267
+ * @returns
268
268
  */
269
269
  static bulkBuild(valueSets, options = {}) {
270
270
  if (!valueSets.length) return [];
@@ -333,7 +333,7 @@ module.exports = Bone => {
333
333
  // static drop() {}
334
334
 
335
335
  static findAll(options = {}) {
336
- let spell = this._find({}, filterOptions(options));
336
+ const spell = this._find({}, filterOptions(options));
337
337
  translateOptions(spell, options);
338
338
  if (options.paranoid === false) return spell.unparanoid;
339
339
  return spell;
package/src/bone.js CHANGED
@@ -10,7 +10,7 @@ const pluralize = require('pluralize');
10
10
  const { executeValidator, LeoricValidateError } = require('./validator');
11
11
  require('reflect-metadata');
12
12
 
13
- const DataTypes = require('./data_types');
13
+ const { default: DataTypes } = require('./data_types');
14
14
  const Collection = require('./collection');
15
15
  const Spell = require('./spell');
16
16
  const Raw = require('./raw');
@@ -102,7 +102,7 @@ function valuesValidate(values, attributes, ctx) {
102
102
  const value = values[valueKey];
103
103
  if (value == null && defaultValue == null) {
104
104
  if (allowNull === false) throw new LeoricValidateError('notNull', name);
105
- if ((allowNull === true || allowNull === undefined) && validate.notNull === undefined ) continue;
105
+ if ((allowNull === true || allowNull === undefined) && validate.notNull === undefined) continue;
106
106
  }
107
107
  if (!validate) continue;
108
108
  for (const key in validate) {
@@ -700,7 +700,7 @@ class Bone {
700
700
  async _update(values, options = {}) {
701
701
  const Model = this.constructor;
702
702
  const { attributes, primaryKey, shardingKey } = Model;
703
- let changes = {};
703
+ const changes = {};
704
704
  if (values == null) {
705
705
  for (const name in attributes) {
706
706
  if (this.changed(name)) changes[name] = this.attribute(name);
@@ -728,7 +728,7 @@ class Bone {
728
728
  if (attributes[updatedAt] && !changes[updatedAt] && !changes[deletedAt] && !options.silent) {
729
729
  changes[updatedAt] = new Date();
730
730
  }
731
- if (options.validate !== false ) {
731
+ if (options.validate !== false) {
732
732
  this._validateAttributes(changes);
733
733
  }
734
734
  const spell = new Spell(Model, options).$where(where).$update(changes);
@@ -911,10 +911,9 @@ class Bone {
911
911
  const { attributes } = Model;
912
912
  for (const key in attributes) {
913
913
  const attribute = attributes[key];
914
- if (attribute.primaryKey) continue;
915
914
  if (values[key] == null && attribute.defaultValue != null) {
916
915
  data[key] = attribute.defaultValue;
917
- } else if (values[key] !== undefined){
916
+ } else if (values[key] !== undefined) {
918
917
  data[key] = values[key];
919
918
  }
920
919
  }
@@ -1668,7 +1667,7 @@ class Bone {
1668
1667
  if (force) {
1669
1668
  await driver.dropTable(table);
1670
1669
  await driver.createTable(table, attributes);
1671
- } else if (alter){
1670
+ } else if (alter) {
1672
1671
  await driver.alterTable(table, compare(attributes, columnMap));
1673
1672
  } else {
1674
1673
  console.warn('[synchronize_fail] %s couldn\'t be synchronized, please use force or alter to specify execution', this.name);