@tstdl/base 0.91.28 → 0.91.30

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.
Files changed (52) hide show
  1. package/.eslintrc.json +5 -1
  2. package/{reflection/reflection-data-map.d.ts → data-structures/context-data-map.d.ts} +3 -2
  3. package/{reflection/reflection-data-map.js → data-structures/context-data-map.js} +13 -6
  4. package/data-structures/index.d.ts +1 -0
  5. package/data-structures/index.js +1 -0
  6. package/examples/orm/drizzle.config.d.ts +2 -0
  7. package/examples/orm/drizzle.config.js +5 -0
  8. package/examples/orm/schemas.d.ts +3 -0
  9. package/examples/orm/schemas.js +4 -0
  10. package/examples/orm/test.js +10 -0
  11. package/examples/orm/user.model.d.ts +9 -0
  12. package/examples/orm/user.model.js +39 -0
  13. package/key-value-store/index.d.ts +0 -1
  14. package/key-value-store/index.js +0 -1
  15. package/key-value-store/key-value.store.d.ts +9 -10
  16. package/key-value-store/mongo/mongo-key-value.store.js +2 -1
  17. package/orm/database-schema.d.ts +8 -0
  18. package/orm/database-schema.js +13 -0
  19. package/orm/decorators.d.ts +26 -0
  20. package/orm/decorators.js +21 -2
  21. package/orm/drizzle/schema-converter.d.ts +19 -0
  22. package/orm/drizzle/schema-converter.js +116 -0
  23. package/orm/entity.d.ts +6 -2
  24. package/orm/entity.js +6 -6
  25. package/orm/index.d.ts +1 -2
  26. package/orm/index.js +1 -2
  27. package/orm/repository.d.ts +27 -24
  28. package/orm/repository.js +124 -7
  29. package/orm/schemas/index.d.ts +1 -0
  30. package/orm/schemas/index.js +1 -0
  31. package/orm/schemas/uuid.d.ts +11 -0
  32. package/orm/schemas/uuid.js +16 -0
  33. package/orm/types.d.ts +20 -7
  34. package/orm/types.js +3 -5
  35. package/package.json +6 -3
  36. package/reflection/registry.d.ts +9 -6
  37. package/reflection/registry.js +25 -14
  38. package/schema/decorators/types.d.ts +1 -0
  39. package/schema/schema.d.ts +1 -1
  40. package/schema/schemas/number.d.ts +1 -0
  41. package/schema/schemas/number.js +2 -0
  42. package/schema/schemas/string.d.ts +1 -1
  43. package/utils/math.d.ts +11 -1
  44. package/utils/math.js +28 -0
  45. package/utils/string/index.d.ts +1 -0
  46. package/utils/string/index.js +1 -0
  47. package/utils/string/snake-case.d.ts +1 -0
  48. package/utils/string/snake-case.js +4 -0
  49. package/orm/schema-converter.d.ts +0 -99
  50. package/orm/schema-converter.js +0 -74
  51. package/orm/schema.d.ts +0 -3
  52. /package/{orm/schema.js → examples/orm/test.d.ts} +0 -0
package/.eslintrc.json CHANGED
@@ -149,7 +149,11 @@
149
149
  "@stylistic/quotes": ["warn", "single"],
150
150
  "@stylistic/semi": ["warn", "always"],
151
151
  "@stylistic/multiline-ternary": ["warn", "always-multiline"],
152
- "@stylistic/generator-star-spacing": ["warn", { "before": false, "after": true }],
152
+ "@stylistic/generator-star-spacing": ["error", {
153
+ "named": "before",
154
+ "anonymous": "neither",
155
+ "method": "before"
156
+ }],
153
157
  "@stylistic/indent": ["off"],
154
158
  "@stylistic/max-len": ["off"],
155
159
  "@stylistic/object-curly-spacing": ["warn", "always"],
@@ -1,10 +1,11 @@
1
1
  import type { Record } from '../types.js';
2
- export declare class ReflectionDataMap {
2
+ export declare class ContextDataMap {
3
3
  private readonly data;
4
4
  constructor();
5
5
  has(key: PropertyKey): boolean;
6
6
  tryGet<T>(key: PropertyKey): T | undefined;
7
+ tryGet<T, D>(key: PropertyKey, defaultValue: D): T | D;
7
8
  get<T>(key: PropertyKey): T;
8
9
  set(key: PropertyKey, value: any, mergeValue?: boolean): void;
9
- setMany(data: Record, mergeValues?: boolean): void;
10
+ setMany(data: Record | readonly [PropertyKey, any][], mergeValues?: boolean): void;
10
11
  }
@@ -1,7 +1,7 @@
1
1
  import { merge } from '../utils/merge.js';
2
2
  import { objectEntries } from '../utils/object/object.js';
3
- import { isUndefined } from '../utils/type-guards.js';
4
- export class ReflectionDataMap {
3
+ import { isArray, isUndefined } from '../utils/type-guards.js';
4
+ export class ContextDataMap {
5
5
  data;
6
6
  constructor() {
7
7
  this.data = new Map();
@@ -9,13 +9,17 @@ export class ReflectionDataMap {
9
9
  has(key) {
10
10
  return this.data.has(key);
11
11
  }
12
- tryGet(key) {
13
- return this.data.get(key);
12
+ tryGet(key, defaultValue) {
13
+ const value = this.data.get(key);
14
+ if (isUndefined(value)) {
15
+ return defaultValue;
16
+ }
17
+ return value;
14
18
  }
15
19
  get(key) {
16
20
  const data = this.tryGet(key);
17
21
  if (isUndefined(data)) {
18
- throw new Error(`No data for ${String(key)} available.`);
22
+ throw new Error(`No data for key "${String(key)}" available.`);
19
23
  }
20
24
  return data;
21
25
  }
@@ -29,7 +33,10 @@ export class ReflectionDataMap {
29
33
  this.data.set(key, newData);
30
34
  }
31
35
  setMany(data, mergeValues = false) {
32
- for (const [key, value] of objectEntries(data)) {
36
+ const entries = isArray(data)
37
+ ? data
38
+ : objectEntries(data);
39
+ for (const [key, value] of entries) {
33
40
  this.set(key, value, mergeValues);
34
41
  }
35
42
  }
@@ -8,6 +8,7 @@ export * from './array-list.js';
8
8
  export * from './cache.js';
9
9
  export * from './circular-buffer.js';
10
10
  export * from './collection.js';
11
+ export * from './context-data-map.js';
11
12
  export * from './dictionary.js';
12
13
  export * from './distinct-collection.js';
13
14
  export * from './index-out-of-bounds.error.js';
@@ -8,6 +8,7 @@ export * from './array-list.js';
8
8
  export * from './cache.js';
9
9
  export * from './circular-buffer.js';
10
10
  export * from './collection.js';
11
+ export * from './context-data-map.js';
11
12
  export * from './dictionary.js';
12
13
  export * from './distinct-collection.js';
13
14
  export * from './index-out-of-bounds.error.js';
@@ -0,0 +1,2 @@
1
+ declare const _default: import("drizzle-kit").Config;
2
+ export default _default;
@@ -0,0 +1,5 @@
1
+ import { defineConfig } from 'drizzle-kit';
2
+ export default defineConfig({
3
+ dialect: 'postgresql',
4
+ schema: './schemas.js'
5
+ });
@@ -0,0 +1,3 @@
1
+ import { User } from './user.model.js';
2
+ export declare const mySchema: import("../../orm/database-schema.js").DatabaseSchema<"my_application">;
3
+ export declare const user: import("../../orm/index.js").PgTableFromType<"my_application", typeof User>;
@@ -0,0 +1,4 @@
1
+ import { databaseSchema } from '../../orm/database-schema.js';
2
+ import { User } from './user.model.js';
3
+ export const mySchema = databaseSchema('my_application');
4
+ export const user = mySchema.getTable(User);
@@ -0,0 +1,10 @@
1
+ import { eq } from 'drizzle-orm';
2
+ import { drizzle } from 'drizzle-orm/node-postgres';
3
+ import { pgSchema, uuid } from 'drizzle-orm/pg-core';
4
+ import * as schema from './schemas.js';
5
+ const db = drizzle('', { schema });
6
+ const query = eq(schema.user, 1);
7
+ const x = pgSchema('').table('test', {
8
+ id: uuid().defaultRandom().primaryKey()
9
+ });
10
+ await db.insert(x).values({});
@@ -0,0 +1,9 @@
1
+ import { Entity, Integer } from '../../orm/index.js';
2
+ export declare class User extends Entity {
3
+ static entityName: string;
4
+ name: string;
5
+ nickNames: string[];
6
+ age: Integer | null;
7
+ hasAge: boolean;
8
+ mail: string;
9
+ }
@@ -0,0 +1,39 @@
1
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
6
+ };
7
+ var __metadata = (this && this.__metadata) || function (k, v) {
8
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
+ };
10
+ import { Entity, Integer, Unique } from '../../orm/index.js';
11
+ import { Array, Property } from '../../schema/index.js';
12
+ export class User extends Entity {
13
+ static entityName = 'User';
14
+ name;
15
+ nickNames;
16
+ age;
17
+ hasAge;
18
+ mail;
19
+ }
20
+ __decorate([
21
+ Property(),
22
+ __metadata("design:type", String)
23
+ ], User.prototype, "name", void 0);
24
+ __decorate([
25
+ Array(String),
26
+ __metadata("design:type", Array)
27
+ ], User.prototype, "nickNames", void 0);
28
+ __decorate([
29
+ Integer({ nullable: true }),
30
+ __metadata("design:type", Object)
31
+ ], User.prototype, "age", void 0);
32
+ __decorate([
33
+ Property(),
34
+ __metadata("design:type", Boolean)
35
+ ], User.prototype, "hasAge", void 0);
36
+ __decorate([
37
+ Unique(),
38
+ __metadata("design:type", String)
39
+ ], User.prototype, "mail", void 0);
@@ -5,4 +5,3 @@
5
5
  */
6
6
  export * from './key-value-store.provider.js';
7
7
  export * from './key-value.store.js';
8
- export * from './mongo/index.js';
@@ -5,4 +5,3 @@
5
5
  */
6
6
  export * from './key-value-store.provider.js';
7
7
  export * from './key-value.store.js';
8
- export * from './mongo/index.js';
@@ -1,24 +1,23 @@
1
- import type { Resolvable } from '../injector/interfaces.js';
2
- import { resolveArgumentType } from '../injector/interfaces.js';
1
+ import { type Resolvable, resolveArgumentType } from '../injector/interfaces.js';
3
2
  import type { StringMap } from '../types.js';
4
- /** key value store module */
3
+ /** Key value store module */
5
4
  export type KeyValueStoreArgument = string;
6
5
  export declare abstract class KeyValueStore<KV extends StringMap> implements Resolvable<KeyValueStoreArgument> {
7
6
  readonly module: string;
8
7
  readonly [resolveArgumentType]: KeyValueStoreArgument;
9
8
  constructor(module: string);
10
- /** get value of key */
9
+ /** Get value of key */
11
10
  abstract get<K extends keyof KV>(key: K): Promise<KV[K] | undefined>;
12
- /** get value of key */
11
+ /** Get value of key */
13
12
  abstract get<K extends keyof KV, D>(key: K, defaultValue: D): Promise<KV[K] | D>;
14
- /** set key */
13
+ /** Set key */
15
14
  abstract set<K extends keyof KV>(key: K, value: KV[K]): Promise<void>;
16
- /** set multiple keys */
15
+ /** Set multiple keys */
17
16
  abstract setMany(keyValues: Partial<KV>): Promise<void>;
18
- /** delete key */
17
+ /** Delete key */
19
18
  abstract delete(key: keyof KV): Promise<boolean>;
20
- /** delete multiple keys */
19
+ /** Delete multiple keys */
21
20
  abstract deleteMany(keys: (keyof KV)[]): Promise<void>;
22
- /** delete all keys */
21
+ /** Delete all keys */
23
22
  abstract clear(): Promise<void>;
24
23
  }
@@ -9,10 +9,11 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  };
10
10
  import { getNewId } from '../../database/index.js';
11
11
  import { Singleton } from '../../injector/index.js';
12
- import { KeyValueStore, MongoKeyValueStoreProvider } from '../../key-value-store/index.js';
12
+ import { KeyValueStore } from '../../key-value-store/index.js';
13
13
  import { currentTimestamp } from '../../utils/date-time.js';
14
14
  import { objectEntries } from '../../utils/object/object.js';
15
15
  import { assertString, isUndefined } from '../../utils/type-guards.js';
16
+ import { MongoKeyValueStoreProvider } from './mongo-key-value-store.provider.js';
16
17
  import { MongoKeyValueRepository } from './mongo-key-value.repository.js';
17
18
  let MongoKeyValueStore = class MongoKeyValueStore extends KeyValueStore {
18
19
  keyValueRepository;
@@ -0,0 +1,8 @@
1
+ import { type PgTableFromType } from './drizzle/schema-converter.js';
2
+ import type { EntityType } from './entity.js';
3
+ export declare class DatabaseSchema<Name extends string> {
4
+ readonly name: Name;
5
+ constructor(name: Name);
6
+ getTable<T extends EntityType>(type: T): PgTableFromType<Name, T>;
7
+ }
8
+ export declare function databaseSchema<Name extends string>(name: Name): DatabaseSchema<Name>;
@@ -0,0 +1,13 @@
1
+ import { getDrizzleTableFromType } from './drizzle/schema-converter.js';
2
+ export class DatabaseSchema {
3
+ name;
4
+ constructor(name) {
5
+ this.name = name;
6
+ }
7
+ getTable(type) {
8
+ return getDrizzleTableFromType(this.name, type);
9
+ }
10
+ }
11
+ export function databaseSchema(name) {
12
+ return new DatabaseSchema(name);
13
+ }
@@ -1 +1,27 @@
1
+ export type OrmTableReflectionData = {
2
+ name?: string;
3
+ unique?: UniqueReflectionData[];
4
+ };
5
+ export type OrmColumnReflectionData = {
6
+ name?: string;
7
+ primaryKey?: boolean;
8
+ unique?: UniqueReflectionData;
9
+ uuid?: {
10
+ defaultRandom?: boolean;
11
+ };
12
+ };
13
+ type UniqueReflectionData = {
14
+ name?: string;
15
+ columns?: string[];
16
+ options?: {
17
+ nulls?: 'distinct' | 'not distinct';
18
+ };
19
+ };
20
+ export declare function createTableDecorator(data?: OrmTableReflectionData): ClassDecorator;
21
+ export declare function createColumnDecorator(data?: OrmColumnReflectionData): PropertyDecorator;
22
+ export declare function createTableAndColumnDecorator(data?: OrmColumnReflectionData): ClassDecorator & PropertyDecorator;
23
+ export declare function Column(options: OrmColumnReflectionData): PropertyDecorator;
1
24
  export declare function PrimaryKey(): PropertyDecorator;
25
+ export declare function Unique(name?: string, options?: UniqueReflectionData['options']): PropertyDecorator;
26
+ export declare function Unique(name: string | undefined, columns: [string, ...string[]], options?: UniqueReflectionData['options']): ClassDecorator;
27
+ export {};
package/orm/decorators.js CHANGED
@@ -1,4 +1,23 @@
1
- import { createPropertyDecorator } from '../reflection/utils.js';
1
+ import { createClassDecorator, createDecorator, createPropertyDecorator } from '../reflection/utils.js';
2
+ import { isArray } from '../utils/type-guards.js';
3
+ export function createTableDecorator(data) {
4
+ return createClassDecorator({ data: { orm: data }, mergeData: true });
5
+ }
6
+ export function createColumnDecorator(data) {
7
+ return createPropertyDecorator({ data: { orm: data }, mergeData: true });
8
+ }
9
+ export function createTableAndColumnDecorator(data) {
10
+ return createDecorator({ class: true, property: true, data: { orm: data }, mergeData: true });
11
+ }
12
+ export function Column(options) {
13
+ return createColumnDecorator({ ...options });
14
+ }
2
15
  export function PrimaryKey() {
3
- return createPropertyDecorator({ data: { orm: { primaryKey: true } }, mergeData: true });
16
+ return createColumnDecorator({ primaryKey: true });
17
+ }
18
+ export function Unique(name, columnsOrOptions, options) {
19
+ if (isArray(columnsOrOptions)) {
20
+ return createTableDecorator({ unique: [{ name, columns: columnsOrOptions, options }] });
21
+ }
22
+ return createColumnDecorator({ unique: { name, options: columnsOrOptions ?? options } });
4
23
  }
@@ -0,0 +1,19 @@
1
+ import type { BuildColumns, NotNull } from 'drizzle-orm';
2
+ import { type PgTableWithColumns } from 'drizzle-orm/pg-core';
3
+ import type { SnakeCase } from 'type-fest';
4
+ import type { Enumeration, Type } from '../../types.js';
5
+ import type { EntityType } from '../entity.js';
6
+ import type { ColumnBuilder } from '../types.js';
7
+ type Column<Name extends string, T> = null extends T ? ColumnBuilder<T, Name> : NotNull<ColumnBuilder<T, Name>>;
8
+ export declare const getDrizzleTableFromType: typeof _getDrizzleTableFromType;
9
+ export type PgTableFromType<S extends string, T extends Type, TableName extends string = T extends EntityType ? SnakeCase<T['entityName']> : string> = PgTableWithColumns<{
10
+ name: TableName;
11
+ schema: S;
12
+ columns: BuildColumns<TableName, {
13
+ [P in keyof InstanceType<T>]: Column<Extract<P, string>, InstanceType<T>[P]>;
14
+ }, 'pg'>;
15
+ dialect: 'pg';
16
+ }>;
17
+ export declare function _getDrizzleTableFromType<S extends string, T extends Type>(schemaName: S, type: T, tableName?: string): PgTableFromType<S, T>;
18
+ export declare function registerEnum(enumeration: Enumeration, name: string): void;
19
+ export {};
@@ -0,0 +1,116 @@
1
+ import { toSnakeCase } from 'drizzle-orm/casing';
2
+ import { boolean, doublePrecision, integer, pgSchema, text, unique, uuid } from 'drizzle-orm/pg-core';
3
+ import { NotSupportedError } from '../../errors/not-supported.error.js';
4
+ import { reflectionRegistry } from '../../reflection/registry.js';
5
+ import { ArraySchema, BooleanSchema, EnumerationSchema, getObjectSchema, NullableSchema, NumberSchema, OptionalSchema, StringSchema } from '../../schema/index.js';
6
+ import { enumValues } from '../../utils/enum.js';
7
+ import { memoize, memoizeSingle } from '../../utils/function/memoize.js';
8
+ import { fromEntries, objectEntries } from '../../utils/object/object.js';
9
+ import { assertDefinedPass, assertStringPass, isArray, isDefined, isString, isUndefined } from '../../utils/type-guards.js';
10
+ import { UuidSchema } from '../schemas/uuid.js';
11
+ const getDbSchema = memoizeSingle(pgSchema);
12
+ export const getDrizzleTableFromType = memoize(_getDrizzleTableFromType);
13
+ export function _getDrizzleTableFromType(schemaName, type, tableName = getDefaultTableName(type)) {
14
+ const metadata = reflectionRegistry.getMetadata(type);
15
+ if (isUndefined(metadata)) {
16
+ throw new Error('Type does not have reflection metadata.');
17
+ }
18
+ const dbSchema = getDbSchema(schemaName);
19
+ const tableReflectionData = metadata.data.tryGet('orm');
20
+ const objectSchema = getObjectSchema(type);
21
+ const entries = objectEntries(objectSchema.properties).map(([property, schema]) => {
22
+ const columnReflectionData = metadata.properties.get(property)?.data.tryGet('orm');
23
+ const propertyName = columnReflectionData?.name ?? assertStringPass(toSnakeCase(property));
24
+ return [
25
+ property,
26
+ getPostgresColumn(tableName, propertyName, dbSchema, schema, columnReflectionData ?? {})
27
+ ];
28
+ });
29
+ function getColumn(table, propertyName) {
30
+ return assertDefinedPass(table[propertyName], `Property "${propertyName}" does not exist on ${type.name}`);
31
+ }
32
+ const drizzleSchema = dbSchema.table(tableName, fromEntries(entries), (table) => {
33
+ const uniqueEntries = tableReflectionData?.unique?.map((data, index) => {
34
+ const columns = data.columns?.map((column) => getColumn(table, column));
35
+ let constraint = unique(isDefined(data.name) ? toSnakeCase(data.name) : undefined).on(...columns);
36
+ if (data.options?.nulls == 'not distinct') {
37
+ constraint = constraint.nullsNotDistinct();
38
+ }
39
+ return [`unique_${index}`, constraint];
40
+ });
41
+ return {
42
+ ...isDefined(uniqueEntries) ? fromEntries(uniqueEntries) : undefined
43
+ };
44
+ });
45
+ return drizzleSchema; // eslint-disable-line @typescript-eslint/no-unsafe-return
46
+ }
47
+ function getPostgresColumn(tableName, columnName, dbSchema, propertySchema, reflectionData) {
48
+ let nullable = false;
49
+ let array = false;
50
+ let baseSchema = propertySchema;
51
+ while (true) {
52
+ if ((baseSchema instanceof NullableSchema) || (baseSchema instanceof OptionalSchema)) {
53
+ nullable = true;
54
+ baseSchema = baseSchema.schema;
55
+ }
56
+ else if (baseSchema instanceof ArraySchema) {
57
+ array = true;
58
+ baseSchema = baseSchema.itemSchema;
59
+ }
60
+ else {
61
+ break;
62
+ }
63
+ }
64
+ let column = getPostgresBaseColumn(tableName, columnName, dbSchema, baseSchema);
65
+ if (array) {
66
+ column = column.array();
67
+ }
68
+ if (!nullable) {
69
+ column = column.notNull();
70
+ }
71
+ if (isDefined(reflectionData.unique)) {
72
+ column = column.unique(reflectionData.unique.name, isString(reflectionData.unique.options?.nulls) ? { nulls: reflectionData.unique.options.nulls } : undefined);
73
+ }
74
+ if (reflectionData.primaryKey == true) {
75
+ column = column.primaryKey();
76
+ }
77
+ return column;
78
+ }
79
+ function getPostgresBaseColumn(tableName, columnName, dbSchema, schema) {
80
+ if (schema instanceof NumberSchema) {
81
+ return schema.integer
82
+ ? integer(columnName)
83
+ : doublePrecision(columnName);
84
+ }
85
+ if (schema instanceof UuidSchema) {
86
+ let column = uuid(columnName);
87
+ if (schema.defaultRandom) {
88
+ column = column.defaultRandom();
89
+ }
90
+ return column;
91
+ }
92
+ if (schema instanceof StringSchema) {
93
+ return text(columnName);
94
+ }
95
+ if (schema instanceof BooleanSchema) {
96
+ return boolean(columnName);
97
+ }
98
+ if (schema instanceof EnumerationSchema) {
99
+ const pgEnum = getPgEnum(tableName, columnName, dbSchema, schema.enumeration);
100
+ return pgEnum(columnName);
101
+ }
102
+ throw new NotSupportedError(`Schema ${schema.constructor.name} not supported`);
103
+ }
104
+ const enums = new Map();
105
+ export function registerEnum(enumeration, name) {
106
+ enums.set(enumeration, name);
107
+ }
108
+ function getPgEnum(tableName, columnName, dbSchema, enumeration) {
109
+ const values = (isArray(enumeration) ? enumeration : enumValues(enumeration))
110
+ .map((value) => value.toString());
111
+ const enumName = enums.get(enumeration) ?? `${tableName}_${columnName}_enum`;
112
+ return dbSchema.enum(enumName, values);
113
+ }
114
+ function getDefaultTableName(type) {
115
+ return toSnakeCase(isString(type.entityName) ? type.entityName : type.name);
116
+ }
package/orm/entity.d.ts CHANGED
@@ -1,4 +1,8 @@
1
- import type { Record, TypedOmit } from '../types.js';
1
+ import type { Record, Type, TypedOmit } from '../types.js';
2
+ import { type HasDefault, type IsPrimaryKey, Uuid } from './types.js';
3
+ export interface EntityType<T extends Entity = Entity> extends Type<T> {
4
+ readonly entityName: string;
5
+ }
2
6
  export declare abstract class EntityMetadata {
3
7
  revision: number;
4
8
  revisionTimestamp: number;
@@ -7,7 +11,7 @@ export declare abstract class EntityMetadata {
7
11
  attributes: Record;
8
12
  }
9
13
  export declare abstract class Entity {
10
- id: string;
14
+ id: IsPrimaryKey<HasDefault<Uuid>>;
11
15
  metadata: EntityMetadata;
12
16
  }
13
17
  export type NewEntity<T extends Entity> = TypedOmit<T, 'id' | 'metadata'> & {
package/orm/entity.js CHANGED
@@ -8,6 +8,8 @@ var __metadata = (this && this.__metadata) || function (k, v) {
8
8
  if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
9
9
  };
10
10
  import { NumberProperty, Property, any, record } from '../schema/index.js';
11
+ import { PrimaryKey } from './decorators.js';
12
+ import { Uuid } from './types.js';
11
13
  export class EntityMetadata {
12
14
  revision;
13
15
  revisionTimestamp;
@@ -37,13 +39,11 @@ __decorate([
37
39
  ], EntityMetadata.prototype, "attributes", void 0);
38
40
  export class Entity {
39
41
  id;
42
+ // @Property(EntityMetadata)
40
43
  metadata;
41
44
  }
42
45
  __decorate([
43
- Property(),
44
- __metadata("design:type", String)
46
+ PrimaryKey(),
47
+ Uuid({ defaultRandom: true }),
48
+ __metadata("design:type", Object)
45
49
  ], Entity.prototype, "id", void 0);
46
- __decorate([
47
- Property(EntityMetadata),
48
- __metadata("design:type", EntityMetadata)
49
- ], Entity.prototype, "metadata", void 0);
package/orm/index.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  export * from './decorators.js';
2
+ export * from './drizzle/schema-converter.js';
2
3
  export * from './entity.js';
3
4
  export * from './query.js';
4
5
  export * from './repository.js';
5
- export * from './schema-converter.js';
6
- export * from './schema.js';
7
6
  export * from './types.js';
package/orm/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  export * from './decorators.js';
2
+ export * from './drizzle/schema-converter.js';
2
3
  export * from './entity.js';
3
4
  export * from './query.js';
4
5
  export * from './repository.js';
5
- export * from './schema-converter.js';
6
- export * from './schema.js';
7
6
  export * from './types.js';
@@ -1,5 +1,6 @@
1
- import type { Paths, Type, TypedOmit } from '../types.js';
2
- import type { Entity, EntityMetadata, NewEntity } from './entity.js';
1
+ import type { Paths, TypedOmit } from '../types.js';
2
+ import { SQL } from 'drizzle-orm';
3
+ import type { Entity, EntityMetadata, EntityType, NewEntity } from './entity.js';
3
4
  import type { Query } from './query.js';
4
5
  export declare const repositoryType: unique symbol;
5
6
  export type OrderOptions<T extends Entity> = {
@@ -17,34 +18,36 @@ export type EntityMetadataUpdate = {
17
18
  metadata?: Partial<EntityMetadata>;
18
19
  };
19
20
  export type EntityUpdate<T extends Entity> = Partial<TypedOmit<T, 'metadata'>> & EntityMetadataUpdate;
21
+ export declare const entityType: import("../injector/token.js").InjectionToken<EntityType<any>, never>;
20
22
  export declare abstract class EntityRepository<T extends Entity = Entity> {
21
- readonly type: Type<T>;
23
+ readonly type: EntityType<T>;
24
+ readonly schema: import("./drizzle/schema-converter.js").PgTableFromType<"", EntityType<Entity>, string>;
22
25
  readonly database: import("drizzle-orm/node-postgres").NodePgDatabase<Record<string, never>> & {
23
- $client: null;
26
+ $client: import("drizzle-orm/node-postgres").NodePgClient;
24
27
  };
25
- load(_id: string): Promise<T>;
26
- abstract tryLoad(id: string): Promise<T | undefined>;
27
- abstract loadByFilter(query: Query<T>, options?: LoadOptions<T>): Promise<T>;
28
- abstract tryLoadByFilter(query: Query<T>, options?: LoadOptions<T>): Promise<T | undefined>;
29
- abstract loadMany(ids: string[], options?: LoadManyOptions<T>): Promise<T[]>;
30
- abstract loadManyCursor(ids: string[], options?: LoadManyOptions<T>): AsyncIterableIterator<T>;
31
- abstract loadManyByFilter(query: Query<T>, options?: LoadManyOptions<T>): Promise<T[]>;
32
- abstract loadManyByFilterCursor(query: Query<T>, options?: LoadManyOptions<T>): AsyncIterableIterator<T>;
33
- abstract loadAll(options?: LoadManyOptions<T>): Promise<T[]>;
34
- abstract loadAllCursor(options?: LoadManyOptions<T>): AsyncIterableIterator<T>;
35
- abstract count(): Promise<number>;
36
- abstract countByFilter(query: Query<T>): Promise<number>;
37
- abstract has(id: string): Promise<boolean>;
38
- abstract hasByFilter(query: Query<T>): Promise<boolean>;
39
- abstract hasAll(ids: string[]): Promise<boolean>;
40
- abstract insert(entity: NewEntity<T>): Promise<T>;
28
+ load(id: string): Promise<T>;
29
+ tryLoad(id: string): Promise<T | undefined>;
30
+ loadByQuery(query: Query<T> | SQL | undefined, options?: LoadOptions<T>): Promise<T>;
31
+ tryLoadByQuery(query: Query<T> | SQL | undefined, options?: LoadOptions<T>): Promise<T | undefined>;
32
+ loadMany(ids: string[], options?: LoadManyOptions<T>): Promise<T[]>;
33
+ loadManyCursor(ids: string[], options?: LoadManyOptions<T>): AsyncIterableIterator<T>;
34
+ loadManyByQuery(query: Query<T> | SQL | undefined, options?: LoadManyOptions<T>): Promise<T[]>;
35
+ loadManyByQueryCursor(query: Query<T> | SQL | undefined, options?: LoadManyOptions<T>): AsyncIterableIterator<T>;
36
+ loadAll(options?: LoadManyOptions<T>): Promise<T[]>;
37
+ loadAllCursor(options?: LoadManyOptions<T>): AsyncIterableIterator<T>;
38
+ count(): Promise<number>;
39
+ countByQuery(query: Query<T> | SQL): Promise<number>;
40
+ has(id: string): Promise<boolean>;
41
+ hasByQuery(query: Query<T> | SQL): Promise<boolean>;
42
+ hasAll(ids: string[]): Promise<boolean>;
43
+ insert(_entity: NewEntity<T>): Promise<T>;
41
44
  abstract insertMany(entities: NewEntity<T>[]): Promise<T[]>;
42
45
  abstract update(id: string, update: EntityUpdate<T>): Promise<void>;
43
- abstract updateByFilter(query: Query<T>, update: EntityUpdate<T>): Promise<void>;
46
+ abstract updateByQuery(query: Query<T>, update: EntityUpdate<T>): Promise<void>;
44
47
  abstract updateMany(ids: string[], update: EntityUpdate<T>): Promise<void>;
45
- abstract updateManyByFilter(filter: Query<T>, update: EntityUpdate<T>): Promise<void>;
48
+ abstract updateManyByQuery(filter: Query<T>, update: EntityUpdate<T>): Promise<void>;
46
49
  abstract delete(id: string): Promise<boolean>;
47
50
  abstract deleteMany(ids: string[]): Promise<number>;
48
- abstract deleteByFilter(query: Query<T>): Promise<boolean>;
49
- abstract deleteManyByFilter(query: Query<T>): Promise<number>;
51
+ abstract deleteByQuery(query: Query<T>): Promise<boolean>;
52
+ abstract deleteManyByQuery(query: Query<T>): Promise<number>;
50
53
  }