@meghjs/core 0.5.0 → 0.5.2
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/README.md +39 -29
- package/dist/common/helpers.d.ts +0 -1
- package/dist/common/helpers.js +0 -1
- package/dist/common/helpers.js.map +1 -1
- package/dist/config/config.js +1 -2
- package/dist/config/config.js.map +1 -1
- package/dist/core/application.d.ts +0 -6
- package/dist/core/application.js +2 -10
- package/dist/core/application.js.map +1 -1
- package/dist/core/router.js +2 -1
- package/dist/core/router.js.map +1 -1
- package/dist/di/container.d.ts +50 -6
- package/dist/di/container.js +183 -29
- package/dist/di/container.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/openapi/branding.d.ts +29 -0
- package/dist/openapi/branding.js +41 -0
- package/dist/openapi/branding.js.map +1 -0
- package/dist/openapi/openapi.d.ts +8 -0
- package/dist/openapi/openapi.js.map +1 -1
- package/dist/orm/casts.d.ts +6 -1
- package/dist/orm/casts.js +18 -7
- package/dist/orm/casts.js.map +1 -1
- package/dist/orm/config.d.ts +63 -0
- package/dist/orm/config.js +127 -0
- package/dist/orm/config.js.map +1 -0
- package/dist/orm/connect.d.ts +11 -0
- package/dist/orm/connect.js +147 -0
- package/dist/orm/connect.js.map +1 -0
- package/dist/orm/connection.d.ts +9 -5
- package/dist/orm/connection.js +41 -14
- package/dist/orm/connection.js.map +1 -1
- package/dist/orm/core.d.ts +43 -0
- package/dist/orm/core.js +23 -0
- package/dist/orm/core.js.map +1 -0
- package/dist/orm/db.d.ts +171 -0
- package/dist/orm/db.js +534 -0
- package/dist/orm/db.js.map +1 -0
- package/dist/orm/decorators.d.ts +143 -0
- package/dist/orm/decorators.js +243 -0
- package/dist/orm/decorators.js.map +1 -0
- package/dist/orm/engine.d.ts +8 -0
- package/dist/orm/engine.js +8 -0
- package/dist/orm/engine.js.map +1 -0
- package/dist/orm/has-relation.d.ts +31 -0
- package/dist/orm/has-relation.js +77 -0
- package/dist/orm/has-relation.js.map +1 -0
- package/dist/orm/index.d.ts +12 -4
- package/dist/orm/index.js +41 -1
- package/dist/orm/index.js.map +1 -1
- package/dist/orm/migrations.d.ts +27 -0
- package/dist/orm/migrations.js +40 -16
- package/dist/orm/migrations.js.map +1 -1
- package/dist/orm/model.d.ts +55 -6
- package/dist/orm/model.js +82 -16
- package/dist/orm/model.js.map +1 -1
- package/dist/orm/naming.d.ts +19 -0
- package/dist/orm/naming.js +96 -0
- package/dist/orm/naming.js.map +1 -0
- package/dist/orm/paging.d.ts +22 -0
- package/dist/orm/paging.js +32 -0
- package/dist/orm/paging.js.map +1 -0
- package/dist/orm/pivot.js +3 -2
- package/dist/orm/pivot.js.map +1 -1
- package/dist/orm/plugin.d.ts +2 -0
- package/dist/orm/plugin.js +50 -0
- package/dist/orm/plugin.js.map +1 -0
- package/dist/orm/query.d.ts +125 -10
- package/dist/orm/query.js +289 -47
- package/dist/orm/query.js.map +1 -1
- package/dist/orm/relations.d.ts +11 -2
- package/dist/orm/relations.js +14 -2
- package/dist/orm/relations.js.map +1 -1
- package/dist/orm/schema.d.ts +229 -0
- package/dist/orm/schema.js +645 -0
- package/dist/orm/schema.js.map +1 -0
- package/dist/orm/state.d.ts +4 -1
- package/dist/orm/state.js +29 -12
- package/dist/orm/state.js.map +1 -1
- package/dist/orm/types.d.ts +18 -5
- package/dist/plugins/builtin.d.ts +2 -0
- package/dist/plugins/builtin.js +12 -0
- package/dist/plugins/builtin.js.map +1 -0
- package/dist/plugins/registry.d.ts +5 -3
- package/dist/plugins/registry.js +14 -4
- package/dist/plugins/registry.js.map +1 -1
- package/package.json +6 -7
- package/dist/common/utils/di-utils.d.ts +0 -1
- package/dist/common/utils/di-utils.js +0 -20
- package/dist/common/utils/di-utils.js.map +0 -1
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decorators that describe a model's table, columns and relations:
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* @Table("users")
|
|
6
|
+
* class User extends Model {
|
|
7
|
+
* @PrimaryKey() @AutoIncrement() id: number;
|
|
8
|
+
* @Column({ unique: true }) email: string;
|
|
9
|
+
* @Column({ type: "json", nullable: true }) settings: Settings | null;
|
|
10
|
+
* @CreatedAt() created_at?: Date;
|
|
11
|
+
*
|
|
12
|
+
* @HasMany(() => Post) posts: HasMany<Post>;
|
|
13
|
+
* }
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* They record metadata (also used later to build schemas) and set the
|
|
17
|
+
* model's static settings (`table`, `primaryKey`, `generated`, …), which the
|
|
18
|
+
* rest of the ORM reads. Models written with static fields keep working.
|
|
19
|
+
*/
|
|
20
|
+
import type { Cast } from "./casts";
|
|
21
|
+
import { type BelongsToManyOptions, type DeclaredRelation } from "./relations";
|
|
22
|
+
import type * as relations from "./relations";
|
|
23
|
+
/** A one-to-many relation field. Loads as `M[]`. */
|
|
24
|
+
export type HasMany<M> = relations.HasMany<M>;
|
|
25
|
+
/** A one-to-one relation field. Loads as `M | null`. */
|
|
26
|
+
export type HasOne<M> = relations.HasOne<M>;
|
|
27
|
+
/** The inverse side of HasMany/HasOne. Loads as `M | null`. */
|
|
28
|
+
export type BelongsTo<M> = relations.BelongsTo<M>;
|
|
29
|
+
/** A many-to-many relation field. Loads as `M[]`, each with `pivot`. */
|
|
30
|
+
export type BelongsToMany<M, Pivot = {}> = relations.BelongsToMany<M, Pivot>;
|
|
31
|
+
/** Column types. They pick the SQL type in schemas and the value conversion. */
|
|
32
|
+
export type ColumnType = "string" | "text" | "integer" | "bigint" | "float" | "double" | "decimal" | "boolean" | "date" | "datetime" | "timestamp" | "time" | "json" | "uuid" | "binary";
|
|
33
|
+
export interface ColumnOptions {
|
|
34
|
+
/**
|
|
35
|
+
* The column's name in the database, when it differs from the property:
|
|
36
|
+
* `@Column({ name: "created_at" }) createdAt`. Without it, the naming
|
|
37
|
+
* strategy decides (`useMeghORM({ naming: "snake_case" })`).
|
|
38
|
+
*/
|
|
39
|
+
name?: string;
|
|
40
|
+
/** Column type. When left out, `boolean` and `Date` fields are read from the TypeScript type. */
|
|
41
|
+
type?: ColumnType;
|
|
42
|
+
/** Allows NULL. */
|
|
43
|
+
nullable?: boolean;
|
|
44
|
+
unique?: boolean;
|
|
45
|
+
/** Default value the database fills in. */
|
|
46
|
+
default?: unknown;
|
|
47
|
+
/** Maximum length, for `string`. */
|
|
48
|
+
length?: number;
|
|
49
|
+
/** Total digits, for `decimal`. */
|
|
50
|
+
precision?: number;
|
|
51
|
+
/** Digits after the decimal point, for `decimal`. */
|
|
52
|
+
scale?: number;
|
|
53
|
+
/** How stored values convert to model values. Defaults from `type`. */
|
|
54
|
+
cast?: Cast;
|
|
55
|
+
}
|
|
56
|
+
export interface ColumnMetadata extends ColumnOptions {
|
|
57
|
+
property: string;
|
|
58
|
+
primary?: boolean;
|
|
59
|
+
/** Filled in by the database: an auto-increment or identity column. */
|
|
60
|
+
generated?: "increment";
|
|
61
|
+
}
|
|
62
|
+
export interface ModelMetadata {
|
|
63
|
+
table?: string;
|
|
64
|
+
columns: Map<string, ColumnMetadata>;
|
|
65
|
+
relations: Map<string, DeclaredRelation>;
|
|
66
|
+
createdAt?: string;
|
|
67
|
+
updatedAt?: string;
|
|
68
|
+
deletedAt?: string;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* A class's metadata merged with its parents', or `undefined` when neither
|
|
72
|
+
* it nor a parent uses decorators.
|
|
73
|
+
*/
|
|
74
|
+
export declare function getModelMetadata(cls: Function): ModelMetadata | undefined;
|
|
75
|
+
/** Casts declared on columns (`type: "json"`, `cast: …`). */
|
|
76
|
+
export declare function columnCasts(cls: Function): Record<string, Cast>;
|
|
77
|
+
/** Decorated column names, or `undefined` for models without column decorators. */
|
|
78
|
+
export declare function columnNames(cls: Function): string[] | undefined;
|
|
79
|
+
type FieldDecorator = (prototype: object, property: string | symbol) => void;
|
|
80
|
+
/**
|
|
81
|
+
* The model's table. Without a name it's the plural snake_case class name:
|
|
82
|
+
* `BlogPost` → `blog_posts`.
|
|
83
|
+
*/
|
|
84
|
+
export declare function Table(name?: string): ClassDecorator;
|
|
85
|
+
/** A column. `@Column()`, `@Column("text")` or `@Column({ type, nullable, … })`. */
|
|
86
|
+
export declare function Column(typeOrOptions?: ColumnType | ColumnOptions, options?: ColumnOptions): FieldDecorator;
|
|
87
|
+
/** The primary key column. Defaults to `id` when no field has it. */
|
|
88
|
+
export declare function PrimaryKey(options?: ColumnOptions): FieldDecorator;
|
|
89
|
+
/**
|
|
90
|
+
* A column the database numbers itself: `SERIAL`/`IDENTITY` on Postgres,
|
|
91
|
+
* `AUTO_INCREMENT` on MySQL, `IDENTITY(1,1)` on SQL Server, `AUTOINCREMENT`
|
|
92
|
+
* on SQLite. It's left out of inserts and optional in `create()`.
|
|
93
|
+
*/
|
|
94
|
+
export declare function AutoIncrement(): FieldDecorator;
|
|
95
|
+
/** Same as {@link AutoIncrement}; the name SQL Server and Entity Framework use. */
|
|
96
|
+
export declare const Identity: typeof AutoIncrement;
|
|
97
|
+
/** Set to the current time when the row is inserted. */
|
|
98
|
+
export declare const CreatedAt: (options?: {
|
|
99
|
+
name?: string;
|
|
100
|
+
}) => FieldDecorator;
|
|
101
|
+
/** Set to the current time on every insert and update. */
|
|
102
|
+
export declare const UpdatedAt: (options?: {
|
|
103
|
+
name?: string;
|
|
104
|
+
}) => FieldDecorator;
|
|
105
|
+
/**
|
|
106
|
+
* Soft deletes: `delete()` sets this column instead of removing the row, and
|
|
107
|
+
* queries skip such rows unless `withTrashed()` / `onlyTrashed()` is used.
|
|
108
|
+
*/
|
|
109
|
+
export declare const DeletedAt: (options?: {
|
|
110
|
+
name?: string;
|
|
111
|
+
}) => FieldDecorator;
|
|
112
|
+
type RelatedClass = abstract new () => any;
|
|
113
|
+
export interface HasOptions {
|
|
114
|
+
/** Column on the related table. Defaults to `<snake_case this class>_id`. */
|
|
115
|
+
foreignKey?: string;
|
|
116
|
+
/** Key on this table it matches. Defaults to the primary key. */
|
|
117
|
+
localKey?: string;
|
|
118
|
+
}
|
|
119
|
+
export interface BelongsToOptions {
|
|
120
|
+
/** Column on this table. Defaults to `<snake_case field>_id`. */
|
|
121
|
+
foreignKey?: string;
|
|
122
|
+
/** Key on the related table. Defaults to its primary key. */
|
|
123
|
+
ownerKey?: string;
|
|
124
|
+
}
|
|
125
|
+
/** One-to-many. Type the field `HasMany<Post>`; it loads as `Post[]`. */
|
|
126
|
+
export declare function HasMany<C extends RelatedClass>(related: () => C, options?: HasOptions): FieldDecorator;
|
|
127
|
+
/** One-to-one. Type the field `HasOne<Profile>`; it loads as `Profile | null`. */
|
|
128
|
+
export declare function HasOne<C extends RelatedClass>(related: () => C, options?: HasOptions): FieldDecorator;
|
|
129
|
+
/** The inverse of HasMany/HasOne. Type the field `BelongsTo<User>`. */
|
|
130
|
+
export declare function BelongsTo<C extends RelatedClass>(related: () => C, options?: BelongsToOptions): FieldDecorator;
|
|
131
|
+
export interface BelongsToManyDecoratorOptions extends BelongsToManyOptions {
|
|
132
|
+
/** Extra pivot columns to load into each related row's `pivot`. */
|
|
133
|
+
pivotColumns?: string[];
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Many-to-many through a pivot table. Type the field
|
|
137
|
+
* `BelongsToMany<Role>` (or `BelongsToMany<Role, { granted_at: Date }>`
|
|
138
|
+
* with `pivotColumns`).
|
|
139
|
+
*/
|
|
140
|
+
export declare function BelongsToMany<C extends RelatedClass>(related: () => C, options?: BelongsToManyDecoratorOptions): FieldDecorator;
|
|
141
|
+
/** Plural of a snake_case name, inflecting the last word: `blog_post` → `blog_posts`. */
|
|
142
|
+
export declare function pluralize(snake: string): string;
|
|
143
|
+
export {};
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DeletedAt = exports.UpdatedAt = exports.CreatedAt = exports.Identity = void 0;
|
|
4
|
+
exports.getModelMetadata = getModelMetadata;
|
|
5
|
+
exports.columnCasts = columnCasts;
|
|
6
|
+
exports.columnNames = columnNames;
|
|
7
|
+
exports.Table = Table;
|
|
8
|
+
exports.Column = Column;
|
|
9
|
+
exports.PrimaryKey = PrimaryKey;
|
|
10
|
+
exports.AutoIncrement = AutoIncrement;
|
|
11
|
+
exports.HasMany = HasMany;
|
|
12
|
+
exports.HasOne = HasOne;
|
|
13
|
+
exports.BelongsTo = BelongsTo;
|
|
14
|
+
exports.BelongsToMany = BelongsToMany;
|
|
15
|
+
exports.pluralize = pluralize;
|
|
16
|
+
const relations_1 = require("./relations");
|
|
17
|
+
const metadata = new WeakMap();
|
|
18
|
+
function own(cls) {
|
|
19
|
+
let meta = metadata.get(cls);
|
|
20
|
+
if (!meta) {
|
|
21
|
+
meta = { columns: new Map(), relations: new Map() };
|
|
22
|
+
metadata.set(cls, meta);
|
|
23
|
+
}
|
|
24
|
+
return meta;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* A class's metadata merged with its parents', or `undefined` when neither
|
|
28
|
+
* it nor a parent uses decorators.
|
|
29
|
+
*/
|
|
30
|
+
function getModelMetadata(cls) {
|
|
31
|
+
const chain = [];
|
|
32
|
+
for (let c = cls; c && c !== Function.prototype; c = Object.getPrototypeOf(c)) {
|
|
33
|
+
const meta = metadata.get(c);
|
|
34
|
+
if (meta)
|
|
35
|
+
chain.unshift(meta);
|
|
36
|
+
}
|
|
37
|
+
if (chain.length === 0)
|
|
38
|
+
return undefined;
|
|
39
|
+
const merged = { columns: new Map(), relations: new Map() };
|
|
40
|
+
for (const meta of chain) {
|
|
41
|
+
for (const [k, v] of meta.columns)
|
|
42
|
+
merged.columns.set(k, v);
|
|
43
|
+
for (const [k, v] of meta.relations)
|
|
44
|
+
merged.relations.set(k, v);
|
|
45
|
+
merged.createdAt = meta.createdAt ?? merged.createdAt;
|
|
46
|
+
merged.updatedAt = meta.updatedAt ?? merged.updatedAt;
|
|
47
|
+
merged.deletedAt = meta.deletedAt ?? merged.deletedAt;
|
|
48
|
+
}
|
|
49
|
+
merged.table = metadata.get(cls)?.table;
|
|
50
|
+
return merged;
|
|
51
|
+
}
|
|
52
|
+
const DEFAULT_CASTS = {
|
|
53
|
+
boolean: "boolean",
|
|
54
|
+
json: "json",
|
|
55
|
+
date: "date",
|
|
56
|
+
datetime: "datetime",
|
|
57
|
+
timestamp: "datetime",
|
|
58
|
+
};
|
|
59
|
+
/** Casts declared on columns (`type: "json"`, `cast: …`). */
|
|
60
|
+
function columnCasts(cls) {
|
|
61
|
+
const out = {};
|
|
62
|
+
for (const column of getModelMetadata(cls)?.columns.values() ?? []) {
|
|
63
|
+
const cast = column.cast ?? (column.type ? DEFAULT_CASTS[column.type] : undefined);
|
|
64
|
+
if (cast)
|
|
65
|
+
out[column.property] = cast;
|
|
66
|
+
}
|
|
67
|
+
return out;
|
|
68
|
+
}
|
|
69
|
+
/** Decorated column names, or `undefined` for models without column decorators. */
|
|
70
|
+
function columnNames(cls) {
|
|
71
|
+
const columns = getModelMetadata(cls)?.columns;
|
|
72
|
+
return columns && columns.size > 0 ? [...columns.keys()] : undefined;
|
|
73
|
+
}
|
|
74
|
+
function define(cls, name, value) {
|
|
75
|
+
Object.defineProperty(cls, name, {
|
|
76
|
+
value,
|
|
77
|
+
writable: false,
|
|
78
|
+
enumerable: true,
|
|
79
|
+
configurable: true,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
/** Set the static settings the ORM reads from the decorator metadata. */
|
|
83
|
+
function sync(cls) {
|
|
84
|
+
const meta = getModelMetadata(cls);
|
|
85
|
+
if (meta.table)
|
|
86
|
+
define(cls, "table", meta.table);
|
|
87
|
+
const columns = [...meta.columns.values()];
|
|
88
|
+
const primary = columns.find((c) => c.primary);
|
|
89
|
+
if (primary)
|
|
90
|
+
define(cls, "primaryKey", primary.property);
|
|
91
|
+
const generated = columns.filter((c) => c.generated).map((c) => c.property);
|
|
92
|
+
if (generated.length)
|
|
93
|
+
define(cls, "generated", generated);
|
|
94
|
+
if (meta.createdAt || meta.updatedAt) {
|
|
95
|
+
define(cls, "timestamps", true);
|
|
96
|
+
define(cls, "createdAt", meta.createdAt ?? null);
|
|
97
|
+
define(cls, "updatedAt", meta.updatedAt ?? null);
|
|
98
|
+
}
|
|
99
|
+
if (meta.deletedAt) {
|
|
100
|
+
define(cls, "softDeletes", true);
|
|
101
|
+
define(cls, "deletedAt", meta.deletedAt);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** The column type TypeScript emitted for a field, when it maps to one. */
|
|
105
|
+
function typeFromMetadata(prototype, property) {
|
|
106
|
+
const getMetadata = Reflect.getMetadata;
|
|
107
|
+
if (typeof getMetadata !== "function")
|
|
108
|
+
return undefined;
|
|
109
|
+
const type = getMetadata("design:type", prototype, property);
|
|
110
|
+
if (type === Boolean)
|
|
111
|
+
return "boolean";
|
|
112
|
+
if (type === Date)
|
|
113
|
+
return "datetime";
|
|
114
|
+
return undefined;
|
|
115
|
+
}
|
|
116
|
+
function updateColumn(prototype, property, change) {
|
|
117
|
+
if (typeof property !== "string") {
|
|
118
|
+
throw new Error("[Megh ORM] Columns must have string names.");
|
|
119
|
+
}
|
|
120
|
+
const cls = prototype.constructor;
|
|
121
|
+
const columns = own(cls).columns;
|
|
122
|
+
const current = columns.get(property) ?? {
|
|
123
|
+
property,
|
|
124
|
+
type: typeFromMetadata(prototype, property),
|
|
125
|
+
};
|
|
126
|
+
columns.set(property, { ...current, ...stripUndefined(change) });
|
|
127
|
+
sync(cls);
|
|
128
|
+
}
|
|
129
|
+
function stripUndefined(value) {
|
|
130
|
+
return Object.fromEntries(Object.entries(value).filter(([, v]) => v !== undefined));
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The model's table. Without a name it's the plural snake_case class name:
|
|
134
|
+
* `BlogPost` → `blog_posts`.
|
|
135
|
+
*/
|
|
136
|
+
function Table(name) {
|
|
137
|
+
return (target) => {
|
|
138
|
+
own(target).table = name ?? pluralize((0, relations_1.snakeCase)(target.name));
|
|
139
|
+
sync(target);
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
/** A column. `@Column()`, `@Column("text")` or `@Column({ type, nullable, … })`. */
|
|
143
|
+
function Column(typeOrOptions, options = {}) {
|
|
144
|
+
const resolved = typeof typeOrOptions === "string"
|
|
145
|
+
? { ...options, type: typeOrOptions }
|
|
146
|
+
: { ...typeOrOptions };
|
|
147
|
+
return (prototype, property) => updateColumn(prototype, property, resolved);
|
|
148
|
+
}
|
|
149
|
+
/** The primary key column. Defaults to `id` when no field has it. */
|
|
150
|
+
function PrimaryKey(options = {}) {
|
|
151
|
+
return (prototype, property) => updateColumn(prototype, property, { ...options, primary: true });
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* A column the database numbers itself: `SERIAL`/`IDENTITY` on Postgres,
|
|
155
|
+
* `AUTO_INCREMENT` on MySQL, `IDENTITY(1,1)` on SQL Server, `AUTOINCREMENT`
|
|
156
|
+
* on SQLite. It's left out of inserts and optional in `create()`.
|
|
157
|
+
*/
|
|
158
|
+
function AutoIncrement() {
|
|
159
|
+
return (prototype, property) => updateColumn(prototype, property, {
|
|
160
|
+
generated: "increment",
|
|
161
|
+
type: "integer",
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
/** Same as {@link AutoIncrement}; the name SQL Server and Entity Framework use. */
|
|
165
|
+
exports.Identity = AutoIncrement;
|
|
166
|
+
function timestampColumn(key, nullable) {
|
|
167
|
+
return (options = {}) => (prototype, property) => {
|
|
168
|
+
if (typeof property !== "string")
|
|
169
|
+
return;
|
|
170
|
+
own(prototype.constructor)[key] = property;
|
|
171
|
+
updateColumn(prototype, property, {
|
|
172
|
+
type: "datetime",
|
|
173
|
+
name: options.name,
|
|
174
|
+
...(nullable ? { nullable: true } : {}),
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/** Set to the current time when the row is inserted. */
|
|
179
|
+
exports.CreatedAt = timestampColumn("createdAt", false);
|
|
180
|
+
/** Set to the current time on every insert and update. */
|
|
181
|
+
exports.UpdatedAt = timestampColumn("updatedAt", false);
|
|
182
|
+
/**
|
|
183
|
+
* Soft deletes: `delete()` sets this column instead of removing the row, and
|
|
184
|
+
* queries skip such rows unless `withTrashed()` / `onlyTrashed()` is used.
|
|
185
|
+
*/
|
|
186
|
+
exports.DeletedAt = timestampColumn("deletedAt", true);
|
|
187
|
+
function relation(def) {
|
|
188
|
+
return (prototype, property) => {
|
|
189
|
+
if (typeof property !== "string")
|
|
190
|
+
return;
|
|
191
|
+
own(prototype.constructor).relations.set(property, def);
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
/** One-to-many. Type the field `HasMany<Post>`; it loads as `Post[]`. */
|
|
195
|
+
function HasMany(related, options = {}) {
|
|
196
|
+
return relation((0, relations_1.declareRelation)("hasMany", related, options));
|
|
197
|
+
}
|
|
198
|
+
/** One-to-one. Type the field `HasOne<Profile>`; it loads as `Profile | null`. */
|
|
199
|
+
function HasOne(related, options = {}) {
|
|
200
|
+
return relation((0, relations_1.declareRelation)("hasOne", related, options));
|
|
201
|
+
}
|
|
202
|
+
/** The inverse of HasMany/HasOne. Type the field `BelongsTo<User>`. */
|
|
203
|
+
function BelongsTo(related, options = {}) {
|
|
204
|
+
return relation((0, relations_1.declareRelation)("belongsTo", related, {
|
|
205
|
+
foreignKey: options.foreignKey,
|
|
206
|
+
localKey: options.ownerKey,
|
|
207
|
+
}));
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Many-to-many through a pivot table. Type the field
|
|
211
|
+
* `BelongsToMany<Role>` (or `BelongsToMany<Role, { granted_at: Date }>`
|
|
212
|
+
* with `pivotColumns`).
|
|
213
|
+
*/
|
|
214
|
+
function BelongsToMany(related, options = {}) {
|
|
215
|
+
const { pivotColumns = [], ...pivot } = options;
|
|
216
|
+
return relation((0, relations_1.declareRelation)("belongsToMany", related, {
|
|
217
|
+
pivot: { ...pivot, columns: [...pivotColumns] },
|
|
218
|
+
}));
|
|
219
|
+
}
|
|
220
|
+
const IRREGULAR = {
|
|
221
|
+
person: "people",
|
|
222
|
+
child: "children",
|
|
223
|
+
man: "men",
|
|
224
|
+
woman: "women",
|
|
225
|
+
mouse: "mice",
|
|
226
|
+
datum: "data",
|
|
227
|
+
};
|
|
228
|
+
/** Plural of a snake_case name, inflecting the last word: `blog_post` → `blog_posts`. */
|
|
229
|
+
function pluralize(snake) {
|
|
230
|
+
const parts = snake.split("_");
|
|
231
|
+
const last = parts.pop() ?? "";
|
|
232
|
+
let plural;
|
|
233
|
+
if (IRREGULAR[last])
|
|
234
|
+
plural = IRREGULAR[last];
|
|
235
|
+
else if (/[^aeiou]y$/.test(last))
|
|
236
|
+
plural = last.slice(0, -1) + "ies";
|
|
237
|
+
else if (/(s|x|z|ch|sh)$/.test(last))
|
|
238
|
+
plural = last + "es";
|
|
239
|
+
else
|
|
240
|
+
plural = last + "s";
|
|
241
|
+
return [...parts, plural].join("_");
|
|
242
|
+
}
|
|
243
|
+
//# sourceMappingURL=decorators.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"decorators.js","sourceRoot":"","sources":["../../src/orm/decorators.ts"],"names":[],"mappings":";;;AA+GA,4CAqBC;AAWD,kCAQC;AAGD,kCAGC;AAyED,sBAKC;AAGD,wBASC;AAGD,gCAGC;AAOD,sCAMC;AAuDD,0BAKC;AAGD,wBAKC;AAGD,8BAUC;AAYD,sCAUC;AAYD,8BASC;AAlXD,2CAKqB;AAuErB,MAAM,QAAQ,GAAG,IAAI,OAAO,EAA2B,CAAC;AAExD,SAAS,GAAG,CAAC,GAAa;IACxB,IAAI,IAAI,GAAG,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC7B,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,IAAI,GAAG,EAAE,OAAO,EAAE,IAAI,GAAG,EAAE,EAAE,SAAS,EAAE,IAA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,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC;SAChE,IAAI,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAAC;;QACtD,MAAM,GAAG,IAAI,GAAG,GAAG,CAAC;IACzB,OAAO,CAAC,GAAG,KAAK,EAAE,MAAM,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACtC,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Kysely, the engine under the ORM, loaded on first use. Kysely is ES
|
|
3
|
+
* modules only, and core loads the ORM plugin in every app: loading Kysely
|
|
4
|
+
* eagerly would make apps that never touch the database depend on
|
|
5
|
+
* `require()` of ES modules (Node.js 22.12+).
|
|
6
|
+
*/
|
|
7
|
+
import type * as KyselyModule from "kysely";
|
|
8
|
+
export declare function kysely(): typeof KyselyModule;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"engine.js","sourceRoot":"","sources":["../../src/orm/engine.ts"],"names":[],"mappings":";;AAUA,wBAEC;AAJD,IAAI,MAAuC,CAAC;AAE5C,SAAgB,MAAM;IACpB,OAAO,CAAC,MAAM,KAAK,OAAO,CAAC,QAAQ,CAAwB,CAAC,CAAC;AAC/D,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { ModelQuery } from "./query";
|
|
2
|
+
import type { RelationDefinition } from "./relations";
|
|
3
|
+
import type { Row, UpdateInput } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* The related rows of one model's `hasMany` / `hasOne` relation, to query or
|
|
6
|
+
* create them with the foreign key filled in:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* const post = await user.related("posts").create({ title: "Hello" });
|
|
10
|
+
* const drafts = await user.related("posts").query().where("published", 0).get();
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export declare class HasRelation<R, Kind extends "many" | "one" = "many"> {
|
|
14
|
+
private readonly parent;
|
|
15
|
+
private readonly def;
|
|
16
|
+
constructor(parent: any, def: RelationDefinition);
|
|
17
|
+
private get relatedClass();
|
|
18
|
+
/** The related model's property holding the foreign key. */
|
|
19
|
+
private get foreignProperty();
|
|
20
|
+
private parentKey;
|
|
21
|
+
/** A query on the related rows. */
|
|
22
|
+
query(): ModelQuery<R>;
|
|
23
|
+
/** The related rows (`hasMany`) or row (`hasOne`, `null` when missing). */
|
|
24
|
+
get(): Promise<Kind extends "many" ? Row<R>[] : Row<R> | null>;
|
|
25
|
+
/** Insert a related row with the foreign key set to this model's key. */
|
|
26
|
+
create(values: UpdateInput<R>): Promise<Row<R>>;
|
|
27
|
+
/** Insert several related rows in one transaction. */
|
|
28
|
+
createMany(list: readonly UpdateInput<R>[]): Promise<Row<R>[]>;
|
|
29
|
+
/** Point `model` at this parent (set its foreign key) and save it. */
|
|
30
|
+
save<T extends R>(model: T): Promise<T>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.HasRelation = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @copyright 2024
|
|
6
|
+
* @author Tareq Hossain
|
|
7
|
+
* @email xtrinsic96@gmail.com
|
|
8
|
+
* @url https://github.com/xtareq
|
|
9
|
+
*/
|
|
10
|
+
const connection_1 = require("./connection");
|
|
11
|
+
const naming_1 = require("./naming");
|
|
12
|
+
const query_1 = require("./query");
|
|
13
|
+
/**
|
|
14
|
+
* The related rows of one model's `hasMany` / `hasOne` relation, to query or
|
|
15
|
+
* create them with the foreign key filled in:
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* const post = await user.related("posts").create({ title: "Hello" });
|
|
19
|
+
* const drafts = await user.related("posts").query().where("published", 0).get();
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
class HasRelation {
|
|
23
|
+
parent;
|
|
24
|
+
def;
|
|
25
|
+
constructor(parent, def) {
|
|
26
|
+
this.parent = parent;
|
|
27
|
+
this.def = def;
|
|
28
|
+
}
|
|
29
|
+
get relatedClass() {
|
|
30
|
+
return this.def.related();
|
|
31
|
+
}
|
|
32
|
+
/** The related model's property holding the foreign key. */
|
|
33
|
+
get foreignProperty() {
|
|
34
|
+
return (0, naming_1.propertyName)(this.relatedClass, this.def.foreignKey);
|
|
35
|
+
}
|
|
36
|
+
parentKey() {
|
|
37
|
+
const key = this.parent[(0, naming_1.propertyName)(this.parent.constructor, this.def.localKey)];
|
|
38
|
+
if (key === null || key === undefined) {
|
|
39
|
+
throw new Error(`[Megh ORM] Save the ${this.parent.constructor.name} before using its related ${this.relatedClass.name} rows.`);
|
|
40
|
+
}
|
|
41
|
+
return key;
|
|
42
|
+
}
|
|
43
|
+
/** A query on the related rows. */
|
|
44
|
+
query() {
|
|
45
|
+
return new query_1.ModelQuery(this.relatedClass).where(this.def.foreignKey, this.parentKey());
|
|
46
|
+
}
|
|
47
|
+
/** The related rows (`hasMany`) or row (`hasOne`, `null` when missing). */
|
|
48
|
+
async get() {
|
|
49
|
+
if (this.def.kind === "hasOne") {
|
|
50
|
+
return ((await this.query().first()) ?? null);
|
|
51
|
+
}
|
|
52
|
+
return (await this.query().get());
|
|
53
|
+
}
|
|
54
|
+
/** Insert a related row with the foreign key set to this model's key. */
|
|
55
|
+
async create(values) {
|
|
56
|
+
return this.relatedClass.create({
|
|
57
|
+
...values,
|
|
58
|
+
[this.foreignProperty]: this.parentKey(),
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
/** Insert several related rows in one transaction. */
|
|
62
|
+
async createMany(list) {
|
|
63
|
+
return (0, connection_1.runInTransaction)(async () => {
|
|
64
|
+
const out = [];
|
|
65
|
+
for (const values of list)
|
|
66
|
+
out.push(await this.create(values));
|
|
67
|
+
return out;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/** Point `model` at this parent (set its foreign key) and save it. */
|
|
71
|
+
async save(model) {
|
|
72
|
+
model[this.foreignProperty] = this.parentKey();
|
|
73
|
+
return model.save();
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
exports.HasRelation = HasRelation;
|
|
77
|
+
//# sourceMappingURL=has-relation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"has-relation.js","sourceRoot":"","sources":["../../src/orm/has-relation.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACH,6CAAgD;AAChD,qCAAwC;AACxC,mCAAqC;AAIrC;;;;;;;;GAQG;AACH,MAAa,WAAW;IAEH;IACA;IAFnB,YACmB,MAAW,EACX,GAAuB;QADvB,WAAM,GAAN,MAAM,CAAK;QACX,QAAG,GAAH,GAAG,CAAoB;IACvC,CAAC;IAEJ,IAAY,YAAY;QACtB,OAAO,IAAI,CAAC,GAAG,CAAC,OAAO,EAA8B,CAAC;IACxD,CAAC;IAED,4DAA4D;IAC5D,IAAY,eAAe;QACzB,OAAO,IAAA,qBAAY,EAAC,IAAI,CAAC,YAAY,EAAE,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IAC9D,CAAC;IAEO,SAAS;QACf,MAAM,GAAG,GACP,IAAI,CAAC,MAAM,CAAC,IAAA,qBAAY,EAAC,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC;QACxE,IAAI,GAAG,KAAK,IAAI,IAAI,GAAG,KAAK,SAAS,EAAE,CAAC;YACtC,MAAM,IAAI,KAAK,CACb,uBAAuB,IAAI,CAAC,MAAM,CAAC,WAAW,CAAC,IAAI,6BAA6B,IAAI,CAAC,YAAY,CAAC,IAAI,QAAQ,CAC/G,CAAC;QACJ,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC;IAED,mCAAmC;IACnC,KAAK;QACH,OAAQ,IAAI,kBAAU,CAAI,IAAI,CAAC,YAAY,CAAqB,CAAC,KA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-
export { ModelQuery, type ComparisonOperator } from "./query";
|
|
3
|
-
export {
|
|
2
|
+
export { ModelQuery, type ComparisonOperator, type Counted, type CursorPageRequest, type CursorPaginated, type ModelAt, type RelationConstraint, type Scope, type SimplePaginated, } from "./query";
|
|
3
|
+
export { HasRelation } from "./has-relation";
|
|
4
|
+
export { hasMany, hasOne, belongsTo, belongsToMany, type BelongsToManyOptions, } from "./relations";
|
|
5
|
+
export { Table, Column, PrimaryKey, AutoIncrement, Identity, CreatedAt, UpdatedAt, DeletedAt, HasMany, HasOne, BelongsTo, BelongsToMany, getModelMetadata, type ColumnType, type ColumnOptions, type ColumnMetadata, type ModelMetadata, type HasOptions, type BelongsToOptions, type BelongsToManyDecoratorOptions, } from "./decorators";
|
|
4
6
|
export { ORM_CONNECTION } from "./connection";
|
|
5
7
|
export { PivotRelation, type RelatedRef } from "./pivot";
|
|
6
8
|
export type { Cast, CustomCast } from "./casts";
|
|
7
|
-
export type { Columns, ColumnKeys, RelationKeys, RelationPaths, BelongsToManyKeys, PivotOf, TimestampKeys, Row, ModelClass, CreateInput, UpdateInput, PrimaryKeyValue, TableOf, DatabaseOf, } from "./types";
|
|
8
|
-
export { Migrations, MigrationError, readMigrationFolder, type Migration, type MigrationsOptions, type MigrationStatus, } from "./migrations";
|
|
9
|
+
export type { Columns, ColumnKeys, RelationKeys, RelationPaths, BelongsToManyKeys, HasRelationKeys, PivotOf, TimestampKeys, Row, ModelClass, CreateInput, UpdateInput, PrimaryKeyValue, TableOf, DatabaseOf, } from "./types";
|
|
10
|
+
export { Migrations, MigrationError, defineMigration, type SchemaMigration, readMigrationFolder, type Migration, type MigrationsOptions, type MigrationStatus, } from "./migrations";
|
|
11
|
+
export { ormPlugin } from "./plugin";
|
|
12
|
+
export { DatabaseConfigError, resolveDatabaseOptions, type DatabaseOptions, type Dialect, type PoolOptions, type ResolvedDatabaseOptions, } from "./config";
|
|
13
|
+
export { createDatabase } from "./connect";
|
|
14
|
+
export { Schema, Blueprint, ColumnDefinition, ForeignKeyDefinition, SchemaError, type ReferentialAction, type SchemaDialect, } from "./schema";
|
|
15
|
+
export { DB, TableQuery, WhereClauses, WhereGroup, type Operator } from "./db";
|
|
16
|
+
export { setNamingStrategy, getNamingStrategy, type NamingStrategy, } from "./naming";
|
package/dist/orm/index.js
CHANGED
|
@@ -1,15 +1,33 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.readMigrationFolder = exports.MigrationError = exports.Migrations = exports.PivotRelation = exports.ORM_CONNECTION = exports.belongsToMany = exports.belongsTo = exports.hasOne = exports.hasMany = exports.ModelQuery = exports.Model = void 0;
|
|
3
|
+
exports.getNamingStrategy = exports.setNamingStrategy = exports.WhereGroup = exports.WhereClauses = exports.TableQuery = exports.DB = exports.SchemaError = exports.ForeignKeyDefinition = exports.ColumnDefinition = exports.Blueprint = exports.Schema = exports.createDatabase = exports.resolveDatabaseOptions = exports.DatabaseConfigError = exports.ormPlugin = exports.readMigrationFolder = exports.defineMigration = exports.MigrationError = exports.Migrations = exports.PivotRelation = exports.ORM_CONNECTION = exports.getModelMetadata = exports.BelongsToMany = exports.BelongsTo = exports.HasOne = exports.HasMany = exports.DeletedAt = exports.UpdatedAt = exports.CreatedAt = exports.Identity = exports.AutoIncrement = exports.PrimaryKey = exports.Column = exports.Table = exports.belongsToMany = exports.belongsTo = exports.hasOne = exports.hasMany = exports.HasRelation = exports.ModelQuery = exports.Model = void 0;
|
|
4
4
|
var model_1 = require("./model");
|
|
5
5
|
Object.defineProperty(exports, "Model", { enumerable: true, get: function () { return model_1.Model; } });
|
|
6
6
|
var query_1 = require("./query");
|
|
7
7
|
Object.defineProperty(exports, "ModelQuery", { enumerable: true, get: function () { return query_1.ModelQuery; } });
|
|
8
|
+
var has_relation_1 = require("./has-relation");
|
|
9
|
+
Object.defineProperty(exports, "HasRelation", { enumerable: true, get: function () { return has_relation_1.HasRelation; } });
|
|
8
10
|
var relations_1 = require("./relations");
|
|
9
11
|
Object.defineProperty(exports, "hasMany", { enumerable: true, get: function () { return relations_1.hasMany; } });
|
|
10
12
|
Object.defineProperty(exports, "hasOne", { enumerable: true, get: function () { return relations_1.hasOne; } });
|
|
11
13
|
Object.defineProperty(exports, "belongsTo", { enumerable: true, get: function () { return relations_1.belongsTo; } });
|
|
12
14
|
Object.defineProperty(exports, "belongsToMany", { enumerable: true, get: function () { return relations_1.belongsToMany; } });
|
|
15
|
+
// HasMany, HasOne, BelongsTo and BelongsToMany are both the relation
|
|
16
|
+
// decorators and the field types: `@HasMany(() => Post) posts: HasMany<Post>`.
|
|
17
|
+
var decorators_1 = require("./decorators");
|
|
18
|
+
Object.defineProperty(exports, "Table", { enumerable: true, get: function () { return decorators_1.Table; } });
|
|
19
|
+
Object.defineProperty(exports, "Column", { enumerable: true, get: function () { return decorators_1.Column; } });
|
|
20
|
+
Object.defineProperty(exports, "PrimaryKey", { enumerable: true, get: function () { return decorators_1.PrimaryKey; } });
|
|
21
|
+
Object.defineProperty(exports, "AutoIncrement", { enumerable: true, get: function () { return decorators_1.AutoIncrement; } });
|
|
22
|
+
Object.defineProperty(exports, "Identity", { enumerable: true, get: function () { return decorators_1.Identity; } });
|
|
23
|
+
Object.defineProperty(exports, "CreatedAt", { enumerable: true, get: function () { return decorators_1.CreatedAt; } });
|
|
24
|
+
Object.defineProperty(exports, "UpdatedAt", { enumerable: true, get: function () { return decorators_1.UpdatedAt; } });
|
|
25
|
+
Object.defineProperty(exports, "DeletedAt", { enumerable: true, get: function () { return decorators_1.DeletedAt; } });
|
|
26
|
+
Object.defineProperty(exports, "HasMany", { enumerable: true, get: function () { return decorators_1.HasMany; } });
|
|
27
|
+
Object.defineProperty(exports, "HasOne", { enumerable: true, get: function () { return decorators_1.HasOne; } });
|
|
28
|
+
Object.defineProperty(exports, "BelongsTo", { enumerable: true, get: function () { return decorators_1.BelongsTo; } });
|
|
29
|
+
Object.defineProperty(exports, "BelongsToMany", { enumerable: true, get: function () { return decorators_1.BelongsToMany; } });
|
|
30
|
+
Object.defineProperty(exports, "getModelMetadata", { enumerable: true, get: function () { return decorators_1.getModelMetadata; } });
|
|
13
31
|
var connection_1 = require("./connection");
|
|
14
32
|
Object.defineProperty(exports, "ORM_CONNECTION", { enumerable: true, get: function () { return connection_1.ORM_CONNECTION; } });
|
|
15
33
|
var pivot_1 = require("./pivot");
|
|
@@ -17,5 +35,27 @@ Object.defineProperty(exports, "PivotRelation", { enumerable: true, get: functio
|
|
|
17
35
|
var migrations_1 = require("./migrations");
|
|
18
36
|
Object.defineProperty(exports, "Migrations", { enumerable: true, get: function () { return migrations_1.Migrations; } });
|
|
19
37
|
Object.defineProperty(exports, "MigrationError", { enumerable: true, get: function () { return migrations_1.MigrationError; } });
|
|
38
|
+
Object.defineProperty(exports, "defineMigration", { enumerable: true, get: function () { return migrations_1.defineMigration; } });
|
|
20
39
|
Object.defineProperty(exports, "readMigrationFolder", { enumerable: true, get: function () { return migrations_1.readMigrationFolder; } });
|
|
40
|
+
var plugin_1 = require("./plugin");
|
|
41
|
+
Object.defineProperty(exports, "ormPlugin", { enumerable: true, get: function () { return plugin_1.ormPlugin; } });
|
|
42
|
+
var config_1 = require("./config");
|
|
43
|
+
Object.defineProperty(exports, "DatabaseConfigError", { enumerable: true, get: function () { return config_1.DatabaseConfigError; } });
|
|
44
|
+
Object.defineProperty(exports, "resolveDatabaseOptions", { enumerable: true, get: function () { return config_1.resolveDatabaseOptions; } });
|
|
45
|
+
var connect_1 = require("./connect");
|
|
46
|
+
Object.defineProperty(exports, "createDatabase", { enumerable: true, get: function () { return connect_1.createDatabase; } });
|
|
47
|
+
var schema_1 = require("./schema");
|
|
48
|
+
Object.defineProperty(exports, "Schema", { enumerable: true, get: function () { return schema_1.Schema; } });
|
|
49
|
+
Object.defineProperty(exports, "Blueprint", { enumerable: true, get: function () { return schema_1.Blueprint; } });
|
|
50
|
+
Object.defineProperty(exports, "ColumnDefinition", { enumerable: true, get: function () { return schema_1.ColumnDefinition; } });
|
|
51
|
+
Object.defineProperty(exports, "ForeignKeyDefinition", { enumerable: true, get: function () { return schema_1.ForeignKeyDefinition; } });
|
|
52
|
+
Object.defineProperty(exports, "SchemaError", { enumerable: true, get: function () { return schema_1.SchemaError; } });
|
|
53
|
+
var db_1 = require("./db");
|
|
54
|
+
Object.defineProperty(exports, "DB", { enumerable: true, get: function () { return db_1.DB; } });
|
|
55
|
+
Object.defineProperty(exports, "TableQuery", { enumerable: true, get: function () { return db_1.TableQuery; } });
|
|
56
|
+
Object.defineProperty(exports, "WhereClauses", { enumerable: true, get: function () { return db_1.WhereClauses; } });
|
|
57
|
+
Object.defineProperty(exports, "WhereGroup", { enumerable: true, get: function () { return db_1.WhereGroup; } });
|
|
58
|
+
var naming_1 = require("./naming");
|
|
59
|
+
Object.defineProperty(exports, "setNamingStrategy", { enumerable: true, get: function () { return naming_1.setNamingStrategy; } });
|
|
60
|
+
Object.defineProperty(exports, "getNamingStrategy", { enumerable: true, get: function () { return naming_1.getNamingStrategy; } });
|
|
21
61
|
//# sourceMappingURL=index.js.map
|
package/dist/orm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/orm/index.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAAvB,8FAAA,KAAK,OAAA;AACd,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/orm/index.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAAvB,8FAAA,KAAK,OAAA;AACd,iCAUiB;AATf,mGAAA,UAAU,OAAA;AAUZ,+CAA6C;AAApC,2GAAA,WAAW,OAAA;AACpB,yCAMqB;AALnB,oGAAA,OAAO,OAAA;AACP,mGAAA,MAAM,OAAA;AACN,sGAAA,SAAS,OAAA;AACT,0GAAA,aAAa,OAAA;AAGf,qEAAqE;AACrE,+EAA+E;AAC/E,2CAqBsB;AApBpB,mGAAA,KAAK,OAAA;AACL,oGAAA,MAAM,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