@tachybase/database 0.23.8

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 (226) hide show
  1. package/.turbo/turbo-build.log +10 -0
  2. package/LICENSE +201 -0
  3. package/lib/collection-factory.d.ts +17 -0
  4. package/lib/collection-factory.js +52 -0
  5. package/lib/collection-group-manager.d.ts +26 -0
  6. package/lib/collection-group-manager.js +57 -0
  7. package/lib/collection-importer.d.ts +7 -0
  8. package/lib/collection-importer.js +74 -0
  9. package/lib/collection.d.ts +138 -0
  10. package/lib/collection.js +686 -0
  11. package/lib/database-utils/index.d.ts +9 -0
  12. package/lib/database-utils/index.js +71 -0
  13. package/lib/database-utils/traverseJSON.d.ts +10 -0
  14. package/lib/database-utils/traverseJSON.js +126 -0
  15. package/lib/database.d.ts +201 -0
  16. package/lib/database.js +765 -0
  17. package/lib/decorators/must-have-filter-decorator.d.ts +2 -0
  18. package/lib/decorators/must-have-filter-decorator.js +38 -0
  19. package/lib/decorators/target-collection-decorator.d.ts +2 -0
  20. package/lib/decorators/target-collection-decorator.js +47 -0
  21. package/lib/decorators/transaction-decorator.d.ts +1 -0
  22. package/lib/decorators/transaction-decorator.js +88 -0
  23. package/lib/eager-loading/eager-loading-tree.d.ts +31 -0
  24. package/lib/eager-loading/eager-loading-tree.js +439 -0
  25. package/lib/errors/identifier-error.d.ts +3 -0
  26. package/lib/errors/identifier-error.js +35 -0
  27. package/lib/errors/zero-column-table-error.d.ts +2 -0
  28. package/lib/errors/zero-column-table-error.js +31 -0
  29. package/lib/features/references-map.d.ts +18 -0
  30. package/lib/features/references-map.js +109 -0
  31. package/lib/features/referential-integrity-check.d.ts +8 -0
  32. package/lib/features/referential-integrity-check.js +83 -0
  33. package/lib/field-repository/array-field-repository.d.ts +28 -0
  34. package/lib/field-repository/array-field-repository.js +197 -0
  35. package/lib/fields/array-field.d.ts +11 -0
  36. package/lib/fields/array-field.js +57 -0
  37. package/lib/fields/belongs-to-field.d.ts +17 -0
  38. package/lib/fields/belongs-to-field.js +154 -0
  39. package/lib/fields/belongs-to-many-field.d.ts +20 -0
  40. package/lib/fields/belongs-to-many-field.js +187 -0
  41. package/lib/fields/boolean-field.d.ts +8 -0
  42. package/lib/fields/boolean-field.js +36 -0
  43. package/lib/fields/context-field.d.ts +14 -0
  44. package/lib/fields/context-field.js +70 -0
  45. package/lib/fields/date-field.d.ts +13 -0
  46. package/lib/fields/date-field.js +64 -0
  47. package/lib/fields/field.d.ts +43 -0
  48. package/lib/fields/field.js +156 -0
  49. package/lib/fields/has-inverse-field.d.ts +4 -0
  50. package/lib/fields/has-inverse-field.js +15 -0
  51. package/lib/fields/has-many-field.d.ts +68 -0
  52. package/lib/fields/has-many-field.js +156 -0
  53. package/lib/fields/has-one-field.d.ts +68 -0
  54. package/lib/fields/has-one-field.js +150 -0
  55. package/lib/fields/index.d.ts +46 -0
  56. package/lib/fields/index.js +65 -0
  57. package/lib/fields/json-field.d.ts +14 -0
  58. package/lib/fields/json-field.js +54 -0
  59. package/lib/fields/nanoid-field.d.ts +13 -0
  60. package/lib/fields/nanoid-field.js +58 -0
  61. package/lib/fields/number-field.d.ts +41 -0
  62. package/lib/fields/number-field.js +85 -0
  63. package/lib/fields/password-field.d.ts +21 -0
  64. package/lib/fields/password-field.js +95 -0
  65. package/lib/fields/radio-field.d.ts +16 -0
  66. package/lib/fields/radio-field.js +66 -0
  67. package/lib/fields/relation-field.d.ts +21 -0
  68. package/lib/fields/relation-field.js +79 -0
  69. package/lib/fields/set-field.d.ts +10 -0
  70. package/lib/fields/set-field.js +46 -0
  71. package/lib/fields/sort-field.d.ts +16 -0
  72. package/lib/fields/sort-field.js +187 -0
  73. package/lib/fields/string-field.d.ts +8 -0
  74. package/lib/fields/string-field.js +36 -0
  75. package/lib/fields/text-field.d.ts +8 -0
  76. package/lib/fields/text-field.js +36 -0
  77. package/lib/fields/time-field.d.ts +8 -0
  78. package/lib/fields/time-field.js +36 -0
  79. package/lib/fields/uid-field.d.ts +13 -0
  80. package/lib/fields/uid-field.js +63 -0
  81. package/lib/fields/uuid-field.d.ts +9 -0
  82. package/lib/fields/uuid-field.js +45 -0
  83. package/lib/fields/virtual-field.d.ts +8 -0
  84. package/lib/fields/virtual-field.js +36 -0
  85. package/lib/filter-match.d.ts +1 -0
  86. package/lib/filter-match.js +66 -0
  87. package/lib/filter-parser.d.ts +21 -0
  88. package/lib/filter-parser.js +230 -0
  89. package/lib/helpers.d.ts +3 -0
  90. package/lib/helpers.js +167 -0
  91. package/lib/index.d.ts +30 -0
  92. package/lib/index.js +114 -0
  93. package/lib/inherited-collection.d.ts +14 -0
  94. package/lib/inherited-collection.js +138 -0
  95. package/lib/inherited-map.d.ts +21 -0
  96. package/lib/inherited-map.js +113 -0
  97. package/lib/inherited-sync-runner.d.ts +4 -0
  98. package/lib/inherited-sync-runner.js +175 -0
  99. package/lib/listeners/adjacency-list.d.ts +2 -0
  100. package/lib/listeners/adjacency-list.js +42 -0
  101. package/lib/listeners/append-child-collection-name-after-repository-find.d.ts +6 -0
  102. package/lib/listeners/append-child-collection-name-after-repository-find.js +66 -0
  103. package/lib/listeners/index.d.ts +2 -0
  104. package/lib/listeners/index.js +33 -0
  105. package/lib/magic-attribute-model.d.ts +8 -0
  106. package/lib/magic-attribute-model.js +203 -0
  107. package/lib/migration.d.ts +35 -0
  108. package/lib/migration.js +103 -0
  109. package/lib/mock-database.d.ts +25 -0
  110. package/lib/mock-database.js +126 -0
  111. package/lib/model-hook.d.ts +12 -0
  112. package/lib/model-hook.js +92 -0
  113. package/lib/model.d.ts +23 -0
  114. package/lib/model.js +152 -0
  115. package/lib/operators/array.d.ts +2 -0
  116. package/lib/operators/array.js +158 -0
  117. package/lib/operators/association.d.ts +2 -0
  118. package/lib/operators/association.js +35 -0
  119. package/lib/operators/boolean.d.ts +2 -0
  120. package/lib/operators/boolean.js +38 -0
  121. package/lib/operators/child-collection.d.ts +2 -0
  122. package/lib/operators/child-collection.js +67 -0
  123. package/lib/operators/date.d.ts +2 -0
  124. package/lib/operators/date.js +140 -0
  125. package/lib/operators/empty.d.ts +5 -0
  126. package/lib/operators/empty.js +89 -0
  127. package/lib/operators/eq.d.ts +2 -0
  128. package/lib/operators/eq.js +35 -0
  129. package/lib/operators/index.d.ts +5 -0
  130. package/lib/operators/index.js +56 -0
  131. package/lib/operators/jsonb.d.ts +2 -0
  132. package/lib/operators/jsonb.js +50 -0
  133. package/lib/operators/ne.d.ts +2 -0
  134. package/lib/operators/ne.js +40 -0
  135. package/lib/operators/notIn.d.ts +2 -0
  136. package/lib/operators/notIn.js +33 -0
  137. package/lib/operators/string.d.ts +2 -0
  138. package/lib/operators/string.js +109 -0
  139. package/lib/operators/utils.d.ts +4 -0
  140. package/lib/operators/utils.js +40 -0
  141. package/lib/options-parser.d.ts +37 -0
  142. package/lib/options-parser.js +332 -0
  143. package/lib/playground.d.ts +1 -0
  144. package/lib/playground.js +43 -0
  145. package/lib/query-interface/mysql-query-interface.d.ts +35 -0
  146. package/lib/query-interface/mysql-query-interface.js +122 -0
  147. package/lib/query-interface/postgres-query-interface.d.ts +32 -0
  148. package/lib/query-interface/postgres-query-interface.js +189 -0
  149. package/lib/query-interface/query-interface-builder.d.ts +2 -0
  150. package/lib/query-interface/query-interface-builder.js +46 -0
  151. package/lib/query-interface/query-interface.d.ts +42 -0
  152. package/lib/query-interface/query-interface.js +46 -0
  153. package/lib/query-interface/sqlite-query-interface.d.ts +35 -0
  154. package/lib/query-interface/sqlite-query-interface.js +131 -0
  155. package/lib/relation-repository/belongs-to-many-repository.d.ts +21 -0
  156. package/lib/relation-repository/belongs-to-many-repository.js +280 -0
  157. package/lib/relation-repository/belongs-to-repository.d.ts +6 -0
  158. package/lib/relation-repository/belongs-to-repository.js +39 -0
  159. package/lib/relation-repository/hasmany-repository.d.ts +10 -0
  160. package/lib/relation-repository/hasmany-repository.js +182 -0
  161. package/lib/relation-repository/hasone-repository.d.ts +6 -0
  162. package/lib/relation-repository/hasone-repository.js +39 -0
  163. package/lib/relation-repository/multiple-relation-repository.d.ts +20 -0
  164. package/lib/relation-repository/multiple-relation-repository.js +229 -0
  165. package/lib/relation-repository/relation-repository.d.ts +29 -0
  166. package/lib/relation-repository/relation-repository.js +202 -0
  167. package/lib/relation-repository/single-relation-repository.d.ts +25 -0
  168. package/lib/relation-repository/single-relation-repository.js +162 -0
  169. package/lib/relation-repository/types.d.ts +7 -0
  170. package/lib/relation-repository/types.js +15 -0
  171. package/lib/repositories/tree-repository/adjacency-list-repository.d.ts +18 -0
  172. package/lib/repositories/tree-repository/adjacency-list-repository.js +183 -0
  173. package/lib/repositories/view-repository.d.ts +3 -0
  174. package/lib/repositories/view-repository.js +32 -0
  175. package/lib/repository.d.ts +206 -0
  176. package/lib/repository.js +597 -0
  177. package/lib/sql-collection/index.d.ts +2 -0
  178. package/lib/sql-collection/index.js +23 -0
  179. package/lib/sql-collection/query-generator.d.ts +10 -0
  180. package/lib/sql-collection/query-generator.js +70 -0
  181. package/lib/sql-collection/sql-collection.d.ts +7 -0
  182. package/lib/sql-collection/sql-collection.js +69 -0
  183. package/lib/sql-collection/sql-model.d.ts +16 -0
  184. package/lib/sql-collection/sql-model.js +168 -0
  185. package/lib/sql-parser/index.d.ts +10 -0
  186. package/lib/sql-parser/index.js +10699 -0
  187. package/lib/sql-parser/postgres.d.ts +10 -0
  188. package/lib/sql-parser/postgres.js +31506 -0
  189. package/lib/sql-parser/sql.pegjs +1297 -0
  190. package/lib/sync-runner.d.ts +30 -0
  191. package/lib/sync-runner.js +277 -0
  192. package/lib/types.d.ts +49 -0
  193. package/lib/types.js +15 -0
  194. package/lib/update-associations.d.ts +60 -0
  195. package/lib/update-associations.js +405 -0
  196. package/lib/update-guard.d.ts +28 -0
  197. package/lib/update-guard.js +202 -0
  198. package/lib/utils.d.ts +7 -0
  199. package/lib/utils.js +127 -0
  200. package/lib/value-parsers/array-value-parser.d.ts +8 -0
  201. package/lib/value-parsers/array-value-parser.js +58 -0
  202. package/lib/value-parsers/base-value-parser.d.ts +12 -0
  203. package/lib/value-parsers/base-value-parser.js +63 -0
  204. package/lib/value-parsers/boolean-value-parser.d.ts +4 -0
  205. package/lib/value-parsers/boolean-value-parser.js +52 -0
  206. package/lib/value-parsers/date-value-parser.d.ts +5 -0
  207. package/lib/value-parsers/date-value-parser.js +88 -0
  208. package/lib/value-parsers/index.d.ts +12 -0
  209. package/lib/value-parsers/index.js +78 -0
  210. package/lib/value-parsers/json-value-parser.d.ts +4 -0
  211. package/lib/value-parsers/json-value-parser.js +47 -0
  212. package/lib/value-parsers/number-value-parser.d.ts +4 -0
  213. package/lib/value-parsers/number-value-parser.js +58 -0
  214. package/lib/value-parsers/string-value-parser.d.ts +8 -0
  215. package/lib/value-parsers/string-value-parser.js +64 -0
  216. package/lib/value-parsers/to-many-value-parser.d.ts +13 -0
  217. package/lib/value-parsers/to-many-value-parser.js +108 -0
  218. package/lib/value-parsers/to-one-value-parser.d.ts +4 -0
  219. package/lib/value-parsers/to-one-value-parser.js +48 -0
  220. package/lib/view/field-type-map.d.ts +97 -0
  221. package/lib/view/field-type-map.js +90 -0
  222. package/lib/view/view-inference.d.ts +32 -0
  223. package/lib/view/view-inference.js +146 -0
  224. package/lib/view-collection.d.ts +6 -0
  225. package/lib/view-collection.js +45 -0
  226. package/package.json +38 -0
@@ -0,0 +1,156 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var field_exports = {};
30
+ __export(field_exports, {
31
+ Field: () => Field
32
+ });
33
+ module.exports = __toCommonJS(field_exports);
34
+ var import_lodash = __toESM(require("lodash"));
35
+ var import_utils = require("../utils");
36
+ const _Field = class _Field {
37
+ options;
38
+ context;
39
+ database;
40
+ collection;
41
+ constructor(options, context) {
42
+ this.context = context;
43
+ this.database = this.context.database;
44
+ this.collection = this.context.collection;
45
+ this.options = options || {};
46
+ this.init();
47
+ }
48
+ get name() {
49
+ return this.options.name;
50
+ }
51
+ get type() {
52
+ return this.options.type;
53
+ }
54
+ async sync(syncOptions) {
55
+ await this.collection.sync({
56
+ ...syncOptions,
57
+ force: false,
58
+ alter: {
59
+ drop: false
60
+ }
61
+ });
62
+ }
63
+ init() {
64
+ }
65
+ on(eventName, listener) {
66
+ this.database.on(`${this.collection.name}.${eventName}`, listener);
67
+ return this;
68
+ }
69
+ off(eventName, listener) {
70
+ this.database.off(`${this.collection.name}.${eventName}`, listener);
71
+ return this;
72
+ }
73
+ get(name) {
74
+ return this.options[name];
75
+ }
76
+ remove() {
77
+ this.collection.removeIndex([this.name]);
78
+ return this.collection.removeField(this.name);
79
+ }
80
+ columnName() {
81
+ if (this.options.field) {
82
+ return this.options.field;
83
+ }
84
+ if (this.database.options.underscored) {
85
+ return (0, import_utils.snakeCase)(this.name);
86
+ }
87
+ return this.name;
88
+ }
89
+ async existsInDb(options) {
90
+ const opts = {
91
+ transaction: options == null ? void 0 : options.transaction
92
+ };
93
+ let sql;
94
+ if (this.database.sequelize.getDialect() === "sqlite") {
95
+ sql = `SELECT *
96
+ from pragma_table_info('${this.collection.model.tableName}')
97
+ WHERE name = '${this.columnName()}'`;
98
+ } else if (this.database.inDialect("mysql", "mariadb")) {
99
+ sql = `
100
+ select column_name
101
+ from INFORMATION_SCHEMA.COLUMNS
102
+ where TABLE_SCHEMA = '${this.database.options.database}'
103
+ AND TABLE_NAME = '${this.collection.model.tableName}'
104
+ AND column_name = '${this.columnName()}'
105
+ `;
106
+ } else {
107
+ sql = `
108
+ select column_name
109
+ from INFORMATION_SCHEMA.COLUMNS
110
+ where TABLE_NAME = '${this.collection.model.tableName}'
111
+ AND column_name = '${this.columnName()}'
112
+ AND table_schema = '${this.collection.collectionSchema() || "public"}'
113
+ `;
114
+ }
115
+ const [rows] = await this.database.sequelize.query(sql, opts);
116
+ return rows.length > 0;
117
+ }
118
+ merge(obj) {
119
+ Object.assign(this.options, obj);
120
+ }
121
+ bind() {
122
+ const { model } = this.context.collection;
123
+ model.rawAttributes[this.name] = this.toSequelize();
124
+ model.refreshAttributes();
125
+ if (this.options.index) {
126
+ this.context.collection.addIndex([this.name]);
127
+ }
128
+ }
129
+ unbind() {
130
+ const { model } = this.context.collection;
131
+ delete model.prototype[this.name];
132
+ model.removeAttribute(this.name);
133
+ if (this.options.index || this.options.unique) {
134
+ this.context.collection.removeIndex([this.name]);
135
+ }
136
+ }
137
+ toSequelize() {
138
+ const opts = import_lodash.default.omit(this.options, ["name"]);
139
+ if (this.dataType) {
140
+ Object.assign(opts, { type: this.database.sequelize.normalizeDataType(this.dataType) });
141
+ }
142
+ return opts;
143
+ }
144
+ isSqlite() {
145
+ return this.database.sequelize.getDialect() === "sqlite";
146
+ }
147
+ typeToString() {
148
+ return this.dataType.toString();
149
+ }
150
+ };
151
+ __name(_Field, "Field");
152
+ let Field = _Field;
153
+ // Annotate the CommonJS export names for ESM import in node:
154
+ 0 && (module.exports = {
155
+ Field
156
+ });
@@ -0,0 +1,4 @@
1
+ import { Field } from './field';
2
+ export interface HasInverseField {
3
+ inverseField: () => Field;
4
+ }
@@ -0,0 +1,15 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __copyProps = (to, from, except, desc) => {
6
+ if (from && typeof from === "object" || typeof from === "function") {
7
+ for (let key of __getOwnPropNames(from))
8
+ if (!__hasOwnProp.call(to, key) && key !== except)
9
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
10
+ }
11
+ return to;
12
+ };
13
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
14
+ var has_inverse_field_exports = {};
15
+ module.exports = __toCommonJS(has_inverse_field_exports);
@@ -0,0 +1,68 @@
1
+ import { AssociationScope, DataType, ForeignKeyOptions, HasManyOptions, HasManyOptions as SequelizeHasManyOptions } from 'sequelize';
2
+ import { Reference } from '../features/references-map';
3
+ import { MultipleRelationFieldOptions, RelationField } from './relation-field';
4
+ export interface HasManyFieldOptions extends HasManyOptions {
5
+ /**
6
+ * The name of the field to use as the key for the association in the source table. Defaults to the primary
7
+ * key of the source table
8
+ */
9
+ sourceKey?: string;
10
+ /**
11
+ * A string or a data type to represent the identifier in the table
12
+ */
13
+ keyType?: DataType;
14
+ scope?: AssociationScope;
15
+ /**
16
+ * The alias of this model, in singular form. See also the `name` option passed to `sequelize.define`. If
17
+ * you create multiple associations between the same tables, you should provide an alias to be able to
18
+ * distinguish between them. If you provide an alias when creating the assocition, you should provide the
19
+ * same alias when eager loading and when getting associated models. Defaults to the singularized name of
20
+ * target
21
+ */
22
+ as?: string | {
23
+ singular: string;
24
+ plural: string;
25
+ };
26
+ /**
27
+ * The name of the foreign key in the target table or an object representing the type definition for the
28
+ * foreign column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property
29
+ * to set the name of the column. Defaults to the name of source + primary key of source
30
+ */
31
+ foreignKey?: string | ForeignKeyOptions;
32
+ /**
33
+ * What happens when delete occurs.
34
+ *
35
+ * Cascade if this is a n:m, and set null if it is a 1:m
36
+ *
37
+ * @default 'SET NULL' or 'CASCADE'
38
+ */
39
+ onDelete?: string;
40
+ /**
41
+ * What happens when update occurs
42
+ *
43
+ * @default 'CASCADE'
44
+ */
45
+ onUpdate?: string;
46
+ /**
47
+ * Should on update and on delete constraints be enabled on the foreign key.
48
+ */
49
+ constraints?: boolean;
50
+ foreignKeyConstraint?: boolean;
51
+ /**
52
+ * If `false` the applicable hooks will not be called.
53
+ * The default value depends on the context.
54
+ */
55
+ hooks?: boolean;
56
+ }
57
+ export declare class HasManyField extends RelationField {
58
+ get dataType(): any;
59
+ get foreignKey(): any;
60
+ reference(association: any): Reference;
61
+ checkAssociationKeys(): void;
62
+ bind(): boolean;
63
+ unbind(): void;
64
+ }
65
+ export interface HasManyFieldOptions extends MultipleRelationFieldOptions, SequelizeHasManyOptions {
66
+ type: 'hasMany';
67
+ target?: string;
68
+ }
@@ -0,0 +1,156 @@
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 has_many_field_exports = {};
20
+ __export(has_many_field_exports, {
21
+ HasManyField: () => HasManyField
22
+ });
23
+ module.exports = __toCommonJS(has_many_field_exports);
24
+ var import_lodash = require("lodash");
25
+ var import_sequelize = require("sequelize");
26
+ var import_references_map = require("../features/references-map");
27
+ var import_utils = require("../utils");
28
+ var import_relation_field = require("./relation-field");
29
+ const _HasManyField = class _HasManyField extends import_relation_field.RelationField {
30
+ get dataType() {
31
+ return "HasMany";
32
+ }
33
+ get foreignKey() {
34
+ if (this.options.foreignKey) {
35
+ return this.options.foreignKey;
36
+ }
37
+ const { model } = this.context.collection;
38
+ return import_sequelize.Utils.camelize([model.options.name.singular, this.sourceKey || model.primaryKeyAttribute].join("_"));
39
+ }
40
+ reference(association) {
41
+ const sourceKey = association.sourceKey;
42
+ return (0, import_references_map.buildReference)({
43
+ sourceCollectionName: this.database.modelCollection.get(association.target).name,
44
+ sourceField: association.foreignKey,
45
+ targetField: sourceKey,
46
+ targetCollectionName: this.database.modelCollection.get(association.source).name,
47
+ onDelete: this.options.onDelete
48
+ });
49
+ }
50
+ checkAssociationKeys() {
51
+ let { foreignKey, sourceKey } = this.options;
52
+ if (!sourceKey) {
53
+ sourceKey = this.collection.model.primaryKeyAttribute;
54
+ }
55
+ if (!foreignKey) {
56
+ foreignKey = import_sequelize.Utils.camelize([import_sequelize.Utils.singularize(this.name), this.collection.model.primaryKeyAttribute].join("_"));
57
+ }
58
+ const foreignKeyAttribute = this.TargetModel.rawAttributes[foreignKey];
59
+ const sourceKeyAttribute = this.collection.model.rawAttributes[sourceKey];
60
+ if (!foreignKeyAttribute || !sourceKeyAttribute) {
61
+ return;
62
+ }
63
+ const foreignKeyType = foreignKeyAttribute.type.constructor.toString();
64
+ const sourceKeyType = sourceKeyAttribute.type.constructor.toString();
65
+ if (!this.keyPairsTypeMatched(foreignKeyType, sourceKeyType)) {
66
+ throw new Error(
67
+ `Foreign key "${foreignKey}" type "${foreignKeyType}" does not match source key "${sourceKey}" type "${sourceKeyType}" in has many relation "${this.name}" of collection "${this.collection.name}"`
68
+ );
69
+ }
70
+ }
71
+ bind() {
72
+ const { database, collection } = this.context;
73
+ const Target = this.TargetModel;
74
+ if (!Target) {
75
+ database.addPendingField(this);
76
+ return false;
77
+ }
78
+ this.checkAssociationKeys();
79
+ if (collection.model.associations[this.name]) {
80
+ delete collection.model.associations[this.name];
81
+ }
82
+ const association = collection.model.hasMany(Target, {
83
+ constraints: false,
84
+ ...(0, import_lodash.omit)(this.options, ["name", "type", "target", "onDelete"]),
85
+ as: this.name,
86
+ foreignKey: this.foreignKey
87
+ });
88
+ database.removePendingField(this);
89
+ if (!this.options.foreignKey) {
90
+ this.options.foreignKey = association.foreignKey;
91
+ }
92
+ if (!this.options.sourceKey) {
93
+ this.options.sourceKey = association.sourceKey;
94
+ }
95
+ try {
96
+ (0, import_utils.checkIdentifier)(this.options.foreignKey);
97
+ } catch (error) {
98
+ this.unbind();
99
+ throw error;
100
+ }
101
+ if (!this.options.sourceKey) {
102
+ this.options.sourceKey = association.sourceKey;
103
+ }
104
+ let tcoll;
105
+ if (this.target === collection.name) {
106
+ tcoll = collection;
107
+ } else {
108
+ tcoll = database.getCollection(this.target);
109
+ }
110
+ if (tcoll) {
111
+ tcoll.addIndex([this.options.foreignKey]);
112
+ }
113
+ this.database.referenceMap.addReference(this.reference(association));
114
+ if (this.options.sortable) {
115
+ const targetCollection = database.modelCollection.get(this.TargetModel);
116
+ const sortFieldName = `${this.options.foreignKey}Sort`;
117
+ targetCollection.setField(sortFieldName, {
118
+ type: "sort",
119
+ hidden: true,
120
+ scopeKey: this.options.foreignKey
121
+ });
122
+ this.options.sortBy = sortFieldName;
123
+ }
124
+ return true;
125
+ }
126
+ unbind() {
127
+ const { database, collection } = this.context;
128
+ database.removePendingField(this);
129
+ const tcoll = database.getCollection(this.target);
130
+ if (tcoll) {
131
+ const foreignKey = this.options.foreignKey;
132
+ const field = tcoll.findField((field2) => {
133
+ if (field2.name === foreignKey) {
134
+ return true;
135
+ }
136
+ return field2.type === "belongsTo" && field2.foreignKey === foreignKey;
137
+ });
138
+ if (!field) {
139
+ tcoll.model.removeAttribute(foreignKey);
140
+ }
141
+ }
142
+ const association = collection.model.associations[this.name];
143
+ if (association && !this.options.inherit) {
144
+ this.database.referenceMap.removeReference(this.reference(association));
145
+ }
146
+ this.clearAccessors();
147
+ delete collection.model.associations[this.name];
148
+ collection.model.refreshAttributes();
149
+ }
150
+ };
151
+ __name(_HasManyField, "HasManyField");
152
+ let HasManyField = _HasManyField;
153
+ // Annotate the CommonJS export names for ESM import in node:
154
+ 0 && (module.exports = {
155
+ HasManyField
156
+ });
@@ -0,0 +1,68 @@
1
+ import { AssociationScope, DataType, ForeignKeyOptions, HasOneOptions, HasOneOptions as SequelizeHasOneOptions } from 'sequelize';
2
+ import { Reference } from '../features/references-map';
3
+ import { BaseRelationFieldOptions, RelationField } from './relation-field';
4
+ export interface HasOneFieldOptions extends HasOneOptions {
5
+ /**
6
+ * The name of the field to use as the key for the association in the source table. Defaults to the primary
7
+ * key of the source table
8
+ */
9
+ sourceKey?: string;
10
+ /**
11
+ * A string or a data type to represent the identifier in the table
12
+ */
13
+ keyType?: DataType;
14
+ scope?: AssociationScope;
15
+ /**
16
+ * The alias of this model, in singular form. See also the `name` option passed to `sequelize.define`. If
17
+ * you create multiple associations between the same tables, you should provide an alias to be able to
18
+ * distinguish between them. If you provide an alias when creating the assocition, you should provide the
19
+ * same alias when eager loading and when getting associated models. Defaults to the singularized name of
20
+ * target
21
+ */
22
+ as?: string | {
23
+ singular: string;
24
+ plural: string;
25
+ };
26
+ /**
27
+ * The name of the foreign key in the target table or an object representing the type definition for the
28
+ * foreign column (see `Sequelize.define` for syntax). When using an object, you can add a `name` property
29
+ * to set the name of the column. Defaults to the name of source + primary key of source
30
+ */
31
+ foreignKey?: string | ForeignKeyOptions;
32
+ /**
33
+ * What happens when delete occurs.
34
+ *
35
+ * Cascade if this is a n:m, and set null if it is a 1:m
36
+ *
37
+ * @default 'SET NULL' or 'CASCADE'
38
+ */
39
+ onDelete?: string;
40
+ /**
41
+ * What happens when update occurs
42
+ *
43
+ * @default 'CASCADE'
44
+ */
45
+ onUpdate?: string;
46
+ /**
47
+ * Should on update and on delete constraints be enabled on the foreign key.
48
+ */
49
+ constraints?: boolean;
50
+ foreignKeyConstraint?: boolean;
51
+ /**
52
+ * If `false` the applicable hooks will not be called.
53
+ * The default value depends on the context.
54
+ */
55
+ hooks?: boolean;
56
+ }
57
+ export declare class HasOneField extends RelationField {
58
+ get dataType(): any;
59
+ get target(): any;
60
+ get foreignKey(): any;
61
+ reference(association: any): Reference;
62
+ checkAssociationKeys(): void;
63
+ bind(): boolean;
64
+ unbind(): void;
65
+ }
66
+ export interface HasOneFieldOptions extends BaseRelationFieldOptions, SequelizeHasOneOptions {
67
+ type: 'hasOne';
68
+ }
@@ -0,0 +1,150 @@
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 has_one_field_exports = {};
20
+ __export(has_one_field_exports, {
21
+ HasOneField: () => HasOneField
22
+ });
23
+ module.exports = __toCommonJS(has_one_field_exports);
24
+ var import_lodash = require("lodash");
25
+ var import_sequelize = require("sequelize");
26
+ var import_references_map = require("../features/references-map");
27
+ var import_utils = require("../utils");
28
+ var import_relation_field = require("./relation-field");
29
+ const _HasOneField = class _HasOneField extends import_relation_field.RelationField {
30
+ get dataType() {
31
+ return "HasOne";
32
+ }
33
+ get target() {
34
+ const { target, name } = this.options;
35
+ return target || import_sequelize.Utils.pluralize(name);
36
+ }
37
+ get foreignKey() {
38
+ const foreignKey = (() => {
39
+ if (this.options.foreignKey) {
40
+ return this.options.foreignKey;
41
+ }
42
+ const { model } = this.context.collection;
43
+ return import_sequelize.Utils.camelize([model.options.name.singular, model.primaryKeyAttribute].join("_"));
44
+ })();
45
+ return foreignKey;
46
+ }
47
+ reference(association) {
48
+ const sourceKey = association.sourceKey;
49
+ return (0, import_references_map.buildReference)({
50
+ sourceCollectionName: this.database.modelCollection.get(association.target).name,
51
+ sourceField: association.foreignKey,
52
+ targetField: sourceKey,
53
+ targetCollectionName: this.database.modelCollection.get(association.source).name,
54
+ onDelete: this.options.onDelete
55
+ });
56
+ }
57
+ checkAssociationKeys() {
58
+ let { foreignKey, sourceKey } = this.options;
59
+ if (!sourceKey) {
60
+ sourceKey = this.collection.model.primaryKeyAttribute;
61
+ }
62
+ if (!foreignKey) {
63
+ foreignKey = import_sequelize.Utils.camelize([import_sequelize.Utils.singularize(this.name), this.collection.model.primaryKeyAttribute].join("_"));
64
+ }
65
+ const foreignKeyAttribute = this.TargetModel.rawAttributes[foreignKey];
66
+ const sourceKeyAttribute = this.collection.model.rawAttributes[sourceKey];
67
+ if (!foreignKeyAttribute || !sourceKeyAttribute) {
68
+ return;
69
+ }
70
+ const foreignKeyType = foreignKeyAttribute.type.constructor.toString();
71
+ const sourceKeyType = sourceKeyAttribute.type.constructor.toString();
72
+ if (!this.keyPairsTypeMatched(foreignKeyType, sourceKeyType)) {
73
+ throw new Error(
74
+ `Foreign key "${foreignKey}" type "${foreignKeyType}" does not match source key "${sourceKey}" type "${sourceKeyType}" in has one relation "${this.name}" of collection "${this.collection.name}"`
75
+ );
76
+ }
77
+ }
78
+ bind() {
79
+ const { database, collection } = this.context;
80
+ const Target = this.TargetModel;
81
+ if (!Target) {
82
+ database.addPendingField(this);
83
+ return false;
84
+ }
85
+ this.checkAssociationKeys();
86
+ const association = collection.model.hasOne(Target, {
87
+ constraints: false,
88
+ ...(0, import_lodash.omit)(this.options, ["name", "type", "target", "onDelete"]),
89
+ as: this.name,
90
+ foreignKey: this.foreignKey
91
+ });
92
+ database.removePendingField(this);
93
+ if (!this.options.foreignKey) {
94
+ this.options.foreignKey = association.foreignKey;
95
+ }
96
+ if (!this.options.sourceKey) {
97
+ this.options.sourceKey = association.sourceKey;
98
+ }
99
+ try {
100
+ (0, import_utils.checkIdentifier)(this.options.foreignKey);
101
+ } catch (error) {
102
+ this.unbind();
103
+ throw error;
104
+ }
105
+ if (!this.options.sourceKey) {
106
+ this.options.sourceKey = association.sourceKey;
107
+ }
108
+ let tcoll;
109
+ if (this.target === collection.name) {
110
+ tcoll = collection;
111
+ } else {
112
+ tcoll = database.getCollection(this.target);
113
+ }
114
+ if (tcoll) {
115
+ tcoll.addIndex([this.options.foreignKey]);
116
+ }
117
+ this.database.referenceMap.addReference(this.reference(association));
118
+ return true;
119
+ }
120
+ unbind() {
121
+ const { database, collection } = this.context;
122
+ database.removePendingField(this);
123
+ const tcoll = database.collections.get(this.target);
124
+ if (tcoll && !this.options.inherit) {
125
+ const foreignKey = this.options.foreignKey;
126
+ const field = tcoll.findField((field2) => {
127
+ if (field2.name === foreignKey) {
128
+ return true;
129
+ }
130
+ return field2.type === "belongsTo" && field2.foreignKey === foreignKey;
131
+ });
132
+ if (!field) {
133
+ tcoll.model.removeAttribute(foreignKey);
134
+ }
135
+ }
136
+ const association = collection.model.associations[this.name];
137
+ if (association && !this.options.inherit) {
138
+ this.database.referenceMap.removeReference(this.reference(association));
139
+ }
140
+ this.clearAccessors();
141
+ delete collection.model.associations[this.name];
142
+ collection.model.refreshAttributes();
143
+ }
144
+ };
145
+ __name(_HasOneField, "HasOneField");
146
+ let HasOneField = _HasOneField;
147
+ // Annotate the CommonJS export names for ESM import in node:
148
+ 0 && (module.exports = {
149
+ HasOneField
150
+ });
@@ -0,0 +1,46 @@
1
+ import { ArrayFieldOptions } from './array-field';
2
+ import { BelongsToFieldOptions } from './belongs-to-field';
3
+ import { BelongsToManyFieldOptions } from './belongs-to-many-field';
4
+ import { BooleanFieldOptions } from './boolean-field';
5
+ import { ContextFieldOptions } from './context-field';
6
+ import { DateFieldOptions } from './date-field';
7
+ import { BaseFieldOptions } from './field';
8
+ import { HasManyFieldOptions } from './has-many-field';
9
+ import { HasOneFieldOptions } from './has-one-field';
10
+ import { JsonbFieldOptions, JsonFieldOptions } from './json-field';
11
+ import { NanoidFieldOptions } from './nanoid-field';
12
+ import { DecimalFieldOptions, DoubleFieldOptions, FloatFieldOptions, IntegerFieldOptions, RealFieldOptions } from './number-field';
13
+ import { PasswordFieldOptions } from './password-field';
14
+ import { RadioFieldOptions } from './radio-field';
15
+ import { SetFieldOptions } from './set-field';
16
+ import { SortFieldOptions } from './sort-field';
17
+ import { StringFieldOptions } from './string-field';
18
+ import { TextFieldOptions } from './text-field';
19
+ import { TimeFieldOptions } from './time-field';
20
+ import { UidFieldOptions } from './uid-field';
21
+ import { UUIDFieldOptions } from './uuid-field';
22
+ import { VirtualFieldOptions } from './virtual-field';
23
+ export * from './array-field';
24
+ export * from './belongs-to-field';
25
+ export * from './belongs-to-many-field';
26
+ export * from './boolean-field';
27
+ export * from './context-field';
28
+ export * from './date-field';
29
+ export * from './field';
30
+ export * from './has-many-field';
31
+ export * from './has-one-field';
32
+ export * from './json-field';
33
+ export * from './number-field';
34
+ export * from './password-field';
35
+ export * from './radio-field';
36
+ export * from './relation-field';
37
+ export * from './set-field';
38
+ export * from './sort-field';
39
+ export * from './string-field';
40
+ export * from './text-field';
41
+ export * from './time-field';
42
+ export * from './uid-field';
43
+ export * from './uuid-field';
44
+ export * from './virtual-field';
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;