0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-17 22:04:19 -05:00

feat(schema): add roles table (#202)

* feat(schema): add roles table

add roles table

* fix(chema): ci fix

ci fix

* fix(cr): cr fix

- rename userRoles to roleNames
- rename role table colmn name

* fix(ut): fix ut

fix ut
This commit is contained in:
simeng-li 2022-01-27 10:48:55 +08:00 committed by GitHub
parent 6fadd4a420
commit 9f23dbbb2a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 63 additions and 12 deletions

View file

@ -7,7 +7,7 @@ import { buildFindMany } from './find-many';
describe('buildFindMany()', () => {
it('matches expected sql', async () => {
const pool = createTestPool(
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt"\nfrom "users"'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "role_names"\nfrom "users"'
);
const findMany = buildFindMany(pool, Users);
await findMany();
@ -15,7 +15,7 @@ describe('buildFindMany()', () => {
it('matches expected sql with where conditions', async () => {
const pool = createTestPool(
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt"\nfrom "users"\nwhere "id"=$1'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "role_names"\nfrom "users"\nwhere "id"=$1'
);
const findMany = buildFindMany(pool, Users);
await findMany({ where: { id: '123' } });
@ -23,7 +23,7 @@ describe('buildFindMany()', () => {
it('matches expected sql with limit', async () => {
const pool = createTestPool(
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt"\nfrom "users"\nlimit $1'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "role_names"\nfrom "users"\nlimit $1'
);
const findMany = buildFindMany(pool, Users);
await findMany({ limit: 10 });
@ -31,7 +31,7 @@ describe('buildFindMany()', () => {
it('matches expected sql with offset', async () => {
const pool = createTestPool(
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt"\nfrom "users"\noffset $1'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "role_names"\nfrom "users"\noffset $1'
);
const findMany = buildFindMany(pool, Users);
await findMany({ offset: 10 });
@ -39,7 +39,7 @@ describe('buildFindMany()', () => {
it('matches expected sql with offset 0', async () => {
const pool = createTestPool(
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt"\nfrom "users"'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "role_names"\nfrom "users"'
);
const findMany = buildFindMany(pool, Users);
await findMany({ offset: 0 });
@ -47,7 +47,7 @@ describe('buildFindMany()', () => {
it('matches expected sql with where conditions, limit and offset', async () => {
const pool = createTestPool(
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt"\nfrom "users"\nwhere "id"=$1\nlimit $2\noffset $3'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "role_names"\nfrom "users"\nwhere "id"=$1\nlimit $2\noffset $3'
);
const findMany = buildFindMany(pool, Users);
await findMany({ where: { id: '123' }, limit: 20, offset: 20 });

View file

@ -69,7 +69,6 @@ describe('convertToPrimitiveOrSql()', () => {
});
it('throws an error when value is not primitive', () => {
// @ts-expect-error
expect(() => convertToPrimitiveOrSql(normalKey, [123, 456])).toThrow(
'Cannot convert foo with 123,456 to primitive'
);

View file

@ -7,6 +7,7 @@ export * from './oidc-model-instance';
export * from './passcode';
export * from './resource-scope';
export * from './resource';
export * from './role';
export * from './setting';
export * from './sign-in-experience';
export * from './sign-in-method';

View file

@ -0,0 +1,31 @@
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
import { z } from 'zod';
import { GeneratedSchema, Guard } from '../foundations';
export type CreateRole = {
name: string;
description: string;
};
export type Role = {
name: string;
description: string;
};
const createGuard: Guard<CreateRole> = z.object({
name: z.string(),
description: z.string(),
});
export const Roles: GeneratedSchema<CreateRole> = Object.freeze({
table: 'roles',
tableSingular: 'role',
fields: {
name: 'name',
description: 'description',
},
fieldKeys: ['name', 'description'],
createGuard,
});

View file

@ -2,7 +2,7 @@
import { z } from 'zod';
import { GeneratedSchema, Guard } from '../foundations';
import { RoleNames, roleNamesGuard, GeneratedSchema, Guard } from '../foundations';
import { PasswordEncryptionMethod } from './custom-types';
export type CreateUser = {
@ -13,6 +13,7 @@ export type CreateUser = {
passwordEncrypted?: string | null;
passwordEncryptionMethod?: PasswordEncryptionMethod | null;
passwordEncryptionSalt?: string | null;
roleNames?: RoleNames | null;
};
export type User = {
@ -23,6 +24,7 @@ export type User = {
passwordEncrypted: string | null;
passwordEncryptionMethod: PasswordEncryptionMethod | null;
passwordEncryptionSalt: string | null;
roleNames: RoleNames | null;
};
const createGuard: Guard<CreateUser> = z.object({
@ -33,6 +35,7 @@ const createGuard: Guard<CreateUser> = z.object({
passwordEncrypted: z.string().nullable().optional(),
passwordEncryptionMethod: z.nativeEnum(PasswordEncryptionMethod).nullable().optional(),
passwordEncryptionSalt: z.string().nullable().optional(),
roleNames: roleNamesGuard.nullable().optional(),
});
export const Users: GeneratedSchema<CreateUser> = Object.freeze({
@ -46,6 +49,7 @@ export const Users: GeneratedSchema<CreateUser> = Object.freeze({
passwordEncrypted: 'password_encrypted',
passwordEncryptionMethod: 'password_encryption_method',
passwordEncryptionSalt: 'password_encryption_salt',
roleNames: 'role_names',
},
fieldKeys: [
'id',
@ -55,6 +59,7 @@ export const Users: GeneratedSchema<CreateUser> = Object.freeze({
'passwordEncrypted',
'passwordEncryptionMethod',
'passwordEncryptionSalt',
'roleNames',
],
createGuard,
});

View file

@ -38,6 +38,13 @@ export const customClientMetadataGuard = z.object({
export type CustomClientMetadata = z.infer<typeof customClientMetadataGuard>;
/**
* Users
*/
export const roleNamesGuard = z.string().array();
export type RoleNames = z.infer<typeof roleNamesGuard>;
/**
* User Logs
*/

View file

@ -1,13 +1,15 @@
import { ZodObject, ZodType, ZodOptional } from 'zod';
type ParseOptional<K> = undefined extends K
? ZodOptional<ZodType<Exclude<K, undefined>>>
: ZodType<K>;
export type Guard<T extends Record<string, unknown>> = ZodObject<{
[key in keyof T]-?: undefined extends T[key]
? ZodOptional<ZodType<Exclude<T[key], undefined>>>
: ZodType<T[key]>;
[key in keyof T]-?: ParseOptional<T[key]>;
}>;
export type SchemaValuePrimitive = string | number | boolean | undefined;
export type SchemaValue = SchemaValuePrimitive | Record<string, unknown> | null;
export type SchemaValue = SchemaValuePrimitive | Record<string, unknown> | unknown[] | null;
export type SchemaLike<Key extends string = string> = {
[key in Key]: SchemaValue;
};

View file

@ -0,0 +1,5 @@
create table roles (
name varchar(128) not null,
description varchar(128) not null,
primary key (name)
);

View file

@ -8,5 +8,6 @@ create table users (
password_encrypted varchar(128),
password_encryption_method password_encryption_method,
password_encryption_salt varchar(128),
role_names jsonb /* @use RoleNames */,
primary key (id)
);