0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-27 21:39:16 -05:00

feat(schema): user identities (#215)

This commit is contained in:
Wang Sijie 2022-02-09 12:55:06 +08:00 committed by GitHub
parent 77ca86cac6
commit e7458f8a2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 7 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", "role_names"\nfrom "users"'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "role_names", "identities"\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", "role_names"\nfrom "users"\nwhere "id"=$1'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "role_names", "identities"\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", "role_names"\nfrom "users"\nlimit $1'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "role_names", "identities"\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", "role_names"\nfrom "users"\noffset $1'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "role_names", "identities"\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", "role_names"\nfrom "users"'
'select "id", "username", "primary_email", "primary_phone", "password_encrypted", "password_encryption_method", "password_encryption_salt", "role_names", "identities"\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", "role_names"\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", "identities"\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

@ -2,7 +2,14 @@
import { z } from 'zod';
import { RoleNames, roleNamesGuard, GeneratedSchema, Guard } from '../foundations';
import {
RoleNames,
roleNamesGuard,
Identities,
identitiesGuard,
GeneratedSchema,
Guard,
} from '../foundations';
import { PasswordEncryptionMethod } from './custom-types';
export type CreateUser = {
@ -14,6 +21,7 @@ export type CreateUser = {
passwordEncryptionMethod?: PasswordEncryptionMethod | null;
passwordEncryptionSalt?: string | null;
roleNames?: RoleNames;
identities?: Identities;
};
export type User = {
@ -25,6 +33,7 @@ export type User = {
passwordEncryptionMethod: PasswordEncryptionMethod | null;
passwordEncryptionSalt: string | null;
roleNames: RoleNames;
identities: Identities;
};
const createGuard: Guard<CreateUser> = z.object({
@ -36,6 +45,7 @@ const createGuard: Guard<CreateUser> = z.object({
passwordEncryptionMethod: z.nativeEnum(PasswordEncryptionMethod).nullable().optional(),
passwordEncryptionSalt: z.string().nullable().optional(),
roleNames: roleNamesGuard.optional(),
identities: identitiesGuard.optional(),
});
export const Users: GeneratedSchema<CreateUser> = Object.freeze({
@ -50,6 +60,7 @@ export const Users: GeneratedSchema<CreateUser> = Object.freeze({
passwordEncryptionMethod: 'password_encryption_method',
passwordEncryptionSalt: 'password_encryption_salt',
roleNames: 'role_names',
identities: 'identities',
},
fieldKeys: [
'id',
@ -60,6 +71,7 @@ export const Users: GeneratedSchema<CreateUser> = Object.freeze({
'passwordEncryptionMethod',
'passwordEncryptionSalt',
'roleNames',
'identities',
],
createGuard,
});

View file

@ -45,6 +45,15 @@ export const roleNamesGuard = z.string().array();
export type RoleNames = z.infer<typeof roleNamesGuard>;
const identityGuard = z.object({
userId: z.string(),
details: z.object({}).optional(), // Connector's userinfo details, schemaless
});
export const identitiesGuard = z.record(identityGuard);
export type Identity = z.infer<typeof identityGuard>;
export type Identities = z.infer<typeof identitiesGuard>;
/**
* User Logs
*/

View file

@ -9,5 +9,6 @@ create table users (
password_encryption_method password_encryption_method,
password_encryption_salt varchar(128),
role_names jsonb /* @use RoleNames */ not null default '[]'::jsonb,
identities jsonb /* @use Identities */ not null default '{}'::jsonb,
primary key (id)
);