|
| 1 | +import { Kysely, sql } from 'kysely'; |
| 2 | +import { ExpoDialect } from 'kysely-expo'; |
| 3 | + |
| 4 | +export const LOCAL_STORAGE_DB_NAME = 'app-database'; |
| 5 | + |
| 6 | +export interface Table { |
| 7 | + parent_ulid: string; |
| 8 | + ulid: string; |
| 9 | + data: string; |
| 10 | + timestamp: number; |
| 11 | + last_synced_at: number | null; |
| 12 | + is_dirty: number; |
| 13 | + is_deleted: number; |
| 14 | +} |
| 15 | + |
| 16 | +export interface Database { |
| 17 | + exercises: Table; |
| 18 | + activities: Table; |
| 19 | + activity_exercises: Table; |
| 20 | + activity_segments: Table; |
| 21 | + programmed_workouts: Table; |
| 22 | + user_feed: Table; |
| 23 | + workout_routines: Table; |
| 24 | + training_programs: Table; |
| 25 | +} |
| 26 | + |
| 27 | +const tables: Array<keyof Database> = [ |
| 28 | + 'exercises', |
| 29 | + 'activities', |
| 30 | + 'activity_exercises', |
| 31 | + 'activity_segments', |
| 32 | + 'programmed_workouts', |
| 33 | + 'user_feed', |
| 34 | + 'workout_routines', |
| 35 | + 'training_programs', |
| 36 | +]; |
| 37 | + |
| 38 | +const dialect = new ExpoDialect({ |
| 39 | + database: LOCAL_STORAGE_DB_NAME, |
| 40 | + // debug: __DEV__, |
| 41 | + debug: false, |
| 42 | + onError: (error) => { |
| 43 | + console.log(`SQLite Error: ${error}`); |
| 44 | + }, |
| 45 | +}); |
| 46 | + |
| 47 | +const db = new Kysely<Database>({ |
| 48 | + dialect, |
| 49 | +}); |
| 50 | + |
| 51 | +tables.forEach(async (table) => { |
| 52 | + await db.schema |
| 53 | + .createTable(table) |
| 54 | + .ifNotExists() |
| 55 | + .addColumn('parent_ulid', 'text') |
| 56 | + .addColumn('ulid', 'text', (col) => col.primaryKey()) |
| 57 | + .addColumn('data', 'text') |
| 58 | + .addColumn('timestamp', 'integer', (col) => |
| 59 | + col.defaultTo(sql`CURRENT_TIMESTAMP`).notNull() |
| 60 | + ) |
| 61 | + .addColumn('last_synced_at', 'integer') |
| 62 | + .addColumn('is_dirty', 'integer', (col) => col.notNull()) |
| 63 | + .addColumn('is_deleted', 'integer', (col) => col.defaultTo(false).notNull()) |
| 64 | + .execute(); |
| 65 | + await db.schema |
| 66 | + .createIndex(`${table}_timestamp`) |
| 67 | + .ifNotExists() |
| 68 | + .on(table) |
| 69 | + .column('timestamp') |
| 70 | + .execute(); |
| 71 | + await db.schema |
| 72 | + .createIndex(`${table}_parent_ulid`) |
| 73 | + .ifNotExists() |
| 74 | + .on(table) |
| 75 | + .column('parent_ulid') |
| 76 | + .execute(); |
| 77 | + |
| 78 | + try { |
| 79 | + await db.schema.alterTable(table).addColumn('parent_ulid', 'text').execute(); |
| 80 | + } catch (error) { |
| 81 | + // Silent the error. The column already exists. |
| 82 | + } |
| 83 | +}); |
| 84 | + |
| 85 | +export interface UserTable { |
| 86 | + id: string; |
| 87 | + email: string; |
| 88 | + name: string; |
| 89 | + created_at: number; |
| 90 | + updated_at: number; |
| 91 | +} |
| 92 | + |
| 93 | +export interface PostTable { |
| 94 | + id: string; |
| 95 | + user_id: string; |
| 96 | + title: string; |
| 97 | + content: string; |
| 98 | + created_at: number; |
| 99 | + updated_at: number; |
| 100 | +} |
| 101 | + |
| 102 | +export interface CommentTable { |
| 103 | + id: string; |
| 104 | + post_id: string; |
| 105 | + user_id: string; |
| 106 | + content: string; |
| 107 | + created_at: number; |
| 108 | + updated_at: number; |
| 109 | +} |
| 110 | + |
| 111 | +export interface SampleDatabase { |
| 112 | + users: UserTable; |
| 113 | + posts: PostTable; |
| 114 | + comments: CommentTable; |
| 115 | +} |
| 116 | + |
| 117 | +export { db, sql }; |
0 commit comments