-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(db): add instance access cache layer over insert operations
- Loading branch information
Showing
12 changed files
with
191 additions
and
32 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 |
---|---|---|
@@ -1,13 +1,9 @@ | ||
import { DocumentNotFoundError } from "../../Errors"; | ||
import { Storage } from "../../Storage"; | ||
import { Remove } from "../Operators"; | ||
import { RemoveOneException } from "./Exceptions"; | ||
import { RemoveOneResult } from "./Result"; | ||
|
||
export async function remove(storage: Storage, operator: Remove): Promise<RemoveOneResult | RemoveOneException> { | ||
if ((await storage.hasDocument(operator.id)) === false) { | ||
return new RemoveOneException(new DocumentNotFoundError({ id: operator.id })); | ||
} | ||
storage.commit("remove", { id: operator.id }); | ||
return new RemoveOneResult(); | ||
} |
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,93 @@ | ||
import { Controller, ViewController } from "@valkyr/react"; | ||
|
||
import { Test } from "./test.model"; | ||
|
||
const mock = [ | ||
{ | ||
type: "insertOne", | ||
data: { | ||
id: "xyz", | ||
foo: "bar" | ||
} | ||
}, | ||
{ | ||
type: "updateOne", | ||
data: { | ||
id: "xyz", | ||
foo: "bar1" | ||
} | ||
}, | ||
{ | ||
type: "updateOne", | ||
data: { | ||
id: "xyz", | ||
foo: "bar2" | ||
} | ||
}, | ||
{ | ||
type: "updateOne", | ||
data: { | ||
id: "xyz", | ||
foo: "bar3" | ||
} | ||
}, | ||
{ | ||
type: "updateOne", | ||
data: { | ||
id: "xyz", | ||
foo: "bar4" | ||
} | ||
}, | ||
{ | ||
type: "updateOne", | ||
data: { | ||
id: "xyz", | ||
foo: "bar5" | ||
} | ||
}, | ||
{ | ||
type: "updateOne", | ||
data: { | ||
id: "xyz", | ||
foo: "bar6" | ||
} | ||
}, | ||
{ | ||
type: "updateOne", | ||
data: { | ||
id: "xyz", | ||
foo: "bar7" | ||
} | ||
} | ||
]; | ||
|
||
class DatabaseController extends Controller<State> { | ||
async onInit() { | ||
return { | ||
tests: await this.query(Test, {}, "tests") | ||
}; | ||
} | ||
|
||
async run() { | ||
for (const { type, data } of mock) { | ||
if (type === "insertOne") { | ||
await Test.insertOne(data); | ||
} else { | ||
await Test.updateOne( | ||
{ id: data.id }, | ||
{ | ||
$set: { | ||
foo: data.foo | ||
} | ||
} | ||
); | ||
} | ||
} | ||
} | ||
} | ||
|
||
type State = { | ||
tests: Test[]; | ||
}; | ||
|
||
export const controller = new ViewController(DatabaseController); |
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,29 @@ | ||
import { render } from "@App/Middleware/Render"; | ||
import { router } from "@App/Services/Router"; | ||
import { database, IndexedDbStorage } from "@valkyr/db"; | ||
import { Route } from "@valkyr/router"; | ||
|
||
import { DatabaseView } from "./database.view"; | ||
import { Test } from "./test.model"; | ||
|
||
/* | ||
|-------------------------------------------------------------------------------- | ||
| Models | ||
|-------------------------------------------------------------------------------- | ||
*/ | ||
|
||
database.register([{ name: "test", model: Test }], IndexedDbStorage); | ||
|
||
/* | ||
|-------------------------------------------------------------------------------- | ||
| Routes | ||
|-------------------------------------------------------------------------------- | ||
*/ | ||
|
||
router.register([ | ||
new Route({ | ||
name: "Database", | ||
path: "/database", | ||
actions: [render(DatabaseView)] | ||
}) | ||
]); |
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,11 @@ | ||
import { controller } from "./database.controller"; | ||
|
||
export const DatabaseView = controller.view(({ state: { tests }, actions: { run } }) => { | ||
return ( | ||
<div> | ||
<div>Database Testing</div> | ||
<button onClick={run}>Run</button> | ||
<pre>{JSON.stringify(tests, null, 2)}</pre> | ||
</div> | ||
); | ||
}); |
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 @@ | ||
import "./database.module"; |
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,11 @@ | ||
import { Document, Model } from "@valkyr/db"; | ||
|
||
export type TestDocument = Document & { | ||
foo: string; | ||
}; | ||
|
||
export class Test extends Model<TestDocument> { | ||
readonly foo!: string; | ||
} | ||
|
||
export type TestModel = typeof Test; |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
import "./Realms"; | ||
import "./Auth"; | ||
import "./Database"; | ||
import "./Realms"; |