@loomcore/common 0.0.38 → 0.0.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,16 +1,17 @@
1
+ import { AppId } from '../types/app.types.js';
1
2
  export interface IAuditable {
2
3
  _created: Date;
3
- _createdBy: string;
4
+ _createdBy: AppId;
4
5
  _updated: Date;
5
- _updatedBy: string;
6
+ _updatedBy: AppId;
6
7
  _deleted?: Date;
7
- _deletedBy?: string;
8
+ _deletedBy?: AppId;
8
9
  }
9
10
  export declare const AuditableSchema: import("@sinclair/typebox").TObject<{
10
11
  _created: import("@sinclair/typebox").TTransform<import("@sinclair/typebox").TString, Date>;
11
- _createdBy: import("@sinclair/typebox").TString;
12
+ _createdBy: import("@sinclair/typebox").TSchema;
12
13
  _updated: import("@sinclair/typebox").TTransform<import("@sinclair/typebox").TString, Date>;
13
- _updatedBy: import("@sinclair/typebox").TString;
14
+ _updatedBy: import("@sinclair/typebox").TSchema;
14
15
  _deleted: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TTransform<import("@sinclair/typebox").TString, Date>>;
15
- _deletedBy: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
16
+ _deletedBy: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TSchema>;
16
17
  }>;
@@ -1,10 +1,11 @@
1
1
  import { Type } from '@sinclair/typebox';
2
- import { TypeboxIsoDate } from '../validation/index.js';
2
+ import { getIdSchema, TypeboxIsoDate } from '../validation/index.js';
3
+ const idSchema = getIdSchema();
3
4
  export const AuditableSchema = Type.Object({
4
5
  _created: TypeboxIsoDate(),
5
- _createdBy: Type.String(),
6
+ _createdBy: idSchema,
6
7
  _updated: TypeboxIsoDate(),
7
- _updatedBy: Type.String(),
8
+ _updatedBy: idSchema,
8
9
  _deleted: Type.Optional(TypeboxIsoDate()),
9
- _deletedBy: Type.Optional(Type.String()),
10
+ _deletedBy: Type.Optional(idSchema),
10
11
  });
@@ -4,6 +4,6 @@ export interface IEntity {
4
4
  _orgId?: AppId;
5
5
  }
6
6
  export declare const EntitySchema: import("@sinclair/typebox").TObject<{
7
- _id: import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TString, import("@sinclair/typebox").TNumber]>;
7
+ _id: import("@sinclair/typebox").TSchema;
8
8
  _orgId: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TUnsafe<unknown>>;
9
9
  }>;
@@ -1,6 +1,7 @@
1
1
  import { Type } from '@sinclair/typebox';
2
- import { IdSchema } from '../validation/id-schema.provider.js';
2
+ import { getIdSchema } from '../validation/typebox-extensions.js';
3
+ const idSchema = getIdSchema();
3
4
  export const EntitySchema = Type.Object({
4
- _id: IdSchema,
5
- _orgId: Type.Optional(Type.Unsafe({ ...IdSchema, title: 'Organization ID' }))
5
+ _id: idSchema,
6
+ _orgId: Type.Optional(Type.Unsafe({ ...idSchema, title: 'Organization ID' }))
6
7
  });
@@ -1,6 +1,7 @@
1
1
  import { IAuditable } from './auditable.model.js';
2
+ import { AppId } from '../types/app.types.js';
2
3
  export interface IOrganization extends IAuditable {
3
- _id: string;
4
+ _id: AppId;
4
5
  name: string;
5
6
  code: string;
6
7
  description?: string;
@@ -9,7 +10,7 @@ export interface IOrganization extends IAuditable {
9
10
  authToken?: string;
10
11
  }
11
12
  export declare const OrganizationSchema: import("@sinclair/typebox").TObject<{
12
- _id: import("@sinclair/typebox").TString;
13
+ _id: import("@sinclair/typebox").TSchema;
13
14
  name: import("@sinclair/typebox").TString;
14
15
  code: import("@sinclair/typebox").TString;
15
16
  description: import("@sinclair/typebox").TOptional<import("@sinclair/typebox").TString>;
@@ -1,9 +1,9 @@
1
1
  import { Type } from '@sinclair/typebox';
2
2
  import { entityUtils } from '../utils/index.js';
3
+ import { getIdSchema } from '../validation/index.js';
4
+ const idSchema = getIdSchema();
3
5
  export const OrganizationSchema = Type.Object({
4
- _id: Type.String({
5
- title: 'ID'
6
- }),
6
+ _id: idSchema,
7
7
  name: Type.String({
8
8
  title: 'Name'
9
9
  }),
@@ -24,4 +24,4 @@ export const OrganizationSchema = Type.Object({
24
24
  title: 'Authentication Token'
25
25
  }))
26
26
  });
27
- export const OrganizationSpec = entityUtils.getModelSpec(OrganizationSchema, { isAuditable: true });
27
+ export const OrganizationSpec = entityUtils.getModelSpec(OrganizationSchema, { isAuditable: true, isEntity: false });
@@ -6,6 +6,7 @@ import { IModelSpec } from '../models/model-spec.interface.js';
6
6
  declare function getValidator(schema: TSchema): ReturnType<typeof TypeCompiler.Compile>;
7
7
  declare function getModelSpec<T extends TSchema>(schema: T, options?: {
8
8
  isAuditable?: boolean;
9
+ isEntity?: boolean;
9
10
  }): IModelSpec;
10
11
  declare function validate(validator: ReturnType<typeof TypeCompiler.Compile>, data: unknown): ValueError[] | null;
11
12
  declare function handleValidationResult(validationErrors: ValueError[] | null, methodName: string): void;
@@ -9,11 +9,14 @@ function getValidator(schema) {
9
9
  return validator;
10
10
  }
11
11
  function getModelSpec(schema, options = {}) {
12
+ const { isAuditable = false, isEntity = true } = options;
12
13
  const partialSchema = Type.Partial(schema);
13
14
  const schemasToIntersect = [];
14
15
  schemasToIntersect.push(schema);
15
- schemasToIntersect.push(EntitySchema);
16
- if (options.isAuditable) {
16
+ if (isEntity) {
17
+ schemasToIntersect.push(EntitySchema);
18
+ }
19
+ if (isAuditable) {
17
20
  schemasToIntersect.push(AuditableSchema);
18
21
  }
19
22
  const fullSchema = Type.Intersect(schemasToIntersect);
@@ -1,4 +1,6 @@
1
1
  import { Kind, TSchema, NumberOptions } from '@sinclair/typebox';
2
+ export declare const setIdSchema: (schema: TSchema) => void;
3
+ export declare const getIdSchema: () => TSchema;
2
4
  export declare function TypeboxIsoDate(options?: object): import("@sinclair/typebox").TTransform<import("@sinclair/typebox").TString, Date>;
3
5
  export declare function TypeboxDate(options?: object): import("@sinclair/typebox").TTransform<import("@sinclair/typebox").TString, Date>;
4
6
  export declare function TypeboxObjectId(options?: object): import("@sinclair/typebox").TString;
@@ -2,6 +2,22 @@ import { Type, Kind, ValueGuard } from '@sinclair/typebox';
2
2
  import { TypeRegistry } from '@sinclair/typebox';
3
3
  import { Decimal as _Decimal } from 'decimal.js';
4
4
  import { Value } from '@sinclair/typebox/value';
5
+ let idSchemaInstance;
6
+ export const setIdSchema = (schema) => {
7
+ if (idSchemaInstance) {
8
+ throw new Error('IdSchema has already been initialized and cannot be set again.');
9
+ }
10
+ if (!schema) {
11
+ throw new Error('Schema cannot be null or undefined.');
12
+ }
13
+ idSchemaInstance = schema;
14
+ };
15
+ export const getIdSchema = () => {
16
+ if (!idSchemaInstance) {
17
+ throw new Error('IdSchema has not been initialized. Please call setIdSchema() at application startup.');
18
+ }
19
+ return idSchemaInstance;
20
+ };
5
21
  export function TypeboxIsoDate(options = {}) {
6
22
  const dateTransform = Type.Transform(Type.String({ format: 'date-time', ...options }))
7
23
  .Decode(value => new Date(value))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loomcore/common",
3
- "version": "0.0.38",
3
+ "version": "0.0.40",
4
4
  "private": false,
5
5
  "description": "Loom Core Models- common models, interfaces, types, and utils for Loom Core. All things common to both api and client apps.",
6
6
  "scripts": {
@@ -1,2 +0,0 @@
1
- export declare let IdSchema: import("@sinclair/typebox").TUnion<[import("@sinclair/typebox").TString, import("@sinclair/typebox").TNumber]>;
2
- export declare function setIdSchema(schema: typeof IdSchema): void;
@@ -1,8 +0,0 @@
1
- import { Type } from '@sinclair/typebox';
2
- export let IdSchema = Type.Union([
3
- Type.String({ title: 'ID' }),
4
- Type.Number({ title: 'ID', integer: true, minimum: 1 })
5
- ]);
6
- export function setIdSchema(schema) {
7
- IdSchema = schema;
8
- }