Skip to content

Commit

Permalink
chore: clean up attachments readme and update demos (#453)
Browse files Browse the repository at this point in the history
  • Loading branch information
DominicGBauer authored Dec 23, 2024
1 parent a9d1954 commit dd4474d
Show file tree
Hide file tree
Showing 5 changed files with 671 additions and 209 deletions.
82 changes: 32 additions & 50 deletions demos/react-native-supabase-todolist/library/powersync/AppSchema.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,40 @@
import { AttachmentTable } from '@powersync/attachments';
import { Column, ColumnType, Index, IndexedColumn, Schema, Table } from '@powersync/react-native';
import { column, Schema, Table } from '@powersync/react-native';

export const TODO_TABLE = 'todos';
export const LIST_TABLE = 'lists';
export const TODO_TABLE = 'todos';

export interface ListRecord {
id: string;
name: string;
created_at: string;
owner_id?: string;
}
const todos = new Table(
{
list_id: column.text,
photo_id: column.text,
created_at: column.text,
completed_at: column.text,
description: column.text,
created_by: column.text,
completed_by: column.text,
completed: column.integer
},
{ indexes: { list: ['list_id'] } }
);

export interface TodoRecord {
id: string;
created_at: string;
completed: boolean;
description: string;
completed_at?: string;
const lists = new Table({
created_at: column.text,
name: column.text,
owner_id: column.text
});

created_by: string;
completed_by?: string;
list_id: string;
export const AppSchema = new Schema({
todos,
lists,
attachments: new AttachmentTable({
name: 'attachments',
}),
});

photo_id?: string; // This is the attachment id, 1:1 relationship with `id` in AttachmentTable
}
export type Database = (typeof AppSchema)['types'];
export type TodoRecord = Database['todos'];
// OR:
// export type Todo = RowType<typeof todos>;

export const AppSchema = new Schema([
new Table({
name: 'todos',
columns: [
new Column({ name: 'list_id', type: ColumnType.TEXT }),
new Column({ name: 'photo_id', type: ColumnType.TEXT }),
new Column({ name: 'created_at', type: ColumnType.TEXT }),
new Column({ name: 'completed_at', type: ColumnType.TEXT }),
new Column({ name: 'description', type: ColumnType.TEXT }),
new Column({ name: 'completed', type: ColumnType.INTEGER }),
new Column({ name: 'created_by', type: ColumnType.TEXT }),
new Column({ name: 'completed_by', type: ColumnType.TEXT })
],
indexes: [
new Index({
name: 'list',
columns: [new IndexedColumn({ name: 'list_id' })]
})
]
}),
new Table({
name: 'lists',
columns: [
new Column({ name: 'created_at', type: ColumnType.TEXT }),
new Column({ name: 'name', type: ColumnType.TEXT }),
new Column({ name: 'owner_id', type: ColumnType.TEXT })
]
}),
// Add Attachment table
new AttachmentTable()
]);
export type ListRecord = Database['lists'];
Original file line number Diff line number Diff line change
@@ -1,58 +1,40 @@
import { AttachmentTable } from '@powersync/attachments';
import { Column, ColumnType, Index, IndexedColumn, Schema, Table } from '@powersync/common';
import { column, Schema, Table } from '@powersync/react-native';

export const TODO_TABLE = 'todos';
export const LIST_TABLE = 'lists';
export const TODO_TABLE = 'todos';

export interface ListRecord {
id: string;
name: string;
created_at: string;
owner_id?: string;
}
const todos = new Table(
{
list_id: column.text,
photo_id: column.text,
created_at: column.text,
completed_at: column.text,
description: column.text,
created_by: column.text,
completed_by: column.text,
completed: column.integer
},
{ indexes: { list: ['list_id'] } }
);

export interface TodoRecord {
id: string;
created_at: string;
completed: boolean;
description: string;
completed_at?: string;
const lists = new Table({
created_at: column.text,
name: column.text,
owner_id: column.text
});

created_by: string;
completed_by?: string;
list_id: string;
export const AppSchema = new Schema({
todos,
lists,
attachments: new AttachmentTable({
name: 'attachments',
}),
});

photo_id?: string; // This is the attachment id, 1:1 relationship with `id` in AttachmentTable
}
export type Database = (typeof AppSchema)['types'];
export type TodoRecord = Database['todos'];
// OR:
// export type Todo = RowType<typeof todos>;

export const AppSchema = new Schema([
new Table({
name: 'todos',
columns: [
new Column({ name: 'list_id', type: ColumnType.TEXT }),
new Column({ name: 'photo_id', type: ColumnType.TEXT }),
new Column({ name: 'created_at', type: ColumnType.TEXT }),
new Column({ name: 'completed_at', type: ColumnType.TEXT }),
new Column({ name: 'description', type: ColumnType.TEXT }),
new Column({ name: 'completed', type: ColumnType.INTEGER }),
new Column({ name: 'created_by', type: ColumnType.TEXT }),
new Column({ name: 'completed_by', type: ColumnType.TEXT })
],
indexes: [
new Index({
name: 'list',
columns: [new IndexedColumn({ name: 'list_id' })]
})
]
}),
new Table({
name: 'lists',
columns: [
new Column({ name: 'created_at', type: ColumnType.TEXT }),
new Column({ name: 'name', type: ColumnType.TEXT }),
new Column({ name: 'owner_id', type: ColumnType.TEXT })
]
}),
// Add Attachment table
new AttachmentTable()
]);
export type ListRecord = Database['lists'];
15 changes: 10 additions & 5 deletions packages/attachments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@ export class AttachmentQueue extends AbstractAttachmentQueue {
```javascript
import { AttachmentTable } from '@powersync/attachments';

const attachmentTable = new AttachmentTable();

const AppSchema = new Schema({
// ... other tables
attachmentTable
attachments: new AttachmentTable({
name: 'attachments',
}),
});
```
Expand All @@ -123,7 +123,7 @@ The default columns in `AttachmentTable`:
| Column Name | Type | Description |
| ------------ | --------- | ----------------------------------------------------------------- |
| `id` | `TEXT` | The ID of the attachment record |
| `filename` | `TEXT` | The filename of the attachment |
| `filename` | `TEXT` | The filename of the attachment |
| `media_type` | `TEXT` | The media type of the attachment |
| `state` | `INTEGER` | The state of the attachment, one of `AttachmentState` enum values |
| `timestamp` | `INTEGER` | The timestamp of last update to the attachment record |
Expand All @@ -136,7 +136,12 @@ The default columns in `AttachmentTable`:
```javascript
this.storage = this.supabaseConnector.storage;
this.powersync = factory.getInstance();
this.powersync = new PowerSyncDatabase({
schema: AppSchema,
database: {
dbFilename: 'sqlite.db'
}
});

this.attachmentQueue = new AttachmentQueue({
powersync: this.powersync,
Expand Down
10 changes: 5 additions & 5 deletions packages/attachments/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
},
"devDependencies": {
"@types/node": "^20.17.6",
"@vitest/browser": "^2.1.4",
"@vitest/browser": "^2.1.8",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"typescript": "^5.6.3",
"vite": "^5.4.10",
"typescript": "^5.7.2",
"vite": "^5.4.11",
"vite-plugin-top-level-await": "^1.4.4",
"vitest": "^2.1.4",
"webdriverio": "^9.2.8"
"vitest": "^2.1.8",
"webdriverio": "^9.4.5"
}
}
Loading

0 comments on commit dd4474d

Please sign in to comment.