-
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.
Signed-off-by: Jim Ezesinachi <ezesinachijim@gmail.com>
- Loading branch information
1 parent
4209cf9
commit 43d03a2
Showing
17 changed files
with
226 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { test, describe, expect } from 'vitest'; | ||
import { DB, from } from '../tests/generated'; | ||
import { Query } from '@synthql/queries'; | ||
import { middleware } from './middleware'; | ||
import { createQueryEngine } from '../tests/queryEngine'; | ||
|
||
// Create type/interface for context | ||
type UserRole = 'user' | 'admin' | 'super'; | ||
interface Session { | ||
id: number; | ||
email: string; | ||
roles: UserRole[]; | ||
isActive: boolean; | ||
} | ||
|
||
// Create middleware | ||
const restrictPaymentsByCustomer = middleware<Query<DB, 'payment'>, Session>({ | ||
predicate: ({ query, context }) => | ||
query?.from === 'payment' && | ||
context?.roles?.includes('user') && | ||
context?.isActive, | ||
transformQuery: ({ query, context }) => ({ | ||
...query, | ||
where: { | ||
...query.where, | ||
customer_id: context.id, | ||
}, | ||
}), | ||
}); | ||
|
||
describe('createExpressSynthqlHandler', async () => { | ||
test('1', async () => { | ||
const queryEngine = createQueryEngine([restrictPaymentsByCustomer]); | ||
|
||
// Create context | ||
// This would be an object generated from a server | ||
// request handler (e.g a parsed cookie/token) | ||
const context: Session = { | ||
id: 1, | ||
email: 'user@example.com', | ||
roles: ['user', 'admin', 'super'], | ||
isActive: true, | ||
}; | ||
|
||
// Create base query | ||
const q = from('payment').one(); | ||
|
||
const queryWithContextManuallyAdded = from('payment') | ||
.where({ | ||
customer_id: context.id, | ||
}) | ||
.one(); | ||
|
||
const result = await queryEngine.executeAndWait(q, context); | ||
|
||
const resultFromQueryWithContextManuallyAdded = | ||
await queryEngine.executeAndWait(queryWithContextManuallyAdded); | ||
|
||
expect(result).toEqual(resultFromQueryWithContextManuallyAdded); | ||
}); | ||
}); |
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,41 @@ | ||
export interface Middleware<TQuery = unknown, TContext = unknown> { | ||
predicate: ({ | ||
query, | ||
context, | ||
}: { | ||
query: TQuery; | ||
context: TContext; | ||
}) => boolean; | ||
transformQuery: ({ | ||
query, | ||
context, | ||
}: { | ||
query: TQuery; | ||
context: TContext; | ||
}) => TQuery; | ||
} | ||
|
||
export function middleware<TQuery = unknown, TContext = unknown>({ | ||
predicate, | ||
transformQuery, | ||
}: { | ||
predicate: ({ | ||
query, | ||
context, | ||
}: { | ||
query: TQuery; | ||
context: TContext; | ||
}) => boolean; | ||
transformQuery: ({ | ||
query, | ||
context, | ||
}: { | ||
query: TQuery; | ||
context: TContext; | ||
}) => TQuery; | ||
}): Middleware<TQuery, TContext> { | ||
return { | ||
predicate, | ||
transformQuery, | ||
}; | ||
} |
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,5 +1,6 @@ | ||
export { collectLast } from './util/generators/collectLast'; | ||
export { composeQuery } from './execution/executors/PgExecutor/composeQuery'; | ||
export { middleware } from './execution/middleware'; | ||
export type * from './types/QueryPlan'; | ||
export * from './QueryEngine'; | ||
export * from './SynthqlError'; |
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
3 changes: 2 additions & 1 deletion
3
packages/backend/src/tests/propertyBased/properties/cardinality.test.ts
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
Oops, something went wrong.