db: voeg deletedAt toe, vervang syncStatus logica door timestamp-based functies
This commit is contained in:
parent
cec6c54e61
commit
79c91eb537
1 changed files with 23 additions and 17 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
import * as SQLite from 'expo-sqlite'
|
import * as SQLite from 'expo-sqlite'
|
||||||
import { Measurement, SyncStatus } from '../types'
|
import { Measurement } from '../types'
|
||||||
|
|
||||||
let db: SQLite.SQLiteDatabase | null = null
|
let db: SQLite.SQLiteDatabase | null = null
|
||||||
|
|
||||||
|
|
@ -20,16 +20,17 @@ export async function initDatabase(): Promise<void> {
|
||||||
measuredAt TEXT NOT NULL,
|
measuredAt TEXT NOT NULL,
|
||||||
createdAt TEXT NOT NULL,
|
createdAt TEXT NOT NULL,
|
||||||
updatedAt TEXT NOT NULL,
|
updatedAt TEXT NOT NULL,
|
||||||
syncStatus TEXT NOT NULL DEFAULT 'local'
|
syncStatus TEXT NOT NULL DEFAULT 'local',
|
||||||
|
deletedAt TEXT
|
||||||
);
|
);
|
||||||
`)
|
`)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function insertMeasurement(m: Measurement): Promise<void> {
|
export async function insertMeasurement(m: Measurement): Promise<void> {
|
||||||
await db!.runAsync(
|
await db!.runAsync(
|
||||||
`INSERT INTO measurements (id, weight, bmi, bodyFatPercentage, fatMass, musclePercentage, muscleMass, visceralFat, bmr, measuredAt, createdAt, updatedAt, syncStatus)
|
`INSERT INTO measurements (id, weight, bmi, bodyFatPercentage, fatMass, musclePercentage, muscleMass, visceralFat, bmr, measuredAt, createdAt, updatedAt, syncStatus, deletedAt)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||||
[m.id, m.weight, m.bmi, m.bodyFatPercentage, m.fatMass, m.musclePercentage, m.muscleMass, m.visceralFat, m.bmr, m.measuredAt, m.createdAt, m.updatedAt, m.syncStatus]
|
[m.id, m.weight, m.bmi, m.bodyFatPercentage, m.fatMass, m.musclePercentage, m.muscleMass, m.visceralFat, m.bmr, m.measuredAt, m.createdAt, m.updatedAt, m.syncStatus, m.deletedAt ?? null]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -56,28 +57,33 @@ export async function getMeasurementByDate(date: string): Promise<Measurement |
|
||||||
return rows.length > 0 ? (rows[0] as Measurement) : null
|
return rows.length > 0 ? (rows[0] as Measurement) : null
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateMeasurement(id: string, syncStatus: SyncStatus): Promise<void> {
|
|
||||||
await db!.runAsync(
|
|
||||||
'UPDATE measurements SET syncStatus = ?, updatedAt = ? WHERE id = ?',
|
|
||||||
[syncStatus, new Date().toISOString(), id]
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function upsertMeasurement(m: Measurement): Promise<void> {
|
export async function upsertMeasurement(m: Measurement): Promise<void> {
|
||||||
await db!.runAsync(
|
await db!.runAsync(
|
||||||
`INSERT OR REPLACE INTO measurements (id, weight, bmi, bodyFatPercentage, fatMass, musclePercentage, muscleMass, visceralFat, bmr, measuredAt, createdAt, updatedAt, syncStatus)
|
`INSERT OR REPLACE INTO measurements (id, weight, bmi, bodyFatPercentage, fatMass, musclePercentage, muscleMass, visceralFat, bmr, measuredAt, createdAt, updatedAt, syncStatus, deletedAt)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
||||||
[m.id, m.weight, m.bmi, m.bodyFatPercentage, m.fatMass, m.musclePercentage, m.muscleMass, m.visceralFat, m.bmr, m.measuredAt, m.createdAt, m.updatedAt, m.syncStatus]
|
[m.id, m.weight, m.bmi, m.bodyFatPercentage, m.fatMass, m.musclePercentage, m.muscleMass, m.visceralFat, m.bmr, m.measuredAt, m.createdAt, m.updatedAt, m.syncStatus, m.deletedAt ?? null]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getUnsyncedMeasurements(): Promise<Measurement[]> {
|
|
||||||
const rows = await db!.getAllAsync<any>(
|
|
||||||
"SELECT * FROM measurements WHERE syncStatus = 'local' ORDER BY updatedAt ASC"
|
|
||||||
)
|
|
||||||
return rows as Measurement[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function deleteMeasurement(id: string): Promise<void> {
|
export async function deleteMeasurement(id: string): Promise<void> {
|
||||||
await db!.runAsync('DELETE FROM measurements WHERE id = ?', [id])
|
await db!.runAsync('DELETE FROM measurements WHERE id = ?', [id])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function getMeasurementsUpdatedSince(since: string): Promise<Measurement[]> {
|
||||||
|
const rows = await db!.getAllAsync<any>(
|
||||||
|
'SELECT * FROM measurements WHERE updatedAt > ? ORDER BY updatedAt ASC',
|
||||||
|
[since]
|
||||||
|
)
|
||||||
|
return rows as Measurement[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function setDeletedAt(id: string): Promise<void> {
|
||||||
|
await db!.runAsync(
|
||||||
|
'UPDATE measurements SET deletedAt = ? WHERE id = ?',
|
||||||
|
[new Date().toISOString(), id]
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function removeMeasurement(id: string): Promise<void> {
|
||||||
|
await db!.runAsync('DELETE FROM measurements WHERE id = ?', [id])
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue