-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
82 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { database } from '../common/database'; | ||
import type { BaseModel } from '.'; | ||
|
||
export const name = 'WebSocket Payload'; | ||
|
||
export const type = 'WebSocketPayload'; | ||
|
||
export const prefix = 'ws-msg'; | ||
|
||
export const canDuplicate = true; | ||
|
||
// @TODO: enable this at some point | ||
export const canSync = false; | ||
|
||
export interface BaseWebSocketPayload { | ||
value: string; | ||
mode: 'application/json' | 'text/plain'; | ||
} | ||
|
||
export type WebSocketPayload = BaseModel & BaseWebSocketPayload & { type: typeof type }; | ||
|
||
export const isWebSocketPayloadId = (id: string | null) => ( | ||
id?.startsWith(`${prefix}_`) | ||
); | ||
|
||
export const init = (): BaseWebSocketPayload => ({ | ||
value: '', | ||
mode: 'application/json', | ||
}); | ||
|
||
export const migrate = (doc: WebSocketPayload) => doc; | ||
|
||
export const create = (patch: Partial<WebSocketPayload> = {}) => { | ||
if (!patch.parentId) { | ||
throw new Error(`New WebSocketPayload missing \`parentId\`: ${JSON.stringify(patch)}`); | ||
} | ||
|
||
return database.docCreate<WebSocketPayload>(type, patch); | ||
}; | ||
|
||
export const remove = (obj: WebSocketPayload) => database.remove(obj); | ||
|
||
export const update = ( | ||
obj: WebSocketPayload, | ||
patch: Partial<WebSocketPayload> = {} | ||
) => database.docUpdate(obj, patch); | ||
|
||
export async function duplicate(request: WebSocketPayload, patch: Partial<WebSocketPayload> = {}) { | ||
// Only set name and "(Copy)" if the patch does | ||
// not define it and the request itself has a name. | ||
// Otherwise leave it blank so the request URL can | ||
// fill it in automatically. | ||
if (!patch.name && request.name) { | ||
patch.name = `${request.name} (Copy)`; | ||
} | ||
|
||
return database.duplicate<WebSocketPayload>(request, { | ||
name, | ||
...patch, | ||
}); | ||
} | ||
|
||
export const getById = (_id: string) => database.getWhere<WebSocketPayload>(type, { _id }); | ||
export const getByParentId = (parentId: string) => database.getWhere<WebSocketPayload>(type, { parentId }); | ||
|
||
export const all = () => database.all<WebSocketPayload>(type); |
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