(related_collections: string[], relation_callback?: (options: M2AOptions) => M2AOptions | void)
| 296 | } |
| 297 | |
| 298 | m2a(related_collections: string[], relation_callback?: (options: M2AOptions) => M2AOptions | void): this { |
| 299 | assert(this._data._kind === 'initial', 'Field type was already set'); |
| 300 | assert(this._schema && this._collection, 'Field needs to be part of a schema'); |
| 301 | |
| 302 | this._data = { |
| 303 | field: this._data.field, |
| 304 | ...FIELD_DEFAULTS, |
| 305 | type: 'alias', |
| 306 | dbType: null, |
| 307 | special: ['m2a'], |
| 308 | _kind: 'finished', |
| 309 | }; |
| 310 | |
| 311 | const junction_name = `${this._collection.get_name()}_builder`; |
| 312 | |
| 313 | let o2m_relation = new RelationBuilder(this._collection.get_name(), this.get_name()) |
| 314 | .o2m(junction_name, `${this._collection.get_name()}_id`) |
| 315 | .options({ |
| 316 | meta: { |
| 317 | junction_field: `item`, |
| 318 | }, |
| 319 | }); |
| 320 | |
| 321 | let a2o_relation = new RelationBuilder(junction_name, 'item').a2o(related_collections).options({ |
| 322 | meta: { |
| 323 | junction_field: `${this._collection.get_name()}_id`, |
| 324 | }, |
| 325 | }); |
| 326 | |
| 327 | if (relation_callback) { |
| 328 | const new_relations = relation_callback({ o2m_relation, a2o_relation }); |
| 329 | |
| 330 | if (new_relations) { |
| 331 | o2m_relation = new_relations.o2m_relation; |
| 332 | a2o_relation = new_relations.a2o_relation; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | this._schema._relations.push(o2m_relation); |
| 337 | this._schema._relations.push(a2o_relation); |
| 338 | |
| 339 | return this; |
| 340 | } |
| 341 | |
| 342 | m2m(related_collection: string, relation_callback?: (options: M2MOptions) => M2MOptions | void): this { |
| 343 | assert(this._data._kind === 'initial', 'Field type was already set'); |
no test coverage detected