Skip to content

Commit

Permalink
feat: add timestamp to model filter
Browse files Browse the repository at this point in the history
  • Loading branch information
r0ohafza committed Jun 26, 2023
1 parent 5b8297b commit 6ce1907
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions packages/core/src/server/graphql/buildPluralField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type PluralArgs = {
skip?: number;
orderBy?: string;
orderDirection?: "asc" | "desc";
timestamp?: number;
};
type PluralResolver = GraphQLFieldResolver<Source, Context, PluralArgs>;

Expand Down Expand Up @@ -176,6 +177,7 @@ const buildPluralField = (
skip: { type: GraphQLInt },
orderBy: { type: GraphQLString },
orderDirection: { type: GraphQLString },
timestamp: { type: GraphQLInt },
},
resolve: resolver,
};
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/server/graphql/buildSingularField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ import {
GraphQLFieldConfig,
GraphQLFieldResolver,
GraphQLID,
GraphQLInt,
GraphQLNonNull,
GraphQLObjectType,
} from "graphql";

import type { Entity } from "@/schema/types";
import { MAX_INTEGER } from "@/user-store/utils";

import type { Context, Source } from "./buildGqlSchema";

type SingularArgs = {
id?: string;
timestamp?: number;
};
type SingularResolver = GraphQLFieldResolver<Source, Context, SingularArgs>;

Expand All @@ -22,11 +25,14 @@ const buildSingularField = (
const resolver: SingularResolver = async (_, args, context) => {
const { store } = context;
const { id } = args;
let { timestamp } = args;

if (!id) return null;
if (!timestamp) timestamp = MAX_INTEGER;

const entityInstance = await store.findUnique({
modelName: entity.name,
timestamp,
id,
});

Expand Down Expand Up @@ -65,6 +71,7 @@ const buildSingularField = (
type: entityType,
args: {
id: { type: new GraphQLNonNull(GraphQLID) },
timestamp: { type: GraphQLInt },
},
resolve: resolver,
};
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/user-store/postgres/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
formatModelInstance,
getWhereOperatorAndValue,
parseModelFilter,
MAX_INTEGER,
} from "../utils";

const gqlScalarToSqlType = {
Expand All @@ -23,8 +24,6 @@ const gqlScalarToSqlType = {
Float: "text",
} as const;

const MAX_INTEGER = 2_147_483_647 as const;

export class PostgresUserStore implements UserStore {
db: Kysely<any>;

Expand Down Expand Up @@ -432,6 +431,8 @@ export class PostgresUserStore implements UserStore {
}) => {
const tableName = `${modelName}_${this.versionId}`;

if (filter.timestamp) timestamp = filter.timestamp;

let query = this.db
.selectFrom(tableName)
.selectAll()
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/user-store/sqlite/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
formatModelInstance,
getWhereOperatorAndValue,
parseModelFilter,
MAX_INTEGER,
} from "../utils";

const gqlScalarToSqlType = {
Expand All @@ -23,8 +24,6 @@ const gqlScalarToSqlType = {
Float: "text",
} as const;

const MAX_INTEGER = 2_147_483_647 as const;

export class SqliteUserStore implements UserStore {
db: Kysely<any>;

Expand Down Expand Up @@ -412,6 +411,8 @@ export class SqliteUserStore implements UserStore {
}) => {
const tableName = `${modelName}_${this.versionId}`;

if (filter.timestamp) timestamp = filter.timestamp;

let query = this.db
.selectFrom(tableName)
.selectAll()
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/user-store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export type ModelFilter = {
skip?: number;
orderBy?: string;
orderDirection?: "asc" | "desc";
timestamp?: number;
};

export type ModelInstance = {
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/user-store/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,13 @@ export function parseModelFilter(filter: ModelFilter = {}): ModelFilter {
parsedFilter.orderBy = filter.orderBy;
parsedFilter.orderDirection = filter.orderDirection;
parsedFilter.where = filter.where;
parsedFilter.timestamp = filter.timestamp;

return parsedFilter;
}

const DEFAULT_LIMIT = 100;
const MAX_LIMIT = 1000;
const MAX_SKIP = 5000;

export const MAX_INTEGER = 2_147_483_647 as const;

0 comments on commit 6ce1907

Please sign in to comment.