@3lineas/d1-orm 1.0.3
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 +207 -0
- package/dist/chunk-X6BYQHVC.mjs +12 -0
- package/dist/cli/index.d.mts +1 -0
- package/dist/cli/index.d.ts +1 -0
- package/dist/cli/index.js +250 -0
- package/dist/cli/index.mjs +260 -0
- package/dist/index.d.mts +155 -0
- package/dist/index.d.ts +155 -0
- package/dist/index.js +474 -0
- package/dist/index.mjs +439 -0
- package/package.json +38 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
__commonJS,
|
|
4
|
+
__esm
|
|
5
|
+
} from "../chunk-X6BYQHVC.mjs";
|
|
6
|
+
|
|
7
|
+
// src/cli/commands/init.ts
|
|
8
|
+
import * as fs from "fs";
|
|
9
|
+
import * as path from "path";
|
|
10
|
+
import * as readline from "readline";
|
|
11
|
+
async function init() {
|
|
12
|
+
const rl = readline.createInterface({
|
|
13
|
+
input: process.stdin,
|
|
14
|
+
output: process.stdout
|
|
15
|
+
});
|
|
16
|
+
const question = (query) => {
|
|
17
|
+
return new Promise((resolve) => {
|
|
18
|
+
rl.question(query, resolve);
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
console.log("Initializing D1 ORM...");
|
|
22
|
+
const defaultModelsPath = "src/models";
|
|
23
|
+
const userModelsPath = await question(
|
|
24
|
+
`Where would you like to install your models? (default: ${defaultModelsPath}): `
|
|
25
|
+
) || defaultModelsPath;
|
|
26
|
+
rl.close();
|
|
27
|
+
const folders = [userModelsPath, "database/migrations", "database/seeders"];
|
|
28
|
+
folders.forEach((folder) => {
|
|
29
|
+
const fullPath = path.join(process.cwd(), folder);
|
|
30
|
+
if (!fs.existsSync(fullPath)) {
|
|
31
|
+
fs.mkdirSync(fullPath, { recursive: true });
|
|
32
|
+
console.log(`Created directory: ${folder}`);
|
|
33
|
+
} else {
|
|
34
|
+
console.log(`Directory already exists: ${folder}`);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
const userModelContent = `import { Model } from 'd1-orm';
|
|
38
|
+
|
|
39
|
+
export class User extends Model {
|
|
40
|
+
// protected static table = 'users';
|
|
41
|
+
|
|
42
|
+
declare id: number;
|
|
43
|
+
declare name: string;
|
|
44
|
+
declare email: string;
|
|
45
|
+
declare password?: string;
|
|
46
|
+
declare created_at: string;
|
|
47
|
+
declare updated_at: string;
|
|
48
|
+
}
|
|
49
|
+
`;
|
|
50
|
+
const userModelPath = path.join(process.cwd(), userModelsPath, "User.ts");
|
|
51
|
+
if (!fs.existsSync(userModelPath)) {
|
|
52
|
+
fs.writeFileSync(userModelPath, userModelContent);
|
|
53
|
+
console.log(`Created model: ${userModelPath}`);
|
|
54
|
+
}
|
|
55
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:]/g, "").split(".")[0].replace("T", "_");
|
|
56
|
+
const migrationName = `${timestamp}_create_users_table.ts`;
|
|
57
|
+
const migrationContent = `import { Blueprint, Schema } from 'd1-orm';
|
|
58
|
+
|
|
59
|
+
export const up = async () => {
|
|
60
|
+
return Schema.create('users', (table: Blueprint) => {
|
|
61
|
+
table.id();
|
|
62
|
+
table.string('name');
|
|
63
|
+
table.string('email').unique();
|
|
64
|
+
table.string('password').nullable();
|
|
65
|
+
table.timestamps();
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export const down = async () => {
|
|
70
|
+
return Schema.dropIfExists('users');
|
|
71
|
+
};
|
|
72
|
+
`;
|
|
73
|
+
const migrationPath = path.join(
|
|
74
|
+
process.cwd(),
|
|
75
|
+
"database/migrations",
|
|
76
|
+
migrationName
|
|
77
|
+
);
|
|
78
|
+
if (!fs.existsSync(migrationPath)) {
|
|
79
|
+
fs.writeFileSync(migrationPath, migrationContent);
|
|
80
|
+
console.log(`Created migration: ${migrationPath}`);
|
|
81
|
+
}
|
|
82
|
+
const seederContent = `import { User } from '${path.relative(
|
|
83
|
+
path.join(process.cwd(), "database/seeders"),
|
|
84
|
+
path.join(process.cwd(), userModelsPath, "User")
|
|
85
|
+
).replace(/\\/g, "/")}';
|
|
86
|
+
|
|
87
|
+
export const seed = async () => {
|
|
88
|
+
await User.create({
|
|
89
|
+
name: 'Juan Perez',
|
|
90
|
+
email: 'juan@perez.com',
|
|
91
|
+
password: 'password'
|
|
92
|
+
});
|
|
93
|
+
};
|
|
94
|
+
`;
|
|
95
|
+
const seederPath = path.join(
|
|
96
|
+
process.cwd(),
|
|
97
|
+
"database/seeders",
|
|
98
|
+
"UserSeeder.ts"
|
|
99
|
+
);
|
|
100
|
+
if (!fs.existsSync(seederPath)) {
|
|
101
|
+
fs.writeFileSync(seederPath, seederContent);
|
|
102
|
+
console.log(`Created seeder: ${seederPath}`);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
var init_init = __esm({
|
|
106
|
+
"src/cli/commands/init.ts"() {
|
|
107
|
+
"use strict";
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// src/cli/commands/make-model.ts
|
|
112
|
+
import * as fs2 from "fs";
|
|
113
|
+
import * as path2 from "path";
|
|
114
|
+
function makeModel(name) {
|
|
115
|
+
const filename = `${name}.ts`;
|
|
116
|
+
const targetPath = path2.join(process.cwd(), "models", filename);
|
|
117
|
+
const template = `import { Model } from 'd1-orm';
|
|
118
|
+
|
|
119
|
+
export class ${name} extends Model {
|
|
120
|
+
// protected static table = '${name.toLowerCase()}s';
|
|
121
|
+
|
|
122
|
+
// Define attributes explicitly for type safety if desired
|
|
123
|
+
// declare id: number;
|
|
124
|
+
// declare created_at: string;
|
|
125
|
+
// declare updated_at: string;
|
|
126
|
+
}
|
|
127
|
+
`;
|
|
128
|
+
if (fs2.existsSync(targetPath)) {
|
|
129
|
+
console.error(`Model ${filename} already exists.`);
|
|
130
|
+
process.exit(1);
|
|
131
|
+
}
|
|
132
|
+
fs2.writeFileSync(targetPath, template);
|
|
133
|
+
console.log(`Created model: models/${filename}`);
|
|
134
|
+
}
|
|
135
|
+
var init_make_model = __esm({
|
|
136
|
+
"src/cli/commands/make-model.ts"() {
|
|
137
|
+
"use strict";
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
// src/cli/commands/make-migration.ts
|
|
142
|
+
import * as fs3 from "fs";
|
|
143
|
+
import * as path3 from "path";
|
|
144
|
+
function makeMigration(name) {
|
|
145
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:]/g, "").split(".")[0].replace("T", "_");
|
|
146
|
+
const filename = `${timestamp}_${name}.ts`;
|
|
147
|
+
const targetPath = path3.join(process.cwd(), "database/migrations", filename);
|
|
148
|
+
const template = `import { Blueprint, Schema } from 'd1-orm';
|
|
149
|
+
|
|
150
|
+
export const up = async () => {
|
|
151
|
+
return Schema.create('${name}', (table: Blueprint) => {
|
|
152
|
+
table.id();
|
|
153
|
+
table.timestamps();
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
export const down = async () => {
|
|
158
|
+
return Schema.dropIfExists('${name}');
|
|
159
|
+
};
|
|
160
|
+
`;
|
|
161
|
+
if (fs3.existsSync(targetPath)) {
|
|
162
|
+
console.error(`Migration ${filename} already exists.`);
|
|
163
|
+
process.exit(1);
|
|
164
|
+
}
|
|
165
|
+
fs3.writeFileSync(targetPath, template);
|
|
166
|
+
console.log(`Created migration: database/migrations/${filename}`);
|
|
167
|
+
}
|
|
168
|
+
var init_make_migration = __esm({
|
|
169
|
+
"src/cli/commands/make-migration.ts"() {
|
|
170
|
+
"use strict";
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
// src/cli/commands/migrate.ts
|
|
175
|
+
import * as fs4 from "fs";
|
|
176
|
+
import * as path4 from "path";
|
|
177
|
+
import { execSync } from "child_process";
|
|
178
|
+
async function migrate(args) {
|
|
179
|
+
console.log("Running migrations...");
|
|
180
|
+
const migrationsDir = path4.join(process.cwd(), "database/migrations");
|
|
181
|
+
if (!fs4.existsSync(migrationsDir)) {
|
|
182
|
+
console.log("No migrations directory found.");
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
const files = fs4.readdirSync(migrationsDir).filter((f) => f.endsWith(".ts") || f.endsWith(".js")).sort();
|
|
186
|
+
for (const file of files) {
|
|
187
|
+
console.log(`Processing ${file}...`);
|
|
188
|
+
const filePath = path4.join(migrationsDir, file);
|
|
189
|
+
try {
|
|
190
|
+
const migration = await import(filePath);
|
|
191
|
+
if (migration.up) {
|
|
192
|
+
const sql = await migration.up();
|
|
193
|
+
if (sql) {
|
|
194
|
+
console.log(`Executing SQL: ${sql}`);
|
|
195
|
+
const isRemote = args.includes("--remote");
|
|
196
|
+
const dbName = "DB";
|
|
197
|
+
const command = isRemote ? "--remote" : "--local";
|
|
198
|
+
try {
|
|
199
|
+
const execCmd = `npx wrangler d1 execute ${dbName} --command "${sql.replace(/"/g, '\\"')}" ${command}`;
|
|
200
|
+
console.log(`Running: ${execCmd}`);
|
|
201
|
+
execSync(execCmd, { stdio: "inherit" });
|
|
202
|
+
} catch (e) {
|
|
203
|
+
console.error(`Failed to execute migration ${file}.`);
|
|
204
|
+
throw e;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
} catch (error) {
|
|
209
|
+
console.error(`Error processing migration ${file}:`, error);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
var init_migrate = __esm({
|
|
214
|
+
"src/cli/commands/migrate.ts"() {
|
|
215
|
+
"use strict";
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
// src/cli/index.ts
|
|
220
|
+
var require_cli = __commonJS({
|
|
221
|
+
"src/cli/index.ts"() {
|
|
222
|
+
init_init();
|
|
223
|
+
init_make_model();
|
|
224
|
+
init_make_migration();
|
|
225
|
+
init_migrate();
|
|
226
|
+
var args = process.argv.slice(2);
|
|
227
|
+
var command = args[0];
|
|
228
|
+
var param = args[1];
|
|
229
|
+
switch (command) {
|
|
230
|
+
case "init":
|
|
231
|
+
init();
|
|
232
|
+
break;
|
|
233
|
+
case "make:model":
|
|
234
|
+
if (!param) {
|
|
235
|
+
console.error("Usage: d1-orm make:model <Name>");
|
|
236
|
+
process.exit(1);
|
|
237
|
+
}
|
|
238
|
+
makeModel(param);
|
|
239
|
+
break;
|
|
240
|
+
case "make:migration":
|
|
241
|
+
if (!param) {
|
|
242
|
+
console.error("Usage: d1-orm make:migration <Name>");
|
|
243
|
+
process.exit(1);
|
|
244
|
+
}
|
|
245
|
+
makeMigration(param);
|
|
246
|
+
break;
|
|
247
|
+
case "migrate":
|
|
248
|
+
const isLocal = args.includes("--local");
|
|
249
|
+
const isRemote = args.includes("--remote");
|
|
250
|
+
migrate(args);
|
|
251
|
+
break;
|
|
252
|
+
default:
|
|
253
|
+
console.log(
|
|
254
|
+
"Available commands: init, make:model, make:migration, migrate"
|
|
255
|
+
);
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
export default require_cli();
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
interface D1Database {
|
|
2
|
+
prepare(query: string): D1PreparedStatement;
|
|
3
|
+
dump(): Promise<ArrayBuffer>;
|
|
4
|
+
batch<T = unknown>(statements: D1PreparedStatement[]): Promise<D1Result<T>[]>;
|
|
5
|
+
exec(query: string): Promise<D1ExecResult>;
|
|
6
|
+
}
|
|
7
|
+
interface D1PreparedStatement {
|
|
8
|
+
bind(...values: unknown[]): D1PreparedStatement;
|
|
9
|
+
first<T = unknown>(colName?: string): Promise<T | null>;
|
|
10
|
+
run<T = unknown>(): Promise<D1Result<T>>;
|
|
11
|
+
all<T = unknown>(): Promise<D1Result<T>>;
|
|
12
|
+
raw<T = unknown>(): Promise<T[]>;
|
|
13
|
+
}
|
|
14
|
+
interface D1Result<T = unknown> {
|
|
15
|
+
results: T[];
|
|
16
|
+
success: boolean;
|
|
17
|
+
meta: any;
|
|
18
|
+
error?: string;
|
|
19
|
+
}
|
|
20
|
+
interface D1ExecResult {
|
|
21
|
+
count: number;
|
|
22
|
+
duration: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
declare class Connection {
|
|
26
|
+
protected db: D1Database;
|
|
27
|
+
constructor(database: D1Database);
|
|
28
|
+
select(query: string, bindings?: any[]): Promise<any[]>;
|
|
29
|
+
insert(query: string, bindings?: any[]): Promise<boolean>;
|
|
30
|
+
update(query: string, bindings?: any[]): Promise<boolean>;
|
|
31
|
+
delete(query: string, bindings?: any[]): Promise<boolean>;
|
|
32
|
+
statement(query: string, bindings?: any[]): Promise<boolean>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type D1Value = string | number | boolean | null;
|
|
36
|
+
declare class QueryBuilder {
|
|
37
|
+
protected connection: Connection;
|
|
38
|
+
protected table: string;
|
|
39
|
+
protected columns: string[];
|
|
40
|
+
protected wheres: string[];
|
|
41
|
+
protected bindings: D1Value[];
|
|
42
|
+
protected orders: string[];
|
|
43
|
+
protected limitValue: number | null;
|
|
44
|
+
protected offsetValue: number | null;
|
|
45
|
+
constructor(connection: Connection, table: string);
|
|
46
|
+
select(...columns: string[]): this;
|
|
47
|
+
where(column: string, operator: string, value?: D1Value): this;
|
|
48
|
+
orWhere(column: string, operator: string, value?: D1Value): this;
|
|
49
|
+
orderBy(column: string, direction?: "asc" | "desc"): this;
|
|
50
|
+
limit(limit: number): this;
|
|
51
|
+
offset(offset: number): this;
|
|
52
|
+
toSql(): {
|
|
53
|
+
sql: string;
|
|
54
|
+
bindings: D1Value[];
|
|
55
|
+
};
|
|
56
|
+
get<T = any>(): Promise<T[]>;
|
|
57
|
+
first<T = any>(): Promise<T | null>;
|
|
58
|
+
insert(data: Record<string, D1Value>): Promise<boolean>;
|
|
59
|
+
update(data: Record<string, D1Value>): Promise<boolean>;
|
|
60
|
+
delete(): Promise<boolean>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
declare class Database {
|
|
64
|
+
private static instance;
|
|
65
|
+
connection: Connection;
|
|
66
|
+
private constructor();
|
|
67
|
+
static setup(d1: D1Database): void;
|
|
68
|
+
static getInstance(): Database;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
declare abstract class Relationship<Related extends Model> {
|
|
72
|
+
protected query: ModelQueryBuilder<Related>;
|
|
73
|
+
protected parent: Model;
|
|
74
|
+
constructor(query: ModelQueryBuilder<Related>, parent: Model);
|
|
75
|
+
abstract addConstraints(): void;
|
|
76
|
+
getQuery(): ModelQueryBuilder<Related>;
|
|
77
|
+
get(): Promise<Related[]>;
|
|
78
|
+
first(): Promise<Related | null>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
declare class HasOne<Related extends Model> extends Relationship<Related> {
|
|
82
|
+
protected foreignKey: string;
|
|
83
|
+
protected localKey: string;
|
|
84
|
+
constructor(query: ModelQueryBuilder<Related>, parent: Model, foreignKey: string, localKey: string);
|
|
85
|
+
addConstraints(): void;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
declare class HasMany<Related extends Model> extends Relationship<Related> {
|
|
89
|
+
protected foreignKey: string;
|
|
90
|
+
protected localKey: string;
|
|
91
|
+
constructor(query: ModelQueryBuilder<Related>, parent: Model, foreignKey: string, localKey: string);
|
|
92
|
+
addConstraints(): void;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
declare class BelongsTo<Related extends Model> extends Relationship<Related> {
|
|
96
|
+
protected foreignKey: string;
|
|
97
|
+
protected ownerKey: string;
|
|
98
|
+
constructor(query: ModelQueryBuilder<Related>, parent: Model, foreignKey: string, ownerKey: string);
|
|
99
|
+
addConstraints(): void;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
declare class Model {
|
|
103
|
+
protected static table: string;
|
|
104
|
+
protected static connectionResolver: () => Database;
|
|
105
|
+
attributes: Record<string, D1Value>;
|
|
106
|
+
protected fillable: string[];
|
|
107
|
+
protected hidden: string[];
|
|
108
|
+
exists: boolean;
|
|
109
|
+
protected original: Record<string, D1Value>;
|
|
110
|
+
constructor(attributes?: Record<string, D1Value>);
|
|
111
|
+
static query<T extends Model>(this: new () => T): ModelQueryBuilder<T>;
|
|
112
|
+
static on(connectionName?: string): ModelQueryBuilder<Model>;
|
|
113
|
+
static all<T extends Model>(this: new () => T): Promise<T[]>;
|
|
114
|
+
static find<T extends Model>(this: new () => T, id: number | string): Promise<T | null>;
|
|
115
|
+
static create<T extends Model>(this: new () => T, attributes: Record<string, D1Value>): Promise<T>;
|
|
116
|
+
fill(attributes: Record<string, D1Value>): this;
|
|
117
|
+
save(): Promise<boolean>;
|
|
118
|
+
delete(): Promise<boolean>;
|
|
119
|
+
getTable(): string;
|
|
120
|
+
getDirty(): Record<string, D1Value>;
|
|
121
|
+
syncOriginal(): void;
|
|
122
|
+
hasOne<Related extends Model>(related: new () => Related, foreignKey?: string, localKey?: string): HasOne<Related>;
|
|
123
|
+
hasMany<Related extends Model>(related: new () => Related, foreignKey?: string, localKey?: string): HasMany<Related>;
|
|
124
|
+
belongsTo<Related extends Model>(related: new () => Related, foreignKey?: string, ownerKey?: string): BelongsTo<Related>;
|
|
125
|
+
getForeignKey(): string;
|
|
126
|
+
newQuery<T extends Model>(this: T): ModelQueryBuilder<T>;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
declare class ModelQueryBuilder<T extends Model> extends QueryBuilder {
|
|
130
|
+
protected modelClass: typeof Model;
|
|
131
|
+
constructor(connection: any, table: string, modelClass: typeof Model);
|
|
132
|
+
get<M = T>(): Promise<M[]>;
|
|
133
|
+
first<M = T>(): Promise<M | null>;
|
|
134
|
+
getModel(): typeof Model;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
declare class Blueprint {
|
|
138
|
+
protected table: string;
|
|
139
|
+
protected columns: string[];
|
|
140
|
+
protected commands: string[];
|
|
141
|
+
constructor(table: string);
|
|
142
|
+
id(): this;
|
|
143
|
+
string(column: string, length?: number): this;
|
|
144
|
+
integer(column: string): this;
|
|
145
|
+
boolean(column: string): this;
|
|
146
|
+
timestamps(): this;
|
|
147
|
+
toSql(): string[];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
declare class Schema {
|
|
151
|
+
static create(table: string, callback: (table: Blueprint) => void): string;
|
|
152
|
+
static dropIfExists(table: string): string;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export { BelongsTo, Blueprint, Connection, type D1Database, type D1ExecResult, type D1PreparedStatement, type D1Result, type D1Value, Database, HasMany, HasOne, Model, ModelQueryBuilder, QueryBuilder, Relationship, Schema };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
interface D1Database {
|
|
2
|
+
prepare(query: string): D1PreparedStatement;
|
|
3
|
+
dump(): Promise<ArrayBuffer>;
|
|
4
|
+
batch<T = unknown>(statements: D1PreparedStatement[]): Promise<D1Result<T>[]>;
|
|
5
|
+
exec(query: string): Promise<D1ExecResult>;
|
|
6
|
+
}
|
|
7
|
+
interface D1PreparedStatement {
|
|
8
|
+
bind(...values: unknown[]): D1PreparedStatement;
|
|
9
|
+
first<T = unknown>(colName?: string): Promise<T | null>;
|
|
10
|
+
run<T = unknown>(): Promise<D1Result<T>>;
|
|
11
|
+
all<T = unknown>(): Promise<D1Result<T>>;
|
|
12
|
+
raw<T = unknown>(): Promise<T[]>;
|
|
13
|
+
}
|
|
14
|
+
interface D1Result<T = unknown> {
|
|
15
|
+
results: T[];
|
|
16
|
+
success: boolean;
|
|
17
|
+
meta: any;
|
|
18
|
+
error?: string;
|
|
19
|
+
}
|
|
20
|
+
interface D1ExecResult {
|
|
21
|
+
count: number;
|
|
22
|
+
duration: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
declare class Connection {
|
|
26
|
+
protected db: D1Database;
|
|
27
|
+
constructor(database: D1Database);
|
|
28
|
+
select(query: string, bindings?: any[]): Promise<any[]>;
|
|
29
|
+
insert(query: string, bindings?: any[]): Promise<boolean>;
|
|
30
|
+
update(query: string, bindings?: any[]): Promise<boolean>;
|
|
31
|
+
delete(query: string, bindings?: any[]): Promise<boolean>;
|
|
32
|
+
statement(query: string, bindings?: any[]): Promise<boolean>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type D1Value = string | number | boolean | null;
|
|
36
|
+
declare class QueryBuilder {
|
|
37
|
+
protected connection: Connection;
|
|
38
|
+
protected table: string;
|
|
39
|
+
protected columns: string[];
|
|
40
|
+
protected wheres: string[];
|
|
41
|
+
protected bindings: D1Value[];
|
|
42
|
+
protected orders: string[];
|
|
43
|
+
protected limitValue: number | null;
|
|
44
|
+
protected offsetValue: number | null;
|
|
45
|
+
constructor(connection: Connection, table: string);
|
|
46
|
+
select(...columns: string[]): this;
|
|
47
|
+
where(column: string, operator: string, value?: D1Value): this;
|
|
48
|
+
orWhere(column: string, operator: string, value?: D1Value): this;
|
|
49
|
+
orderBy(column: string, direction?: "asc" | "desc"): this;
|
|
50
|
+
limit(limit: number): this;
|
|
51
|
+
offset(offset: number): this;
|
|
52
|
+
toSql(): {
|
|
53
|
+
sql: string;
|
|
54
|
+
bindings: D1Value[];
|
|
55
|
+
};
|
|
56
|
+
get<T = any>(): Promise<T[]>;
|
|
57
|
+
first<T = any>(): Promise<T | null>;
|
|
58
|
+
insert(data: Record<string, D1Value>): Promise<boolean>;
|
|
59
|
+
update(data: Record<string, D1Value>): Promise<boolean>;
|
|
60
|
+
delete(): Promise<boolean>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
declare class Database {
|
|
64
|
+
private static instance;
|
|
65
|
+
connection: Connection;
|
|
66
|
+
private constructor();
|
|
67
|
+
static setup(d1: D1Database): void;
|
|
68
|
+
static getInstance(): Database;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
declare abstract class Relationship<Related extends Model> {
|
|
72
|
+
protected query: ModelQueryBuilder<Related>;
|
|
73
|
+
protected parent: Model;
|
|
74
|
+
constructor(query: ModelQueryBuilder<Related>, parent: Model);
|
|
75
|
+
abstract addConstraints(): void;
|
|
76
|
+
getQuery(): ModelQueryBuilder<Related>;
|
|
77
|
+
get(): Promise<Related[]>;
|
|
78
|
+
first(): Promise<Related | null>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
declare class HasOne<Related extends Model> extends Relationship<Related> {
|
|
82
|
+
protected foreignKey: string;
|
|
83
|
+
protected localKey: string;
|
|
84
|
+
constructor(query: ModelQueryBuilder<Related>, parent: Model, foreignKey: string, localKey: string);
|
|
85
|
+
addConstraints(): void;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
declare class HasMany<Related extends Model> extends Relationship<Related> {
|
|
89
|
+
protected foreignKey: string;
|
|
90
|
+
protected localKey: string;
|
|
91
|
+
constructor(query: ModelQueryBuilder<Related>, parent: Model, foreignKey: string, localKey: string);
|
|
92
|
+
addConstraints(): void;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
declare class BelongsTo<Related extends Model> extends Relationship<Related> {
|
|
96
|
+
protected foreignKey: string;
|
|
97
|
+
protected ownerKey: string;
|
|
98
|
+
constructor(query: ModelQueryBuilder<Related>, parent: Model, foreignKey: string, ownerKey: string);
|
|
99
|
+
addConstraints(): void;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
declare class Model {
|
|
103
|
+
protected static table: string;
|
|
104
|
+
protected static connectionResolver: () => Database;
|
|
105
|
+
attributes: Record<string, D1Value>;
|
|
106
|
+
protected fillable: string[];
|
|
107
|
+
protected hidden: string[];
|
|
108
|
+
exists: boolean;
|
|
109
|
+
protected original: Record<string, D1Value>;
|
|
110
|
+
constructor(attributes?: Record<string, D1Value>);
|
|
111
|
+
static query<T extends Model>(this: new () => T): ModelQueryBuilder<T>;
|
|
112
|
+
static on(connectionName?: string): ModelQueryBuilder<Model>;
|
|
113
|
+
static all<T extends Model>(this: new () => T): Promise<T[]>;
|
|
114
|
+
static find<T extends Model>(this: new () => T, id: number | string): Promise<T | null>;
|
|
115
|
+
static create<T extends Model>(this: new () => T, attributes: Record<string, D1Value>): Promise<T>;
|
|
116
|
+
fill(attributes: Record<string, D1Value>): this;
|
|
117
|
+
save(): Promise<boolean>;
|
|
118
|
+
delete(): Promise<boolean>;
|
|
119
|
+
getTable(): string;
|
|
120
|
+
getDirty(): Record<string, D1Value>;
|
|
121
|
+
syncOriginal(): void;
|
|
122
|
+
hasOne<Related extends Model>(related: new () => Related, foreignKey?: string, localKey?: string): HasOne<Related>;
|
|
123
|
+
hasMany<Related extends Model>(related: new () => Related, foreignKey?: string, localKey?: string): HasMany<Related>;
|
|
124
|
+
belongsTo<Related extends Model>(related: new () => Related, foreignKey?: string, ownerKey?: string): BelongsTo<Related>;
|
|
125
|
+
getForeignKey(): string;
|
|
126
|
+
newQuery<T extends Model>(this: T): ModelQueryBuilder<T>;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
declare class ModelQueryBuilder<T extends Model> extends QueryBuilder {
|
|
130
|
+
protected modelClass: typeof Model;
|
|
131
|
+
constructor(connection: any, table: string, modelClass: typeof Model);
|
|
132
|
+
get<M = T>(): Promise<M[]>;
|
|
133
|
+
first<M = T>(): Promise<M | null>;
|
|
134
|
+
getModel(): typeof Model;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
declare class Blueprint {
|
|
138
|
+
protected table: string;
|
|
139
|
+
protected columns: string[];
|
|
140
|
+
protected commands: string[];
|
|
141
|
+
constructor(table: string);
|
|
142
|
+
id(): this;
|
|
143
|
+
string(column: string, length?: number): this;
|
|
144
|
+
integer(column: string): this;
|
|
145
|
+
boolean(column: string): this;
|
|
146
|
+
timestamps(): this;
|
|
147
|
+
toSql(): string[];
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
declare class Schema {
|
|
151
|
+
static create(table: string, callback: (table: Blueprint) => void): string;
|
|
152
|
+
static dropIfExists(table: string): string;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export { BelongsTo, Blueprint, Connection, type D1Database, type D1ExecResult, type D1PreparedStatement, type D1Result, type D1Value, Database, HasMany, HasOne, Model, ModelQueryBuilder, QueryBuilder, Relationship, Schema };
|