-
Notifications
You must be signed in to change notification settings - Fork 14
Feat/soft delete #166
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
Open
lesagi
wants to merge
22
commits into
release/0.2.0
Choose a base branch
from
feat/softDelete
base: release/0.2.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feat/soft delete #166
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
fb24c55
Add the option to set a custom_id for users with admin right only
lesagi 3b08252
Merge branch 'release/0.2.0' of https://github.com/mini-services/mini…
lesagi 67ef7b9
Merge branch 'release/0.2.0' of github.com:SnirShechter/miniurl into …
SnirShechter 8712965
Update src/services/storage/drivers/relational/index.ts
lesagi f3cca37
Update src/routes/url.ts
lesagi 10839e3
Update src/services/storage/drivers/relational/index.ts
lesagi a7f4980
Update src/services/storage/drivers/relational/index.ts
lesagi 5bf4b3c
Unauthorized Error on Custom ID
lesagi ff59372
Use postgres Error number to identify id duplicates
lesagi ceb52c3
Check if custom_id already exists before saving
lesagi 5119db4
Merge remote-tracking branch 'origin/release/0.2.0' into release/0.2.0
lesagi f5d58ab
Add 'deletedAt' property to StoredUrl type
lesagi eb1c327
Replace hard delete with soft delete for InMemory Driver
lesagi a1f5d2f
Store ISOString in the 'deletedAt' field instead of UTCString
lesagi 274e13e
Prevent unauthorized user to fetch soft deleted url
lesagi 2f0c51d
Make the hard delete optional along with soft delete
lesagi e3707fa
Add the option for soft delete for Relational DB
lesagi 1fafdc8
Relational: Prevent unauthorized user to fetch soft deleted url
lesagi 0039269
Update Soft Delete
lesagi 0350ad8
Remove unecessary import
lesagi d9195ff
Refactor softDelete function location in InMemoryStorage
lesagi df3172f
Add a call to storage.softDelete in Overdue
lesagi 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -69,15 +69,15 @@ export class RelationalStorage implements StorageDriver { | |
| url = new (class RelationalUrlStorage { | ||
| constructor(public storage: RelationalStorage) { } | ||
|
|
||
| public async get(id: string, options = { withInfo: false }): Promise<StoredUrl | UrlWithInformation> { | ||
| public async get(id: string, options = { withInfo: false, includeDeleted: true }): Promise<StoredUrl | UrlWithInformation> { | ||
| let storedUrl, urlInfo | ||
| storedUrl = await this.storage.db | ||
| .table<StoredUrl & { serial?: number }>('urls') | ||
| .select('*') | ||
| .where('id', id) | ||
| .first() | ||
| if (storedUrl?.deletedAt) throw new NotFoundError(); | ||
| if (!options.withInfo) { | ||
| storedUrl = await this.storage.db | ||
| .table<StoredUrl & { serial?: number }>('urls') | ||
| .select('*') | ||
| .where('id', id) | ||
| .first() | ||
|
|
||
| delete storedUrl?.serial | ||
| } else { | ||
| urlInfo = await this.storage.db | ||
|
|
@@ -93,14 +93,19 @@ export class RelationalStorage implements StorageDriver { | |
|
|
||
| //All Info associated with that Urls also get deleted, automatically. | ||
| //https://stackoverflow.com/questions/53859207/deleting-data-from-associated-tables-using-knex-js | ||
| public async delete(id: string): Promise<void> { | ||
| await this.storage.db.table<StoredUrl>('urls').where('id', id).delete() | ||
| return | ||
| public async delete(id: string, options: { softDelete: true }): Promise<void> { | ||
| if (!await this.storage.db.table<StoredUrl>('urls').where('id', id)) throw new NotFoundError(); | ||
|
|
||
| if (options.softDelete) { | ||
| await this.storage.db.table<StoredUrl>('urls').where('id', id).delete() | ||
| } else { | ||
| await this.storage.db.table<StoredUrl>('urls').update({ deletedAt: new Date().toISOString() }).where('id', id) | ||
| } | ||
| } | ||
|
|
||
| public async deleteOverdue(timespanMs: number): Promise<number> { | ||
|
Member
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. What about deleteOverdue? We need it to soft-delete too.
Contributor
Author
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. added a deleteOverdue + migration to support the soft delete in postgres |
||
| const deleteBefore = new Date(new Date().getTime() - timespanMs) | ||
| return await this.storage.db.table<StoredUrl>('urls').where('updatedAt', '<', deleteBefore).delete() | ||
| return await this.storage.db.table<StoredUrl>('urls').update({ deletedAt: new Date().toISOString() }).where('updatedAt', '<', deleteBefore); | ||
| } | ||
|
|
||
| public async edit(id: string, url: string): Promise<StoredUrl> { | ||
|
|
||
17 changes: 17 additions & 0 deletions
17
src/services/storage/drivers/relational/migrations/20210401143602_softDelete.ts
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import * as Knex from "knex"; | ||
|
|
||
|
|
||
| export async function up(knex: Knex): Promise<void> { | ||
| return await knex.schema.table('urls', table => { | ||
| table.string('deletedAt'); | ||
| }) | ||
|
|
||
| } | ||
|
|
||
|
|
||
| export async function down(knex: Knex): Promise<void> { | ||
| return knex.schema.table('urls', table => { | ||
| table.dropColumn('deletedAt'); | ||
| }) | ||
| } | ||
|
|
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
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.