Skip to content

Commit 27d4f6e

Browse files
author
Lee Richmond
committed
Move to predictive temp IDs
Temp IDs don't have to actually be unique - they just have to match up the relationships/included sections of the payload, and serve as a reference to an unpersisted object on the client. They should be unique, not universally or globally unique. Because of this, let's generate more predictive, simpler temp IDs. This improves readability, and avoids mocking difficulties.
1 parent b9e7b33 commit 27d4f6e

File tree

5 files changed

+15
-16
lines changed

5 files changed

+15
-16
lines changed

src/util/temp-id.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
let memo = 0
2+
const generate = function() : string {
3+
memo++
4+
return `temp-id-${memo}`
5+
}
6+
7+
export default { generate }

src/util/uuid.ts

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/util/write-payload.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Model from '../model';
22
import IncludeDirective from './include-directive';
33
import * as _snakeCase from './snakecase';
4-
import uuid from './uuid';
4+
import tempId from './temp-id';
55
let snakeCase: any = (<any>_snakeCase).default || _snakeCase;
66
snakeCase = snakeCase['default'] || snakeCase;
77

@@ -134,7 +134,7 @@ export default class WritePayload {
134134
model.clearErrors();
135135

136136
if (!model.isPersisted()) {
137-
model.temp_id = uuid.generate();
137+
model.temp_id = tempId.generate()
138138
}
139139

140140
let wp = new WritePayload(model, nested);

test/integration/nested-persistence-test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { sinon, expect, fetchMock } from '../test-helper';
22
import { Author, Book, Genre } from '../fixtures';
3-
import uuid from '../../src/util/uuid';
3+
import tempId from '../../src/util/temp-id';
44

55
let fetchMock = require('fetch-mock');
66

@@ -184,15 +184,15 @@ describe('nested persistence', function() {
184184

185185
let tempIdIndex = 0;
186186
beforeEach(function() {
187-
sinon.stub(uuid, 'generate').callsFake(function() {
187+
sinon.stub(tempId, 'generate').callsFake(function() {
188188
tempIdIndex++
189189
return `abc${tempIdIndex}`;
190190
});
191191
});
192192

193193
afterEach(function() {
194194
tempIdIndex = 0;
195-
uuid.generate['restore']();
195+
tempId.generate['restore']();
196196
});
197197

198198
describe('basic nested create', function() {

test/integration/validations-test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { expect, sinon, fetchMock } from '../test-helper';
22
import { Author, Book, Genre } from '../fixtures';
3-
import uuid from '../../src/util/uuid';
3+
import tempId from '../../src/util/temp-id';
44

55
const resetMocks = function() {
66
fetchMock.restore();
@@ -94,7 +94,7 @@ describe('validations', function() {
9494
});
9595

9696
beforeEach(function() {
97-
sinon.stub(uuid, 'generate').callsFake(function() {
97+
sinon.stub(tempId, 'generate').callsFake(function() {
9898
tempIdIndex++
9999
return `abc${tempIdIndex}`;
100100
});
@@ -108,7 +108,7 @@ describe('validations', function() {
108108

109109
afterEach(function() {
110110
tempIdIndex = 0;
111-
uuid.generate['restore']();
111+
tempId.generate['restore']();
112112
});
113113

114114
it('applies errors to the instance', function(done) {

0 commit comments

Comments
 (0)