Code Monkey home page Code Monkey logo

ember-cli-uuid's Introduction

Ember CLI UUID

npm version Build Status Ember Observer Score Code Climate

This addon adds a hook to your Ember Data Adapter (and all adapters extended from DS.Adapter) to generate the id of each record you create on the client-side. This hook can be deactivated from your config (see Deactivate the "auto UUID" feature). You can also choose to use this addon for certain adapters only through a mixin (see Use the UUID Adapter mixin as an alternative).

The created ids are v4 UUIDs which are random "enough", quoting wikipedia:

the annual risk of a given person being hit by a meteorite is estimated to be one chance in 17 billion, which means the probability is about 0.00000000006 (6 ร— 10โˆ’11), equivalent to the odds of creating a few tens of trillions of UUIDs in a year and having one duplicate. In other words, only after generating 1 billion UUIDs every second for the next 100 years, the probability of creating just one duplicate would be about 50%.

Addon's associated blog post : Using uuids with ember data (warning: old ember version).

Installation

ember install ember-cli-uuid

Usage

This Ember CLI module is intended for use with Ember Data. Once you install it (and the blueprint is applied) your ember data models will get a new behaviour when being created on the client-side. When creating a model like that :

store.createRecord('post', {
  title: 'My post'
});

Your model will get a v4 (random) UUID as his primary key :

{
  id: 'b4301bcb-a687-4f91-86c8-8b9241f6e6bc',
  title: 'My post'
}

Deactivate the "auto UUID" feature

You might need to turn off the auto generation of UUIDs on your Ember Data models. To achieve that, you will need to edit your config/environment.js file like this :

const ENV = {
  // ... others config vars
  'ember-cli-uuid': {
    defaultUUID: false
  }
};

Use the UUID Adapter mixin as an alternative

Even with the auto generation of UUID's disabled you still have the option to use the provided adapter mixin to opt in to UUID generation either globally via an ApplicationAdapter or on an Adapter by Adapter basis. One benefit of creating UUIDs via the adapter mixin is that adapters will still have the feature in both unit and integration tests. You can use the adapter mixin like this:

import AdaptersUuidMixin from 'ember-cli-uuid/mixins/adapters/uuid';
import DS from 'ember-data';

export default DS.Adapter.extend(AdaptersUuidMixin);

Direct usage

You can also (if you need a UUID v4 somewhere in your app) use it like that :

import {uuid} from 'ember-cli-uuid';

uuid(); // 6c84fb90-12c4-11e1-840d-7b25c5ee775a

ember-cli-uuid's People

Contributors

thaume avatar kennethdavidbuck avatar kidgodzilla avatar dependabot[bot] avatar loganrosen avatar remkoboschker avatar robbiethewagner avatar ember-tomster avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.