@@ -274,7 +274,7 @@ const Model = class Model {
274
274
*
275
275
* @param {Object } userProps - the new {@link Model}'s properties.
276
276
* @return {Model } a new {@link Model} instance.
277
- */
277
+
278
278
static create(userProps) {
279
279
if (typeof this._session === "undefined") {
280
280
throw new Error(
@@ -313,7 +313,7 @@ const Model = class Model {
313
313
* from the target models when refreshing the M2M relations.
314
314
* If the relationship does have an accessor (`as`) field then we do want to keep this
315
315
* original value in the props to expose the raw list of IDs from the instance.
316
- */
316
+
317
317
delete props[key];
318
318
}
319
319
}
@@ -345,6 +345,89 @@ const Model = class Model {
345
345
const instance = new ThisModel(newEntry);
346
346
instance._refreshMany2Many(m2mRelations); // eslint-disable-line no-underscore-dangle
347
347
return instance;
348
+ }*/
349
+
350
+ static createOneRecord ( item ) {
351
+ if ( typeof this . _session === 'undefined' ) {
352
+ throw new Error ( [
353
+ `Tried to create a ${ this . modelName } model instance without a session. ` ,
354
+ 'Create a session using `session = orm.session()` and call ' ,
355
+ `\`session["${ this . modelName } "].create\` instead.` ,
356
+ ] . join ( '' ) ) ;
357
+ }
358
+ const props = { ...item } ;
359
+
360
+ const m2mRelations = { } ;
361
+
362
+ const declaredFieldNames = Object . keys ( this . fields ) ;
363
+ const declaredVirtualFieldNames = Object . keys ( this . virtualFields ) ;
364
+
365
+ declaredFieldNames . forEach ( ( key ) => {
366
+ const field = this . fields [ key ] ;
367
+ const valuePassed = item . hasOwnProperty ( key ) ;
368
+ if ( ! ( field instanceof ManyToMany ) ) {
369
+ if ( valuePassed ) {
370
+ const value = item [ key ] ;
371
+ props [ key ] = normalizeEntity ( value ) ;
372
+ } else if ( field . getDefault ) {
373
+ props [ key ] = field . getDefault ( ) ;
374
+ }
375
+ } else if ( valuePassed ) {
376
+ // If a value is supplied for a ManyToMany field,
377
+ // discard them from props and save for later processing.
378
+ m2mRelations [ key ] = item [ key ] ;
379
+ delete props [ key ] ;
380
+ }
381
+ } ) ;
382
+
383
+ // add backward many-many if required
384
+ declaredVirtualFieldNames . forEach ( ( key ) => {
385
+ if ( ! m2mRelations . hasOwnProperty ( key ) ) {
386
+ const field = this . virtualFields [ key ] ;
387
+ if ( item . hasOwnProperty ( key ) && field instanceof ManyToMany ) {
388
+ // If a value is supplied for a ManyToMany field,
389
+ // discard them from props and save for later processing.
390
+ m2mRelations [ key ] = item [ key ] ;
391
+ delete props [ key ] ;
392
+ }
393
+ }
394
+ } ) ;
395
+
396
+ const newEntry = this . session . applyUpdate ( {
397
+ action : CREATE ,
398
+ table : this . modelName ,
399
+ payload : props ,
400
+ } ) ;
401
+
402
+ const ThisModel = this ;
403
+ const instance = new ThisModel ( newEntry ) ;
404
+ instance . _refreshMany2Many ( m2mRelations ) ; // eslint-disable-line no-underscore-dangle
405
+ return instance ;
406
+ }
407
+
408
+ /**
409
+ * Creates a new record in the database, instantiates a {@link Model} and returns it.
410
+ *
411
+ * If you pass values for many-to-many fields, instances are created on the through
412
+ * model as well.
413
+ *
414
+ * @param {props } userProps - the new {@link Model}'s properties.
415
+ * @return {Model } a new {@link Model} instance.
416
+ */
417
+ static create ( userProps ) {
418
+ let results ;
419
+
420
+ if ( Array . isArray ( userProps ) ) {
421
+ console . log ( 'is batch update' ) ;
422
+ results = [ ] ;
423
+ userProps . forEach ( ( item ) => {
424
+ const instance = this . createOneRecord ( item ) ;
425
+ results . push ( instance ) ;
426
+ } ) ;
427
+ } else {
428
+ results = this . createOneRecord ( userProps ) ;
429
+ }
430
+ return results ;
348
431
}
349
432
350
433
/**
0 commit comments