@h3ravel/arquebus 0.4.0 → 0.5.0
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/bin/index.cjs +47 -56
- package/bin/index.js +49 -57
- package/dist/browser/index.d.cts +3648 -176
- package/dist/browser/index.d.ts +3648 -176
- package/dist/index.cjs +21 -28
- package/dist/index.d.cts +3757 -287
- package/dist/index.d.ts +3757 -287
- package/dist/index.js +22 -29
- package/dist/inspector/index.cjs +17 -13
- package/dist/inspector/index.js +17 -13
- package/dist/migrations/index.cjs +30 -39
- package/dist/migrations/index.d.cts +3500 -18
- package/dist/migrations/index.d.ts +3500 -18
- package/dist/migrations/index.js +43 -45
- package/package.json +17 -17
- package/types/builder.ts +21 -16
- package/types/generics.ts +1 -0
- package/types/modeling.ts +7 -7
- package/types/query-builder.ts +9 -2
- package/types/query-methods.ts +20 -0
- package/types/utils.ts +1 -0
package/dist/migrations/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { __export } from "./chunk-PECeCxCb.js";
|
|
2
|
-
import { Logger } from "@h3ravel/shared";
|
|
2
|
+
import { FileSystem, Logger, TaskManager } from "@h3ravel/shared";
|
|
3
3
|
import { assign, camel, diff, flat, get, isArray, isEmpty, isEqual, isString, omit, pick, set, snake, trim } from "radashi";
|
|
4
4
|
import advancedFormat from "dayjs/plugin/advancedFormat.js";
|
|
5
5
|
import dayjs from "dayjs";
|
|
@@ -8,12 +8,11 @@ import Knex$1 from "knex";
|
|
|
8
8
|
import path from "path";
|
|
9
9
|
import { existsSync } from "fs";
|
|
10
10
|
import pluralize from "pluralize";
|
|
11
|
-
import fs from "node:fs/promises";
|
|
11
|
+
import fs, { copyFile, mkdir, readFile, readdir, writeFile } from "node:fs/promises";
|
|
12
12
|
import { access } from "fs/promises";
|
|
13
13
|
import escalade from "escalade/sync";
|
|
14
14
|
import resolveFrom from "resolve-from";
|
|
15
|
-
import
|
|
16
|
-
import { dirname } from "node:path";
|
|
15
|
+
import path$1, { dirname } from "node:path";
|
|
17
16
|
import { fileURLToPath } from "node:url";
|
|
18
17
|
|
|
19
18
|
//#region src/migrations/migration-repository.ts
|
|
@@ -2338,8 +2337,8 @@ var HasManyThrough = class extends relation_default {
|
|
|
2338
2337
|
const results = await this.take(1).get(columns);
|
|
2339
2338
|
return results.count() > 0 ? results.first() : null;
|
|
2340
2339
|
}
|
|
2341
|
-
async firstOrFail(columns
|
|
2342
|
-
const model = await this.first(columns);
|
|
2340
|
+
async firstOrFail(...columns) {
|
|
2341
|
+
const model = await this.first(...columns);
|
|
2343
2342
|
if (model) return model;
|
|
2344
2343
|
throw new ModelNotFoundError().setModel(this.related.constructor);
|
|
2345
2344
|
}
|
|
@@ -2378,7 +2377,7 @@ var HasManyThrough = class extends relation_default {
|
|
|
2378
2377
|
}
|
|
2379
2378
|
async paginate(perPage = null, columns = ["*"], pageName = "page", page = null) {
|
|
2380
2379
|
this.query.addSelect(this.shouldSelect(columns));
|
|
2381
|
-
return await this.query.paginate(perPage, columns, pageName, page);
|
|
2380
|
+
return await this.query.paginate(perPage ?? 15, columns, pageName, page);
|
|
2382
2381
|
}
|
|
2383
2382
|
shouldSelect(columns = ["*"]) {
|
|
2384
2383
|
if ((columns === null || columns === void 0 ? void 0 : columns.at(0)) == "*") columns = [this.related.getTable() + ".*"];
|
|
@@ -2656,9 +2655,6 @@ const UniqueIds = (Model$1) => {
|
|
|
2656
2655
|
uniqueIds() {
|
|
2657
2656
|
return [];
|
|
2658
2657
|
}
|
|
2659
|
-
newUniqueId() {
|
|
2660
|
-
return null;
|
|
2661
|
-
}
|
|
2662
2658
|
setUniqueIds() {
|
|
2663
2659
|
const uniqueIds = this.uniqueIds();
|
|
2664
2660
|
for (const column of uniqueIds) if (this[column] === null || this[column] === void 0) this[column] = this.newUniqueId();
|
|
@@ -2680,6 +2676,7 @@ var Paginator = class {
|
|
|
2680
2676
|
options = {};
|
|
2681
2677
|
static setFormatter(formatter) {
|
|
2682
2678
|
if (typeof formatter !== "function" && formatter !== null && formatter !== void 0) throw new Error("Paginator formatter must be a function or null");
|
|
2679
|
+
if (!formatter) return;
|
|
2683
2680
|
this.formatter = formatter;
|
|
2684
2681
|
}
|
|
2685
2682
|
constructor(items, total, perPage, currentPage = 1, options = {}) {
|
|
@@ -2768,8 +2765,10 @@ var QueryBuilder = class QueryBuilder extends Inference$2 {
|
|
|
2768
2765
|
asProxy() {
|
|
2769
2766
|
return new Proxy(this, {
|
|
2770
2767
|
get: function(target, prop) {
|
|
2768
|
+
var _target$connector$cli;
|
|
2771
2769
|
if (typeof target[prop] !== "undefined") return target[prop];
|
|
2772
2770
|
if (["destroy", "schema"].includes(prop)) return target.connector.schema;
|
|
2771
|
+
const skipReturning = !!((_target$connector$cli = target.connector.client.config) === null || _target$connector$cli === void 0 || (_target$connector$cli = _target$connector$cli.client) === null || _target$connector$cli === void 0 ? void 0 : _target$connector$cli.includes("mysql")) && prop === "returning";
|
|
2773
2772
|
if ([
|
|
2774
2773
|
"select",
|
|
2775
2774
|
"from",
|
|
@@ -2832,7 +2831,7 @@ var QueryBuilder = class QueryBuilder extends Inference$2 {
|
|
|
2832
2831
|
"clearWhere",
|
|
2833
2832
|
"clearHaving",
|
|
2834
2833
|
"clearGroup"
|
|
2835
|
-
].includes(prop)) return (...args) => {
|
|
2834
|
+
].includes(prop) && !skipReturning) return (...args) => {
|
|
2836
2835
|
target.connector[prop](...args);
|
|
2837
2836
|
return target.asProxy();
|
|
2838
2837
|
};
|
|
@@ -2889,7 +2888,7 @@ var QueryBuilder = class QueryBuilder extends Inference$2 {
|
|
|
2889
2888
|
} while (countResults === count);
|
|
2890
2889
|
return true;
|
|
2891
2890
|
}
|
|
2892
|
-
async paginate(page = 1, perPage = 15) {
|
|
2891
|
+
async paginate(page = 1, perPage = 15, _pageName, _page) {
|
|
2893
2892
|
const total = await this.clone().clearOrder().count("*");
|
|
2894
2893
|
let results;
|
|
2895
2894
|
if (total > 0) {
|
|
@@ -3128,14 +3127,15 @@ var arquebus_default = arquebus;
|
|
|
3128
3127
|
|
|
3129
3128
|
//#endregion
|
|
3130
3129
|
//#region src/model.ts
|
|
3131
|
-
const
|
|
3130
|
+
const ModelClass = class {};
|
|
3131
|
+
const BaseModel = compose(ModelClass, has_attributes_default, hides_attributes_default, has_relations_default, has_timestamps_default, has_hooks_default, has_global_scopes_default, unique_ids_default);
|
|
3132
3132
|
var Model = class Model extends BaseModel {
|
|
3133
|
-
primaryKey = "id";
|
|
3134
3133
|
builder = null;
|
|
3135
3134
|
table = null;
|
|
3136
3135
|
keyType = "int";
|
|
3137
3136
|
incrementing = true;
|
|
3138
3137
|
withCount = [];
|
|
3138
|
+
primaryKey = "id";
|
|
3139
3139
|
perPage = 15;
|
|
3140
3140
|
static globalScopes = {};
|
|
3141
3141
|
static pluginInitializers = {};
|
|
@@ -3145,6 +3145,7 @@ var Model = class Model extends BaseModel {
|
|
|
3145
3145
|
eagerLoad = {};
|
|
3146
3146
|
exists = false;
|
|
3147
3147
|
with = [];
|
|
3148
|
+
name;
|
|
3148
3149
|
trx = null;
|
|
3149
3150
|
constructor(attributes = {}) {
|
|
3150
3151
|
super();
|
|
@@ -3916,8 +3917,8 @@ var BelongsToMany = class extends compose(relation_default, interacts_with_pivot
|
|
|
3916
3917
|
const results = await this.take(1).get(columns);
|
|
3917
3918
|
return results.count() > 0 ? results.first() : null;
|
|
3918
3919
|
}
|
|
3919
|
-
async firstOrFail(columns
|
|
3920
|
-
const model = await this.first(columns);
|
|
3920
|
+
async firstOrFail(...columns) {
|
|
3921
|
+
const model = await this.first(...columns);
|
|
3921
3922
|
if (model !== null) return model;
|
|
3922
3923
|
throw new ModelNotFoundError().setModel(this.related.constructor);
|
|
3923
3924
|
}
|
|
@@ -4093,7 +4094,9 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4093
4094
|
}
|
|
4094
4095
|
asProxy() {
|
|
4095
4096
|
return new Proxy(this, { get(target, prop) {
|
|
4097
|
+
var _target$query$connect;
|
|
4096
4098
|
if (typeof target[prop] !== "undefined") return target[prop];
|
|
4099
|
+
const skipReturning = !!((_target$query$connect = target.query.connector) === null || _target$query$connect === void 0 || (_target$query$connect = _target$query$connect.client.config) === null || _target$query$connect === void 0 || (_target$query$connect = _target$query$connect.client) === null || _target$query$connect === void 0 ? void 0 : _target$query$connect.includes("mysql")) && prop === "returning";
|
|
4097
4100
|
if ([
|
|
4098
4101
|
"select",
|
|
4099
4102
|
"from",
|
|
@@ -4156,7 +4159,7 @@ var Builder = class Builder extends Inference$1 {
|
|
|
4156
4159
|
"clearWhere",
|
|
4157
4160
|
"clearHaving",
|
|
4158
4161
|
"clearGroup"
|
|
4159
|
-
].includes(prop)) return (...args) => {
|
|
4162
|
+
].includes(prop) && !skipReturning) return (...args) => {
|
|
4160
4163
|
target.query[prop](...args);
|
|
4161
4164
|
return target.asProxy();
|
|
4162
4165
|
};
|
|
@@ -4988,13 +4991,13 @@ var Migrator = class {
|
|
|
4988
4991
|
async runUp(file, batch, _pretend) {
|
|
4989
4992
|
const migration = await this.resolvePath(file);
|
|
4990
4993
|
const name = this.getMigrationName(file);
|
|
4991
|
-
await
|
|
4994
|
+
await TaskManager.taskRunner(name, () => this.runMigration(migration, "up"));
|
|
4992
4995
|
await this.repository.log(name, batch);
|
|
4993
4996
|
}
|
|
4994
4997
|
async runDown(file, migration, _pretend) {
|
|
4995
4998
|
const instance = await this.resolvePath(file);
|
|
4996
4999
|
const name = this.getMigrationName(file);
|
|
4997
|
-
await
|
|
5000
|
+
await TaskManager.taskRunner(name, () => this.runMigration(instance, "down"));
|
|
4998
5001
|
await this.repository.delete(migration);
|
|
4999
5002
|
}
|
|
5000
5003
|
async rollback(paths = [], options = {}) {
|
|
@@ -5045,7 +5048,7 @@ var Migrator = class {
|
|
|
5045
5048
|
const inspector = SchemaInspector.inspect(connection);
|
|
5046
5049
|
await connection.raw("SET foreign_key_checks = 0");
|
|
5047
5050
|
/** Drop all existing tables */
|
|
5048
|
-
for (const table of await inspector.tables())
|
|
5051
|
+
for (const table of await inspector.tables()) await TaskManager.taskRunner(`Dropping ${Logger.parse([[table, "grey"]], "", false)} table`, () => connection.schema.dropTableIfExists(table));
|
|
5049
5052
|
await connection.raw("SET foreign_key_checks = 1");
|
|
5050
5053
|
/** Create the migration repository */
|
|
5051
5054
|
await this.repository.createRepository();
|
|
@@ -5126,17 +5129,6 @@ var Migrator = class {
|
|
|
5126
5129
|
write(...args) {
|
|
5127
5130
|
if (this.output) console.log(...args);
|
|
5128
5131
|
}
|
|
5129
|
-
async taskRunner(description, task) {
|
|
5130
|
-
const startTime = process.hrtime();
|
|
5131
|
-
let result = false;
|
|
5132
|
-
try {
|
|
5133
|
-
result = await Promise.all([(task || (() => true))()].flat());
|
|
5134
|
-
} finally {
|
|
5135
|
-
const endTime = process.hrtime(startTime);
|
|
5136
|
-
const duration = (endTime[0] * 1e9 + endTime[1]) / 1e6;
|
|
5137
|
-
Logger.twoColumnLog(Logger.parse([[description, "green"]], "", false), [Logger.parse([[`${Math.floor(duration)}ms`, "gray"]], "", false), Logger.parse([[result !== false ? "✔" : "✘", result !== false ? "green" : "red"]], "", false)].join(" "));
|
|
5138
|
-
}
|
|
5139
|
-
}
|
|
5140
5132
|
};
|
|
5141
5133
|
var migrator_default = Migrator;
|
|
5142
5134
|
|
|
@@ -5164,8 +5156,8 @@ var Utils = class {
|
|
|
5164
5156
|
if (!resolved) return;
|
|
5165
5157
|
return path.join(path.dirname(resolved), parts.join("/"));
|
|
5166
5158
|
}
|
|
5167
|
-
static async getMigrationPaths(cwd, migrator, defaultPath, path$
|
|
5168
|
-
if (path$
|
|
5159
|
+
static async getMigrationPaths(cwd, migrator, defaultPath, path$2) {
|
|
5160
|
+
if (path$2) return [join(cwd, path$2)];
|
|
5169
5161
|
return [...migrator.getPaths(), join(cwd, defaultPath)];
|
|
5170
5162
|
}
|
|
5171
5163
|
/**
|
|
@@ -5174,9 +5166,9 @@ var Utils = class {
|
|
|
5174
5166
|
* @param path
|
|
5175
5167
|
* @returns
|
|
5176
5168
|
*/
|
|
5177
|
-
static async fileExists(path$
|
|
5169
|
+
static async fileExists(path$2) {
|
|
5178
5170
|
try {
|
|
5179
|
-
await access(path$
|
|
5171
|
+
await access(path$2);
|
|
5180
5172
|
return true;
|
|
5181
5173
|
} catch {
|
|
5182
5174
|
return false;
|
|
@@ -5347,6 +5339,12 @@ var Migrate = class {
|
|
|
5347
5339
|
}
|
|
5348
5340
|
};
|
|
5349
5341
|
|
|
5342
|
+
//#endregion
|
|
5343
|
+
//#region node_modules/.pnpm/tsdown@0.15.4_typescript@5.9.2/node_modules/tsdown/esm-shims.js
|
|
5344
|
+
const getFilename = () => fileURLToPath(import.meta.url);
|
|
5345
|
+
const getDirname = () => path$1.dirname(getFilename());
|
|
5346
|
+
const __dirname = /* @__PURE__ */ getDirname();
|
|
5347
|
+
|
|
5350
5348
|
//#endregion
|
|
5351
5349
|
//#region src/migrations/migration-creator.ts
|
|
5352
5350
|
var MigrationCreator = class {
|
|
@@ -5365,10 +5363,10 @@ var MigrationCreator = class {
|
|
|
5365
5363
|
* @returns
|
|
5366
5364
|
*/
|
|
5367
5365
|
async create(name, dir, table, create = false) {
|
|
5368
|
-
const stub = this.getStub(table, create);
|
|
5366
|
+
const stub = await this.getStub(table, create);
|
|
5369
5367
|
const filePath = this.getPath(name, dir);
|
|
5370
5368
|
await this.ensureDirectoryExists(path.dirname(filePath));
|
|
5371
|
-
await
|
|
5369
|
+
await writeFile(filePath, this.populateStub(stub, table));
|
|
5372
5370
|
await this.firePostCreateHooks(table, filePath);
|
|
5373
5371
|
return filePath;
|
|
5374
5372
|
}
|
|
@@ -5379,29 +5377,29 @@ var MigrationCreator = class {
|
|
|
5379
5377
|
* @param callback
|
|
5380
5378
|
*/
|
|
5381
5379
|
async publish(dir, callback) {
|
|
5382
|
-
const migrationFiles = await
|
|
5380
|
+
const migrationFiles = await readdir(this.customStubPath ?? "");
|
|
5383
5381
|
await this.ensureDirectoryExists(dir);
|
|
5384
5382
|
for (const migrationFile of migrationFiles) {
|
|
5385
5383
|
const sourceFilePath = path.join(this.customStubPath ?? "", migrationFile);
|
|
5386
5384
|
const destinationFilePath = path.join(dir, migrationFile);
|
|
5387
|
-
await
|
|
5385
|
+
await copyFile(sourceFilePath, destinationFilePath);
|
|
5388
5386
|
if (callback) callback(migrationFile, sourceFilePath, destinationFilePath);
|
|
5389
5387
|
}
|
|
5390
5388
|
}
|
|
5391
|
-
getStub(table, create = false) {
|
|
5389
|
+
async getStub(table, create = false) {
|
|
5392
5390
|
let stub;
|
|
5393
5391
|
if (!table) {
|
|
5394
5392
|
const customPath = path.join(this.customStubPath ?? "", `migration-${this.type}.stub`);
|
|
5395
5393
|
console.log("\n", customPath, "---");
|
|
5396
|
-
stub =
|
|
5394
|
+
stub = await FileSystem.fileExists(customPath) ? customPath : this.stubPath(`/migration-${this.type}.stub`);
|
|
5397
5395
|
} else if (create) {
|
|
5398
5396
|
const customPath = path.join(this.customStubPath ?? "", `migration.create-${this.type}.stub`);
|
|
5399
|
-
stub =
|
|
5397
|
+
stub = await FileSystem.fileExists(customPath) ? customPath : this.stubPath(`/migration.create-${this.type}.stub`);
|
|
5400
5398
|
} else {
|
|
5401
5399
|
const customPath = path.join(this.customStubPath ?? "", `migration.update-${this.type}.stub`);
|
|
5402
|
-
stub =
|
|
5400
|
+
stub = await FileSystem.fileExists(customPath) ? customPath : this.stubPath(`/migration.update-${this.type}.stub`);
|
|
5403
5401
|
}
|
|
5404
|
-
return
|
|
5402
|
+
return await readFile(stub, "utf-8");
|
|
5405
5403
|
}
|
|
5406
5404
|
populateStub(stub, table) {
|
|
5407
5405
|
if (table !== null) stub = stub.replace(/DummyTable|{{\s*table\s*}}/g, table);
|
|
@@ -5421,7 +5419,7 @@ var MigrationCreator = class {
|
|
|
5421
5419
|
this.postCreate.push(callback);
|
|
5422
5420
|
}
|
|
5423
5421
|
async ensureDirectoryExists(dir) {
|
|
5424
|
-
await
|
|
5422
|
+
await mkdir(dir, { recursive: true });
|
|
5425
5423
|
}
|
|
5426
5424
|
stubPath(stub = "") {
|
|
5427
5425
|
const __dirname$1 = this.getDirname(import.meta);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@h3ravel/arquebus",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Arquebus ORM is a Beautiful, expressive ORM inspired by Laravel's Eloquent, designed for TypeScript applications and for the H3ravel Framework.",
|
|
5
5
|
"homepage": "https://h3ravel.toneflix.net/arquebus",
|
|
6
6
|
"bin": {
|
|
@@ -123,18 +123,18 @@
|
|
|
123
123
|
]
|
|
124
124
|
},
|
|
125
125
|
"dependencies": {
|
|
126
|
-
"@h3ravel/shared": "^0.
|
|
127
|
-
"chalk": "^5.6.
|
|
126
|
+
"@h3ravel/shared": "^0.20.1",
|
|
127
|
+
"chalk": "^5.6.2",
|
|
128
128
|
"collect.js": "^4.36.1",
|
|
129
|
-
"commander": "^14.0.
|
|
129
|
+
"commander": "^14.0.1",
|
|
130
130
|
"cross-env": "^10.0.0",
|
|
131
|
-
"dayjs": "^1.11.
|
|
132
|
-
"dotenv": "^17.2.
|
|
131
|
+
"dayjs": "^1.11.18",
|
|
132
|
+
"dotenv": "^17.2.2",
|
|
133
133
|
"escalade": "^3.2.0",
|
|
134
134
|
"husky": "^9.1.7",
|
|
135
135
|
"knex": "^3.1.0",
|
|
136
|
-
"lint-staged": "^16.
|
|
137
|
-
"mysql2": "^3.
|
|
136
|
+
"lint-staged": "^16.2.0",
|
|
137
|
+
"mysql2": "^3.15.0",
|
|
138
138
|
"pg": "^8.16.3",
|
|
139
139
|
"pluralize": "^8.0.0",
|
|
140
140
|
"prettier": "^3.6.2",
|
|
@@ -143,21 +143,21 @@
|
|
|
143
143
|
"tedious": "^19.0.0"
|
|
144
144
|
},
|
|
145
145
|
"devDependencies": {
|
|
146
|
-
"@eslint/js": "^9.
|
|
147
|
-
"@types/node": "^24.
|
|
146
|
+
"@eslint/js": "^9.36.0",
|
|
147
|
+
"@types/node": "^24.5.2",
|
|
148
148
|
"@types/pluralize": "^0.0.33",
|
|
149
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
150
|
-
"@typescript-eslint/parser": "^8.
|
|
149
|
+
"@typescript-eslint/eslint-plugin": "^8.44.0",
|
|
150
|
+
"@typescript-eslint/parser": "^8.44.0",
|
|
151
151
|
"@vitest/coverage-v8": "^3.2.4",
|
|
152
|
-
"eslint": "^9.
|
|
152
|
+
"eslint": "^9.36.0",
|
|
153
153
|
"jsdom": "^26.1.0",
|
|
154
154
|
"sqlite3": "5.1.7",
|
|
155
|
-
"terser": "^5.
|
|
155
|
+
"terser": "^5.44.0",
|
|
156
156
|
"ts-node": "^10.9.2",
|
|
157
157
|
"tsdown": "^0.15.4",
|
|
158
|
-
"tsx": "^4.20.
|
|
158
|
+
"tsx": "^4.20.5",
|
|
159
159
|
"typescript": "^5.9.2",
|
|
160
|
-
"typescript-eslint": "^8.
|
|
160
|
+
"typescript-eslint": "^8.44.0",
|
|
161
161
|
"vite-tsconfig-paths": "^5.1.4",
|
|
162
162
|
"vitest": "^3.2.4"
|
|
163
163
|
},
|
|
@@ -185,6 +185,6 @@
|
|
|
185
185
|
"test:postgres": "cross-env DB=postgres vitest --project node",
|
|
186
186
|
"test:sqlite": "cross-env DB=sqlite vitest --project node",
|
|
187
187
|
"test:browser": "vitest --project browser",
|
|
188
|
-
"release:patch": "pnpm build && git add . && git commit -m \"version: bump version
|
|
188
|
+
"release:patch": "pnpm build && git add . && git commit -m \"version: bump version\" && pnpm version patch && pnpm publish --tag latest"
|
|
189
189
|
}
|
|
190
190
|
}
|
package/types/builder.ts
CHANGED
|
@@ -5,25 +5,29 @@ import type BModel from 'src/browser/model'
|
|
|
5
5
|
import type Builder from 'src/builder'
|
|
6
6
|
import type { IModel } from './modeling'
|
|
7
7
|
import type { IQueryBuilder } from './query-builder'
|
|
8
|
+
import type { Knex } from 'knex'
|
|
8
9
|
import type Model from 'src/model'
|
|
10
|
+
import type { TFunction } from './generics'
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
type BaseBuilder<M extends Model | BModel, R = ICollection<M> | IModel> = Omit<
|
|
13
|
+
IQueryBuilder<M, R>,
|
|
14
|
+
'destroy' | 'clone' | 'get' | 'skip' | 'limit' | 'take' | 'offset' | 'chunk' | 'forPage' | 'orWhere' | 'pluck'
|
|
15
|
+
>
|
|
14
16
|
|
|
15
17
|
export interface IScope {
|
|
16
18
|
apply (builder: Builder<any>, model: Model): void;
|
|
17
19
|
}
|
|
18
20
|
|
|
19
|
-
|
|
20
|
-
export interface IBuilder<M extends Model | BModel, R = ICollection<M> | IModel> extends
|
|
21
|
+
// @ts-expect-error Errors will come from builder differences but that is fine
|
|
22
|
+
export interface IBuilder<M extends Model | BModel, R = ICollection<M> | IModel> extends BaseBuilder<M, R> {
|
|
23
|
+
connector: IQueryBuilder<M, R> & Knex.QueryBuilder & { _statements: any[], _single: any } & Knex
|
|
21
24
|
asProxy (): IQueryBuilder<M, R>;
|
|
22
|
-
|
|
25
|
+
chunk (count: number, callback: (rows: ICollection<M>) => any): Promise<boolean>;
|
|
23
26
|
enforceOrderBy (): void;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
+
idOf (id: string | number): this;
|
|
28
|
+
clone (): IBuilder<M, R>;
|
|
29
|
+
forPage (page: number, perPage?: number): this;
|
|
30
|
+
insert (...attributes: any[]): Promise<any>;
|
|
27
31
|
update (attributes: any): Promise<any>;
|
|
28
32
|
increment (column: string, amount?: number, extra?: any): Promise<any>;
|
|
29
33
|
decrement (column: string, amount?: number, extra?: any): Promise<any>;
|
|
@@ -54,6 +58,7 @@ export interface IBuilder<M extends Model | BModel, R = ICollection<M> | IModel>
|
|
|
54
58
|
doesntHave (relation: string, boolean?: any, callback?: (builder: IBuilder<any>) => void | null): this;
|
|
55
59
|
orDoesntHave (relation: string): this;
|
|
56
60
|
whereHas (relation: string, callback?: (builder: IBuilder<any>) => void | IBuilder<any> | null, operator?: any, count?: number): this;
|
|
61
|
+
orWhere (...args: any[]): this
|
|
57
62
|
orWhereHas (relation: string, callback?: (builder: IBuilder<any>) => void | IBuilder<any> | null, operator?: any, count?: number): this;
|
|
58
63
|
whereRelation (relation: string, column: string, operator?: any, value?: any): this;
|
|
59
64
|
hasNested (relation: string, operator?: any, count?: number, boolean?: any, callback?: (builder: IBuilder<any>) => void | null): this;
|
|
@@ -68,10 +73,10 @@ export interface IBuilder<M extends Model | BModel, R = ICollection<M> | IModel>
|
|
|
68
73
|
withSum (relation: WithRelationType, column: string): this;
|
|
69
74
|
withExists (relation: WithRelationType): this;
|
|
70
75
|
related (relation: string): this;
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
76
|
+
take (count: number): this;
|
|
77
|
+
skip (count: number): this;
|
|
78
|
+
limit (count: number): this;
|
|
79
|
+
offset (count: number): this;
|
|
75
80
|
first (column?: string | string[]): Promise<M | null | undefined>;
|
|
76
81
|
firstOrFail (column?: string | string[]): Promise<M>;
|
|
77
82
|
findOrFail (key: string | number, columns?: string[]): Promise<M>;
|
|
@@ -86,8 +91,8 @@ export interface IBuilder<M extends Model | BModel, R = ICollection<M> | IModel>
|
|
|
86
91
|
find (key: string | number, columns?: string[]): Promise<M | null | undefined>;
|
|
87
92
|
findMany (keys: string[] | number[] | ICollection<any>, columns?: string[]): Promise<ICollection<M>>;
|
|
88
93
|
pluck<X extends Model = any | M> (column: string): Promise<ICollection<X>>;
|
|
89
|
-
|
|
90
|
-
|
|
94
|
+
destroy (ids?: string | number | string[] | number[] | TFunction | ICollection<any>): Promise<number>;
|
|
95
|
+
get (columns?: string[]): Promise<ICollection<M>>;
|
|
91
96
|
all (columns?: string[]): Promise<ICollection<M>>;
|
|
92
97
|
paginate<F extends IPaginatorParams> (page?: number, perPage?: number): Promise<IPaginator<M, F>>;
|
|
93
98
|
[value: string]: any;
|
package/types/generics.ts
CHANGED
|
@@ -47,6 +47,7 @@ export type RelationNames<T> = FunctionPropertyNames<T> extends infer R
|
|
|
47
47
|
: never;
|
|
48
48
|
|
|
49
49
|
export type MixinConstructor<T = TGeneric> = new (...args: any[]) => T
|
|
50
|
+
export type AbstractConstructor<T = TGeneric> = abstract new (...args: any[]) => T;
|
|
50
51
|
|
|
51
52
|
// Helper type: combine all mixin instance types into a single intersection
|
|
52
53
|
export type MixinReturn<Base extends MixinConstructor, Mixins extends ((base: any) => any)[]> =
|
package/types/modeling.ts
CHANGED
|
@@ -68,15 +68,15 @@ export interface IModel {
|
|
|
68
68
|
attributes: any
|
|
69
69
|
relations: any
|
|
70
70
|
exists: boolean
|
|
71
|
-
primaryKey: string
|
|
72
|
-
builder?: IBuilder<any, any> | null
|
|
73
|
-
table: string | null
|
|
71
|
+
// primaryKey: string
|
|
72
|
+
// builder?: IBuilder<any, any> | null
|
|
73
|
+
// table: string | null
|
|
74
74
|
connection?: TBaseConfig['client'] | null
|
|
75
|
-
keyType: string
|
|
76
|
-
incrementing: boolean
|
|
75
|
+
// keyType: string
|
|
76
|
+
// incrementing: boolean
|
|
77
77
|
perPage: number
|
|
78
78
|
with: string | string[] | TGeneric<(...args: any[]) => IBuilder<Model>>
|
|
79
|
-
withCount: string[]
|
|
79
|
+
// withCount: string[]
|
|
80
80
|
trx: AnyQueryBuilder | null
|
|
81
81
|
timestamps: boolean
|
|
82
82
|
dateFormat: string
|
|
@@ -111,7 +111,7 @@ export interface IModel {
|
|
|
111
111
|
setConnection (connection: TBaseConfig['client'] | null): this;
|
|
112
112
|
usesUniqueIds (): boolean;
|
|
113
113
|
uniqueIds (): string[];
|
|
114
|
-
newUniqueId (): string;
|
|
114
|
+
// newUniqueId (): string;
|
|
115
115
|
setUniqueIds (): void;
|
|
116
116
|
getKeyType (): string;
|
|
117
117
|
getIncrementing (): boolean;
|
package/types/query-builder.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import type { AnyQueryBuilder, GroupByMethod, JoinMethod, JoinRawMethod, OrderByMethod, OrderByRawMethod, RawInterface, SelectMethod, SetOperationsMethod, UnionMethod, WhereBetweenMethod, WhereColumnMethod, WhereExistsMethod, WhereFieldExpressionMethod, WhereInMethod, WhereJsonExpressionMethod, WhereMethod, WhereNullMethod, WhereRawMethod, WhereWrappedMethod } from './query-methods'
|
|
2
|
-
import type {
|
|
1
|
+
import type { AddSelectMethod, AnyQueryBuilder, FirstOrFailMethod, ForceDeleteMethod, GroupByMethod, JoinMethod, JoinRawMethod, OrderByMethod, OrderByRawMethod, RawInterface, RestoreMethod, ReturningMethod, SelectMethod, SetOperationsMethod, UnionMethod, WhereBetweenMethod, WhereColumnMethod, WhereExistsMethod, WhereFieldExpressionMethod, WhereInMethod, WhereJsonExpressionMethod, WhereMethod, WhereNullMethod, WhereRawMethod, WhereWrappedMethod } from './query-methods'
|
|
2
|
+
import type { IPaginator, IPaginatorParams } from './utils'
|
|
3
3
|
import type { TFunction, TGeneric } from './generics'
|
|
4
4
|
|
|
5
5
|
import type BModel from 'src/browser/model'
|
|
6
6
|
import type { Column } from 'src/inspector/types/column'
|
|
7
7
|
import type { ForeignKey } from 'src/inspector/types/foreign-key'
|
|
8
|
+
import type { IBuilder } from './builder'
|
|
8
9
|
import type { Knex } from 'knex'
|
|
9
10
|
import type Model from 'src/model'
|
|
10
11
|
import type { Table } from 'src/inspector/types/table'
|
|
@@ -64,17 +65,22 @@ export type IConnector<M extends TGeneric = any, R = any> = Knex & Knex.QueryBui
|
|
|
64
65
|
|
|
65
66
|
export interface IQueryBuilder<M extends Model | BModel = Model, R = M[] | M> {
|
|
66
67
|
// connector: IQueryBuilder<M, R>
|
|
68
|
+
query: IBuilder<M, R>
|
|
67
69
|
schema: SchemaBuilder
|
|
68
70
|
_statements: IStatement[],
|
|
69
71
|
table (name: string): IQueryBuilder<M, R>
|
|
70
72
|
select: SelectMethod<this>
|
|
73
|
+
addSelect: AddSelectMethod<this>
|
|
71
74
|
columns: SelectMethod<this>
|
|
72
75
|
column: SelectMethod<this>
|
|
73
76
|
distinct: SelectMethod<this>
|
|
77
|
+
returning: ReturningMethod<this>
|
|
74
78
|
distinctOn: SelectMethod<this>
|
|
75
79
|
as: AsMethod<this>
|
|
76
80
|
asProxy (): IQueryBuilder<M, R>
|
|
77
81
|
where: WhereMethod<this>
|
|
82
|
+
firstOrFail: FirstOrFailMethod<this>
|
|
83
|
+
forceDelete: ForceDeleteMethod
|
|
78
84
|
andWhere: WhereMethod<this>
|
|
79
85
|
// orWhere: WhereMethod<this>
|
|
80
86
|
orWhere (...args: any[]): this
|
|
@@ -94,6 +100,7 @@ export interface IQueryBuilder<M extends Model | BModel = Model, R = M[] | M> {
|
|
|
94
100
|
whereNotExists: WhereExistsMethod<this>
|
|
95
101
|
orWhereNotExists: WhereExistsMethod<this>
|
|
96
102
|
|
|
103
|
+
restore: RestoreMethod
|
|
97
104
|
whereIn: WhereInMethod<this>
|
|
98
105
|
orWhereIn: WhereInMethod<this>
|
|
99
106
|
whereNotIn: WhereInMethod<this>
|
package/types/query-methods.ts
CHANGED
|
@@ -114,6 +114,26 @@ export interface WhereJsonExpressionMethod<QB extends AnyQueryBuilder> {
|
|
|
114
114
|
(fieldExpression: FieldExpression, keys: string | string[]): QB;
|
|
115
115
|
}
|
|
116
116
|
|
|
117
|
+
export interface ReturningMethod<QB extends AnyQueryBuilder> {
|
|
118
|
+
(key: FieldExpression | FieldExpression[], options?: { [key: string]: any }): QB;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface FirstOrFailMethod<QB extends AnyQueryBuilder> {
|
|
122
|
+
(): Promise<QB>;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export interface AddSelectMethod<QB extends AnyQueryBuilder> {
|
|
126
|
+
(params: string[]): QB;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export interface ForceDeleteMethod {
|
|
130
|
+
(): Promise<boolean | number>;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export interface RestoreMethod {
|
|
134
|
+
(): Promise<number>;
|
|
135
|
+
}
|
|
136
|
+
|
|
117
137
|
export interface WhereColumnMethod<QB extends AnyQueryBuilder> {
|
|
118
138
|
(col1: ColumnRef, op: Operator, col2: ColumnRef): QB;
|
|
119
139
|
(col1: ColumnRef, col2: ColumnRef): QB;
|