-
Notifications
You must be signed in to change notification settings - Fork 247
feat(save-user-data): create a user data abstract class COMPASS-9543 #7103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3f336ec
feat(save-user-data): create a user data abstract class
myang1220 ad5e416
Merge branch 'main' into user-data-interface
myang1220 78026a3
fix: rm defaults and add semaphore to FileUserData
myang1220 b24d748
merge remote branch back into local
myang1220 8413d43
Merge branch 'main' into user-data-interface
myang1220 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| export type { Stats, ReadAllResult, ReadAllWithStatsResult } from './user-data'; | ||
| export { UserData } from './user-data'; | ||
| export { IUserData, FileUserData } from './user-data'; | ||
| export { z } from 'zod'; |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -12,14 +12,19 @@ type SerializeContent<I> = (content: I) => string; | |
| type DeserializeContent = (content: string) => unknown; | ||
| type GetFileName = (id: string) => string; | ||
|
|
||
| export type UserDataOptions<Input> = { | ||
| export type FileUserDataOptions<Input> = { | ||
| subdir: string; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The naming on this one is unfortunate, but I think it's also relevant for the shared userdata interface |
||
| basePath?: string; | ||
| serialize?: SerializeContent<Input>; | ||
| deserialize?: DeserializeContent; | ||
| getFileName?: GetFileName; | ||
myang1220 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| export type AtlasUserDataOptions<Input> = { | ||
| serialize?: SerializeContent<Input>; | ||
| deserialize?: DeserializeContent; | ||
| }; | ||
|
|
||
| type ReadOptions = { | ||
| ignoreErrors: boolean; | ||
| }; | ||
|
|
@@ -63,28 +68,54 @@ export interface ReadAllWithStatsResult<T extends z.Schema> { | |
| errors: Error[]; | ||
| } | ||
|
|
||
| export class UserData<T extends z.Schema> { | ||
| export abstract class IUserData<T extends z.Schema> { | ||
| protected readonly validator: T; | ||
| protected readonly serialize: SerializeContent<z.input<T>>; | ||
| protected readonly deserialize: DeserializeContent; | ||
| protected static readonly semaphore = new Semaphore(100); | ||
myang1220 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| constructor( | ||
| validator: T, | ||
| { | ||
| serialize = (content: z.input<T>) => JSON.stringify(content, null, 2), | ||
| deserialize = JSON.parse, | ||
| }: { | ||
| serialize?: SerializeContent<z.input<T>>; | ||
| deserialize?: DeserializeContent; | ||
| } = {} | ||
| ) { | ||
| this.validator = validator; | ||
| this.serialize = serialize; | ||
| this.deserialize = deserialize; | ||
| } | ||
|
|
||
| abstract write(id: string, content: z.input<T>): Promise<boolean>; | ||
| abstract delete(id: string): Promise<boolean>; | ||
| abstract readAll(options?: ReadOptions): Promise<ReadAllResult<T>>; | ||
| abstract updateAttributes( | ||
| id: string, | ||
| data: Partial<z.input<T>> | ||
| ): Promise<z.output<T>>; | ||
| } | ||
|
|
||
| export class FileUserData<T extends z.Schema> extends IUserData<T> { | ||
| private readonly subdir: string; | ||
| private readonly basePath?: string; | ||
| private readonly serialize: SerializeContent<z.input<T>>; | ||
| private readonly deserialize: DeserializeContent; | ||
| private readonly getFileName: GetFileName; | ||
| private readonly semaphore = new Semaphore(100); | ||
|
|
||
| constructor( | ||
| private readonly validator: T, | ||
| validator: T, | ||
| { | ||
| subdir, | ||
| basePath, | ||
| serialize = (content: z.input<T>) => JSON.stringify(content, null, 2), | ||
myang1220 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| deserialize = JSON.parse, | ||
| getFileName = (id) => `${id}.json`, | ||
| }: UserDataOptions<z.input<T>> | ||
| }: FileUserDataOptions<z.input<T>> | ||
| ) { | ||
| super(validator, { serialize, deserialize }); | ||
| this.subdir = subdir; | ||
| this.basePath = basePath; | ||
| this.deserialize = deserialize; | ||
| this.serialize = serialize; | ||
| this.getFileName = getFileName; | ||
| } | ||
|
|
||
|
|
@@ -126,7 +157,7 @@ export class UserData<T extends z.Schema> { | |
| let handle: fs.FileHandle | undefined = undefined; | ||
| let release: (() => void) | undefined = undefined; | ||
| try { | ||
| release = await this.semaphore.waitForRelease(); | ||
| release = await IUserData.semaphore.waitForRelease(); | ||
| handle = await fs.open(absolutePath, 'r'); | ||
| [stats, data] = await Promise.all([ | ||
| handle.stat(), | ||
|
|
@@ -290,4 +321,15 @@ export class UserData<T extends z.Schema> { | |
| ) { | ||
| return (await this.readOneWithStats(id, options))?.[0]; | ||
| } | ||
|
|
||
| async updateAttributes( | ||
| id: string, | ||
| data: Partial<z.input<T>> | ||
| ): Promise<z.output<T>> { | ||
| await this.write(id, { | ||
| ...((await this.readOne(id)) ?? {}), | ||
| ...data, | ||
| }); | ||
| return await this.readOne(id); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.