@nocobase/database 0.20.0-alpha.1 → 0.20.0-alpha.10

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.
@@ -278,11 +278,15 @@ const _EagerLoadingTree = class _EagerLoadingTree {
278
278
  }
279
279
  if (associationType == "BelongsToMany") {
280
280
  const foreignKeyValues = node.parent.instances.map((instance) => instance.get(association.sourceKey));
281
- const pivotAssoc = new import_sequelize.HasOne(association.target, association.through.model, {
281
+ const hasOneOptions = {
282
282
  as: "_pivot_",
283
283
  foreignKey: association.otherKey,
284
284
  sourceKey: association.targetKey
285
- });
285
+ };
286
+ if (association.through.scope) {
287
+ hasOneOptions.scope = association.through.scope;
288
+ }
289
+ const pivotAssoc = new import_sequelize.HasOne(association.target, association.through.model, hasOneOptions);
286
290
  instances = await node.model.findAll({
287
291
  transaction,
288
292
  attributes: node.attributes,
@@ -19,6 +19,7 @@ import { TimeFieldOptions } from './time-field';
19
19
  import { UidFieldOptions } from './uid-field';
20
20
  import { UUIDFieldOptions } from './uuid-field';
21
21
  import { VirtualFieldOptions } from './virtual-field';
22
+ import { NanoidFieldOptions } from './nanoid-field';
22
23
  export * from './array-field';
23
24
  export * from './belongs-to-field';
24
25
  export * from './belongs-to-many-field';
@@ -41,4 +42,5 @@ export * from './time-field';
41
42
  export * from './uid-field';
42
43
  export * from './uuid-field';
43
44
  export * from './virtual-field';
44
- export type FieldOptions = BaseFieldOptions | StringFieldOptions | IntegerFieldOptions | FloatFieldOptions | DecimalFieldOptions | DoubleFieldOptions | RealFieldOptions | JsonFieldOptions | JsonbFieldOptions | BooleanFieldOptions | RadioFieldOptions | SortFieldOptions | TextFieldOptions | VirtualFieldOptions | ArrayFieldOptions | SetFieldOptions | TimeFieldOptions | DateFieldOptions | UidFieldOptions | UUIDFieldOptions | PasswordFieldOptions | ContextFieldOptions | BelongsToFieldOptions | HasOneFieldOptions | HasManyFieldOptions | BelongsToManyFieldOptions;
45
+ export * from './nanoid-field';
46
+ export type FieldOptions = BaseFieldOptions | StringFieldOptions | IntegerFieldOptions | FloatFieldOptions | DecimalFieldOptions | DoubleFieldOptions | RealFieldOptions | JsonFieldOptions | JsonbFieldOptions | BooleanFieldOptions | RadioFieldOptions | SortFieldOptions | TextFieldOptions | VirtualFieldOptions | ArrayFieldOptions | SetFieldOptions | TimeFieldOptions | DateFieldOptions | UidFieldOptions | UUIDFieldOptions | NanoidFieldOptions | PasswordFieldOptions | ContextFieldOptions | BelongsToFieldOptions | HasOneFieldOptions | HasManyFieldOptions | BelongsToManyFieldOptions;
@@ -36,6 +36,7 @@ __reExport(fields_exports, require("./time-field"), module.exports);
36
36
  __reExport(fields_exports, require("./uid-field"), module.exports);
37
37
  __reExport(fields_exports, require("./uuid-field"), module.exports);
38
38
  __reExport(fields_exports, require("./virtual-field"), module.exports);
39
+ __reExport(fields_exports, require("./nanoid-field"), module.exports);
39
40
  // Annotate the CommonJS export names for ESM import in node:
40
41
  0 && (module.exports = {
41
42
  ...require("./array-field"),
@@ -59,5 +60,6 @@ __reExport(fields_exports, require("./virtual-field"), module.exports);
59
60
  ...require("./time-field"),
60
61
  ...require("./uid-field"),
61
62
  ...require("./uuid-field"),
62
- ...require("./virtual-field")
63
+ ...require("./virtual-field"),
64
+ ...require("./nanoid-field")
63
65
  });
@@ -0,0 +1,13 @@
1
+ import { DataTypes } from 'sequelize';
2
+ import { BaseColumnFieldOptions, Field } from './field';
3
+ export declare class NanoidField extends Field {
4
+ get dataType(): DataTypes.StringDataTypeConstructor;
5
+ init(): void;
6
+ bind(): void;
7
+ unbind(): void;
8
+ }
9
+ export interface NanoidFieldOptions extends BaseColumnFieldOptions {
10
+ type: 'nanoid';
11
+ size?: number;
12
+ customAlphabet?: string;
13
+ }
@@ -0,0 +1,58 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var nanoid_field_exports = {};
20
+ __export(nanoid_field_exports, {
21
+ NanoidField: () => NanoidField
22
+ });
23
+ module.exports = __toCommonJS(nanoid_field_exports);
24
+ var import_sequelize = require("sequelize");
25
+ var import_field = require("./field");
26
+ var import_nanoid = require("nanoid");
27
+ const DEFAULT_SIZE = 12;
28
+ const _NanoidField = class _NanoidField extends import_field.Field {
29
+ get dataType() {
30
+ return import_sequelize.DataTypes.STRING;
31
+ }
32
+ init() {
33
+ const { name, size, customAlphabet: customAlphabetOptions } = this.options;
34
+ this.listener = async (instance) => {
35
+ const value = instance.get(name);
36
+ if (!value) {
37
+ const nanoIdFunc = customAlphabetOptions ? (0, import_nanoid.customAlphabet)(customAlphabetOptions) : import_nanoid.nanoid;
38
+ instance.set(name, nanoIdFunc(size || DEFAULT_SIZE));
39
+ }
40
+ };
41
+ }
42
+ bind() {
43
+ super.bind();
44
+ this.on("beforeCreate", this.listener);
45
+ this.on("beforeUpdate", this.listener);
46
+ }
47
+ unbind() {
48
+ super.unbind();
49
+ this.off("beforeCreate", this.listener);
50
+ this.off("beforeUpdate", this.listener);
51
+ }
52
+ };
53
+ __name(_NanoidField, "NanoidField");
54
+ let NanoidField = _NanoidField;
55
+ // Annotate the CommonJS export names for ESM import in node:
56
+ 0 && (module.exports = {
57
+ NanoidField
58
+ });
@@ -96,8 +96,16 @@ const _SortField = class _SortField extends import_field.Field {
96
96
  const quotedOrderField = queryInterface.quoteIdentifier(orderField);
97
97
  const sortColumnName = queryInterface.quoteIdentifier(this.collection.model.rawAttributes[this.name].field);
98
98
  let sql;
99
- const whereClause = scopeKey2 && scopeValue ? `
100
- WHERE ${queryInterface.quoteIdentifier(scopeKey2)} IN (${scopeValue.filter((v) => v !== null).map((v) => `'${v}'`).join(", ")})${scopeValue.includes(null) ? ` OR ${queryInterface.quoteIdentifier(scopeKey2)} IS NULL` : ""}` : "";
99
+ const whereClause = scopeKey2 && scopeValue ? (() => {
100
+ const filteredScopeValue = scopeValue.filter((v) => v !== null);
101
+ if (filteredScopeValue.length === 0) {
102
+ return "";
103
+ }
104
+ const initialClause = `
105
+ WHERE ${queryInterface.quoteIdentifier(scopeKey2)} IN (${filteredScopeValue.map((v) => `'${v}'`).join(", ")})`;
106
+ const nullCheck = scopeValue.includes(null) ? ` OR ${queryInterface.quoteIdentifier(scopeKey2)} IS NULL` : "";
107
+ return initialClause + nullCheck;
108
+ })() : "";
101
109
  if (this.collection.db.inDialect("postgres")) {
102
110
  sql = `
103
111
  UPDATE ${this.collection.quotedTableName()}
@@ -2,6 +2,7 @@ var __defProp = Object.defineProperty;
2
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
4
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
5
6
  var __export = (target, all) => {
6
7
  for (var name in all)
7
8
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -22,83 +23,87 @@ __export(string_exports, {
22
23
  module.exports = __toCommonJS(string_exports);
23
24
  var import_sequelize = require("sequelize");
24
25
  var import_utils = require("./utils");
26
+ function escapeLike(value) {
27
+ return value.replace(/[_%]/g, "\\$&");
28
+ }
29
+ __name(escapeLike, "escapeLike");
25
30
  var string_default = {
26
31
  $includes(value, ctx) {
27
32
  if (Array.isArray(value)) {
28
33
  const conditions = value.map((item) => ({
29
- [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.iLike : import_sequelize.Op.like]: `%${item}%`
34
+ [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.iLike : import_sequelize.Op.like]: `%${escapeLike(item)}%`
30
35
  }));
31
36
  return {
32
37
  [import_sequelize.Op.or]: conditions
33
38
  };
34
39
  }
35
40
  return {
36
- [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.iLike : import_sequelize.Op.like]: `%${value}%`
41
+ [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.iLike : import_sequelize.Op.like]: `%${escapeLike(value)}%`
37
42
  };
38
43
  },
39
44
  $notIncludes(value, ctx) {
40
45
  if (Array.isArray(value)) {
41
46
  const conditions = value.map((item) => ({
42
- [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.notILike : import_sequelize.Op.notLike]: `%${item}%`
47
+ [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.notILike : import_sequelize.Op.notLike]: `%${escapeLike(item)}%`
43
48
  }));
44
49
  return {
45
50
  [import_sequelize.Op.and]: conditions
46
51
  };
47
52
  }
48
53
  return {
49
- [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.notILike : import_sequelize.Op.notLike]: `%${value}%`
54
+ [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.notILike : import_sequelize.Op.notLike]: `%${escapeLike(value)}%`
50
55
  };
51
56
  },
52
57
  $startsWith(value, ctx) {
53
58
  if (Array.isArray(value)) {
54
59
  const conditions = value.map((item) => ({
55
- [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.iLike : import_sequelize.Op.like]: `${item}%`
60
+ [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.iLike : import_sequelize.Op.like]: `${escapeLike(item)}%`
56
61
  }));
57
62
  return {
58
63
  [import_sequelize.Op.or]: conditions
59
64
  };
60
65
  }
61
66
  return {
62
- [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.iLike : import_sequelize.Op.like]: `${value}%`
67
+ [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.iLike : import_sequelize.Op.like]: `${escapeLike(value)}%`
63
68
  };
64
69
  },
65
70
  $notStartsWith(value, ctx) {
66
71
  if (Array.isArray(value)) {
67
72
  const conditions = value.map((item) => ({
68
- [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.notILike : import_sequelize.Op.notLike]: `${item}%`
73
+ [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.notILike : import_sequelize.Op.notLike]: `${escapeLike(item)}%`
69
74
  }));
70
75
  return {
71
76
  [import_sequelize.Op.and]: conditions
72
77
  };
73
78
  }
74
79
  return {
75
- [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.notILike : import_sequelize.Op.notLike]: `${value}%`
80
+ [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.notILike : import_sequelize.Op.notLike]: `${escapeLike(value)}%`
76
81
  };
77
82
  },
78
83
  $endWith(value, ctx) {
79
84
  if (Array.isArray(value)) {
80
85
  const conditions = value.map((item) => ({
81
- [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.iLike : import_sequelize.Op.like]: `%${item}`
86
+ [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.iLike : import_sequelize.Op.like]: `%${escapeLike(item)}`
82
87
  }));
83
88
  return {
84
89
  [import_sequelize.Op.or]: conditions
85
90
  };
86
91
  }
87
92
  return {
88
- [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.iLike : import_sequelize.Op.like]: `%${value}`
93
+ [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.iLike : import_sequelize.Op.like]: `%${escapeLike(value)}`
89
94
  };
90
95
  },
91
96
  $notEndWith(value, ctx) {
92
97
  if (Array.isArray(value)) {
93
98
  const conditions = value.map((item) => ({
94
- [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.notILike : import_sequelize.Op.notLike]: `%${item}`
99
+ [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.notILike : import_sequelize.Op.notLike]: `%${escapeLike(item)}`
95
100
  }));
96
101
  return {
97
102
  [import_sequelize.Op.and]: conditions
98
103
  };
99
104
  }
100
105
  return {
101
- [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.notILike : import_sequelize.Op.notLike]: `%${value}`
106
+ [(0, import_utils.isPg)(ctx) ? import_sequelize.Op.notILike : import_sequelize.Op.notLike]: `%${escapeLike(value)}`
102
107
  };
103
108
  }
104
109
  };
@@ -134,9 +134,13 @@ const _PostgresQueryInterface = class _PostgresQueryInterface extends import_que
134
134
  const columnUsage = columnUsages.find((columnUsage2) => {
135
135
  let columnExprTable = column.expr.table;
136
136
  const from = ast[0].from;
137
- const findAs = from.find((from2) => from2.as === columnExprTable);
138
- if (findAs) {
139
- columnExprTable = findAs.table;
137
+ if (columnExprTable === null && column.expr.type === "column_ref") {
138
+ columnExprTable = from[0].table;
139
+ } else {
140
+ const findAs = from.find((from2) => from2.as === columnExprTable);
141
+ if (findAs) {
142
+ columnExprTable = findAs.table;
143
+ }
140
144
  }
141
145
  return columnUsage2.column_name === column.expr.column && columnUsage2.table_name === columnExprTable;
142
146
  });
@@ -1,10 +1,10 @@
1
1
  declare const _default: {
2
2
  postgres: {
3
- 'character varying': string;
4
- varchar: string;
3
+ 'character varying': string[];
4
+ varchar: string[];
5
+ char: string[];
5
6
  character: string;
6
7
  text: string;
7
- char: string;
8
8
  oid: string;
9
9
  name: string;
10
10
  smallint: string[];
@@ -34,10 +34,10 @@ declare const _default: {
34
34
  'smallint unsigned': string[];
35
35
  'tinyint unsigned': string[];
36
36
  'mediumint unsigned': string[];
37
- char: string;
37
+ char: string[];
38
+ varchar: string[];
38
39
  date: string;
39
40
  time: string;
40
- varchar: string;
41
41
  text: string;
42
42
  longtext: string;
43
43
  int: string[];
@@ -55,7 +55,7 @@ declare const _default: {
55
55
  };
56
56
  sqlite: {
57
57
  text: string;
58
- varchar: string;
58
+ varchar: string[];
59
59
  integer: string;
60
60
  real: string;
61
61
  datetime: string;
@@ -72,10 +72,10 @@ declare const _default: {
72
72
  'smallint unsigned': string[];
73
73
  'tinyint unsigned': string[];
74
74
  'mediumint unsigned': string[];
75
- char: string;
75
+ char: string[];
76
+ varchar: string[];
76
77
  date: string;
77
78
  time: string;
78
- varchar: string;
79
79
  text: string;
80
80
  longtext: string;
81
81
  int: string[];
@@ -21,11 +21,11 @@ __export(field_type_map_exports, {
21
21
  });
22
22
  module.exports = __toCommonJS(field_type_map_exports);
23
23
  const postgres = {
24
- "character varying": "string",
25
- varchar: "string",
24
+ "character varying": ["string", "uuid", "nanoid"],
25
+ varchar: ["string", "uuid", "nanoid"],
26
+ char: ["string", "uuid", "nanoid"],
26
27
  character: "string",
27
28
  text: "text",
28
- char: "string",
29
29
  oid: "string",
30
30
  name: "string",
31
31
  smallint: ["integer", "sort"],
@@ -46,7 +46,7 @@ const postgres = {
46
46
  path: "json",
47
47
  polygon: "json",
48
48
  circle: "json",
49
- uuid: "string"
49
+ uuid: "uuid"
50
50
  };
51
51
  const mysql = {
52
52
  smallint: ["integer", "boolean", "sort"],
@@ -55,10 +55,10 @@ const mysql = {
55
55
  "smallint unsigned": ["integer", "boolean", "sort"],
56
56
  "tinyint unsigned": ["integer", "boolean", "sort"],
57
57
  "mediumint unsigned": ["integer", "boolean", "sort"],
58
- char: "string",
58
+ char: ["string", "uuid", "nanoid"],
59
+ varchar: ["string", "uuid", "nanoid"],
59
60
  date: "date",
60
61
  time: "time",
61
- varchar: "string",
62
62
  text: "text",
63
63
  longtext: "text",
64
64
  int: ["integer", "sort"],
@@ -76,7 +76,7 @@ const mysql = {
76
76
  };
77
77
  const sqlite = {
78
78
  text: "text",
79
- varchar: "string",
79
+ varchar: ["string", "uuid", "nanoid"],
80
80
  integer: "integer",
81
81
  real: "real",
82
82
  datetime: "date",
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/database",
3
- "version": "0.20.0-alpha.1",
3
+ "version": "0.20.0-alpha.10",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
7
7
  "license": "Apache-2.0",
8
8
  "dependencies": {
9
- "@nocobase/logger": "0.20.0-alpha.1",
10
- "@nocobase/utils": "0.20.0-alpha.1",
9
+ "@nocobase/logger": "0.20.0-alpha.10",
10
+ "@nocobase/utils": "0.20.0-alpha.10",
11
11
  "async-mutex": "^0.3.2",
12
12
  "chalk": "^4.1.1",
13
13
  "cron-parser": "4.4.0",
@@ -35,5 +35,5 @@
35
35
  "url": "git+https://github.com/nocobase/nocobase.git",
36
36
  "directory": "packages/database"
37
37
  },
38
- "gitHead": "516c0b1470d77ff499f533ae6b57e4c084450ff5"
38
+ "gitHead": "453e0774dae21750e96c7ddd2019c8889a0314d6"
39
39
  }