Skip to content

Commit

Permalink
change data to cache
Browse files Browse the repository at this point in the history
  • Loading branch information
neurosnap committed Nov 20, 2023
1 parent 74013fa commit 03d1421
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 20 deletions.
4 changes: 2 additions & 2 deletions query/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const testStore = () => {
const schema = createSchema({
users: slice.table<User>({ empty: emptyUser }),
loaders: slice.loader(),
data: slice.table({ empty: {} }),
cache: slice.table({ empty: {} }),
});
const store = configureStore(schema);
return { schema, store };
Expand Down Expand Up @@ -294,7 +294,7 @@ it(tests, "createApi with hash key on a large post", async () => {
});

expect([8, 9].includes(expectedKey.split("|")[1].length)).toBeTruthy();
expect(s.data[expectedKey]).toEqual({
expect(s.cache[expectedKey]).toEqual({
"1": { id: "1", name: "test", email: email, largetext: largetext },
});
});
Expand Down
10 changes: 5 additions & 5 deletions query/fetch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const delay = (n = 200) =>
const testStore = () => {
const schema = createSchema({
loaders: slice.loader(),
data: slice.table({ empty: {} }),
cache: slice.table({ empty: {} }),
});
const store = configureStore(schema);
return { schema, store };
Expand Down Expand Up @@ -69,7 +69,7 @@ it(
await delay();

const state = store.getState();
expect(state.data[action.payload.key]).toEqual(mockUser);
expect(state.cache[action.payload.key]).toEqual(mockUser);

expect(actual).toEqual([{
url: `${baseUrl}/users`,
Expand Down Expand Up @@ -156,7 +156,7 @@ it(tests, "fetch - error handling", async () => {
await delay();

const state = store.getState();
expect(state.data[action.payload.key]).toEqual(errMsg);
expect(state.cache[action.payload.key]).toEqual(errMsg);
expect(actual).toEqual({ ok: false, data: errMsg });
});

Expand Down Expand Up @@ -196,7 +196,7 @@ it(tests, "fetch - status 204", async () => {
await delay();

const state = store.getState();
expect(state.data[action.payload.key]).toEqual({});
expect(state.cache[action.payload.key]).toEqual({});
expect(actual).toEqual({ ok: true, data: {} });
});

Expand Down Expand Up @@ -425,7 +425,7 @@ it(
await delay();

const state = store.getState();
expect(state.data[action.payload.key]).toEqual(mockUser);
expect(state.cache[action.payload.key]).toEqual(mockUser);
expect(actual).toEqual({ ok: true, data: mockUser });
},
);
Expand Down
6 changes: 3 additions & 3 deletions query/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const testStore = () => {
const schema = createSchema({
users: slice.table<User>({ empty: emptyUser }),
loaders: slice.loader(),
data: slice.table({ empty: {} }),
cache: slice.table({ empty: {} }),
});
const store = configureStore(schema);
return { schema, store };
Expand Down Expand Up @@ -414,7 +414,7 @@ it(tests, "createApi with own key", async () => {
: createKey("/users [POST]", { email: newUEmail });

const s = store.getState();
asserts.assertEquals(schema.db.data.selectById(s, { id: expectedKey }), {
asserts.assertEquals(schema.db.cache.selectById(s, { id: expectedKey }), {
"1": { id: "1", name: "test", email: newUEmail },
});

Expand Down Expand Up @@ -483,7 +483,7 @@ it(tests, "createApi with custom key but no payload", async () => {
: createKey("/users [GET]", null);

const s = store.getState();
asserts.assertEquals(schema.db.data.selectById(s, { id: expectedKey }), {
asserts.assertEquals(schema.db.cache.selectById(s, { id: expectedKey }), {
"1": mockUser,
});

Expand Down
6 changes: 3 additions & 3 deletions store/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import { TableOutput } from "./slice/table.ts";
export function storeMdw<
Ctx extends ApiCtx = ApiCtx,
M extends AnyState = AnyState,
>({ data, loaders, errorFn }: {
>({ cache, loaders, errorFn }: {
loaders: LoaderOutput<M, AnyState>;
data: TableOutput<any, AnyState>;
cache: TableOutput<any, AnyState>;
errorFn?: (ctx: Ctx) => string;
}) {
return compose<Ctx>([
dispatchActions,
loadingMonitor(loaders, errorFn),
simpleCache(data),
simpleCache(cache),
]);
}

Expand Down
6 changes: 3 additions & 3 deletions store/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ it(tests, "general types and functionality", async () => {
counter: slice.num(),
dev: slice.any<boolean>(false),
currentUser: slice.obj<User>(emptyUser),
data: slice.table({ empty: {} }),
cache: slice.table({ empty: {} }),
loaders: slice.loader(),
});
const db = schema.db;
Expand All @@ -37,7 +37,7 @@ it(tests, "general types and functionality", async () => {
counter: 0,
dev: false,
currentUser: { id: "", name: "" },
data: {},
cache: {},
loaders: {},
});
const userMap = schema.db.users.selectTable(store.getState());
Expand Down Expand Up @@ -76,7 +76,7 @@ it(tests, "general types and functionality", async () => {
it(tests, "can work with a nested object", async () => {
const schema = createSchema({
currentUser: slice.obj<UserWithRoles>({ id: "", name: "", roles: [] }),
data: slice.table({ empty: {} }),
cache: slice.table({ empty: {} }),
loaders: slice.loader(),
});
const db = schema.db;
Expand Down
6 changes: 2 additions & 4 deletions store/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import { BaseSchema, FxStore, StoreUpdater } from "./types.ts";
export function createSchema<
O extends {
loaders: <M extends AnyState>(s: string) => LoaderOutput<M, AnyState>;
data: (s: string) => TableOutput<any, AnyState>;
cache: (s: string) => TableOutput<any, AnyState>;
[key: string]: (name: string) => BaseSchema<unknown>;
},
S extends { [key in keyof O]: ReturnType<O[key]>["initialState"] },
>(
slices: O,
): {
db: {
[key in keyof O]: ReturnType<O[key]>;
};
db: { [key in keyof O]: ReturnType<O[key]> };
initialState: S;
update: FxStore<S>["update"];
} {
Expand Down

0 comments on commit 03d1421

Please sign in to comment.