From abdd2b5badffc54f07c8a4c1edc85f8c5ddb962b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20Gon=C3=A7alves?= Date: Sun, 25 Aug 2024 18:21:58 -0300 Subject: [PATCH] feat: create database structure --- src/app/db/index.ts | 35 +++++++++++++++++++++++++++++++++++ src/app/db/migrations.ts | 7 +++++++ src/app/db/schema.ts | 21 +++++++++++++++++++++ tsconfig.json | 3 ++- 4 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 src/app/db/index.ts create mode 100644 src/app/db/migrations.ts create mode 100644 src/app/db/schema.ts diff --git a/src/app/db/index.ts b/src/app/db/index.ts new file mode 100644 index 00000000..48e0935d --- /dev/null +++ b/src/app/db/index.ts @@ -0,0 +1,35 @@ +import { Platform } from 'react-native' +import { Database, tableSchema } from '@nozbe/watermelondb' +import SQLiteAdapter from '@nozbe/watermelondb/adapters/sqlite' + +import schema from './schema' +import migrations from './migrations' +import User from '../model/User' +// import Post from './model/Post' // ⬅️ You'll import your Models here + +// First, create the adapter to the underlying database: +const adapter = new SQLiteAdapter({ + schema, + // (You might want to comment it out for development purposes -- see Migrations documentation) + migrations, + // (optional database name or file system path) + // dbName: 'myapp', + // (recommended option, should work flawlessly out of the box on iOS. On Android, + // additional installation steps have to be taken - disable if you run into issues...) + jsi: true, /* Platform.OS === 'ios' */ + // (optional, but you should implement this method) + onSetUpError: error => { + // Database failed to load -- offer the user to reload the app or log out + } +}) + +// Then, make a Watermelon database from it! +const database = new Database({ + adapter, + modelClasses: [ + // Post, // ⬅️ You'll add Models to Watermelon here + User + ], +}); + +export default database; diff --git a/src/app/db/migrations.ts b/src/app/db/migrations.ts new file mode 100644 index 00000000..edd8b7bb --- /dev/null +++ b/src/app/db/migrations.ts @@ -0,0 +1,7 @@ +import { schemaMigrations } from '@nozbe/watermelondb/Schema/migrations' + +export default schemaMigrations({ + migrations: [ + // We'll add migration definitions here later + ], +}) \ No newline at end of file diff --git a/src/app/db/schema.ts b/src/app/db/schema.ts new file mode 100644 index 00000000..3b702c61 --- /dev/null +++ b/src/app/db/schema.ts @@ -0,0 +1,21 @@ +import { appSchema, tableSchema } from '@nozbe/watermelondb' + +export default appSchema({ + version: 1, + tables: [ + // We'll add tableSchemas here later + tableSchema({ + name: 'users', + columns: [ + { name: 'external_id', type: 'string' }, + { name: 'name', type: 'string' }, + { name: 'email', type: 'string' }, + { name: 'photo', type: 'string' }, + { name: 'admin', type: 'boolean'}, + { name: 'password', type: 'string' }, + { name: 'created_at', type: 'number' }, + { name: 'updated_at', type: 'number' } + ] + }) + ] +}) \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 7886cc19..631db683 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,7 @@ "extends": "expo/tsconfig.base", "compilerOptions": { "jsx": "react", - "strict": true + "strict": true, + "experimentalDecorators": true } }