-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dependabot-npm_and_yarn-typescript-5.4.2
- Loading branch information
Showing
21 changed files
with
843 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import Realm, { ObjectSchema } from 'realm'; | ||
|
||
export class DriveFileRealmSchema extends Realm.Object<DriveFileRealmSchema> { | ||
id!: number; | ||
name!: string; | ||
type?: string; | ||
uuid!: string; | ||
fileId!: string; | ||
folder_id!: number; | ||
folder_uuid!: string; | ||
bucket!: string; | ||
relative_path!: string; | ||
created_at!: Date; | ||
updated_at!: Date; | ||
size!: number; | ||
status!: 'EXISTS' | 'REMOVED' | 'TRASHED'; | ||
static readonly schema: ObjectSchema = { | ||
name: 'DriveFile', | ||
properties: { | ||
id: 'int', | ||
name: 'string', | ||
type: 'string?', | ||
uuid: { type: 'string', indexed: true }, | ||
file_id: 'string', | ||
folder_id: 'int', | ||
folder_uuid: 'string', | ||
bucket: 'string', | ||
relative_path: { type: 'string', indexed: true }, | ||
created_at: 'date', | ||
updated_at: 'date', | ||
size: 'int', | ||
status: 'string', | ||
}, | ||
primaryKey: 'id', | ||
}; | ||
} | ||
|
||
export class DriveFilesRealm { | ||
constructor(private realm: Realm) {} | ||
|
||
async findByRelativePath(relativePath: string): Promise<DriveFileRealmSchema | null> { | ||
const object = this.realm | ||
.objects<DriveFileRealmSchema>('DriveFile') | ||
.filtered('relative_path = $0', relativePath) | ||
.find((file) => file.relative_path === relativePath); | ||
|
||
return object ?? null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import Realm, { ObjectSchema } from 'realm'; | ||
import { DriveFolderItem } from '../../types/drive.types'; | ||
|
||
export class DriveFolderRealmSchema extends Realm.Object<DriveFolderRealmSchema> { | ||
id!: number; | ||
name!: string; | ||
uuid!: string; | ||
relative_path!: string; | ||
parent_id?: number; | ||
created_at!: Date; | ||
updated_at!: Date; | ||
status!: 'EXISTS' | 'REMOVED' | 'TRASHED'; | ||
static readonly schema: ObjectSchema = { | ||
name: 'DriveFolder', | ||
properties: { | ||
id: 'int', | ||
name: 'string', | ||
uuid: { type: 'string', indexed: true }, | ||
relative_path: { type: 'string', indexed: true }, | ||
parent_id: { type: 'int', indexed: true, default: -1 }, | ||
created_at: 'date', | ||
updated_at: 'date', | ||
status: 'string', | ||
}, | ||
primaryKey: 'id', | ||
}; | ||
} | ||
|
||
export class DriveFoldersRealm { | ||
constructor(private realm: Realm) {} | ||
|
||
async findByRelativePath(relativePath: string): Promise<DriveFolderRealmSchema | null> { | ||
const object = this.realm | ||
.objects<DriveFolderRealmSchema>('DriveFolder') | ||
.filtered('relative_path = $0', relativePath) | ||
.find((folder) => folder.relative_path === relativePath); | ||
|
||
return object ?? null; | ||
} | ||
|
||
async findByParentId(parentId: number | null): Promise<DriveFolderRealmSchema | null> { | ||
// -1 is root as we cannot index null fields | ||
const parentFolder = this.realm.objectForPrimaryKey<DriveFolderRealmSchema>('DriveFolder', parentId ?? -1); | ||
|
||
return parentFolder; | ||
} | ||
|
||
async createOrReplace(driveFolder: DriveFolderItem, relativePath: string) { | ||
const existingObject = this.realm.objectForPrimaryKey<DriveFolderRealmSchema>('DriveFolder', driveFolder.id); | ||
|
||
this.realm.write(() => { | ||
if (existingObject) { | ||
this.realm.delete(existingObject); | ||
} | ||
|
||
this.realm.create<DriveFolderRealmSchema>('DriveFolder', { | ||
id: driveFolder.id, | ||
name: driveFolder.name, | ||
uuid: driveFolder.uuid, | ||
parent_id: driveFolder.parentId ?? -1, | ||
created_at: driveFolder.createdAt, | ||
updated_at: driveFolder.updatedAt, | ||
status: 'EXISTS', | ||
relative_path: relativePath, | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import path from 'path'; | ||
import { DriveFolderItem } from '../../types/drive.types'; | ||
import { DriveFileRealmSchema, DriveFilesRealm } from './drive-files.realm'; | ||
import { DriveFolderRealmSchema, DriveFoldersRealm } from './drive-folders.realm'; | ||
|
||
export class DriveRealmManager { | ||
constructor( | ||
private driveFilesRealm: DriveFilesRealm, | ||
private driveFoldersRealm: DriveFoldersRealm, | ||
) {} | ||
|
||
async findByRelativePath(relativePath: string): Promise<DriveFolderRealmSchema | DriveFileRealmSchema | null> { | ||
const driveFile = await this.driveFilesRealm.findByRelativePath(relativePath); | ||
|
||
if (driveFile) return driveFile; | ||
|
||
const driveFolder = await this.driveFoldersRealm.findByRelativePath(relativePath); | ||
|
||
if (driveFolder) return driveFolder; | ||
|
||
return null; | ||
} | ||
|
||
async createFolder(driveFolder: DriveFolderItem) { | ||
const relativePath = await this.buildRelativePathForFolder(driveFolder.name, driveFolder.parentId ?? null); | ||
|
||
return this.driveFoldersRealm.createOrReplace(driveFolder, relativePath); | ||
} | ||
|
||
async buildRelativePathForFolder(folderName: string, parentId: number | null): Promise<string> { | ||
const parentFolder = await this.driveFoldersRealm.findByParentId(parentId); | ||
|
||
if (!parentFolder) { | ||
return path.join('/', folderName, '/'); | ||
} | ||
|
||
const parentPath = await this.buildRelativePathForFolder(parentFolder.name, parentFolder.parent_id ?? null); | ||
|
||
return path.join(parentPath, folderName, '/'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.