@kitledger/core 0.0.6 → 0.0.8

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.
package/dist/art.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare function getAsciiLogo(): string;
package/dist/art.js ADDED
@@ -0,0 +1,37 @@
1
+ export function getAsciiLogo() {
2
+ return `.................................................................
3
+ .................................................................
4
+ .................................................................
5
+ .................................................................
6
+ .................................................................
7
+ .................................................................
8
+ .................................................................
9
+ .................................................................
10
+ ............################.......################-.............
11
+ ............#++++++++++###.......###++++++++++###................
12
+ ............#++++++++###.......###++++++++++###..................
13
+ ............#++++++###.......###++++++++++###....................
14
+ ............#++++###.......###++++++++++###......................
15
+ ............#++###.......###++++++++++###........................
16
+ ............####.......###++++++++++###..........................
17
+ ............##.......###++++++++++###............................
18
+ ...................###++++++++++###..............................
19
+ .................###++++++++++###................................
20
+ ...............###++++++++++###..................................
21
+ .............###++++++++++###....................................
22
+ ............##++++++++++###........####..........................
23
+ ............#+++++++++###........###++###........................
24
+ ............#++++++++##........###++++++###......................
25
+ ............#++++++++#.......###++++++++++###....................
26
+ ............#++++++++###.......###++++++++++###..................
27
+ ............#++++++++++###.......###++++++++++###................
28
+ ............################.......################..............
29
+ .................................................................
30
+ .................................................................
31
+ .................................................................
32
+ .................................................................
33
+ .................................................................
34
+ .................................................................
35
+ .................................................................
36
+ .................................................................`;
37
+ }
package/dist/db.d.ts CHANGED
@@ -2,26 +2,19 @@ import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
2
2
  import postgres from "postgres";
3
3
  import * as v from "valibot";
4
4
  import * as schema from "./schema.js";
5
- type DbOptions = {
5
+ export type KitledgerDbOptions = {
6
6
  url: string;
7
7
  ssl?: boolean;
8
8
  max?: number;
9
- migrations_table?: string;
10
- migrations_schema?: string;
9
+ migrationsTable?: string;
10
+ migrationsSchema?: string;
11
+ autoMigrate?: boolean;
11
12
  };
12
13
  export type KitledgerDb = PostgresJsDatabase<typeof schema> & {
13
14
  $client: postgres.Sql<{}>;
14
15
  };
15
- export declare function initializeDatabase(options: DbOptions): Promise<PostgresJsDatabase<typeof schema> & {
16
- $client: postgres.Sql<{}>;
17
- }>;
18
- /**
19
- * Common database helper for timestamps
20
- */
21
- export declare const timestamps: {
22
- created_at: import("drizzle-orm").NotNull<import("drizzle-orm").HasDefault<import("drizzle-orm/pg-core").PgTimestampBuilderInitial<"created_at">>>;
23
- updated_at: import("drizzle-orm/pg-core").PgTimestampBuilderInitial<"updated_at">;
24
- };
16
+ export declare function runMigrations(db: KitledgerDb, migrationsTable: string, migrationsSchema: string): Promise<void>;
17
+ export declare function initializeDatabase(options: KitledgerDbOptions): Promise<KitledgerDb>;
25
18
  /**
26
19
  * Supported operation types for get operations.
27
20
  */
@@ -75,4 +68,3 @@ export declare const maxLimit = 1000;
75
68
  * Default value for pagination offset.
76
69
  */
77
70
  export declare const defaultOffset = 0;
78
- export {};
package/dist/db.js CHANGED
@@ -1,11 +1,16 @@
1
- import { timestamp } from "drizzle-orm/pg-core";
2
1
  import { drizzle } from "drizzle-orm/postgres-js";
3
2
  import { migrate } from "drizzle-orm/postgres-js/migrator";
3
+ import { dirname, resolve } from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ import postgres from "postgres";
4
6
  import * as v from "valibot";
5
7
  import * as schema from "./schema.js";
6
- async function runMigrations(db, migrationsTable, migrationsSchema) {
8
+ const __filename = fileURLToPath(import.meta.url);
9
+ const __dirname = dirname(__filename);
10
+ export async function runMigrations(db, migrationsTable, migrationsSchema) {
11
+ const migrationsPath = resolve(__dirname, "../dist/migrations");
7
12
  await migrate(db, {
8
- migrationsFolder: "./migrations",
13
+ migrationsFolder: migrationsPath,
9
14
  migrationsTable: migrationsTable,
10
15
  migrationsSchema: migrationsSchema,
11
16
  });
@@ -15,23 +20,34 @@ export async function initializeDatabase(options) {
15
20
  url: options.url,
16
21
  ssl: options.ssl ? options.ssl : false,
17
22
  max: options.max ? options.max : 10,
23
+ autoMigrate: options.autoMigrate ?? true,
18
24
  };
25
+ const queryClient = postgres(dbConfig.url, {
26
+ ssl: dbConfig.ssl,
27
+ max: dbConfig.max,
28
+ onnotice: (msg) => {
29
+ /**
30
+ * Ignore notices about skipping already applied migrations
31
+ */
32
+ if (!msg.message.includes("skipping")) {
33
+ console.log("Kitledger Postgres notice:", msg);
34
+ }
35
+ },
36
+ });
19
37
  const db = drizzle({
20
- connection: dbConfig,
38
+ client: queryClient,
21
39
  schema: schema,
22
40
  });
23
- const migrationsTable = options.migrations_table || "schema_history";
24
- const migrationsSchema = options.migrations_schema || "public";
25
- await runMigrations(db, migrationsTable, migrationsSchema);
41
+ const migrationsTable = options.migrationsTable || "schema_history";
42
+ const migrationsSchema = options.migrationsSchema || "public";
43
+ /**
44
+ * Auto-migrate database schema if enabled
45
+ */
46
+ if (dbConfig.autoMigrate) {
47
+ await runMigrations(db, migrationsTable, migrationsSchema);
48
+ }
26
49
  return db;
27
50
  }
28
- /**
29
- * Common database helper for timestamps
30
- */
31
- export const timestamps = {
32
- created_at: timestamp("created_at", { mode: "date" }).defaultNow().notNull(),
33
- updated_at: timestamp("updated_at", { mode: "date" }),
34
- };
35
51
  /**
36
52
  * Supported operation types for get operations.
37
53
  */
@@ -4,13 +4,13 @@ export declare enum EntityModelStatus {
4
4
  INACTIVE = 1
5
5
  }
6
6
  export type InferEntityMetaType<TFields extends readonly Field[]> = {
7
- [K in TFields[number] as K["ref_id"]]: K["__primitive_type"];
7
+ [K in TFields[number] as K["refId"]]: K["__valueType"];
8
8
  };
9
9
  export type Entity<TData = Record<string, any>> = {
10
10
  id: string;
11
- model_ref_id: string;
12
- created_at: Date;
13
- updated_at: Date;
11
+ modelRefId: string;
12
+ createdAt: Date;
13
+ updatedAt: Date;
14
14
  data: TData;
15
15
  };
16
16
  export type EntityHook<TData> = (entity: Entity<TData>) => Promise<Entity<TData>>;
@@ -23,19 +23,21 @@ export type EntityHooks<TData = Record<string, any>> = {
23
23
  deleted?: EntityHook<TData>[];
24
24
  };
25
25
  export type EntityModel = {
26
- ref_id: string;
27
- alt_id?: string;
26
+ refId: string;
27
+ altId?: string;
28
28
  name: string;
29
29
  status: EntityModelStatus;
30
30
  fields?: Field[];
31
31
  hooks?: EntityHooks<any>;
32
32
  };
33
33
  export type EntityModelOptions<TFields extends readonly Field[]> = {
34
- ref_id: string;
35
- alt_id?: string;
34
+ refId: string;
35
+ altId?: string;
36
36
  name: string;
37
37
  status?: EntityModelStatus;
38
38
  fields?: TFields;
39
39
  hooks?: EntityHooks<InferEntityMetaType<TFields>>;
40
40
  };
41
- export declare function defineEntityModel<const TFields extends readonly Field[]>(options: EntityModelOptions<TFields>): EntityModel;
41
+ export declare function defineEntityModel<const TFields extends readonly Field[]>(options: EntityModelOptions<TFields>): EntityModel & {
42
+ fields: TFields;
43
+ };
package/dist/entities.js CHANGED
@@ -4,11 +4,10 @@ export var EntityModelStatus;
4
4
  EntityModelStatus[EntityModelStatus["INACTIVE"] = 1] = "INACTIVE";
5
5
  })(EntityModelStatus || (EntityModelStatus = {}));
6
6
  export function defineEntityModel(options) {
7
- const entityModel = {
7
+ return {
8
8
  ...options,
9
9
  status: options.status ?? EntityModelStatus.ACTIVE,
10
10
  fields: options.fields,
11
11
  hooks: options.hooks,
12
12
  };
13
- return entityModel;
14
13
  }
package/dist/fields.d.ts CHANGED
@@ -8,13 +8,14 @@ export declare enum FieldType {
8
8
  RELATION = "relation"
9
9
  }
10
10
  interface BaseField {
11
- readonly ref_id: string;
11
+ readonly refId: string;
12
12
  name: string;
13
13
  description?: string;
14
+ required: boolean;
14
15
  }
15
16
  export interface TextField extends BaseField {
16
17
  type: FieldType.TEXT;
17
- readonly __primitive_type?: string;
18
+ readonly __valueType: string;
18
19
  maxLength?: number;
19
20
  format?: "email" | "plain" | "rich_text";
20
21
  }
@@ -29,31 +30,31 @@ export type NumberFormatting = {
29
30
  };
30
31
  export interface NumberField extends BaseField {
31
32
  type: FieldType.NUMBER;
32
- readonly __primitive_type?: number;
33
+ readonly __valueType: number;
33
34
  min?: number;
34
35
  max?: number;
35
36
  formatting: NumberFormatting;
36
37
  }
37
38
  export interface DateField extends BaseField {
38
39
  type: FieldType.DATE;
39
- readonly __primitive_type?: Date;
40
+ readonly __valueType: Date;
40
41
  includeTime: boolean;
41
42
  formatStr: string;
42
43
  }
43
44
  export interface BooleanField extends BaseField {
44
45
  type: FieldType.BOOLEAN;
45
- readonly __primitive_type?: boolean;
46
+ readonly __valueType: boolean;
46
47
  defaultValue?: boolean;
47
48
  }
48
49
  export interface URLField extends BaseField {
49
50
  type: FieldType.URL;
50
- readonly __primitive_type?: string;
51
+ readonly __valueType: string;
51
52
  defaultValue?: string;
52
53
  }
53
54
  export type QueryConfig = {};
54
55
  export interface SelectField extends BaseField {
55
56
  type: FieldType.SELECT;
56
- readonly __primitive_type?: string | number | (string | number)[];
57
+ readonly __valueType: string | number | (string | number)[];
57
58
  multiSelect: boolean;
58
59
  items: Array<{
59
60
  label: string;
@@ -64,53 +65,81 @@ export interface SelectField extends BaseField {
64
65
  }
65
66
  export interface RelationField extends BaseField {
66
67
  type: FieldType.RELATION;
67
- readonly __primitive_type?: any;
68
+ readonly __valueType: any;
68
69
  multiSelect: boolean;
69
70
  targetEntityId: string;
70
71
  displayFieldId: string;
71
72
  query: QueryConfig;
72
73
  }
73
74
  export type Field = TextField | NumberField | DateField | BooleanField | URLField | SelectField | RelationField;
74
- export type TextFieldOptions = Omit<TextField, "type" | "__primitive_type" | "ref_id">;
75
- export type NumberFieldOptions = Omit<NumberField, "type" | "__primitive_type" | "ref_id">;
76
- export type DateFieldOptions = Omit<DateField, "type" | "__primitive_type" | "ref_id">;
77
- export type BooleanFieldOptions = Omit<BooleanField, "type" | "__primitive_type" | "ref_id">;
78
- export type URLFieldOptions = Omit<URLField, "type" | "__primitive_type" | "ref_id">;
79
- export type SelectFieldOptions = Omit<SelectField, "type" | "__primitive_type" | "ref_id">;
80
- export type RelationFieldOptions = Omit<RelationField, "type" | "__primitive_type" | "ref_id">;
81
- export declare function defineTextField<const T extends string>(options: TextFieldOptions & {
82
- ref_id: T;
75
+ export type TextFieldOptions = Omit<TextField, "type" | "__valueType" | "refId" | "required"> & {
76
+ required?: boolean;
77
+ };
78
+ export type NumberFieldOptions = Omit<NumberField, "type" | "__valueType" | "refId" | "required"> & {
79
+ required?: boolean;
80
+ };
81
+ export type DateFieldOptions = Omit<DateField, "type" | "__valueType" | "refId" | "required"> & {
82
+ required?: boolean;
83
+ };
84
+ export type BooleanFieldOptions = Omit<BooleanField, "type" | "__valueType" | "refId" | "required"> & {
85
+ required?: boolean;
86
+ };
87
+ export type URLFieldOptions = Omit<URLField, "type" | "__valueType" | "refId" | "required"> & {
88
+ required?: boolean;
89
+ };
90
+ export type SelectFieldOptions = Omit<SelectField, "type" | "__valueType" | "refId" | "required"> & {
91
+ required?: boolean;
92
+ };
93
+ export type RelationFieldOptions = Omit<RelationField, "type" | "__valueType" | "refId" | "required"> & {
94
+ required?: boolean;
95
+ };
96
+ export declare function defineTextField<const T extends string, const R extends boolean = false>(options: TextFieldOptions & {
97
+ refId: T;
98
+ required?: R;
83
99
  }): TextField & {
84
- ref_id: T;
100
+ refId: T;
101
+ required: R;
85
102
  };
86
- export declare function defineNumberField<const T extends string>(options: NumberFieldOptions & {
87
- ref_id: T;
103
+ export declare function defineNumberField<const T extends string, const R extends boolean = false>(options: NumberFieldOptions & {
104
+ refId: T;
105
+ required?: R;
88
106
  }): NumberField & {
89
- ref_id: T;
107
+ refId: T;
108
+ required: R;
90
109
  };
91
- export declare function defineDateField<const T extends string>(options: DateFieldOptions & {
92
- ref_id: T;
110
+ export declare function defineDateField<const T extends string, const R extends boolean = false>(options: DateFieldOptions & {
111
+ refId: T;
112
+ required?: R;
93
113
  }): DateField & {
94
- ref_id: T;
114
+ refId: T;
115
+ required: R;
95
116
  };
96
- export declare function defineBooleanField<const T extends string>(options: BooleanFieldOptions & {
97
- ref_id: T;
117
+ export declare function defineBooleanField<const T extends string, const R extends boolean = false>(options: BooleanFieldOptions & {
118
+ refId: T;
119
+ required?: R;
98
120
  }): BooleanField & {
99
- ref_id: T;
121
+ refId: T;
122
+ required: R;
100
123
  };
101
- export declare function defineURLField<const T extends string>(options: URLFieldOptions & {
102
- ref_id: T;
124
+ export declare function defineURLField<const T extends string, const R extends boolean = false>(options: URLFieldOptions & {
125
+ refId: T;
126
+ required?: R;
103
127
  }): URLField & {
104
- ref_id: T;
128
+ refId: T;
129
+ required: R;
105
130
  };
106
- export declare function defineSelectField<const T extends string>(options: SelectFieldOptions & {
107
- ref_id: T;
131
+ export declare function defineSelectField<const T extends string, const R extends boolean = false>(options: SelectFieldOptions & {
132
+ refId: T;
133
+ required?: R;
108
134
  }): SelectField & {
109
- ref_id: T;
135
+ refId: T;
136
+ required: R;
110
137
  };
111
- export declare function defineRelationField<const T extends string>(options: RelationFieldOptions & {
112
- ref_id: T;
138
+ export declare function defineRelationField<const T extends string, const R extends boolean = false>(options: RelationFieldOptions & {
139
+ refId: T;
140
+ required?: R;
113
141
  }): RelationField & {
114
- ref_id: T;
142
+ refId: T;
143
+ required: R;
115
144
  };
116
145
  export {};
package/dist/fields.js CHANGED
@@ -9,23 +9,58 @@ export var FieldType;
9
9
  FieldType["RELATION"] = "relation";
10
10
  })(FieldType || (FieldType = {}));
11
11
  export function defineTextField(options) {
12
- return { type: FieldType.TEXT, ...options };
12
+ const { required = false, ...rest } = options;
13
+ return {
14
+ type: FieldType.TEXT,
15
+ required,
16
+ ...rest,
17
+ };
13
18
  }
14
19
  export function defineNumberField(options) {
15
- return { type: FieldType.NUMBER, ...options };
20
+ const { required = false, ...rest } = options;
21
+ return {
22
+ type: FieldType.NUMBER,
23
+ required,
24
+ ...rest,
25
+ };
16
26
  }
17
27
  export function defineDateField(options) {
18
- return { type: FieldType.DATE, ...options };
28
+ const { required = false, ...rest } = options;
29
+ return {
30
+ type: FieldType.DATE,
31
+ required,
32
+ ...rest,
33
+ };
19
34
  }
20
35
  export function defineBooleanField(options) {
21
- return { type: FieldType.BOOLEAN, ...options };
36
+ const { required = false, ...rest } = options;
37
+ return {
38
+ type: FieldType.BOOLEAN,
39
+ required,
40
+ ...rest,
41
+ };
22
42
  }
23
43
  export function defineURLField(options) {
24
- return { type: FieldType.URL, ...options };
44
+ const { required = false, ...rest } = options;
45
+ return {
46
+ type: FieldType.URL,
47
+ required,
48
+ ...rest,
49
+ };
25
50
  }
26
51
  export function defineSelectField(options) {
27
- return { type: FieldType.SELECT, ...options };
52
+ const { required = false, ...rest } = options;
53
+ return {
54
+ type: FieldType.SELECT,
55
+ required,
56
+ ...rest,
57
+ };
28
58
  }
29
59
  export function defineRelationField(options) {
30
- return { type: FieldType.RELATION, ...options };
60
+ const { required = false, ...rest } = options;
61
+ return {
62
+ type: FieldType.RELATION,
63
+ required,
64
+ ...rest,
65
+ };
31
66
  }
@@ -0,0 +1,67 @@
1
+ import type { EntityModel } from "./entities.js";
2
+ import type { Field } from "./fields.js";
3
+ import type { TransactionModel } from "./transactions.js";
4
+ import type { UnitModel } from "./units.js";
5
+ export declare enum FormType {
6
+ ENTITY = "ENTITY",
7
+ TRANSACTION = "TRANSACTION",
8
+ UNIT = "UNIT"
9
+ }
10
+ export type FormFieldDisplay = "normal" | "inline" | "disabled" | "hidden";
11
+ export type FormFieldConfig = {
12
+ label?: string;
13
+ description?: string;
14
+ display?: FormFieldDisplay;
15
+ };
16
+ type InferFieldConfig<TFields extends readonly Field[]> = {
17
+ [K in TFields[number] as K["refId"]]?: FormFieldConfig & {
18
+ required?: K["required"] extends true ? true : boolean;
19
+ };
20
+ };
21
+ export interface BaseForm {
22
+ refId: string;
23
+ modelRefId: string;
24
+ name: string;
25
+ description?: string;
26
+ fieldOrder?: string[];
27
+ }
28
+ export type EntityFormOptions<TModel extends {
29
+ fields?: readonly any[];
30
+ }> = {
31
+ name: string;
32
+ description?: string;
33
+ refId: string;
34
+ fields: InferFieldConfig<NonNullable<TModel["fields"]>>;
35
+ };
36
+ export type UnitFormOptions<TModel extends {
37
+ fields?: readonly any[];
38
+ }> = {
39
+ name: string;
40
+ description?: string;
41
+ refId: string;
42
+ fields: InferFieldConfig<NonNullable<TModel["fields"]>>;
43
+ };
44
+ export type TransactionFormOptions<TModel extends {
45
+ fields?: readonly any[];
46
+ }> = {
47
+ name: string;
48
+ description?: string;
49
+ refId: string;
50
+ fields: InferFieldConfig<NonNullable<TModel["fields"]>>;
51
+ };
52
+ export interface TransactionForm<TModel extends TransactionModel> extends BaseForm {
53
+ type: FormType.TRANSACTION;
54
+ fields: InferFieldConfig<NonNullable<TModel["fields"]>>;
55
+ }
56
+ export interface EntityForm<TModel extends EntityModel> extends BaseForm {
57
+ type: FormType.ENTITY;
58
+ fields: InferFieldConfig<NonNullable<TModel["fields"]>>;
59
+ }
60
+ export interface UnitForm<TModel extends UnitModel> extends BaseForm {
61
+ type: FormType.UNIT;
62
+ fields: InferFieldConfig<NonNullable<TModel["fields"]>>;
63
+ }
64
+ export declare function defineTransactionForm<const TModel extends TransactionModel>(model: TModel, config: TransactionFormOptions<TModel>): TransactionForm<TModel>;
65
+ export declare function defineEntityForm<const TModel extends EntityModel>(model: TModel, config: EntityFormOptions<TModel>): EntityForm<TModel>;
66
+ export declare function defineUnitForm<const TModel extends UnitModel>(model: TModel, config: UnitFormOptions<TModel>): UnitForm<TModel>;
67
+ export {};
package/dist/forms.js ADDED
@@ -0,0 +1,38 @@
1
+ // --- CORE TYPES ---
2
+ export var FormType;
3
+ (function (FormType) {
4
+ FormType["ENTITY"] = "ENTITY";
5
+ FormType["TRANSACTION"] = "TRANSACTION";
6
+ FormType["UNIT"] = "UNIT";
7
+ })(FormType || (FormType = {}));
8
+ // --- FACTORIES (Pure Configuration) ---
9
+ export function defineTransactionForm(model, config) {
10
+ return {
11
+ refId: config.refId,
12
+ name: config.name,
13
+ description: config.description,
14
+ type: FormType.TRANSACTION,
15
+ modelRefId: model.refId,
16
+ fields: config.fields,
17
+ };
18
+ }
19
+ export function defineEntityForm(model, config) {
20
+ return {
21
+ refId: config.refId,
22
+ name: config.name,
23
+ description: config.description,
24
+ type: FormType.ENTITY,
25
+ modelRefId: model.refId,
26
+ fields: config.fields,
27
+ };
28
+ }
29
+ export function defineUnitForm(model, config) {
30
+ return {
31
+ refId: config.refId,
32
+ name: config.name,
33
+ description: config.description,
34
+ type: FormType.UNIT,
35
+ modelRefId: model.refId,
36
+ fields: config.fields,
37
+ };
38
+ }
package/dist/main.d.ts CHANGED
@@ -1,7 +1,15 @@
1
+ import type { TransactionForm, EntityForm, UnitForm } from "./forms.js";
2
+ import { KitledgerDb } from "./db.js";
1
3
  import { EntityModel } from "./entities.js";
2
4
  import { TransactionModel } from "./transactions.js";
5
+ import { UnitModel } from "./units.js";
3
6
  export interface KitledgerConfig {
4
- transactionModels: TransactionModel[];
7
+ database: KitledgerDb;
5
8
  entityModels: EntityModel[];
9
+ transactionModels: TransactionModel[];
10
+ unitModels: UnitModel[];
11
+ transactionForms?: TransactionForm<TransactionModel>[];
12
+ entityForms?: EntityForm<EntityModel>[];
13
+ unitForms?: UnitForm<UnitModel>[];
6
14
  }
7
15
  export declare function defineConfig(config: KitledgerConfig): KitledgerConfig;