npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-data-copyable

v1.3.1

Published

Intelligently copy an Ember Data model and all of its relationships

Downloads

17,851

Readme

Ember Data Copyable

Build Status npm version

Intelligently copy an Ember Data model and all of its relationships

Features

  • Shallow & deep copy an Ember Data model
  • Shallow & deep copy model relationships
  • Handles cyclical relationships
  • Handles custom transforms to create true copies
  • Overwrite, ignore attributes, and copy objects by reference
  • Intelligent failure and cleanup
  • Uses ember-concurrency to allow cancelling a copy task

Installation

ember install ember-data-copyable

Helpful Links

Looking for help?

If it is a bug please open an issue on GitHub.

Setup

This addon provides a Copyable mixin which allows you to copy a model. To get started, just import the mixin and add it to your model.

// models/user.js

import DS from 'ember-data';
import Copyable from 'ember-data-copyable';

export default DS.Model.extend(Copyable, {
  guid: DS.attr('number'),
  firstName: DS.attr('string'),
  lastName: DS.attr('string'),
  address: DS.belongsTo('address'),
  friends: DS.hasMany('user'),

  // Specific copy options for the model
  copyableOptions: {}
});

In your model you can specify default options via the copyableOptions object. Please see options for more details.

Copying Relationships

To copy a model's relationship, that relational model must have the Copyable mixin or else it will just be copied by reference.

Usage

Once the model is setup, we can use the copy method on an instance to duplicate it.

Copy Method Signature

async function copy(deep = false, options = {}) {}
  • deep : Boolean

    If false a shallow copy of the model's attributes will be created. All relationships will be copied over by reference.

    If true, a deep copy of the model and it's relationships will be created. If a relational model has the Copyable mixin, it will also be deep copied.

  • options : Object

    Copy options. See options for more details.

    NOTE: Options passed into the copy method take precedence over those specified on the model.

Returns a cancelable promise like ember-concurrency TaskInstance.

Examples

Normal Usage

const model = this.get('store').peekRecord('user', 1);

model
  .copy(true, {
    ignoreAttributes: ['guid'],
    copyByReference: ['address'],
    overwrite: {
      id: 2,
      firstName: 'Offir'
    }
  })
  .then(
    copy => {
      // Handle success
      return copy.save();
    },
    e => {
      // Handle error or cancellation
    }
  );

Task Cancellation

Since the copy method returns an ember-concurrency TaskInstance, we have the ability to cancel a running copy task at any time.

const model = this.get('store').peekRecord('user', 1);
const copyTaskInstance = model.copy(true);

// Cancel our copy task
copyTaskInstance.cancel();

Options

These options can either be specified via the copyableOptions object on the DS.Model class or passed into the copy method.

ignoreAttributes

Attributes to ignore when copying.

ignoreAttributes: ['guid', 'address'];

otherAttributes

Other attributes to copy over that are not defined via DS.attr, DS.belongsTo, or DS.hasMany.

otherAttributes: ['timestamp', 'someFlag'];

copyByReference

Attributes to copy only by reference. If the attribute has the Copyable mixin, it will be ignored and not be copied, just copied by reference.

copyByReference: ['friends'];

overwrite

Overwrite attributes with the specified values. This will also add properties that are not found on the model.

overwrite: {
  id: 2,
  firstName: 'Offir - Copy',
  address: this.get('store').createRecord('address', { /* ... */ }),
  unknownProperty: 'Foo Bar'
}

relationships

Specify options to nested models. Nested relationships options can also include a deep option. If set to false, the relationship will be shallow copied.

relationships: {
  address: {
    ignoreAttributes: ['streetName'],
    overwrite: {
      state: 'CA'
    }
  },
  friends: {
    copyByReference: ['address']
  },
  profile: {
    deep: false
  }
}