2022-09-04 02:05:25 +00:00
|
|
|
import {
|
|
|
|
enablePromise,
|
|
|
|
openDatabase,
|
|
|
|
SQLiteDatabase,
|
|
|
|
} from 'react-native-sqlite-storage';
|
2022-07-03 01:50:01 +00:00
|
|
|
|
|
|
|
enablePromise(true);
|
|
|
|
|
2022-08-30 11:43:39 +00:00
|
|
|
const createSets = `
|
2022-07-03 01:50:01 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS sets (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
name TEXT NOT NULL,
|
|
|
|
reps INTEGER NOT NULL,
|
|
|
|
weight INTEGER NOT NULL,
|
|
|
|
created TEXT NOT NULL,
|
|
|
|
unit TEXT DEFAULT 'kg'
|
|
|
|
);
|
|
|
|
`;
|
|
|
|
|
2022-08-30 11:43:39 +00:00
|
|
|
const createPlans = `
|
2022-07-06 05:40:53 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS plans (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
days TEXT NOT NULL,
|
|
|
|
workouts TEXT NOT NULL
|
|
|
|
);
|
|
|
|
`;
|
2022-08-24 00:01:39 +00:00
|
|
|
|
2022-08-30 11:43:39 +00:00
|
|
|
const createSettings = `
|
2022-08-24 00:01:39 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS settings (
|
|
|
|
minutes INTEGER NOT NULL DEFAULT 3,
|
|
|
|
seconds INTEGER NOT NULL DEFAULT 30,
|
|
|
|
alarm BOOLEAN NOT NULL DEFAULT false,
|
|
|
|
vibrate BOOLEAN NOT NULL DEFAULT true,
|
|
|
|
predict BOOLEAN NOT NULL DEFAULT true,
|
|
|
|
sets INTEGER NOT NULL DEFAULT 3
|
|
|
|
);
|
|
|
|
`;
|
2022-08-25 08:41:01 +00:00
|
|
|
|
2022-08-30 11:43:39 +00:00
|
|
|
const addSound = `
|
2022-08-25 08:41:01 +00:00
|
|
|
ALTER TABLE settings ADD COLUMN sound TEXT NULL;
|
|
|
|
`;
|
2022-08-26 03:10:28 +00:00
|
|
|
|
2022-08-30 11:43:39 +00:00
|
|
|
const createWorkouts = `
|
2022-08-26 03:10:28 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS workouts(
|
|
|
|
name TEXT PRIMARY KEY,
|
|
|
|
sets INTEGER DEFAULT 3
|
|
|
|
);
|
|
|
|
`;
|
|
|
|
|
2022-08-30 11:43:39 +00:00
|
|
|
const addHidden = `
|
2022-08-26 03:10:28 +00:00
|
|
|
ALTER TABLE sets ADD COLUMN hidden DEFAULT false;
|
|
|
|
`;
|
2022-08-27 06:08:23 +00:00
|
|
|
|
2022-08-30 11:43:39 +00:00
|
|
|
const addNotify = `
|
2022-08-27 06:08:23 +00:00
|
|
|
ALTER TABLE settings ADD COLUMN notify DEFAULT false;
|
|
|
|
`;
|
2022-08-28 08:55:12 +00:00
|
|
|
|
2022-08-30 11:43:39 +00:00
|
|
|
const addImage = `
|
2022-08-28 08:55:12 +00:00
|
|
|
ALTER TABLE sets ADD COLUMN image TEXT NULL;
|
|
|
|
`;
|
2022-08-30 11:43:39 +00:00
|
|
|
|
|
|
|
const addImages = `
|
|
|
|
ALTER TABLE settings ADD COLUMN images BOOLEAN DEFAULT false;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const selectSettings = `
|
|
|
|
SELECT * FROM settings LIMIT 1
|
|
|
|
`;
|
|
|
|
|
|
|
|
const insertSettings = `
|
2022-09-01 01:06:49 +00:00
|
|
|
INSERT INTO settings(minutes) VALUES(3);
|
2022-08-30 11:43:39 +00:00
|
|
|
`;
|
|
|
|
|
2022-09-18 06:08:09 +00:00
|
|
|
const addSteps = `
|
|
|
|
ALTER TABLE workouts ADD COLUMN steps TEXT NULL;
|
|
|
|
`;
|
|
|
|
|
2022-09-18 06:30:06 +00:00
|
|
|
const insertWorkouts = `
|
2022-09-18 08:30:30 +00:00
|
|
|
INSERT OR IGNORE INTO workouts (name) SELECT DISTINCT name FROM sets;
|
2022-09-18 06:30:06 +00:00
|
|
|
`;
|
|
|
|
|
2022-09-04 02:05:25 +00:00
|
|
|
export let db: SQLiteDatabase;
|
|
|
|
|
|
|
|
export const migrations = async () => {
|
|
|
|
db = await openDatabase({name: 'massive.db'});
|
2022-08-30 11:43:39 +00:00
|
|
|
await db.executeSql(createPlans);
|
|
|
|
await db.executeSql(createSets);
|
|
|
|
await db.executeSql(createSettings);
|
|
|
|
await db.executeSql(createWorkouts);
|
|
|
|
await db.executeSql(addSound).catch(() => null);
|
|
|
|
await db.executeSql(addHidden).catch(() => null);
|
|
|
|
await db.executeSql(addNotify).catch(() => null);
|
|
|
|
await db.executeSql(addImage).catch(() => null);
|
|
|
|
await db.executeSql(addImages).catch(() => null);
|
2022-09-18 06:08:09 +00:00
|
|
|
await db.executeSql(addSteps).catch(() => null);
|
2022-09-18 06:30:06 +00:00
|
|
|
await db.executeSql(insertWorkouts).catch(() => null);
|
2022-08-30 11:43:39 +00:00
|
|
|
const [result] = await db.executeSql(selectSettings);
|
|
|
|
if (result.rows.length === 0) await db.executeSql(insertSettings);
|
2022-09-04 03:28:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export interface PageParams {
|
|
|
|
search: string;
|
|
|
|
limit: number;
|
|
|
|
offset: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const defaultSet = {
|
|
|
|
name: '',
|
|
|
|
id: 0,
|
|
|
|
reps: 10,
|
|
|
|
weight: 20,
|
|
|
|
unit: 'kg',
|
|
|
|
};
|