@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.
Files changed (92) hide show
  1. package/README.md +39 -29
  2. package/dist/common/helpers.d.ts +0 -1
  3. package/dist/common/helpers.js +0 -1
  4. package/dist/common/helpers.js.map +1 -1
  5. package/dist/config/config.js +1 -2
  6. package/dist/config/config.js.map +1 -1
  7. package/dist/core/application.d.ts +0 -6
  8. package/dist/core/application.js +2 -10
  9. package/dist/core/application.js.map +1 -1
  10. package/dist/core/router.js +2 -1
  11. package/dist/core/router.js.map +1 -1
  12. package/dist/di/container.d.ts +50 -6
  13. package/dist/di/container.js +183 -29
  14. package/dist/di/container.js.map +1 -1
  15. package/dist/index.d.ts +2 -2
  16. package/dist/index.js +3 -1
  17. package/dist/index.js.map +1 -1
  18. package/dist/openapi/branding.d.ts +29 -0
  19. package/dist/openapi/branding.js +41 -0
  20. package/dist/openapi/branding.js.map +1 -0
  21. package/dist/openapi/openapi.d.ts +8 -0
  22. package/dist/openapi/openapi.js.map +1 -1
  23. package/dist/orm/casts.d.ts +6 -1
  24. package/dist/orm/casts.js +18 -7
  25. package/dist/orm/casts.js.map +1 -1
  26. package/dist/orm/config.d.ts +63 -0
  27. package/dist/orm/config.js +127 -0
  28. package/dist/orm/config.js.map +1 -0
  29. package/dist/orm/connect.d.ts +11 -0
  30. package/dist/orm/connect.js +147 -0
  31. package/dist/orm/connect.js.map +1 -0
  32. package/dist/orm/connection.d.ts +9 -5
  33. package/dist/orm/connection.js +41 -14
  34. package/dist/orm/connection.js.map +1 -1
  35. package/dist/orm/core.d.ts +43 -0
  36. package/dist/orm/core.js +23 -0
  37. package/dist/orm/core.js.map +1 -0
  38. package/dist/orm/db.d.ts +171 -0
  39. package/dist/orm/db.js +534 -0
  40. package/dist/orm/db.js.map +1 -0
  41. package/dist/orm/decorators.d.ts +143 -0
  42. package/dist/orm/decorators.js +243 -0
  43. package/dist/orm/decorators.js.map +1 -0
  44. package/dist/orm/engine.d.ts +8 -0
  45. package/dist/orm/engine.js +8 -0
  46. package/dist/orm/engine.js.map +1 -0
  47. package/dist/orm/has-relation.d.ts +31 -0
  48. package/dist/orm/has-relation.js +77 -0
  49. package/dist/orm/has-relation.js.map +1 -0
  50. package/dist/orm/index.d.ts +12 -4
  51. package/dist/orm/index.js +41 -1
  52. package/dist/orm/index.js.map +1 -1
  53. package/dist/orm/migrations.d.ts +27 -0
  54. package/dist/orm/migrations.js +40 -16
  55. package/dist/orm/migrations.js.map +1 -1
  56. package/dist/orm/model.d.ts +55 -6
  57. package/dist/orm/model.js +82 -16
  58. package/dist/orm/model.js.map +1 -1
  59. package/dist/orm/naming.d.ts +19 -0
  60. package/dist/orm/naming.js +96 -0
  61. package/dist/orm/naming.js.map +1 -0
  62. package/dist/orm/paging.d.ts +22 -0
  63. package/dist/orm/paging.js +32 -0
  64. package/dist/orm/paging.js.map +1 -0
  65. package/dist/orm/pivot.js +3 -2
  66. package/dist/orm/pivot.js.map +1 -1
  67. package/dist/orm/plugin.d.ts +2 -0
  68. package/dist/orm/plugin.js +50 -0
  69. package/dist/orm/plugin.js.map +1 -0
  70. package/dist/orm/query.d.ts +125 -10
  71. package/dist/orm/query.js +289 -47
  72. package/dist/orm/query.js.map +1 -1
  73. package/dist/orm/relations.d.ts +11 -2
  74. package/dist/orm/relations.js +14 -2
  75. package/dist/orm/relations.js.map +1 -1
  76. package/dist/orm/schema.d.ts +229 -0
  77. package/dist/orm/schema.js +645 -0
  78. package/dist/orm/schema.js.map +1 -0
  79. package/dist/orm/state.d.ts +4 -1
  80. package/dist/orm/state.js +29 -12
  81. package/dist/orm/state.js.map +1 -1
  82. package/dist/orm/types.d.ts +18 -5
  83. package/dist/plugins/builtin.d.ts +2 -0
  84. package/dist/plugins/builtin.js +12 -0
  85. package/dist/plugins/builtin.js.map +1 -0
  86. package/dist/plugins/registry.d.ts +5 -3
  87. package/dist/plugins/registry.js +14 -4
  88. package/dist/plugins/registry.js.map +1 -1
  89. package/package.json +6 -7
  90. package/dist/common/utils/di-utils.d.ts +0 -1
  91. package/dist/common/utils/di-utils.js +0 -20
  92. package/dist/common/utils/di-utils.js.map +0 -1
@@ -0,0 +1,147 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createDatabase = createDatabase;
4
+ exports.verifyConnection = verifyConnection;
5
+ const core_1 = require("./core");
6
+ const engine_1 = require("./engine");
7
+ const config_1 = require("./config");
8
+ const DRIVERS = {
9
+ postgres: ["pg"],
10
+ mysql: ["mysql2"],
11
+ sqlite: ["better-sqlite3"],
12
+ mssql: ["tedious", "tarn"],
13
+ };
14
+ /** Load the driver packages a dialect needs, or explain how to install them. */
15
+ function drivers(dialect) {
16
+ const missing = [];
17
+ const loaded = DRIVERS[dialect].map((name) => {
18
+ try {
19
+ return (0, core_1.loadPackageFromClient)(name);
20
+ }
21
+ catch {
22
+ missing.push(name);
23
+ return undefined;
24
+ }
25
+ });
26
+ if (missing.length) {
27
+ throw new config_1.DatabaseConfigError(`The ${dialect} connection needs ${missing.map((m) => `"${m}"`).join(" and ")}. Install it: npm install ${missing.join(" ")}`);
28
+ }
29
+ return loaded;
30
+ }
31
+ function defaultExport(mod) {
32
+ return mod?.default ?? mod;
33
+ }
34
+ function postgres(o) {
35
+ const [pg] = drivers("postgres");
36
+ const Pool = pg.Pool ?? defaultExport(pg).Pool;
37
+ return new ((0, engine_1.kysely)().PostgresDialect)({
38
+ pool: new Pool({
39
+ connectionString: o.url,
40
+ host: o.host,
41
+ port: o.port,
42
+ user: o.user,
43
+ password: o.password,
44
+ database: o.database,
45
+ min: o.pool.min,
46
+ max: o.pool.max,
47
+ ssl: o.ssl,
48
+ }),
49
+ });
50
+ }
51
+ function mysql(o) {
52
+ const [mysql2] = drivers("mysql");
53
+ const settings = {
54
+ host: o.host,
55
+ port: o.port,
56
+ user: o.user,
57
+ password: o.password,
58
+ database: o.database,
59
+ connectionLimit: o.pool.max,
60
+ ssl: o.ssl === true ? {} : o.ssl || undefined,
61
+ };
62
+ return new ((0, engine_1.kysely)().MysqlDialect)({
63
+ pool: defaultExport(mysql2).createPool(o.url ? { uri: o.url, ...stripUndefined(settings) } : settings),
64
+ });
65
+ }
66
+ function sqlite(o) {
67
+ const [BetterSqlite3] = drivers("sqlite");
68
+ const Database = defaultExport(BetterSqlite3);
69
+ // A factory, so a bad path fails on the first query (the startup check)
70
+ // rather than when the app is created.
71
+ return new ((0, engine_1.kysely)().SqliteDialect)({
72
+ database: async () => new Database(o.database),
73
+ });
74
+ }
75
+ /** SQL Server settings from an `mssql://user:pass@host:1433/db?encrypt=false` URL. */
76
+ function mssqlUrl(url) {
77
+ const parsed = new URL(url);
78
+ const flag = (name) => {
79
+ const value = parsed.searchParams.get(name);
80
+ return value === null ? undefined : value === "true";
81
+ };
82
+ return stripUndefined({
83
+ host: parsed.hostname,
84
+ port: parsed.port ? Number(parsed.port) : undefined,
85
+ user: parsed.username ? decodeURIComponent(parsed.username) : undefined,
86
+ password: parsed.password ? decodeURIComponent(parsed.password) : undefined,
87
+ database: parsed.pathname.replace(/^\//, "") || undefined,
88
+ ssl: flag("encrypt"),
89
+ trustServerCertificate: flag("trustServerCertificate"),
90
+ });
91
+ }
92
+ function mssql(options) {
93
+ const [tedious, tarn] = drivers("mssql");
94
+ const o = options.url
95
+ ? {
96
+ ...mssqlUrl(options.url),
97
+ ...stripUndefined({ ...options, url: undefined }),
98
+ }
99
+ : options;
100
+ const Tedious = defaultExport(tedious);
101
+ return new ((0, engine_1.kysely)().MssqlDialect)({
102
+ tarn: {
103
+ ...defaultExport(tarn),
104
+ options: { min: options.pool.min ?? 0, max: options.pool.max ?? 10 },
105
+ },
106
+ tedious: {
107
+ ...Tedious,
108
+ connectionFactory: () => new Tedious.Connection({
109
+ server: o.host,
110
+ authentication: {
111
+ type: "default",
112
+ options: { userName: o.user, password: o.password },
113
+ },
114
+ options: {
115
+ port: o.port ?? 1433,
116
+ database: o.database,
117
+ encrypt: o.ssl !== false,
118
+ trustServerCertificate: o.trustServerCertificate ?? false,
119
+ },
120
+ }),
121
+ },
122
+ });
123
+ }
124
+ function stripUndefined(value) {
125
+ return Object.fromEntries(Object.entries(value).filter(([, v]) => v !== undefined));
126
+ }
127
+ const FACTORIES = {
128
+ postgres,
129
+ mysql,
130
+ sqlite,
131
+ mssql,
132
+ };
133
+ /** A connection for `options`. Nothing connects until the first query. */
134
+ function createDatabase(options) {
135
+ const { Kysely: KyselyClass } = (0, engine_1.kysely)();
136
+ return new KyselyClass({ dialect: FACTORIES[options.dialect](options) });
137
+ }
138
+ /** Run a trivial query, so a wrong host or password fails at startup with a clear message. */
139
+ async function verifyConnection(db, options) {
140
+ try {
141
+ await (0, engine_1.kysely)().sql `select 1`.execute(db);
142
+ }
143
+ catch (err) {
144
+ throw new config_1.DatabaseConfigError(`Could not connect to ${(0, config_1.describeConnection)(options)}: ${err?.message ?? err}`);
145
+ }
146
+ }
147
+ //# sourceMappingURL=connect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"connect.js","sourceRoot":"","sources":["../../src/orm/connect.ts"],"names":[],"mappings":";;AAgKA,wCAGC;AAGD,4CAWC;AA3KD,iCAA+C;AAC/C,qCAAkC;AAClC,qCAKkB;AAElB,MAAM,OAAO,GAA8B;IACzC,QAAQ,EAAE,CAAC,IAAI,CAAC;IAChB,KAAK,EAAE,CAAC,QAAQ,CAAC;IACjB,MAAM,EAAE,CAAC,gBAAgB,CAAC;IAC1B,KAAK,EAAE,CAAC,SAAS,EAAE,MAAM,CAAC;CAC3B,CAAC;AAEF,gFAAgF;AAChF,SAAS,OAAO,CAAC,OAAgB;IAC/B,MAAM,OAAO,GAAa,EAAE,CAAC;IAC7B,MAAM,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;QAC3C,IAAI,CAAC;YACH,OAAO,IAAA,4BAAqB,EAAC,IAAI,CAAC,CAAC;QAC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AM,EAAE,WAAW,EAAE,GAAG,IAAA,eAAM,GAAE,CAAC;IACzC,OAAO,IAAI,WAAW,CAAM,EAAE,OAAO,EAAE,SAAS,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;AAChF,CAAC;AAED,8FAA8F;AACvF,KAAK,UAAU,gBAAgB,CACpC,EAAe,EACf,OAAgC;IAEhC,IAAI,CAAC;QACH,MAAM,IAAA,eAAM,GAAE,CAAC,GAAG,CAAA,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAC3C,CAAC;IAAC,OAAO,GAAQ,EAAE,CAAC;QAClB,MAAM,IAAI,4BAAmB,CAC3B,wBAAwB,IAAA,2BAAkB,EAAC,OAAO,CAAC,KAAK,GAAG,EAAE,OAAO,IAAI,GAAG,EAAE,CAC9E,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import type { Kysely, Transaction } from "kysely";
2
- import { Token } from "../di";
2
+ import { Token } from "./core";
3
3
  /** DI token holding the ORM's root Kysely instance. */
4
4
  export declare const ORM_CONNECTION: Token<Kysely<any>>;
5
5
  export declare function setConnection(db: Kysely<any>): void;
@@ -7,8 +7,12 @@ export declare function setConnection(db: Kysely<any>): void;
7
7
  export declare function getConnection(): Kysely<any>;
8
8
  /**
9
9
  * Run `fn` in a transaction. Every model query made inside it, however deep
10
- * in the call chain, uses the transaction. A nested call joins the outer
11
- * transaction. The transaction commits when `fn` resolves and rolls back if
12
- * it throws.
10
+ * in the call chain, uses the transaction. The transaction commits when `fn`
11
+ * resolves and rolls back if it throws.
12
+ *
13
+ * A nested call joins the outer transaction; with `savepoint: true` it runs
14
+ * in a savepoint instead, so its failure rolls back only its own work.
13
15
  */
14
- export declare function runInTransaction<T>(fn: (trx: Transaction<any>) => Promise<T>): Promise<T>;
16
+ export declare function runInTransaction<T>(fn: (trx: Transaction<any>) => Promise<T>, options?: {
17
+ savepoint?: boolean;
18
+ }): Promise<T>;
@@ -11,23 +11,25 @@ exports.runInTransaction = runInTransaction;
11
11
  * @url https://github.com/xtareq
12
12
  *
13
13
  * The ORM's Kysely connection, and the transaction the current async call
14
- * chain runs in. Only Kysely's types are imported: the app passes its own
15
- * Kysely instance, so core never loads Kysely itself.
14
+ * chain runs in. Kysely is the engine under the ORM: apps configure the
15
+ * connection with `app.useMeghORM()` and never create it themselves.
16
16
  */
17
17
  const node_async_hooks_1 = require("node:async_hooks");
18
- const di_1 = require("../di");
18
+ const core_1 = require("./core");
19
+ const engine_1 = require("./engine");
20
+ const casts_1 = require("./casts");
19
21
  /** DI token holding the ORM's root Kysely instance. */
20
- exports.ORM_CONNECTION = new di_1.Token("MEGH_ORM_CONNECTION");
22
+ exports.ORM_CONNECTION = new core_1.Token("MEGH_ORM_CONNECTION");
21
23
  const currentTransaction = new node_async_hooks_1.AsyncLocalStorage();
22
24
  function setConnection(db) {
23
- di_1.Container.set(exports.ORM_CONNECTION, db);
25
+ core_1.Container.set(exports.ORM_CONNECTION, db);
24
26
  }
25
27
  function rootConnection() {
26
- if (!di_1.Container.has(exports.ORM_CONNECTION)) {
27
- throw new Error("[Megh ORM] No database connection. Call app.useDatabase(kysely) " +
28
- "(or Model.useConnection(kysely) outside an app) first.");
28
+ if (!core_1.Container.has(exports.ORM_CONNECTION)) {
29
+ throw new Error("[Megh ORM] No database connection. Call app.useMeghORM() " +
30
+ "(or Model.useConnection(db) outside an app) first.");
29
31
  }
30
- return di_1.Container.get(exports.ORM_CONNECTION);
32
+ return core_1.Container.get(exports.ORM_CONNECTION);
31
33
  }
32
34
  /** The transaction this call chain runs in, else the root connection. */
33
35
  function getConnection() {
@@ -35,16 +37,41 @@ function getConnection() {
35
37
  }
36
38
  /**
37
39
  * Run `fn` in a transaction. Every model query made inside it, however deep
38
- * in the call chain, uses the transaction. A nested call joins the outer
39
- * transaction. The transaction commits when `fn` resolves and rolls back if
40
- * it throws.
40
+ * in the call chain, uses the transaction. The transaction commits when `fn`
41
+ * resolves and rolls back if it throws.
42
+ *
43
+ * A nested call joins the outer transaction; with `savepoint: true` it runs
44
+ * in a savepoint instead, so its failure rolls back only its own work.
41
45
  */
42
- function runInTransaction(fn) {
46
+ function runInTransaction(fn, options = {}) {
43
47
  const outer = currentTransaction.getStore();
44
48
  if (outer)
45
- return fn(outer);
49
+ return options.savepoint ? inSavepoint(outer, fn) : fn(outer);
46
50
  return rootConnection()
47
51
  .transaction()
48
52
  .execute((trx) => currentTransaction.run(trx, () => fn(trx)));
49
53
  }
54
+ let savepoints = 0;
55
+ async function inSavepoint(trx, fn) {
56
+ const name = `megh_sp_${++savepoints}`;
57
+ // SQL Server has its own syntax and no RELEASE.
58
+ const mssql = (0, casts_1.usesAdapter)(trx, "MssqlAdapter");
59
+ const run = (text) => raw(text).execute(trx);
60
+ await run(mssql ? `save transaction ${name}` : `savepoint ${name}`);
61
+ try {
62
+ const result = await fn(trx);
63
+ if (!mssql)
64
+ await run(`release savepoint ${name}`);
65
+ return result;
66
+ }
67
+ catch (err) {
68
+ await run(mssql ? `rollback transaction ${name}` : `rollback to savepoint ${name}`);
69
+ if (!mssql)
70
+ await run(`release savepoint ${name}`);
71
+ throw err;
72
+ }
73
+ }
74
+ function raw(text) {
75
+ return (0, engine_1.kysely)().sql.raw(text);
76
+ }
50
77
  //# sourceMappingURL=connection.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"connection.js","sourceRoot":"","sources":["../../src/orm/connection.ts"],"names":[],"mappings":";;;AAmBA,sCAEC;AAaD,sCAEC;AAQD,4CAQC;AApDD;;;;;;;;;GASG;AACH,uDAAqD;AAErD,8BAAyC;AAEzC,uDAAuD;AAC1C,QAAA,cAAc,GAAG,IAAI,UAAK,CAAc,qBAAqB,CAAC,CAAC;AAE5E,MAAM,kBAAkB,GAAG,IAAI,oCAAiB,EAAoB,CAAC;AAErE,SAAgB,aAAa,CAAC,EAAe;IAC3C,cAAS,CAAC,GAAG,CAAC,sBAAc,EAAE,EAAE,CAAC,CAAC;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AM,MAAM,GAAG,MAAM,EAAE,CAAC,GAAG,CAAC,CAAC;QAC7B,IAAI,CAAC,KAAK;YAAE,MAAM,GAAG,CAAC,qBAAqB,IAAI,EAAE,CAAC,CAAC;QACnD,OAAO,MAAM,CAAC;IAChB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,MAAM,GAAG,CACP,KAAK,CAAC,CAAC,CAAC,wBAAwB,IAAI,EAAE,CAAC,CAAC,CAAC,yBAAyB,IAAI,EAAE,CACzE,CAAC;QACF,IAAI,CAAC,KAAK;YAAE,MAAM,GAAG,CAAC,qBAAqB,IAAI,EAAE,CAAC,CAAC;QACnD,MAAM,GAAG,CAAC;IACZ,CAAC;AACH,CAAC;AAED,SAAS,GAAG,CAAC,IAAY;IACvB,OAAO,IAAA,eAAM,GAAE,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AAChC,CAAC"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Everything the ORM uses from Megh's core, in one place.
3
+ *
4
+ * The ORM is written as a plugin so it can move to its own package,
5
+ * `@meghjs/orm`. Files in `orm/` import core only through this module (a lint
6
+ * rule enforces it), so the move is: point these exports at "@meghjs/core"
7
+ * and change the `declare module` path below.
8
+ */
9
+ import type { DatabaseOptions } from "./config";
10
+ import type { MeghConfigClass } from "../config/config";
11
+ import type { Kysely } from "kysely";
12
+ export { Container, Token } from "../di";
13
+ export type { ContainerApi } from "../di";
14
+ export { BadRequestException, NotFoundException } from "../common/exceptions";
15
+ export { SystemUseError } from "../common/exceptions/system-exception";
16
+ export { normalizePageRequest, paginated, type PageRequest, type Paginated, } from "../collection/pagination";
17
+ export { loadPackageFromClient } from "../common/utils/common-utils";
18
+ export { MeghConfig, type MeghConfigClass } from "../config/config";
19
+ export { Environment } from "../config/environment-variables";
20
+ export { definePlugin, type PluginContext } from "../plugins/plugin";
21
+ declare module "../core/application" {
22
+ interface MeghApplication {
23
+ /**
24
+ * Connect Megh's ORM. Pass the connection settings, an `@AppConfig`
25
+ * class that builds them, or nothing to read them from the environment
26
+ * (`DATABASE_URL`, or `DB_DIALECT`, `DB_HOST`, `DB_PORT`, `DB_USER`,
27
+ * `DB_PASSWORD`, `DB_NAME`).
28
+ *
29
+ * The connection is checked when the app starts and closed when it
30
+ * shuts down.
31
+ *
32
+ * ```ts
33
+ * app.useMeghORM({ dialect: "postgres", url: process.env.DATABASE_URL });
34
+ * ```
35
+ */
36
+ useMeghORM(options?: DatabaseOptions | MeghConfigClass<DatabaseOptions>): this;
37
+ /**
38
+ * @deprecated Use `useMeghORM()`, which creates the connection for you.
39
+ * Kept for apps that build their own Kysely instance.
40
+ */
41
+ useDatabase(db: Kysely<any>): this;
42
+ }
43
+ }
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.definePlugin = exports.Environment = exports.MeghConfig = exports.loadPackageFromClient = exports.paginated = exports.normalizePageRequest = exports.SystemUseError = exports.NotFoundException = exports.BadRequestException = exports.Token = exports.Container = void 0;
4
+ var di_1 = require("../di");
5
+ Object.defineProperty(exports, "Container", { enumerable: true, get: function () { return di_1.Container; } });
6
+ Object.defineProperty(exports, "Token", { enumerable: true, get: function () { return di_1.Token; } });
7
+ var exceptions_1 = require("../common/exceptions");
8
+ Object.defineProperty(exports, "BadRequestException", { enumerable: true, get: function () { return exceptions_1.BadRequestException; } });
9
+ Object.defineProperty(exports, "NotFoundException", { enumerable: true, get: function () { return exceptions_1.NotFoundException; } });
10
+ var system_exception_1 = require("../common/exceptions/system-exception");
11
+ Object.defineProperty(exports, "SystemUseError", { enumerable: true, get: function () { return system_exception_1.SystemUseError; } });
12
+ var pagination_1 = require("../collection/pagination");
13
+ Object.defineProperty(exports, "normalizePageRequest", { enumerable: true, get: function () { return pagination_1.normalizePageRequest; } });
14
+ Object.defineProperty(exports, "paginated", { enumerable: true, get: function () { return pagination_1.paginated; } });
15
+ var common_utils_1 = require("../common/utils/common-utils");
16
+ Object.defineProperty(exports, "loadPackageFromClient", { enumerable: true, get: function () { return common_utils_1.loadPackageFromClient; } });
17
+ var config_1 = require("../config/config");
18
+ Object.defineProperty(exports, "MeghConfig", { enumerable: true, get: function () { return config_1.MeghConfig; } });
19
+ var environment_variables_1 = require("../config/environment-variables");
20
+ Object.defineProperty(exports, "Environment", { enumerable: true, get: function () { return environment_variables_1.Environment; } });
21
+ var plugin_1 = require("../plugins/plugin");
22
+ Object.defineProperty(exports, "definePlugin", { enumerable: true, get: function () { return plugin_1.definePlugin; } });
23
+ //# sourceMappingURL=core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core.js","sourceRoot":"","sources":["../../src/orm/core.ts"],"names":[],"mappings":";;;AAYA,4BAAyC;AAAhC,+FAAA,SAAS,OAAA;AAAE,2FAAA,KAAK,OAAA;AAEzB,mDAA8E;AAArE,iHAAA,mBAAmB,OAAA;AAAE,+GAAA,iBAAiB,OAAA;AAC/C,0EAAuE;AAA9D,kHAAA,cAAc,OAAA;AACvB,uDAKkC;AAJhC,kHAAA,oBAAoB,OAAA;AACpB,uGAAA,SAAS,OAAA;AAIX,6DAAqE;AAA5D,qHAAA,qBAAqB,OAAA;AAC9B,2CAAoE;AAA3D,oGAAA,UAAU,OAAA;AACnB,yEAA8D;AAArD,oHAAA,WAAW,OAAA;AACpB,4CAAqE;AAA5D,sGAAA,YAAY,OAAA"}
@@ -0,0 +1,171 @@
1
+ import type { PageRequest, Paginated } from "./core";
2
+ export type Operator = "=" | "!=" | "<>" | "<" | "<=" | ">" | ">=" | "like" | "not like" | "ilike" | "in" | "not in" | "is" | "is not";
3
+ type Row = Record<string, any>;
4
+ type Expr = (eb: any, sqlite: boolean) => any;
5
+ type Clause = {
6
+ or: boolean;
7
+ build: Expr;
8
+ };
9
+ /** `where` conditions, shared by queries and nested groups. */
10
+ export declare class WhereClauses<Self> {
11
+ /** @internal */
12
+ protected clauses: Clause[];
13
+ private push;
14
+ private comparison;
15
+ /**
16
+ * `where("status", "paid")`, `where("total", ">", 100)`,
17
+ * `where({ status: "paid", currency: "EUR" })`, or a group:
18
+ * `where((q) => q.where("a", 1).orWhere("b", 2))`.
19
+ */
20
+ where(column: string, value: unknown): Self;
21
+ where(column: string, operator: Operator, value: unknown): Self;
22
+ where(group: (query: WhereGroup) => void): Self;
23
+ where(values: Row): Self;
24
+ orWhere(column: string, value: unknown): Self;
25
+ orWhere(column: string, operator: Operator, value: unknown): Self;
26
+ orWhere(group: (query: WhereGroup) => void): Self;
27
+ orWhere(values: Row): Self;
28
+ whereIn(column: string, values: readonly unknown[]): Self;
29
+ whereNotIn(column: string, values: readonly unknown[]): Self;
30
+ whereNull(column: string): Self;
31
+ whereNotNull(column: string): Self;
32
+ orWhereNull(column: string): Self;
33
+ orWhereNotNull(column: string): Self;
34
+ whereBetween(column: string, [from, to]: readonly [unknown, unknown]): Self;
35
+ whereNotBetween(column: string, [from, to]: readonly [unknown, unknown]): Self;
36
+ /** Compare two columns: `whereColumn("updated_at", ">", "created_at")`. */
37
+ whereColumn(left: string, operator: Operator, right: string): Self;
38
+ /** A condition in SQL, with `?` placeholders: `whereRaw("lower(email) = ?", [email])`. */
39
+ whereRaw(sql: string, bindings?: readonly unknown[]): Self;
40
+ orWhereRaw(sql: string, bindings?: readonly unknown[]): Self;
41
+ /**
42
+ * @internal The conditions as one expression. `and` binds tighter than
43
+ * `or`, as in SQL: `a or b and c` is `a or (b and c)`.
44
+ */
45
+ toExpression(eb: any, sqlite: boolean): any;
46
+ /** @internal */
47
+ hasClauses(): boolean;
48
+ }
49
+ /** The conditions inside `where((q) => …)`, put in parentheses. */
50
+ export declare class WhereGroup extends WhereClauses<WhereGroup> {
51
+ }
52
+ /** A query on one table. Build it with `DB.table(name)`. */
53
+ export declare class TableQuery<T extends Row = Row> extends WhereClauses<TableQuery<T>> {
54
+ private readonly tableName;
55
+ private columns;
56
+ private rawColumns;
57
+ private isDistinct;
58
+ private joins;
59
+ private orders;
60
+ private groups;
61
+ private havings;
62
+ private limitValue?;
63
+ private offsetValue?;
64
+ constructor(tableName: string);
65
+ /** Columns to return; `"users.email as contact"` aliases one. All columns by default. */
66
+ select(...columns: string[]): this;
67
+ /** A selected expression in SQL: `selectRaw("count(*) as total")`. */
68
+ selectRaw(sql: string, bindings?: readonly unknown[]): this;
69
+ distinct(): this;
70
+ /** Inner join: `join("users", "users.id", "orders.user_id")` or with an operator. */
71
+ join(table: string, left: string, right: string): this;
72
+ join(table: string, left: string, operator: Operator, right: string): this;
73
+ leftJoin(table: string, left: string, right: string): this;
74
+ leftJoin(table: string, left: string, operator: Operator, right: string): this;
75
+ rightJoin(table: string, left: string, right: string): this;
76
+ rightJoin(table: string, left: string, operator: Operator, right: string): this;
77
+ private addJoin;
78
+ orderBy(column: string, direction?: "asc" | "desc"): this;
79
+ orderByDesc(column: string): this;
80
+ /** Order in SQL: `orderByRaw("random()")`. */
81
+ orderByRaw(sql: string): this;
82
+ /** Newest first, by `created_at` unless another column is given. */
83
+ latest(column?: string): this;
84
+ oldest(column?: string): this;
85
+ groupBy(...columns: string[]): this;
86
+ /** A condition on grouped rows: `having("total", ">", 100)`. */
87
+ having(column: string, operator: Operator, value: unknown): this;
88
+ havingRaw(sql: string, bindings?: readonly unknown[]): this;
89
+ limit(count: number): this;
90
+ offset(count: number): this;
91
+ /** Same as `limit()`. */
92
+ take(count: number): this;
93
+ /** Same as `offset()`. */
94
+ skip(count: number): this;
95
+ /** Limit and offset for page `page` (from 1) of `perPage` rows. */
96
+ forPage(page: number, perPage?: number): this;
97
+ /** Every matching row. */
98
+ get(): Promise<T[]>;
99
+ /** The first matching row, or `undefined`. */
100
+ first(): Promise<T | undefined>;
101
+ /** The first matching row, or a 404 `NotFoundException`. */
102
+ firstOrFail(): Promise<T>;
103
+ /** One column of the first matching row. */
104
+ value<V = unknown>(column: string): Promise<V | undefined>;
105
+ /** One column of every matching row. */
106
+ pluck<V = unknown>(column: string): Promise<V[]>;
107
+ exists(): Promise<boolean>;
108
+ doesntExist(): Promise<boolean>;
109
+ /** Number of matching rows (of non-null `column` values, if given). */
110
+ count(column?: string): Promise<number>;
111
+ /** Sum of `column`; `0` when nothing matches. */
112
+ sum(column: string): Promise<number>;
113
+ /** Average of `column`; `null` when nothing matches. */
114
+ avg(column: string): Promise<number | null>;
115
+ min<V = unknown>(column: string): Promise<V | null>;
116
+ max<V = unknown>(column: string): Promise<V | null>;
117
+ /** One page of rows and the total: `{ data, meta: { total, page, perPage, lastPage, … } }`. */
118
+ paginate(request?: PageRequest): Promise<Paginated<T>>;
119
+ /**
120
+ * Insert one row or several, in order. Consecutive rows with the same
121
+ * columns go in one statement; a column a row leaves out gets its
122
+ * database default.
123
+ */
124
+ insert(rows: Row | Row[]): Promise<void>;
125
+ /** Insert a row and return the id the database gave it. */
126
+ insertGetId<Id = number>(row: Row, idColumn?: string): Promise<Id>;
127
+ /** Update the matching rows. Returns how many were updated. */
128
+ update(values: Row): Promise<number>;
129
+ /** Add `amount` (default 1) to `column` on the matching rows, and set `extra` columns. */
130
+ increment(column: string, amount?: number, extra?: Row): Promise<number>;
131
+ decrement(column: string, amount?: number, extra?: Row): Promise<number>;
132
+ /** Delete the matching rows. Returns how many were deleted. */
133
+ delete(): Promise<number>;
134
+ /** The SQL and parameters this query would run, for debugging. */
135
+ toSQL(): {
136
+ sql: string;
137
+ parameters: readonly unknown[];
138
+ };
139
+ /** A copy to build on without changing this query. */
140
+ clone(): TableQuery<T>;
141
+ private reset;
142
+ private whereOn;
143
+ private base;
144
+ private selectQuery;
145
+ private aggregate;
146
+ private adjust;
147
+ }
148
+ /**
149
+ * The query builder and raw SQL.
150
+ *
151
+ * ```ts
152
+ * await DB.table("users").where("active", true).count();
153
+ * await DB.select("select * from users where email = ?", [email]);
154
+ * await DB.transaction(async () => { … });
155
+ * ```
156
+ */
157
+ export declare const DB: {
158
+ /** A query on `table`. Type the rows with `DB.table<{ id: number; email: string }>("users")`. */
159
+ table<T extends Row = Row>(table: string): TableQuery<T>;
160
+ /** Run a query in SQL and return its rows. Use `?` for values. */
161
+ select<T extends Row = Row>(sql: string, bindings?: readonly unknown[]): Promise<T[]>;
162
+ /** Run a statement in SQL (insert, update, DDL, …). Returns the number of rows it changed, when known. */
163
+ statement(sql: string, bindings?: readonly unknown[]): Promise<number>;
164
+ /**
165
+ * Run `fn` in a transaction that every query inside uses, models included.
166
+ * Commits when `fn` resolves, rolls back if it throws. Nested calls run in
167
+ * a savepoint.
168
+ */
169
+ transaction<R>(fn: () => Promise<R>): Promise<R>;
170
+ };
171
+ export {};