@rwillians/qx 0.1.20 → 0.2.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/cjs/adapter.js +27 -0
- package/dist/cjs/bun-sqlite.js +545 -0
- package/dist/cjs/console-logger.js +135 -0
- package/dist/cjs/experimental-migrations.js +90 -0
- package/dist/cjs/index.js +718 -0
- package/dist/cjs/standard-schema.js +239 -0
- package/dist/cjs/utils.js +92 -0
- package/dist/esm/adapter.js +23 -0
- package/dist/esm/bun-sqlite.js +544 -0
- package/dist/esm/console-logger.js +132 -0
- package/dist/esm/experimental-migrations.js +86 -0
- package/dist/esm/index.js +711 -0
- package/dist/esm/standard-schema.js +235 -0
- package/dist/esm/utils.js +79 -0
- package/dist/types/adapter.d.ts +16 -0
- package/dist/types/bun-sqlite.d.ts +65 -0
- package/dist/types/console-logger.d.ts +21 -0
- package/dist/types/experimental-migrations.d.ts +63 -0
- package/dist/types/index.d.ts +1272 -0
- package/dist/types/standard-schema.d.ts +189 -0
- package/dist/types/utils.d.ts +79 -0
- package/package.json +4 -3
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defineMigrations = exports.create = void 0;
|
|
4
|
+
const index_1 = require("./index");
|
|
5
|
+
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
6
|
+
// CREATE STATEMENTS //
|
|
7
|
+
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
8
|
+
/**
|
|
9
|
+
* @public Create statement builder.
|
|
10
|
+
* @since 0.1.0
|
|
11
|
+
* @version 1
|
|
12
|
+
*/
|
|
13
|
+
exports.create = {
|
|
14
|
+
/**
|
|
15
|
+
* @public Prepares a create table statement.
|
|
16
|
+
* @since 0.1.0
|
|
17
|
+
* @version 1
|
|
18
|
+
*/
|
|
19
|
+
table: (table, options = {}) => ({
|
|
20
|
+
/**
|
|
21
|
+
* @public Executes the create table statement onto the given
|
|
22
|
+
* database.
|
|
23
|
+
* @since 0.1.0
|
|
24
|
+
* @version 1
|
|
25
|
+
*/
|
|
26
|
+
onto: async (db) => db.createTable({
|
|
27
|
+
...options,
|
|
28
|
+
table: table.name,
|
|
29
|
+
columns: Object.values(table.columns),
|
|
30
|
+
}),
|
|
31
|
+
}),
|
|
32
|
+
};
|
|
33
|
+
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
34
|
+
// MIGRATIONS MANAGEMENT //
|
|
35
|
+
// // // // // // // // // // // // // // // // // // // // // // // //
|
|
36
|
+
/**
|
|
37
|
+
* @private Defines the schema migrations table.
|
|
38
|
+
* @since 0.1.6
|
|
39
|
+
* @version 1
|
|
40
|
+
*/
|
|
41
|
+
const migrations = (0, index_1.table)('schema_migrations', t => ({
|
|
42
|
+
id: t.string({ size: 36 }).primaryKey(),
|
|
43
|
+
timestamp: t.datetime(),
|
|
44
|
+
}));
|
|
45
|
+
/**
|
|
46
|
+
* @public Defines a set of migrations to be executed against a
|
|
47
|
+
* database.
|
|
48
|
+
* @since 0.1.6
|
|
49
|
+
* @version 2
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* // src/db/migrations.ts
|
|
54
|
+
* import { create, defineMigrations } from '@rwillians/qx/experimental-migrations';
|
|
55
|
+
* import { users } from './users';
|
|
56
|
+
* import { profiles } from './profiles';
|
|
57
|
+
*
|
|
58
|
+
* export const migrate = defineMigrations({
|
|
59
|
+
* '0001': async (db) => {
|
|
60
|
+
* await create.table(users).onto(db);
|
|
61
|
+
* },
|
|
62
|
+
* '0002': async (db) => {
|
|
63
|
+
* await create.table(profiles).onto(db);
|
|
64
|
+
* },
|
|
65
|
+
* });
|
|
66
|
+
*
|
|
67
|
+
* // src/index.ts
|
|
68
|
+
* import * as sqlite from '@rwillians/qx/bun-sqlite';
|
|
69
|
+
* import { migrate } from './db/migrations';
|
|
70
|
+
*
|
|
71
|
+
* const db = sqlite.connect('./db.sqlite');
|
|
72
|
+
* await migrate(db)
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
const defineMigrations = (migs) => async (db) => {
|
|
76
|
+
await exports.create.table(migrations, { ifNotExists: true }).onto(db);
|
|
77
|
+
const { mostRecentId } = await (0, index_1.from)(migrations.as('m'))
|
|
78
|
+
.orderBy(({ m }) => [index_1.expr.desc(m.timestamp)])
|
|
79
|
+
.select(({ m }) => ({ mostRecentId: m.id }))
|
|
80
|
+
.one(db) || { mostRecentId: '' };
|
|
81
|
+
for (const [id, migrate] of Object.entries(migs)) {
|
|
82
|
+
if (id <= mostRecentId)
|
|
83
|
+
continue;
|
|
84
|
+
await (0, index_1.transaction)(db, async () => {
|
|
85
|
+
await migrate(db);
|
|
86
|
+
await (0, index_1.into)(migrations).values({ id, timestamp: new Date() }).insert(db);
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
exports.defineMigrations = defineMigrations;
|