0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-06 20:40:08 -05:00

feat(core): add custom_data to the user schema (#230)

This commit is contained in:
Xiao Yijun 2022-02-15 16:59:28 +08:00 committed by GitHub
parent 86ce5a9640
commit e8cbe00d3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 6 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", "name", "avatar", "role_names", "identities"\nfrom "users"'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "name", "avatar", "role_names", "identities", "custom_data"\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", "name", "avatar", "role_names", "identities"\nfrom "users"\nwhere "id"=$1'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "name", "avatar", "role_names", "identities", "custom_data"\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", "name", "avatar", "role_names", "identities"\nfrom "users"\nlimit $1'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "name", "avatar", "role_names", "identities", "custom_data"\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", "name", "avatar", "role_names", "identities"\nfrom "users"\noffset $1'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "name", "avatar", "role_names", "identities", "custom_data"\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", "name", "avatar", "role_names", "identities"\nfrom "users"'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "name", "avatar", "role_names", "identities", "custom_data"\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", "name", "avatar", "role_names", "identities"\nfrom "users"\nwhere "id"=$1\nlimit $2\noffset $3'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "name", "avatar", "role_names", "identities", "custom_data"\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

@ -7,6 +7,8 @@ import {
roleNamesGuard,
Identities,
identitiesGuard,
CustomData,
customDataGuard,
GeneratedSchema,
Guard,
} from '../foundations';
@ -24,6 +26,7 @@ export type CreateUser = {
avatar?: string | null;
roleNames?: RoleNames;
identities?: Identities;
customData?: CustomData;
};
export type User = {
@ -38,6 +41,7 @@ export type User = {
avatar: string | null;
roleNames: RoleNames;
identities: Identities;
customData: CustomData;
};
const createGuard: Guard<CreateUser> = z.object({
@ -52,6 +56,7 @@ const createGuard: Guard<CreateUser> = z.object({
avatar: z.string().nullable().optional(),
roleNames: roleNamesGuard.optional(),
identities: identitiesGuard.optional(),
customData: customDataGuard.optional(),
});
export const Users: GeneratedSchema<CreateUser> = Object.freeze({
@ -69,6 +74,7 @@ export const Users: GeneratedSchema<CreateUser> = Object.freeze({
avatar: 'avatar',
roleNames: 'role_names',
identities: 'identities',
customData: 'custom_data',
},
fieldKeys: [
'id',
@ -82,6 +88,7 @@ export const Users: GeneratedSchema<CreateUser> = Object.freeze({
'avatar',
'roleNames',
'identities',
'customData',
],
createGuard,
});

View file

@ -54,6 +54,11 @@ export const identitiesGuard = z.record(identityGuard);
export type Identity = z.infer<typeof identityGuard>;
export type Identities = z.infer<typeof identitiesGuard>;
// TODO: LOG-1553 support empty shape of object
export const customDataGuard = z.object({}).catchall(z.unknown());
export type CustomData = z.infer<typeof customDataGuard>;
/**
* User Logs
*/

View file

@ -12,5 +12,6 @@ create table users (
avatar varchar(256),
role_names jsonb /* @use RoleNames */ not null default '[]'::jsonb,
identities jsonb /* @use Identities */ not null default '{}'::jsonb,
custom_data jsonb /* @use CustomData */ not null default '{}'::jsonb,
primary key (id)
);