Skip to content

Commit

Permalink
feat: create database structure
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorJorgeFGA committed Aug 25, 2024
1 parent 71c6dbf commit abdd2b5
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/app/db/index.ts
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 7 additions & 0 deletions src/app/db/migrations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { schemaMigrations } from '@nozbe/watermelondb/Schema/migrations'

export default schemaMigrations({
migrations: [
// We'll add migration definitions here later
],
})
21 changes: 21 additions & 0 deletions src/app/db/schema.ts
Original file line number Diff line number Diff line change
@@ -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' }
]
})
]
})
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "expo/tsconfig.base",
"compilerOptions": {
"jsx": "react",
"strict": true
"strict": true,
"experimentalDecorators": true
}
}

0 comments on commit abdd2b5

Please sign in to comment.