Skip to content
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

Rename which to where #68

Merged
merged 1 commit into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ setupWorker(
// Retrieve a single user from the database by ID.
rest.get('/user/:userId', (req, res, ctx) => {
const user = db.user.findFirst({
which: {
where: {
id: {
equals: req.params.userId,
},
Expand Down Expand Up @@ -141,7 +141,7 @@ Returns the first entity that satisfies the given query.

```js
const user = db.user.findFirst({
which: {
where: {
id: {
equals: 'abc-123',
},
Expand All @@ -155,7 +155,7 @@ Returns all the entities that satisfy the given query.

```js
const users = db.user.findMany({
which: {
where: {
followersCount: {
gte: 1000,
},
Expand All @@ -178,7 +178,7 @@ Can accept an optional query argument to filter the records before counting them

```js
db.user.count({
which: {
where: {
role: {
equals: 'reader',
},
Expand All @@ -201,7 +201,7 @@ Updates the first entity that matches the query.
```js
const updatedUser = db.user.update({
// Query for the entity to modify.
which: {
where: {
id: {
equals: 'abc-123',
},
Expand All @@ -226,7 +226,7 @@ Updates multiple entities that match the query.
```js
const updatedUsers = db.user.updateMany({
// Query for the entity to modify.
which: {
where: {
id: {
in: ['abc-123', 'def-456'],
},
Expand All @@ -245,7 +245,7 @@ Deletes the entity that satisfies the given query.

```js
const deletedUser = db.user.delete({
which: {
where: {
followersCount: {
equals: 0,
},
Expand All @@ -259,7 +259,7 @@ Deletes multiple entities that match the query.

```js
const deletedUsers = db.user.deleteMany({
which: {
where: {
followersCount: {
lt: 10,
},
Expand Down Expand Up @@ -323,13 +323,13 @@ setupWorker(...db.user.toHandlers('graphql'))

The following GraphQL queries and mutations are generated:

- `user(which: UserQueryInput): User`, returns a user matching the query.
- `users(which: UserQueryInput, cursor: ID, skip: Int, take: Int): [User!]`, returns all users matching the query (supports [pagination](#pagination)).
- `user(where: UserQueryInput): User`, returns a user matching the query.
- `users(where: UserQueryInput, cursor: ID, skip: Int, take: Int): [User!]`, returns all users matching the query (supports [pagination](#pagination)).
- `createUser(data: UserInput!): User!`, creates a new user.
- `updateUser(which: UserQueryInput!, data: UserInput!): User!`, updates a user.
- `updateUsers(which: UserQueryInput!, data: UserInput!): [User!]`, updates multiple users.
- `deleteUser(which: UserQueryInput!): User!`, deletes a user.
- `deleteUsers(which: UserQueryInput!): [User!]`, deletes multiple users.
- `updateUser(where: UserQueryInput!, data: UserInput!): User!`, updates a user.
- `updateUsers(where: UserQueryInput!, data: UserInput!): [User!]`, updates multiple users.
- `deleteUser(where: UserQueryInput!): User!`, deletes a user.
- `deleteUsers(where: UserQueryInput!): [User!]`, deletes multiple users.

> Notice how some operation names contain the plural model name to emphasize that they work on a collection of entities.

Expand Down Expand Up @@ -411,7 +411,7 @@ const db = factory({
// Returns the list of `post` entities
// that satisfy the given query.
const popularPosts = db.post.findMany({
which: {
where: {
likes: {
gte: 1000,
},
Expand Down Expand Up @@ -440,7 +440,7 @@ db.user.create({ id: 'abc-123' })
// This will throw an exception, because there are
// no "user" entities matching this query.
db.user.findFirst({
which: {
where: {
id: {
equals: 'def-456',
},
Expand Down Expand Up @@ -585,7 +585,7 @@ const db = factory({
})

db.post.findMany({
which: {
where: {
category: {
equals: 'Science',
},
Expand All @@ -609,7 +609,7 @@ const db = factory({
})

const firstPage = db.post.findMany({
which: {
where: {
category: {
equals: 'Science',
},
Expand All @@ -619,7 +619,7 @@ const firstPage = db.post.findMany({
})

const secondPage = db.post.findMany({
which: {
where: {
category: {
equals: 'Science',
},
Expand Down
2 changes: 1 addition & 1 deletion src/db/drop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { factory } from '../factory'
export function drop(db: ReturnType<typeof factory>): void {
Object.values(db).forEach((model) => {
model.deleteMany({
which: {},
where: {},
})
})
}
14 changes: 7 additions & 7 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ function createModelApi<
invariant(
query.strict && !firstResult,
`Failed to execute "findFirst" on the "${modelName}" model: no entity found matching the query "${JSON.stringify(
query.which,
query.where,
)}".`,
new OperationError(OperationErrorType.EntityNotFound),
)
Expand All @@ -121,7 +121,7 @@ function createModelApi<
invariant(
query.strict && results.length === 0,
`Failed to execute "findMany" on the "${modelName}" model: no entities found matching the query "${JSON.stringify(
query.which,
query.where,
)}".`,
new OperationError(OperationErrorType.EntityNotFound),
)
Expand All @@ -138,7 +138,7 @@ function createModelApi<
invariant(
strict,
`Failed to execute "update" on the "${modelName}" model: no entity found matching the query "${JSON.stringify(
query.which,
query.where,
)}".`,
new OperationError(OperationErrorType.EntityNotFound),
)
Expand Down Expand Up @@ -173,7 +173,7 @@ function createModelApi<
invariant(
strict,
`Failed to execute "updateMany" on the "${modelName}" model: no entities found matching the query "${JSON.stringify(
query.which,
query.where,
)}".`,
new OperationError(OperationErrorType.EntityNotFound),
)
Expand All @@ -191,7 +191,7 @@ function createModelApi<
invariant(
db.has(modelName, nextRecord[prevRecord.__primaryKey]),
`Failed to execute "updateMany" on the "${modelName}" model: no entities found matching the query "${JSON.stringify(
query.which,
query.where,
)}".`,
new OperationError(OperationErrorType.EntityNotFound),
)
Expand All @@ -210,7 +210,7 @@ function createModelApi<
invariant(
strict,
`Failed to execute "delete" on the "${modelName}" model: no entity found matching the query "${JSON.stringify(
query.which,
query.where,
)}".`,
new OperationError(OperationErrorType.EntityNotFound),
)
Expand All @@ -228,7 +228,7 @@ function createModelApi<
invariant(
strict,
`Failed to execute "deleteMany" on the "${modelName}" model: no entities found matching the query "${JSON.stringify(
query.which,
query.where,
)}".`,
new OperationError(OperationErrorType.EntityNotFound),
)
Expand Down
4 changes: 2 additions & 2 deletions src/model/defineRelationalProperties.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function defineRelationalProperties(
entity.__type,
entity.__primaryKey,
{
which: {
where: {
[property]: {
[firstRef.__primaryKey]: {
in: relation.refs.map((ref) => ref.__nodeId),
Expand Down Expand Up @@ -75,7 +75,7 @@ export function defineRelationalProperties(
entityRef.__type,
entityRef.__primaryKey,
{
which: {
where: {
[entityRef.__primaryKey]: {
equals: entityRef.__nodeId,
},
Expand Down
26 changes: 13 additions & 13 deletions src/model/generateGraphQLHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export function declarationToFields(
types.inputFields[key] = { type: valueType }

// Query input fields describe a type that is used
// as a "which" query, with its comparator function types.
// as a "where" query, with its comparator function types.
types.queryInputFields[key] = { type: queryType }

return types
Expand Down Expand Up @@ -175,25 +175,25 @@ export function generateGraphQLHandlers<
[modelName]: {
type: EntityType,
args: {
which: { type: EntityQueryInputType },
where: { type: EntityQueryInputType },
},
resolve(_, args) {
return model.findFirst({ which: args.which })
return model.findFirst({ where: args.where })
},
},
// Get all entities.
[pluralModelName]: {
type: new GraphQLList(EntityType),
args: {
...paginationArgs,
which: { type: EntityQueryInputType },
where: { type: EntityQueryInputType },
},
resolve(_, args) {
const shouldQuery = Object.keys(args).length > 0

return shouldQuery
? model.findMany({
which: args.which,
where: args.where,
skip: args.skip,
take: args.take,
cursor: args.cursor,
Expand All @@ -220,12 +220,12 @@ export function generateGraphQLHandlers<
[`update${capitalModelName}`]: {
type: EntityType,
args: {
which: { type: EntityQueryInputType },
where: { type: EntityQueryInputType },
data: { type: EntityInputType },
},
resolve(_, args) {
return model.update({
which: args.which,
where: args.where,
data: args.data,
})
},
Expand All @@ -234,12 +234,12 @@ export function generateGraphQLHandlers<
[`update${capitalize(pluralModelName)}`]: {
type: new GraphQLList(EntityType),
args: {
which: { type: EntityQueryInputType },
where: { type: EntityQueryInputType },
data: { type: EntityInputType },
},
resolve(_, args) {
return model.updateMany({
which: args.which,
where: args.where,
data: args.data,
})
},
Expand All @@ -248,20 +248,20 @@ export function generateGraphQLHandlers<
[`delete${capitalModelName}`]: {
type: EntityType,
args: {
which: { type: EntityQueryInputType },
where: { type: EntityQueryInputType },
},
resolve(_, args) {
return model.delete({ which: args.which })
return model.delete({ where: args.where })
},
},
// Delete multiple entities.
[`delete${capitalize(pluralModelName)}`]: {
type: new GraphQLList(EntityType),
args: {
which: { type: EntityQueryInputType },
where: { type: EntityQueryInputType },
},
resolve(_, args) {
return model.deleteMany({ which: args.which })
return model.deleteMany({ where: args.where })
},
},
},
Expand Down
16 changes: 8 additions & 8 deletions src/model/generateRestHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { GetQueryFor } from '../query/queryTypes'
import { OperationErrorType, OperationError } from '../errors/OperationError'
import { removeInternalProperties } from '../utils/removeInternalProperties'

interface WeakQuerySelectorWhich<KeyType extends PrimaryKeyType> {
interface WeakQuerySelectorWhere<KeyType extends PrimaryKeyType> {
[key: string]: Partial<GetQueryFor<KeyType>>
}

Expand Down Expand Up @@ -82,7 +82,7 @@ export function generateRestHandlers<
const skip = parseInt(rawSkip ?? '0', 10)
const take = rawTake == null ? rawTake : parseInt(rawTake, 10)

let options = { which: {} }
let options = { where: {} }

if (take && !isNaN(take) && !isNaN(skip)) {
options = Object.assign(options, { take, skip })
Expand All @@ -103,14 +103,14 @@ export function generateRestHandlers<
RequestParams<PrimaryKeyType>
>((req, res, ctx) => {
const id = req.params[primaryKey]
const which: WeakQuerySelectorWhich<typeof primaryKey> = {
const where: WeakQuerySelectorWhere<typeof primaryKey> = {
[primaryKey]: {
equals: id,
},
}
const entity = model.findFirst({
strict: true,
which: which as any,
where: where as any,
})

return res(ctx.json(removeInternalProperties(entity)))
Expand All @@ -133,14 +133,14 @@ export function generateRestHandlers<
RequestParams<PrimaryKeyType>
>((req, res, ctx) => {
const id = req.params[primaryKey]
const which: WeakQuerySelectorWhich<typeof primaryKey> = {
const where: WeakQuerySelectorWhere<typeof primaryKey> = {
[primaryKey]: {
equals: id,
},
}
const updatedEntity = model.update({
strict: true,
which: which as any,
where: where as any,
data: req.body,
})!

Expand All @@ -154,14 +154,14 @@ export function generateRestHandlers<
RequestParams<PrimaryKeyType>
>((req, res, ctx) => {
const id = req.params[primaryKey]
const which: WeakQuerySelectorWhich<typeof primaryKey> = {
const where: WeakQuerySelectorWhere<typeof primaryKey> = {
[primaryKey]: {
equals: id,
},
}
const deletedEntity = model.delete({
strict: true,
which: which as any,
where: where as any,
})!

return res(ctx.json(removeInternalProperties(deletedEntity)))
Expand Down
Loading