diff --git a/src/service-object.ts b/src/service-object.ts index 550d88e5..7da92b1d 100644 --- a/src/service-object.ts +++ b/src/service-object.ts @@ -250,6 +250,9 @@ class ServiceObject extends EventEmitter { const [err, instance] = args; if (!err) { self.metadata = instance.metadata; + if (self.id && instance.metadata) { + self.id = instance.metadata.id; + } args[1] = self; // replace the created `instance` with this one. } callback!(...(args as {} as [Error, T])); diff --git a/test/service-object.ts b/test/service-object.ts index eab42d7d..6cba89b0 100644 --- a/test/service-object.ts +++ b/test/service-object.ts @@ -175,6 +175,28 @@ describe('ServiceObject', () => { serviceObject.create(options, done); }); + it('should update id with metadata id', done => { + const config = extend({}, CONFIG, { + createMethod, + }); + const options = {}; + + function createMethod( + id: string, + options_: {}, + callback: (err: Error | null, a: {}, b: {}) => void + ) { + assert.strictEqual(id, config.id); + assert.strictEqual(options_, options); + callback(null, {metadata: {id: 14}}, {}); + } + + const serviceObject = new ServiceObject(config); + serviceObject.create(options); + assert.strictEqual(serviceObject.id, 14); + done(); + }); + it('should not require options', done => { const config = extend({}, CONFIG, { createMethod,