Skip to content

Commit

Permalink
Extend Table instead of using createAttachmentTable function
Browse files Browse the repository at this point in the history
  • Loading branch information
Manrich121 committed Oct 31, 2023
1 parent 186bf4f commit 025088a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
4 changes: 0 additions & 4 deletions packages/powersync-attachments/src/AbstractAttachmentQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
StorageAdapter,
AttachmentRecord,
AttachmentState,
createAttachmentTable,
} from "./definitions";

export interface AttachmentQueueOptions {
Expand Down Expand Up @@ -46,9 +45,6 @@ export abstract class AbstractAttachmentQueue<
this.uploading = false;
this.downloading = false;
this.initialSync = this.options.performInitialSync;

// Add Attachment table to schema tables
this.powersync.schema.tables.push(createAttachmentTable());
}

/**
Expand Down
40 changes: 27 additions & 13 deletions packages/powersync-attachments/src/definitions/Schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { Column, ColumnType, Table } from "@journeyapps/powersync-sdk-common";
import {
Column,
ColumnType,
Table,
TableOptions,
} from "@journeyapps/powersync-sdk-common";

export const ATTACHMENT_TABLE = "attachments";

Expand All @@ -20,16 +25,25 @@ export enum AttachmentState {
ARCHIVED = 4, // Attachment has been orphaned, i.e. the associated record has been deleted
}

export function createAttachmentTable(name: string = ATTACHMENT_TABLE) {
return Table.createLocalOnly({
name: name,
columns: [
new Column({ name: "filename", type: ColumnType.TEXT }),
new Column({ name: "local_uri", type: ColumnType.TEXT }),
new Column({ name: "timestamp", type: ColumnType.INTEGER }),
new Column({ name: "size", type: ColumnType.INTEGER }),
new Column({ name: "media_type", type: ColumnType.TEXT }),
new Column({ name: "state", type: ColumnType.INTEGER }), // Corresponds to AttachmentState
],
});
export interface AttachmentTableOptions extends Omit<TableOptions, "name"> {
name?: string;
additionalColumns?: Column[];
}

export class AttachmentTable extends Table {
constructor(options: AttachmentTableOptions) {
super({
...options,
name: options.name ?? ATTACHMENT_TABLE,
columns: [
new Column({ name: "filename", type: ColumnType.TEXT }),
new Column({ name: "local_uri", type: ColumnType.TEXT }),
new Column({ name: "timestamp", type: ColumnType.INTEGER }),
new Column({ name: "size", type: ColumnType.INTEGER }),
new Column({ name: "media_type", type: ColumnType.TEXT }),
new Column({ name: "state", type: ColumnType.INTEGER }), // Corresponds to AttachmentState
...(options.additionalColumns ?? []),
],
});
}
}

0 comments on commit 025088a

Please sign in to comment.