From 9f6bcf71a51fc53e28b33df8a7ddd9e54e7166c2 Mon Sep 17 00:00:00 2001 From: Lars-Erik Roald Date: Tue, 14 May 2024 05:08:47 +0000 Subject: [PATCH] closes #86 --- src/client/index.js | 4 ++-- src/patchTable.js | 2 -- tests/initMs.js | 10 ++++----- tests/initOracle.js | 10 ++++----- tests/initSap.js | 10 ++++----- tests/update.test.js | 49 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 66 insertions(+), 19 deletions(-) diff --git a/src/client/index.js b/src/client/index.js index 64f34209..1e4ee074 100644 --- a/src/client/index.js +++ b/src/client/index.js @@ -411,7 +411,7 @@ function rdbClient(options = {}) { const concurrency = undefined; const args = [concurrency].concat(rest); if (Array.isArray(rows)) { - let proxy = proxify([], args[0]); + let proxy = proxify([], rest[0]); proxy.splice.apply(proxy, [0, 0, ...rows]); await proxy.saveChanges.apply(proxy, args); return proxy; @@ -420,7 +420,7 @@ function rdbClient(options = {}) { let proxy = proxify([], args[0]); proxy.splice.apply(proxy, [0, 0, rows]); await proxy.saveChanges.apply(proxy, args); - return proxify(proxy[0], args[0]); + return proxify(proxy[0], rest[0]); } } diff --git a/src/patchTable.js b/src/patchTable.js index 675f61eb..a3f4d447 100644 --- a/src/patchTable.js +++ b/src/patchTable.js @@ -119,8 +119,6 @@ async function patchTableCore(table, patches, { strategy = undefined, deduceStra else if (isOneRelation(property, table)) { let relation = table[property]._relation; let subRow = await row[property]; - if (!subRow) - throw new Error(`${property} was not found`); strategy[property] = strategy[property] || {}; options[property] = inferOptions(options, property); diff --git a/tests/initMs.js b/tests/initMs.js index 2ec1bb16..8bcecdfb 100644 --- a/tests/initMs.js +++ b/tests/initMs.js @@ -54,11 +54,11 @@ CREATE TABLE package ( CREATE TABLE deliveryAddress ( id int IDENTITY(1,1) PRIMARY KEY, orderId INTEGER REFERENCES torder, - name VARCHAR(100), - street VARCHAR(100), - postalCode VARCHAR(100), - postalPlace VARCHAR(100), - countryCode VARCHAR(100) + name VARCHAR(100) NULL, + street VARCHAR(100) NULL, + postalCode VARCHAR(100) NULL, + postalPlace VARCHAR(100) NULL, + countryCode VARCHAR(100) NULL ) diff --git a/tests/initOracle.js b/tests/initOracle.js index 1b500507..55ff3117 100644 --- a/tests/initOracle.js +++ b/tests/initOracle.js @@ -92,11 +92,11 @@ BEGIN `CREATE TABLE deliveryAddress ( id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, orderId INTEGER, - name VARCHAR2(100), - street VARCHAR2(100), - postalCode VARCHAR2(100), - postalPlace VARCHAR2(100), - countryCode VARCHAR2(100), + name VARCHAR2(100) null, + street VARCHAR2(100) null, + postalCode VARCHAR2(100) null, + postalPlace VARCHAR2(100) null, + countryCode VARCHAR2(100) null, FOREIGN KEY (orderId) REFERENCES torder(id) ) `, diff --git a/tests/initSap.js b/tests/initSap.js index 2a35a87f..3c5d1094 100644 --- a/tests/initSap.js +++ b/tests/initSap.js @@ -132,11 +132,11 @@ GO CREATE TABLE deliveryAddress ( id int IDENTITY PRIMARY KEY, orderId INTEGER REFERENCES torder, - name VARCHAR(100), - street VARCHAR(100), - postalCode VARCHAR(100), - postalPlace VARCHAR(100), - countryCode VARCHAR(100) + name VARCHAR(100) NULL, + street VARCHAR(100) NULL, + postalCode VARCHAR(100) NULL, + postalPlace VARCHAR(100) NULL, + countryCode VARCHAR(100) NULL ) `; diff --git a/tests/update.test.js b/tests/update.test.js index fe6344d7..0f7093aa 100644 --- a/tests/update.test.js +++ b/tests/update.test.js @@ -276,6 +276,55 @@ describe('update date', () => { } }); + +describe('add hasOne', () => { + + test('pg', async () => await verify('pg')); + test('oracle', async () => await verify('oracle')); + test('mssql', async () => await verify('mssql')); + if (major === 18) + test('mssqlNative', async () => await verify('mssqlNative')); + test('mysql', async () => await verify('mysql')); + test('sap', async () => await verify('sap')); + test('sqlite', async () => await verify('sqlite')); + test('http', async () => await verify('http')); + + async function verify(dbName) { + const { db, init } = getDb(dbName); + if (dbName === 'http') { + const { db, init } = getDb('sqlite2'); + await init(db); + } + else + await init(db); + + const row = await db.order.insert({orderDate: date1}, {deliveryAddress: true}); + row.deliveryAddress = { + name: 'bar', + street: 'Node street 2', + }; + await row.saveChanges({}); + row.orderDate = dateToISOString(new Date(row.orderDate)); + + const expected = { + id: 1, + customerId: null, + orderDate: dateToISOString(date1), + deliveryAddress: { + id: 1, + orderId: 1, + name: 'bar', + street: 'Node street 2', + postalCode: null, + postalPlace: null, + countryCode: null + } + }; + expect(row).toEqual(expected); + } +}); + + const pathSegments = fileURLToPath(import.meta.url).split('/'); const lastSegment = pathSegments[pathSegments.length - 1]; const fileNameWithoutExtension = lastSegment.split('.')[0];