@yandjin-mikro-orm/knex 6.1.4-rc-sti-changes-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.
Files changed (52) hide show
  1. package/AbstractSqlConnection.d.ts +58 -0
  2. package/AbstractSqlConnection.js +206 -0
  3. package/AbstractSqlDriver.d.ts +73 -0
  4. package/AbstractSqlDriver.js +1378 -0
  5. package/AbstractSqlPlatform.d.ts +25 -0
  6. package/AbstractSqlPlatform.js +86 -0
  7. package/LICENSE +21 -0
  8. package/MonkeyPatchable.d.ts +11 -0
  9. package/MonkeyPatchable.js +39 -0
  10. package/PivotCollectionPersister.d.ts +17 -0
  11. package/PivotCollectionPersister.js +131 -0
  12. package/README.md +383 -0
  13. package/SqlEntityManager.d.ts +25 -0
  14. package/SqlEntityManager.js +40 -0
  15. package/SqlEntityRepository.d.ts +24 -0
  16. package/SqlEntityRepository.js +36 -0
  17. package/index.d.ts +18 -0
  18. package/index.js +40 -0
  19. package/index.mjs +215 -0
  20. package/package.json +71 -0
  21. package/query/ArrayCriteriaNode.d.ts +10 -0
  22. package/query/ArrayCriteriaNode.js +25 -0
  23. package/query/CriteriaNode.d.ts +31 -0
  24. package/query/CriteriaNode.js +147 -0
  25. package/query/CriteriaNodeFactory.d.ts +12 -0
  26. package/query/CriteriaNodeFactory.js +90 -0
  27. package/query/ObjectCriteriaNode.d.ts +15 -0
  28. package/query/ObjectCriteriaNode.js +233 -0
  29. package/query/QueryBuilder.d.ts +291 -0
  30. package/query/QueryBuilder.js +1445 -0
  31. package/query/QueryBuilderHelper.d.ts +64 -0
  32. package/query/QueryBuilderHelper.js +747 -0
  33. package/query/ScalarCriteriaNode.d.ts +10 -0
  34. package/query/ScalarCriteriaNode.js +56 -0
  35. package/query/enums.d.ts +15 -0
  36. package/query/enums.js +20 -0
  37. package/query/index.d.ts +8 -0
  38. package/query/index.js +24 -0
  39. package/schema/DatabaseSchema.d.ts +29 -0
  40. package/schema/DatabaseSchema.js +140 -0
  41. package/schema/DatabaseTable.d.ts +61 -0
  42. package/schema/DatabaseTable.js +727 -0
  43. package/schema/SchemaComparator.d.ts +59 -0
  44. package/schema/SchemaComparator.js +603 -0
  45. package/schema/SchemaHelper.d.ts +56 -0
  46. package/schema/SchemaHelper.js +274 -0
  47. package/schema/SqlSchemaGenerator.d.ts +63 -0
  48. package/schema/SqlSchemaGenerator.js +598 -0
  49. package/schema/index.d.ts +5 -0
  50. package/schema/index.js +21 -0
  51. package/typings.d.ts +174 -0
  52. package/typings.js +2 -0
@@ -0,0 +1,10 @@
1
+ import { CriteriaNode } from "./CriteriaNode";
2
+ import type { IQueryBuilder, ICriteriaNodeProcessOptions } from "../typings";
3
+ /**
4
+ * @internal
5
+ */
6
+ export declare class ScalarCriteriaNode<T extends object> extends CriteriaNode<T> {
7
+ process(qb: IQueryBuilder<T>, options?: ICriteriaNodeProcessOptions): any;
8
+ willAutoJoin<T>(qb: IQueryBuilder<T>, alias?: string): boolean;
9
+ shouldJoin(): boolean;
10
+ }
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ScalarCriteriaNode = void 0;
4
+ const core_1 = require("@yandjin-mikro-orm/core");
5
+ const CriteriaNode_1 = require("./CriteriaNode");
6
+ const enums_1 = require("./enums");
7
+ /**
8
+ * @internal
9
+ */
10
+ class ScalarCriteriaNode extends CriteriaNode_1.CriteriaNode {
11
+ process(qb, options) {
12
+ if (this.shouldJoin()) {
13
+ const path = this.getPath();
14
+ const parentPath = this.parent.getPath(); // the parent is always there, otherwise `shouldJoin` would return `false`
15
+ const nestedAlias = qb.getAliasForJoinPath(path) ||
16
+ qb.getNextAlias(this.prop?.pivotTable ?? this.entityName);
17
+ const field = this.aliased(this.prop.name, options?.alias);
18
+ const type = this.prop.kind === core_1.ReferenceKind.MANY_TO_MANY
19
+ ? enums_1.JoinType.pivotJoin
20
+ : enums_1.JoinType.leftJoin;
21
+ qb.join(field, nestedAlias, undefined, type, path);
22
+ // select the owner as virtual property when joining from 1:1 inverse side, but only if the parent is root entity
23
+ if (this.prop.kind === core_1.ReferenceKind.ONE_TO_ONE &&
24
+ !parentPath.includes(".") &&
25
+ !qb._fields?.includes(field)) {
26
+ qb.addSelect(field);
27
+ }
28
+ }
29
+ if (this.payload && typeof this.payload === "object") {
30
+ const keys = Object.keys(this.payload).filter((key) => core_1.Utils.isArrayOperator(key) && Array.isArray(this.payload[key]));
31
+ for (const key of keys) {
32
+ this.payload[key] = JSON.stringify(this.payload[key]);
33
+ }
34
+ }
35
+ return this.payload;
36
+ }
37
+ willAutoJoin(qb, alias) {
38
+ return this.shouldJoin();
39
+ }
40
+ shouldJoin() {
41
+ if (!this.parent || !this.prop) {
42
+ return false;
43
+ }
44
+ switch (this.prop.kind) {
45
+ case core_1.ReferenceKind.ONE_TO_MANY:
46
+ return true;
47
+ case core_1.ReferenceKind.MANY_TO_MANY:
48
+ return true;
49
+ case core_1.ReferenceKind.ONE_TO_ONE:
50
+ return !this.prop.owner;
51
+ default:
52
+ return false; // SCALAR, MANY_TO_ONE
53
+ }
54
+ }
55
+ }
56
+ exports.ScalarCriteriaNode = ScalarCriteriaNode;
@@ -0,0 +1,15 @@
1
+ export declare enum QueryType {
2
+ TRUNCATE = "TRUNCATE",
3
+ SELECT = "SELECT",
4
+ COUNT = "COUNT",
5
+ INSERT = "INSERT",
6
+ UPDATE = "UPDATE",
7
+ DELETE = "DELETE"
8
+ }
9
+ export declare enum JoinType {
10
+ leftJoin = "left join",
11
+ innerJoin = "inner join",
12
+ pivotJoin = "pivot join",
13
+ innerJoinLateral = "inner join lateral",
14
+ leftJoinLateral = "left join lateral"
15
+ }
package/query/enums.js ADDED
@@ -0,0 +1,20 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JoinType = exports.QueryType = void 0;
4
+ var QueryType;
5
+ (function (QueryType) {
6
+ QueryType["TRUNCATE"] = "TRUNCATE";
7
+ QueryType["SELECT"] = "SELECT";
8
+ QueryType["COUNT"] = "COUNT";
9
+ QueryType["INSERT"] = "INSERT";
10
+ QueryType["UPDATE"] = "UPDATE";
11
+ QueryType["DELETE"] = "DELETE";
12
+ })(QueryType || (exports.QueryType = QueryType = {}));
13
+ var JoinType;
14
+ (function (JoinType) {
15
+ JoinType["leftJoin"] = "left join";
16
+ JoinType["innerJoin"] = "inner join";
17
+ JoinType["pivotJoin"] = "pivot join";
18
+ JoinType["innerJoinLateral"] = "inner join lateral";
19
+ JoinType["leftJoinLateral"] = "left join lateral";
20
+ })(JoinType || (exports.JoinType = JoinType = {}));
@@ -0,0 +1,8 @@
1
+ export * from './enums';
2
+ export * from './QueryBuilderHelper';
3
+ export * from './QueryBuilder';
4
+ export * from './CriteriaNode';
5
+ export * from './ArrayCriteriaNode';
6
+ export * from './ObjectCriteriaNode';
7
+ export * from './ScalarCriteriaNode';
8
+ export * from './CriteriaNodeFactory';
package/query/index.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./enums"), exports);
18
+ __exportStar(require("./QueryBuilderHelper"), exports);
19
+ __exportStar(require("./QueryBuilder"), exports);
20
+ __exportStar(require("./CriteriaNode"), exports);
21
+ __exportStar(require("./ArrayCriteriaNode"), exports);
22
+ __exportStar(require("./ObjectCriteriaNode"), exports);
23
+ __exportStar(require("./ScalarCriteriaNode"), exports);
24
+ __exportStar(require("./CriteriaNodeFactory"), exports);
@@ -0,0 +1,29 @@
1
+ import { type Configuration, type Dictionary, type EntityMetadata } from "@yandjin-mikro-orm/core";
2
+ import { DatabaseTable } from "./DatabaseTable";
3
+ import type { AbstractSqlConnection } from "../AbstractSqlConnection";
4
+ import type { AbstractSqlPlatform } from "../AbstractSqlPlatform";
5
+ /**
6
+ * @internal
7
+ */
8
+ export declare class DatabaseSchema {
9
+ private readonly platform;
10
+ readonly name: string;
11
+ private tables;
12
+ private namespaces;
13
+ private nativeEnums;
14
+ constructor(platform: AbstractSqlPlatform, name: string);
15
+ addTable(name: string, schema: string | undefined | null, comment?: string): DatabaseTable;
16
+ getTables(): DatabaseTable[];
17
+ getTable(name: string): DatabaseTable | undefined;
18
+ hasTable(name: string): boolean;
19
+ setNativeEnums(nativeEnums: Dictionary<unknown[]>): void;
20
+ getNativeEnums(): Dictionary<unknown[]>;
21
+ hasNamespace(namespace: string): boolean;
22
+ getNamespaces(): string[];
23
+ static create(connection: AbstractSqlConnection, platform: AbstractSqlPlatform, config: Configuration, schemaName?: string, schemas?: string[]): Promise<DatabaseSchema>;
24
+ static fromMetadata(metadata: EntityMetadata[], platform: AbstractSqlPlatform, config: Configuration, schemaName?: string): DatabaseSchema;
25
+ private static getSchemaName;
26
+ private static shouldHaveColumn;
27
+ toJSON(): Dictionary;
28
+ prune(schema: string | undefined, wildcardSchemaTables: string[]): void;
29
+ }
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DatabaseSchema = void 0;
4
+ const core_1 = require("@yandjin-mikro-orm/core");
5
+ const DatabaseTable_1 = require("./DatabaseTable");
6
+ /**
7
+ * @internal
8
+ */
9
+ class DatabaseSchema {
10
+ platform;
11
+ name;
12
+ tables = [];
13
+ namespaces = new Set();
14
+ nativeEnums = {}; // for postgres
15
+ constructor(platform, name) {
16
+ this.platform = platform;
17
+ this.name = name;
18
+ }
19
+ addTable(name, schema, comment) {
20
+ const namespaceName = schema ?? this.name;
21
+ const table = new DatabaseTable_1.DatabaseTable(this.platform, name, namespaceName);
22
+ table.nativeEnums = this.nativeEnums;
23
+ table.comment = comment;
24
+ this.tables.push(table);
25
+ if (namespaceName != null) {
26
+ this.namespaces.add(namespaceName);
27
+ }
28
+ return table;
29
+ }
30
+ getTables() {
31
+ return this.tables;
32
+ }
33
+ getTable(name) {
34
+ return this.tables.find((t) => t.name === name || `${t.schema}.${t.name}` === name);
35
+ }
36
+ hasTable(name) {
37
+ return !!this.getTable(name);
38
+ }
39
+ setNativeEnums(nativeEnums) {
40
+ this.nativeEnums = nativeEnums;
41
+ this.tables.forEach((t) => (t.nativeEnums = nativeEnums));
42
+ }
43
+ getNativeEnums() {
44
+ return this.nativeEnums;
45
+ }
46
+ hasNamespace(namespace) {
47
+ return this.namespaces.has(namespace);
48
+ }
49
+ getNamespaces() {
50
+ return [...this.namespaces];
51
+ }
52
+ static async create(connection, platform, config, schemaName, schemas) {
53
+ const schema = new DatabaseSchema(platform, schemaName ?? config.get("schema") ?? platform.getDefaultSchemaName());
54
+ const allTables = await connection.execute(platform.getSchemaHelper().getListTablesSQL());
55
+ const parts = config.get("migrations").tableName.split(".");
56
+ const migrationsTableName = parts[1] ?? parts[0];
57
+ const migrationsSchemaName = parts.length > 1
58
+ ? parts[0]
59
+ : config.get("schema", platform.getDefaultSchemaName());
60
+ const tables = allTables.filter((t) => t.table_name !== migrationsTableName ||
61
+ (t.schema_name && t.schema_name !== migrationsSchemaName));
62
+ await platform
63
+ .getSchemaHelper()
64
+ .loadInformationSchema(schema, connection, tables, schemas && schemas.length > 0 ? schemas : undefined);
65
+ return schema;
66
+ }
67
+ static fromMetadata(metadata, platform, config, schemaName) {
68
+ const schema = new DatabaseSchema(platform, schemaName ?? config.get("schema"));
69
+ const nativeEnums = {};
70
+ for (const meta of metadata) {
71
+ meta.props
72
+ .filter((prop) => prop.nativeEnumName)
73
+ .forEach((prop) => (nativeEnums[prop.nativeEnumName] =
74
+ prop.items?.map((val) => "" + val) ?? []));
75
+ }
76
+ schema.setNativeEnums(nativeEnums);
77
+ const rootMetadata = core_1.Utils.uniqueBy(metadata.map((e) => e.getStiRoot()), "name");
78
+ for (const meta of rootMetadata) {
79
+ // const table = schema.getTable(meta.collection) ?? schema.addTable(meta.collection, this.getSchemaName(meta, config, schemaName));
80
+ const table = schema.addTable(meta.collection, this.getSchemaName(meta, config, schemaName));
81
+ table.comment = meta.comment;
82
+ meta.props
83
+ .filter((prop) => this.shouldHaveColumn(meta, prop))
84
+ .forEach((prop) => table.addColumnFromProperty(prop, meta, config));
85
+ meta.indexes.forEach((index) => table.addIndex(meta, index, "index"));
86
+ meta.uniques.forEach((index) => table.addIndex(meta, index, "unique"));
87
+ table.addIndex(meta, {
88
+ properties: meta.props
89
+ .filter((prop) => prop.primary)
90
+ .map((prop) => prop.name),
91
+ }, "primary");
92
+ meta.checks.forEach((check) => {
93
+ const columnName = check.property
94
+ ? meta.properties[check.property].fieldNames[0]
95
+ : undefined;
96
+ table.addCheck({
97
+ name: check.name,
98
+ expression: check.expression,
99
+ definition: `check ((${check.expression}))`,
100
+ columnName,
101
+ });
102
+ });
103
+ }
104
+ return schema;
105
+ }
106
+ static getSchemaName(meta, config, schema) {
107
+ return (meta.schema === "*" ? schema : meta.schema) ?? config.get("schema");
108
+ }
109
+ static shouldHaveColumn(meta, prop) {
110
+ if (prop.persist === false || (prop.columnTypes?.length ?? 0) === 0) {
111
+ return false;
112
+ }
113
+ if (meta.pivotTable ||
114
+ (prop.kind === core_1.ReferenceKind.EMBEDDED && prop.object)) {
115
+ return true;
116
+ }
117
+ const getRootProperty = (prop) => prop.embedded ? getRootProperty(meta.properties[prop.embedded[0]]) : prop;
118
+ const rootProp = getRootProperty(prop);
119
+ if (rootProp.kind === core_1.ReferenceKind.EMBEDDED) {
120
+ return prop === rootProp || !rootProp.object;
121
+ }
122
+ return ([core_1.ReferenceKind.SCALAR, core_1.ReferenceKind.MANY_TO_ONE].includes(prop.kind) ||
123
+ (prop.kind === core_1.ReferenceKind.ONE_TO_ONE && prop.owner));
124
+ }
125
+ toJSON() {
126
+ const { platform, namespaces, ...rest } = this;
127
+ return { namespaces: [...namespaces], ...rest };
128
+ }
129
+ prune(schema, wildcardSchemaTables) {
130
+ const hasWildcardSchema = wildcardSchemaTables.length > 0;
131
+ this.tables = this.tables.filter((table) => {
132
+ return ((!schema && !hasWildcardSchema) || // no schema specified and we don't have any multi-schema entity
133
+ table.schema === schema || // specified schema matches the table's one
134
+ (!schema && !wildcardSchemaTables.includes(table.name))); // no schema specified and the table has fixed one provided
135
+ });
136
+ // remove namespaces of ignored tables
137
+ this.namespaces.forEach((ns) => !this.tables.find((t) => t.schema === ns) && this.namespaces.delete(ns));
138
+ }
139
+ }
140
+ exports.DatabaseSchema = DatabaseSchema;
@@ -0,0 +1,61 @@
1
+ import { type Configuration, type Dictionary, type EntityMetadata, type EntityProperty, type NamingStrategy } from "@yandjin-mikro-orm/core";
2
+ import type { SchemaHelper } from "./SchemaHelper";
3
+ import type { CheckDef, Column, ForeignKey, IndexDef } from "../typings";
4
+ import type { AbstractSqlPlatform } from "../AbstractSqlPlatform";
5
+ /**
6
+ * @internal
7
+ */
8
+ export declare class DatabaseTable {
9
+ private readonly platform;
10
+ readonly name: string;
11
+ readonly schema?: string | undefined;
12
+ private columns;
13
+ private indexes;
14
+ private checks;
15
+ private foreignKeys;
16
+ nativeEnums: Dictionary<unknown[]>;
17
+ comment?: string;
18
+ constructor(platform: AbstractSqlPlatform, name: string, schema?: string | undefined);
19
+ getColumns(): Column[];
20
+ getColumn(name: string): Column | undefined;
21
+ removeColumn(name: string): void;
22
+ getIndexes(): IndexDef[];
23
+ getChecks(): CheckDef[];
24
+ init(cols: Column[], indexes: IndexDef[] | undefined, checks: CheckDef<unknown>[] | undefined, pks: string[], fks?: Dictionary<ForeignKey>, enums?: Dictionary<string[]>): void;
25
+ addColumn(column: Column): void;
26
+ addColumnFromProperty(prop: EntityProperty, meta: EntityMetadata, config: Configuration): void;
27
+ private getIndexName;
28
+ getEntityDeclaration(namingStrategy: NamingStrategy, schemaHelper: SchemaHelper, scalarPropertiesForRelations: "always" | "never" | "smart"): EntityMetadata;
29
+ private foreignKeysToProps;
30
+ private findFkIndex;
31
+ private getIndexProperties;
32
+ private getSafeBaseNameForFkProp;
33
+ /**
34
+ * The shortest name is stripped of the default namespace. All other namespaced elements are returned as full-qualified names.
35
+ */
36
+ getShortestName(): string;
37
+ getForeignKeys(): Dictionary<ForeignKey>;
38
+ hasColumn(columnName: string): boolean;
39
+ getIndex(indexName: string): IndexDef | undefined;
40
+ hasIndex(indexName: string): boolean;
41
+ getCheck(checkName: string): CheckDef<unknown> | undefined;
42
+ hasCheck(checkName: string): boolean;
43
+ getPrimaryKey(): IndexDef | undefined;
44
+ hasPrimaryKey(): boolean;
45
+ private getForeignKeyDeclaration;
46
+ private getPropertyDeclaration;
47
+ private getReferenceKind;
48
+ private getPropertyName;
49
+ private getPropertyTypeForForeignKey;
50
+ private getPropertyTypeForColumn;
51
+ private getPropertyDefaultValue;
52
+ addIndex(meta: EntityMetadata, index: {
53
+ properties: string | string[];
54
+ name?: string;
55
+ type?: string;
56
+ expression?: string;
57
+ options?: Dictionary;
58
+ }, type: "index" | "unique" | "primary"): void;
59
+ addCheck(check: CheckDef): void;
60
+ toJSON(): Dictionary;
61
+ }