diff --git a/CliClient/app/command-apidoc.js b/CliClient/app/command-apidoc.js index 880fcccd2..95e7c4750 100644 --- a/CliClient/app/command-apidoc.js +++ b/CliClient/app/command-apidoc.js @@ -253,6 +253,12 @@ class Command extends BaseCommand { lines.push(''); lines.push(' curl --data \'{ "title": "Image test", "body": "Here is Joplin icon:", "image_data_url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAIAAABLbSncAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAANZJREFUeNoAyAA3/wFwtO3K6gUB/vz2+Prw9fj/+/r+/wBZKAAExOgF4/MC9ff+MRH6Ui4E+/0Bqc/zutj6AgT+/Pz7+vv7++nu82c4DlMqCvLs8goA/gL8/fz09fb59vXa6vzZ6vjT5fbn6voD/fwC8vX4UiT9Zi//APHyAP8ACgUBAPv5APz7BPj2+DIaC2o3E+3o6ywaC5fT6gD6/QD9/QEVf9kD+/dcLQgJA/7v8vqfwOf18wA1IAIEVycAyt//v9XvAPv7APz8LhoIAPz9Ri4OAgwARgx4W/6fVeEAAAAASUVORK5CYII="}\' http://127.0.0.1:41184/notes'); lines.push(''); + lines.push('### Creating a note with a specific ID'); + lines.push(''); + lines.push('When a new note is created, it is automatically assigned a new unique ID so **normally you do not need to set the ID**. However, if for some reason you want to set it, you can supply it as the `id` property. It needs to be a 32 characters long hexadecimal string. **Make sure it is unique**, for example by generating it using whatever GUID function is available in your programming language.'); + lines.push(''); + lines.push(' curl --data \'{ "id": "00a87474082744c1a8515da6aa5792d2", "title": "My note with custom ID"}\' http://127.0.0.1:41184/notes'); + lines.push(''); } lines.push('## PUT /' + tableName + '/:id'); diff --git a/CliClient/tests/services_rest_Api.js b/CliClient/tests/services_rest_Api.js index 94dbaded1..1515d4072 100644 --- a/CliClient/tests/services_rest_Api.js +++ b/CliClient/tests/services_rest_Api.js @@ -156,6 +156,20 @@ describe('services_rest_Api', function() { done(); }); + it('should create notes with supplied ID', async (done) => { + let response = null; + const f = await Folder.save({ title: "mon carnet" }); + + response = await api.route('POST', 'notes', null, JSON.stringify({ + id: '12345678123456781234567812345678', + title: 'testing', + parent_id: f.id, + })); + expect(response.id).toBe('12345678123456781234567812345678'); + + done(); + }); + it('should create notes with images', async (done) => { let response = null; const f = await Folder.save({ title: "mon carnet" }); diff --git a/ReactNativeClient/lib/services/rest/Api.js b/ReactNativeClient/lib/services/rest/Api.js index 775e74095..32001467c 100644 --- a/ReactNativeClient/lib/services/rest/Api.js +++ b/ReactNativeClient/lib/services/rest/Api.js @@ -339,7 +339,9 @@ class Api { this.logger().info('Request (' + requestId + '): Saving note...'); - note = await Note.save(note); + const saveOptions = {}; + if (note.id) saveOptions.isNew = true; + note = await Note.save(note, saveOptions); if (requestNote.tags) { const tagTitles = requestNote.tags.split(','); @@ -378,6 +380,8 @@ class Api { body: requestNote.body ? requestNote.body : '', }; + if (requestNote.id) output.id = requestNote.id; + if (requestNote.body_html) { // Parsing will not work if the HTML is not wrapped in a top level tag, which is not guaranteed // when getting the content from elsewhere. So here wrap it - it won't change anything to the final