sqlite-replication
Advanced tools
Comparing version 0.0.21-b to 0.0.22-b
@@ -8,2 +8,5 @@ import { SQLiteDBConnection } from '@capacitor-community/sqlite'; | ||
}): ReplicationCollectionOptions; | ||
static getUpsertStatement(collectionName: string, document: any, option?: { | ||
ignoreUndefinedProperties: boolean; | ||
}): string | Promise<void>; | ||
static safeValue(value: any): any; | ||
@@ -10,0 +13,0 @@ static ensureRequiredColumns(db: ReplicationStorage, collections: ReplicationCollectionOptions[]): Promise<void>; |
@@ -44,4 +44,13 @@ const EXPECTED_COLUMNS = ['id', '_forkParent', 'updatedAt', 'deletedAt']; | ||
} | ||
static getUpsertStatement(collectionName, document, option = { ignoreUndefinedProperties: true }) { | ||
if (!document) | ||
return Promise.resolve(); | ||
const keys = Object.keys(document).filter((key) => typeof document[key] !== 'undefined' || !option.ignoreUndefinedProperties); | ||
const values = keys.map((key) => ReplicationHelpers.safeValue(document[key])).join(); | ||
const conflictUpdate = keys.map((key) => `"${key}"=excluded."${key}"`).join(); | ||
return `INSERT INTO "${collectionName}" (${keys.map((key) => `"${key}"`).join()}) values (${values}) | ||
ON CONFLICT DO UPDATE SET ${conflictUpdate}`; | ||
} | ||
static safeValue(value) { | ||
if (value === null) { | ||
if (value === null || typeof value === 'undefined') { | ||
return 'NULL'; | ||
@@ -48,0 +57,0 @@ } |
{ | ||
"name": "sqlite-replication", | ||
"version": "0.0.21b", | ||
"version": "0.0.22b", | ||
"description": "A Typescript module to replicate SQLite DB with server.", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -73,2 +73,41 @@ import { ReplicationHelpers } from './replicationHelpers'; | ||
}); | ||
describe('upsert', () => { | ||
it('should generate SQL from random object', async () => { | ||
const resultingSQL = await ReplicationHelpers.getUpsertStatement('users', { id: 123, firstName: 'Andrew' }); | ||
expect(resultingSQL).toEqual( | ||
`INSERT INTO "users" ("id","firstName") values (123,'Andrew') | ||
ON CONFLICT DO UPDATE SET "id"=excluded."id","firstName"=excluded."firstName"`, | ||
); | ||
}); | ||
it('should generate SQL from random object changing undefined to null', async () => { | ||
const resultingSQL = await ReplicationHelpers.getUpsertStatement( | ||
'users', | ||
{ | ||
id: 123, | ||
firstName: 'Andrew', | ||
lastName: undefined, | ||
}, | ||
{ ignoreUndefinedProperties: false }, | ||
); | ||
expect(resultingSQL).toEqual( | ||
`INSERT INTO "users" ("id","firstName","lastName") values (123,'Andrew',NULL) | ||
ON CONFLICT DO UPDATE SET "id"=excluded."id","firstName"=excluded."firstName","lastName"=excluded."lastName"`, | ||
); | ||
}); | ||
it('should generate SQL from random object ignored undefined properties', async () => { | ||
const resultingSQL = await ReplicationHelpers.getUpsertStatement( | ||
'users', | ||
{ | ||
id: 123, | ||
firstName: 'Andrew', | ||
lastName: undefined, | ||
}, | ||
{ ignoreUndefinedProperties: true }, | ||
); | ||
expect(resultingSQL).toEqual( | ||
`INSERT INTO "users" ("id","firstName") values (123,'Andrew') | ||
ON CONFLICT DO UPDATE SET "id"=excluded."id","firstName"=excluded."firstName"`, | ||
); | ||
}); | ||
}); | ||
describe('safeValue', () => { | ||
@@ -92,2 +131,6 @@ it('should wrap strings with single quotes', async () => { | ||
}); | ||
it('should map undefined to null', async () => { | ||
let val; | ||
expect(ReplicationHelpers.safeValue(val)).toEqual('NULL'); | ||
}); | ||
it('should convert boolean to 0/1', async () => { | ||
@@ -94,0 +137,0 @@ expect(ReplicationHelpers.safeValue(true)).toEqual('1'); |
import { SQLiteDBConnection } from '@capacitor-community/sqlite'; | ||
import { ReplicationCollectionOptions, ReplicationConfig, ReplicationOptions, ReplicationStorage } from './replication'; | ||
const EXPECTED_COLUMNS = ['id', '_forkParent', 'updatedAt', 'deletedAt']; | ||
@@ -59,4 +58,19 @@ | ||
static getUpsertStatement( | ||
collectionName: string, | ||
document: any, | ||
option: { ignoreUndefinedProperties: boolean } = { ignoreUndefinedProperties: true }, | ||
) { | ||
if (!document) return Promise.resolve(); | ||
const keys = Object.keys(document).filter( | ||
(key) => typeof document[key] !== 'undefined' || !option.ignoreUndefinedProperties, | ||
); | ||
const values = keys.map((key) => ReplicationHelpers.safeValue(document[key])).join(); | ||
const conflictUpdate = keys.map((key) => `"${key}"=excluded."${key}"`).join(); | ||
return `INSERT INTO "${collectionName}" (${keys.map((key) => `"${key}"`).join()}) values (${values}) | ||
ON CONFLICT DO UPDATE SET ${conflictUpdate}`; | ||
} | ||
static safeValue(value: any) { | ||
if (value === null) { | ||
if (value === null || typeof value === 'undefined') { | ||
return 'NULL'; | ||
@@ -63,0 +77,0 @@ } else if (value.toISOString) { |
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
95891
1821