@guren/orm 0.1.1-alpha.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/dist/drizzle.d.ts +2 -0
- package/dist/drizzle.js +23 -0
- package/dist/index.d.ts +94 -0
- package/dist/index.js +359 -0
- package/package.json +57 -0
package/dist/drizzle.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// src/drizzle.ts
|
|
2
|
+
import { sql } from "drizzle-orm";
|
|
3
|
+
import {
|
|
4
|
+
pgTable,
|
|
5
|
+
serial,
|
|
6
|
+
text,
|
|
7
|
+
integer,
|
|
8
|
+
bigint,
|
|
9
|
+
timestamp,
|
|
10
|
+
boolean,
|
|
11
|
+
uniqueIndex
|
|
12
|
+
} from "drizzle-orm/pg-core";
|
|
13
|
+
export {
|
|
14
|
+
bigint,
|
|
15
|
+
boolean,
|
|
16
|
+
integer,
|
|
17
|
+
pgTable,
|
|
18
|
+
serial,
|
|
19
|
+
sql,
|
|
20
|
+
text,
|
|
21
|
+
timestamp,
|
|
22
|
+
uniqueIndex
|
|
23
|
+
};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { PostgresJsDatabase } from 'drizzle-orm/postgres-js';
|
|
2
|
+
import postgres from 'postgres';
|
|
3
|
+
|
|
4
|
+
type PlainObject = Record<string, unknown>;
|
|
5
|
+
type WhereClause = Record<string, unknown>;
|
|
6
|
+
interface ORMAdapter {
|
|
7
|
+
findMany<TRecord = PlainObject>(table: unknown, where?: WhereClause): Promise<TRecord[]>;
|
|
8
|
+
findUnique<TRecord = PlainObject>(table: unknown, where: WhereClause): Promise<TRecord | null>;
|
|
9
|
+
create<TRecord = PlainObject>(table: unknown, data: PlainObject): Promise<TRecord>;
|
|
10
|
+
update?<TRecord = PlainObject>(table: unknown, where: WhereClause, data: PlainObject): Promise<TRecord>;
|
|
11
|
+
delete?<TRecord = PlainObject>(table: unknown, where: WhereClause): Promise<number | PlainObject | void>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Minimal ActiveRecord-like wrapper around the configured ORM adapter. The API
|
|
15
|
+
* mirrors a subset of Laravel's Eloquent helpers and can be expanded over time.
|
|
16
|
+
*/
|
|
17
|
+
declare abstract class Model<TRecord extends object = PlainObject> {
|
|
18
|
+
protected static ormAdapter: ORMAdapter;
|
|
19
|
+
protected static table: unknown;
|
|
20
|
+
static readonly recordType: unknown;
|
|
21
|
+
static useAdapter(adapter: ORMAdapter): void;
|
|
22
|
+
static getAdapter(): ORMAdapter;
|
|
23
|
+
protected static resolveTable(): unknown;
|
|
24
|
+
static all<T extends typeof Model>(this: T): Promise<Array<TRecordFor<T>>>;
|
|
25
|
+
static find<T extends typeof Model>(this: T, id: unknown, key?: string): Promise<TRecordFor<T> | null>;
|
|
26
|
+
static where<T extends typeof Model>(this: T, where: WhereClause): Promise<TRecordFor<T>[]>;
|
|
27
|
+
static create<T extends typeof Model>(this: T, data: PlainObject): Promise<TRecordFor<T>>;
|
|
28
|
+
static update<T extends typeof Model>(this: T, where: WhereClause, data: PlainObject): Promise<TRecordFor<T>>;
|
|
29
|
+
static delete<T extends typeof Model>(this: T, where: WhereClause): Promise<number | PlainObject | void>;
|
|
30
|
+
}
|
|
31
|
+
type TRecordFor<T extends typeof Model> = T extends {
|
|
32
|
+
recordType: infer R;
|
|
33
|
+
} ? R extends object ? R : PlainObject : PlainObject;
|
|
34
|
+
|
|
35
|
+
type DrizzleLikeSelect = {
|
|
36
|
+
where?: (clause: unknown) => DrizzleLikeSelect;
|
|
37
|
+
all?: () => Promise<unknown[]>;
|
|
38
|
+
get?: () => Promise<unknown>;
|
|
39
|
+
};
|
|
40
|
+
type DrizzleLikeInsert = {
|
|
41
|
+
values: (record: PlainObject) => DrizzleLikeInsertResult;
|
|
42
|
+
};
|
|
43
|
+
type DrizzleLikeInsertResult = {
|
|
44
|
+
returning?: () => Promise<unknown[]>;
|
|
45
|
+
run?: () => Promise<unknown>;
|
|
46
|
+
};
|
|
47
|
+
type DrizzleLikeUpdate = {
|
|
48
|
+
set: (record: PlainObject) => DrizzleLikeUpdate;
|
|
49
|
+
where: (clause: unknown) => DrizzleLikeUpdate;
|
|
50
|
+
returning?: () => Promise<unknown[]>;
|
|
51
|
+
};
|
|
52
|
+
type DrizzleLikeDelete = {
|
|
53
|
+
where: (clause: unknown) => DrizzleLikeDelete;
|
|
54
|
+
returning?: () => Promise<unknown[]>;
|
|
55
|
+
run?: () => Promise<unknown>;
|
|
56
|
+
};
|
|
57
|
+
type DrizzleDatabase = {
|
|
58
|
+
select(): {
|
|
59
|
+
from(table: unknown): DrizzleLikeSelect;
|
|
60
|
+
};
|
|
61
|
+
insert(table: unknown): DrizzleLikeInsert;
|
|
62
|
+
update?(table: unknown): DrizzleLikeUpdate;
|
|
63
|
+
delete?(table: unknown): DrizzleLikeDelete;
|
|
64
|
+
};
|
|
65
|
+
declare const DrizzleAdapter: ORMAdapter & {
|
|
66
|
+
configure(db: DrizzleDatabase): void;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
type ConnectionResolver = string | (() => string | undefined);
|
|
70
|
+
interface PostgresDatabaseOptions<TSchema extends Record<string, unknown>> {
|
|
71
|
+
schema: TSchema;
|
|
72
|
+
migrationsFolder: string | URL;
|
|
73
|
+
connectionString?: ConnectionResolver;
|
|
74
|
+
clientOptions?: postgres.Options<Record<string, never>>;
|
|
75
|
+
seedersFolder?: string | URL;
|
|
76
|
+
}
|
|
77
|
+
interface PostgresDatabase<TSchema extends Record<string, unknown>> {
|
|
78
|
+
getDatabase(): Promise<PostgresJsDatabase<TSchema>>;
|
|
79
|
+
migrateDatabase(): Promise<void>;
|
|
80
|
+
closeDatabase(): Promise<void>;
|
|
81
|
+
configureOrm(): Promise<void>;
|
|
82
|
+
seedDatabase(): Promise<void>;
|
|
83
|
+
}
|
|
84
|
+
declare function createPostgresDatabase<TSchema extends Record<string, unknown>>(options: PostgresDatabaseOptions<TSchema>): PostgresDatabase<TSchema>;
|
|
85
|
+
|
|
86
|
+
interface SeederContext<TSchema extends Record<string, unknown>> {
|
|
87
|
+
db: PostgresJsDatabase<TSchema>;
|
|
88
|
+
}
|
|
89
|
+
type SeederHandler<TSchema extends Record<string, unknown>> = (context: SeederContext<TSchema>) => unknown;
|
|
90
|
+
declare function loadSeeders<TSchema extends Record<string, unknown>>(directory: string | URL): Promise<Array<SeederHandler<TSchema>>>;
|
|
91
|
+
declare function runSeeders<TSchema extends Record<string, unknown>>(db: PostgresJsDatabase<TSchema>, directory: string | URL): Promise<void>;
|
|
92
|
+
declare function defineSeeder<TSchema extends Record<string, unknown>>(handler: SeederHandler<TSchema>): SeederHandler<TSchema>;
|
|
93
|
+
|
|
94
|
+
export { DrizzleAdapter, Model, type ORMAdapter, type PlainObject, type PostgresDatabase, type PostgresDatabaseOptions, type SeederContext, type SeederHandler, type WhereClause, createPostgresDatabase, defineSeeder, loadSeeders, runSeeders };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,359 @@
|
|
|
1
|
+
// src/adapters/drizzle-adapter.ts
|
|
2
|
+
import { and, eq, inArray, isNull } from "drizzle-orm";
|
|
3
|
+
var database;
|
|
4
|
+
function ensureDatabase() {
|
|
5
|
+
if (!database) {
|
|
6
|
+
throw new Error("DrizzleAdapter: database has not been configured. Call DrizzleAdapter.configure(db).");
|
|
7
|
+
}
|
|
8
|
+
return database;
|
|
9
|
+
}
|
|
10
|
+
async function resolveList(result) {
|
|
11
|
+
if (isPromiseLike(result)) {
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
if (typeof result.all === "function") {
|
|
15
|
+
return result.all();
|
|
16
|
+
}
|
|
17
|
+
if (typeof result.get === "function") {
|
|
18
|
+
const item = await result.get();
|
|
19
|
+
return item ? [item] : [];
|
|
20
|
+
}
|
|
21
|
+
return [];
|
|
22
|
+
}
|
|
23
|
+
function resolveWhere(table, where) {
|
|
24
|
+
if (!where || typeof where !== "object") {
|
|
25
|
+
return where;
|
|
26
|
+
}
|
|
27
|
+
const tableRecord = table;
|
|
28
|
+
const clauses = Object.entries(where).filter(([, value]) => value !== void 0).map(([key, value]) => {
|
|
29
|
+
const column = tableRecord[key];
|
|
30
|
+
if (!column) {
|
|
31
|
+
throw new Error(`DrizzleAdapter: unknown column "${key}" on provided table.`);
|
|
32
|
+
}
|
|
33
|
+
if (Array.isArray(value)) {
|
|
34
|
+
return inArray(column, value);
|
|
35
|
+
}
|
|
36
|
+
if (value === null) {
|
|
37
|
+
return isNull(column);
|
|
38
|
+
}
|
|
39
|
+
return eq(column, value);
|
|
40
|
+
}).filter(Boolean);
|
|
41
|
+
if (clauses.length === 0) {
|
|
42
|
+
return void 0;
|
|
43
|
+
}
|
|
44
|
+
if (clauses.length === 1) {
|
|
45
|
+
return clauses[0];
|
|
46
|
+
}
|
|
47
|
+
return and(...clauses);
|
|
48
|
+
}
|
|
49
|
+
async function resolveSingle(result) {
|
|
50
|
+
if (isPromiseLike(result)) {
|
|
51
|
+
const list = await result ?? [];
|
|
52
|
+
return Array.isArray(list) ? list[0] ?? null : list ?? null;
|
|
53
|
+
}
|
|
54
|
+
if (typeof result.get === "function") {
|
|
55
|
+
const item = await result.get();
|
|
56
|
+
return item ?? null;
|
|
57
|
+
}
|
|
58
|
+
if (typeof result.all === "function") {
|
|
59
|
+
const list = await result.all();
|
|
60
|
+
return list[0] ?? null;
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
async function resolveMutation(result) {
|
|
65
|
+
if (isPromiseLike(result)) {
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
if ("returning" in result && typeof result.returning === "function") {
|
|
69
|
+
const rows = await result.returning();
|
|
70
|
+
return Array.isArray(rows) ? rows[0] ?? rows : rows;
|
|
71
|
+
}
|
|
72
|
+
if ("run" in result && typeof result.run === "function") {
|
|
73
|
+
return result.run();
|
|
74
|
+
}
|
|
75
|
+
return result;
|
|
76
|
+
}
|
|
77
|
+
function isPromiseLike(value) {
|
|
78
|
+
return typeof value === "object" && value !== null && "then" in value && typeof value.then === "function";
|
|
79
|
+
}
|
|
80
|
+
var DrizzleAdapter = {
|
|
81
|
+
configure(db) {
|
|
82
|
+
database = db;
|
|
83
|
+
},
|
|
84
|
+
async findMany(table, where) {
|
|
85
|
+
const db = ensureDatabase();
|
|
86
|
+
let query = db.select().from(table);
|
|
87
|
+
if (typeof query.where === "function") {
|
|
88
|
+
const clause = resolveWhere(table, where);
|
|
89
|
+
if (clause) {
|
|
90
|
+
query = query.where(clause);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const rows = await resolveList(query);
|
|
94
|
+
return rows;
|
|
95
|
+
},
|
|
96
|
+
async findUnique(table, where) {
|
|
97
|
+
const db = ensureDatabase();
|
|
98
|
+
let query = db.select().from(table);
|
|
99
|
+
if (typeof query.where === "function") {
|
|
100
|
+
const clause = resolveWhere(table, where);
|
|
101
|
+
if (clause) {
|
|
102
|
+
query = query.where(clause);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
const row = await resolveSingle(query);
|
|
106
|
+
if (row == null) {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
return row;
|
|
110
|
+
},
|
|
111
|
+
async create(table, data) {
|
|
112
|
+
const db = ensureDatabase();
|
|
113
|
+
const result = await resolveMutation(db.insert(table).values(data));
|
|
114
|
+
return result;
|
|
115
|
+
},
|
|
116
|
+
async update(table, where, data) {
|
|
117
|
+
const db = ensureDatabase();
|
|
118
|
+
if (!db.update) {
|
|
119
|
+
throw new Error("DrizzleAdapter: configured database does not support updates.");
|
|
120
|
+
}
|
|
121
|
+
const clause = resolveWhere(table, where);
|
|
122
|
+
const query = db.update(table).set(data);
|
|
123
|
+
const result = await resolveMutation(clause ? query.where(clause) : query);
|
|
124
|
+
return result;
|
|
125
|
+
},
|
|
126
|
+
async delete(table, where) {
|
|
127
|
+
const db = ensureDatabase();
|
|
128
|
+
if (!db.delete) {
|
|
129
|
+
throw new Error("DrizzleAdapter: configured database does not support deletes.");
|
|
130
|
+
}
|
|
131
|
+
const clause = resolveWhere(table, where);
|
|
132
|
+
const query = db.delete(table);
|
|
133
|
+
const result = await resolveMutation(clause ? query.where(clause) : query);
|
|
134
|
+
return result;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
// src/Model.ts
|
|
139
|
+
var Model = class {
|
|
140
|
+
static ormAdapter = DrizzleAdapter;
|
|
141
|
+
static table;
|
|
142
|
+
static recordType = void 0;
|
|
143
|
+
static useAdapter(adapter) {
|
|
144
|
+
this.ormAdapter = adapter;
|
|
145
|
+
}
|
|
146
|
+
static getAdapter() {
|
|
147
|
+
return this.ormAdapter;
|
|
148
|
+
}
|
|
149
|
+
static resolveTable() {
|
|
150
|
+
if (!this.table) {
|
|
151
|
+
throw new Error(`${this.name}.table must be defined before using the model.`);
|
|
152
|
+
}
|
|
153
|
+
return this.table;
|
|
154
|
+
}
|
|
155
|
+
static async all() {
|
|
156
|
+
const table = this.resolveTable();
|
|
157
|
+
return this.getAdapter().findMany(table);
|
|
158
|
+
}
|
|
159
|
+
static async find(id, key = "id") {
|
|
160
|
+
const table = this.resolveTable();
|
|
161
|
+
const where = { [key]: id };
|
|
162
|
+
return this.getAdapter().findUnique(table, where);
|
|
163
|
+
}
|
|
164
|
+
static async where(where) {
|
|
165
|
+
const table = this.resolveTable();
|
|
166
|
+
return this.getAdapter().findMany(table, where);
|
|
167
|
+
}
|
|
168
|
+
static async create(data) {
|
|
169
|
+
const table = this.resolveTable();
|
|
170
|
+
return this.getAdapter().create(table, data);
|
|
171
|
+
}
|
|
172
|
+
static async update(where, data) {
|
|
173
|
+
const table = this.resolveTable();
|
|
174
|
+
const adapter = this.getAdapter();
|
|
175
|
+
if (!adapter.update) {
|
|
176
|
+
throw new Error("Configured adapter does not support update operations.");
|
|
177
|
+
}
|
|
178
|
+
return adapter.update(table, where, data);
|
|
179
|
+
}
|
|
180
|
+
static async delete(where) {
|
|
181
|
+
const table = this.resolveTable();
|
|
182
|
+
const adapter = this.getAdapter();
|
|
183
|
+
if (!adapter.delete) {
|
|
184
|
+
throw new Error("Configured adapter does not support delete operations.");
|
|
185
|
+
}
|
|
186
|
+
return adapter.delete(table, where);
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// src/postgres.ts
|
|
191
|
+
import { drizzle } from "drizzle-orm/postgres-js";
|
|
192
|
+
import { migrate } from "drizzle-orm/postgres-js/migrator";
|
|
193
|
+
import { resolve as resolve2 } from "path";
|
|
194
|
+
import { fileURLToPath as fileURLToPath2 } from "url";
|
|
195
|
+
import postgres from "postgres";
|
|
196
|
+
|
|
197
|
+
// src/seeder.ts
|
|
198
|
+
import { readdir } from "fs/promises";
|
|
199
|
+
import { extname, resolve } from "path";
|
|
200
|
+
import { fileURLToPath, pathToFileURL } from "url";
|
|
201
|
+
var SUPPORTED_EXTENSIONS = /* @__PURE__ */ new Set([".js", ".mjs", ".cjs", ".ts"]);
|
|
202
|
+
function normalizeSeeder(candidate) {
|
|
203
|
+
if (!candidate) {
|
|
204
|
+
return void 0;
|
|
205
|
+
}
|
|
206
|
+
if (typeof candidate === "function") {
|
|
207
|
+
if ("prototype" in candidate && typeof candidate.prototype === "object") {
|
|
208
|
+
const prototype = candidate.prototype;
|
|
209
|
+
if (prototype && typeof prototype.run === "function") {
|
|
210
|
+
return async (context) => {
|
|
211
|
+
const instance = new candidate();
|
|
212
|
+
await instance.run(context);
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return candidate;
|
|
217
|
+
}
|
|
218
|
+
if (typeof candidate === "object") {
|
|
219
|
+
const run = candidate.run;
|
|
220
|
+
if (typeof run === "function") {
|
|
221
|
+
return run;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return void 0;
|
|
225
|
+
}
|
|
226
|
+
async function loadSeederModule(path) {
|
|
227
|
+
const module = await import(pathToFileURL(path).href);
|
|
228
|
+
const candidates = [
|
|
229
|
+
module.default,
|
|
230
|
+
module.seed,
|
|
231
|
+
module.run,
|
|
232
|
+
module.Seeder,
|
|
233
|
+
module.default && typeof module.default === "object" ? module.default.run : void 0
|
|
234
|
+
];
|
|
235
|
+
for (const candidate of candidates) {
|
|
236
|
+
const handler = normalizeSeeder(candidate);
|
|
237
|
+
if (handler) {
|
|
238
|
+
return handler;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
return void 0;
|
|
242
|
+
}
|
|
243
|
+
async function loadSeeders(directory) {
|
|
244
|
+
const root = directory instanceof URL ? fileURLToPath(directory) : resolve(directory);
|
|
245
|
+
const entries = await readdir(root, { withFileTypes: true });
|
|
246
|
+
const files = entries.filter((entry) => entry.isFile() && SUPPORTED_EXTENSIONS.has(extname(entry.name))).map((entry) => resolve(root, entry.name)).sort();
|
|
247
|
+
const seeders = [];
|
|
248
|
+
for (const file of files) {
|
|
249
|
+
const handler = await loadSeederModule(file);
|
|
250
|
+
if (handler) {
|
|
251
|
+
seeders.push(handler);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return seeders;
|
|
255
|
+
}
|
|
256
|
+
async function runSeeders(db, directory) {
|
|
257
|
+
const seeders = await loadSeeders(directory);
|
|
258
|
+
for (const handler of seeders) {
|
|
259
|
+
await handler({ db });
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
function defineSeeder(handler) {
|
|
263
|
+
return handler;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// src/postgres.ts
|
|
267
|
+
function createPostgresDatabase(options) {
|
|
268
|
+
const { schema, migrationsFolder, connectionString, clientOptions, seedersFolder } = options;
|
|
269
|
+
const resolvedMigrationsFolder = migrationsFolder instanceof URL ? fileURLToPath2(migrationsFolder) : resolve2(String(migrationsFolder));
|
|
270
|
+
const resolvedSeedersFolder = seedersFolder == null ? void 0 : seedersFolder instanceof URL ? fileURLToPath2(seedersFolder) : resolve2(String(seedersFolder));
|
|
271
|
+
let migrationsPromise;
|
|
272
|
+
let databasePromise;
|
|
273
|
+
let client;
|
|
274
|
+
function resolveConnectionString() {
|
|
275
|
+
const value = typeof connectionString === "function" ? connectionString() : connectionString;
|
|
276
|
+
const resolved = value ?? process.env.DATABASE_URL;
|
|
277
|
+
if (!resolved) {
|
|
278
|
+
throw new Error("DATABASE_URL is not set and no connection string was provided.");
|
|
279
|
+
}
|
|
280
|
+
return resolved;
|
|
281
|
+
}
|
|
282
|
+
async function migrateOnce() {
|
|
283
|
+
if (migrationsPromise) {
|
|
284
|
+
return migrationsPromise;
|
|
285
|
+
}
|
|
286
|
+
const promise = (async () => {
|
|
287
|
+
const url = resolveConnectionString();
|
|
288
|
+
const migrationClient = postgres(url, {
|
|
289
|
+
max: 1,
|
|
290
|
+
...clientOptions
|
|
291
|
+
});
|
|
292
|
+
try {
|
|
293
|
+
const db = drizzle(migrationClient, { schema });
|
|
294
|
+
await migrate(db, { migrationsFolder: resolvedMigrationsFolder });
|
|
295
|
+
} finally {
|
|
296
|
+
await migrationClient.end({ timeout: 0 });
|
|
297
|
+
}
|
|
298
|
+
})();
|
|
299
|
+
migrationsPromise = promise.catch((error) => {
|
|
300
|
+
migrationsPromise = void 0;
|
|
301
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
302
|
+
throw new Error(`Failed to run database migrations: ${reason}`);
|
|
303
|
+
});
|
|
304
|
+
await migrationsPromise;
|
|
305
|
+
}
|
|
306
|
+
async function getDatabase() {
|
|
307
|
+
if (databasePromise) {
|
|
308
|
+
return databasePromise;
|
|
309
|
+
}
|
|
310
|
+
const promise = (async () => {
|
|
311
|
+
await migrateOnce();
|
|
312
|
+
const url = resolveConnectionString();
|
|
313
|
+
client = postgres(url, {
|
|
314
|
+
max: 1,
|
|
315
|
+
...clientOptions
|
|
316
|
+
});
|
|
317
|
+
return drizzle(client, { schema });
|
|
318
|
+
})();
|
|
319
|
+
databasePromise = promise.catch((error) => {
|
|
320
|
+
databasePromise = void 0;
|
|
321
|
+
throw error;
|
|
322
|
+
});
|
|
323
|
+
return databasePromise;
|
|
324
|
+
}
|
|
325
|
+
async function closeDatabase() {
|
|
326
|
+
if (!client) {
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
await client.end({ timeout: 0 });
|
|
330
|
+
client = void 0;
|
|
331
|
+
databasePromise = void 0;
|
|
332
|
+
}
|
|
333
|
+
async function configureOrm() {
|
|
334
|
+
const db = await getDatabase();
|
|
335
|
+
DrizzleAdapter.configure(db);
|
|
336
|
+
}
|
|
337
|
+
async function seedDatabase() {
|
|
338
|
+
if (!resolvedSeedersFolder) {
|
|
339
|
+
throw new Error('No seeders folder configured. Provide "seedersFolder" when calling createPostgresDatabase().');
|
|
340
|
+
}
|
|
341
|
+
const db = await getDatabase();
|
|
342
|
+
await runSeeders(db, resolvedSeedersFolder);
|
|
343
|
+
}
|
|
344
|
+
return {
|
|
345
|
+
getDatabase,
|
|
346
|
+
migrateDatabase: migrateOnce,
|
|
347
|
+
closeDatabase,
|
|
348
|
+
configureOrm,
|
|
349
|
+
seedDatabase
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
export {
|
|
353
|
+
DrizzleAdapter,
|
|
354
|
+
Model,
|
|
355
|
+
createPostgresDatabase,
|
|
356
|
+
defineSeeder,
|
|
357
|
+
loadSeeders,
|
|
358
|
+
runSeeders
|
|
359
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@guren/orm",
|
|
3
|
+
"version": "0.1.1-alpha.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/gurenjs/guren",
|
|
9
|
+
"directory": "packages/orm"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/gurenjs/guren#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/gurenjs/guren/issues"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist"
|
|
20
|
+
],
|
|
21
|
+
"main": "dist/index.js",
|
|
22
|
+
"types": "dist/index.d.ts",
|
|
23
|
+
"exports": {
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
},
|
|
28
|
+
"./drizzle": {
|
|
29
|
+
"types": "./dist/drizzle.d.ts",
|
|
30
|
+
"default": "./dist/drizzle.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsup",
|
|
35
|
+
"dev": "bun --watch src/index.ts",
|
|
36
|
+
"typecheck": "tsc --noEmit"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"drizzle-orm": "^0.44.7",
|
|
40
|
+
"postgres": "^3.4.3"
|
|
41
|
+
},
|
|
42
|
+
"peerDependenciesMeta": {
|
|
43
|
+
"drizzle-orm": {
|
|
44
|
+
"optional": true
|
|
45
|
+
},
|
|
46
|
+
"postgres": {
|
|
47
|
+
"optional": true
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"@types/node": "^20.14.10",
|
|
52
|
+
"drizzle-orm": "^0.44.7",
|
|
53
|
+
"postgres": "^3.4.3",
|
|
54
|
+
"tsup": "^8.5.0",
|
|
55
|
+
"typescript": "^5.4.0"
|
|
56
|
+
}
|
|
57
|
+
}
|