@rwillians/qx 0.1.5 → 0.1.6
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.
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defineMigrations = void 0;
|
|
4
|
+
const index_1 = require("./index");
|
|
5
|
+
const migrations = (0, index_1.table)('migrations', t => ({
|
|
6
|
+
id: t.string({ size: 36 }).primaryKey(),
|
|
7
|
+
migratedAt: t.datetime(),
|
|
8
|
+
}));
|
|
9
|
+
/**
|
|
10
|
+
* @public Defines and runs migrations against the given database.
|
|
11
|
+
* @since 0.1.6
|
|
12
|
+
* @version 1
|
|
13
|
+
*/
|
|
14
|
+
const defineMigrations = (migs) => async (db) => {
|
|
15
|
+
await index_1.create.table(migrations, { ifNotExists: true }).onto(db);
|
|
16
|
+
for (const [id, migration] of Object.entries(migs)) {
|
|
17
|
+
const alreadyMigrated = await (0, index_1.from)(migrations.as('m'))
|
|
18
|
+
.where(({ m }) => index_1.expr.eq(m.id, id))
|
|
19
|
+
.exists(db);
|
|
20
|
+
if (alreadyMigrated)
|
|
21
|
+
continue;
|
|
22
|
+
console.log(`running migration ${id}...`);
|
|
23
|
+
await migration(db);
|
|
24
|
+
console.log('done');
|
|
25
|
+
await (0, index_1.into)(migrations)
|
|
26
|
+
.insert({ id, migratedAt: new Date() })
|
|
27
|
+
.run(db);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
exports.defineMigrations = defineMigrations;
|
package/dist/cjs/index.js
CHANGED
|
@@ -600,6 +600,16 @@ class QueryBuilder {
|
|
|
600
600
|
// @TODO calls query engine with the query object
|
|
601
601
|
return db.query(toSelectStatement(this.query));
|
|
602
602
|
}
|
|
603
|
+
/**
|
|
604
|
+
* @public Checks whether any rows exist matching the query.
|
|
605
|
+
* @since 0.1.6
|
|
606
|
+
* @version 1
|
|
607
|
+
*/
|
|
608
|
+
async exists(db) {
|
|
609
|
+
// @TODO optimize this, maybe do a SELECT 1 or something
|
|
610
|
+
const results = await db.query(toSelectStatement({ ...this.query, limit: 1, offset: 0 }));
|
|
611
|
+
return results.length > 0;
|
|
612
|
+
}
|
|
603
613
|
/**
|
|
604
614
|
* @public Sets a limit on the number of rows to be returned.
|
|
605
615
|
* @since 0.1.0
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { create, expr, from, into, table } from './index';
|
|
2
|
+
const migrations = table('migrations', t => ({
|
|
3
|
+
id: t.string({ size: 36 }).primaryKey(),
|
|
4
|
+
migratedAt: t.datetime(),
|
|
5
|
+
}));
|
|
6
|
+
/**
|
|
7
|
+
* @public Defines and runs migrations against the given database.
|
|
8
|
+
* @since 0.1.6
|
|
9
|
+
* @version 1
|
|
10
|
+
*/
|
|
11
|
+
export const defineMigrations = (migs) => async (db) => {
|
|
12
|
+
await create.table(migrations, { ifNotExists: true }).onto(db);
|
|
13
|
+
for (const [id, migration] of Object.entries(migs)) {
|
|
14
|
+
const alreadyMigrated = await from(migrations.as('m'))
|
|
15
|
+
.where(({ m }) => expr.eq(m.id, id))
|
|
16
|
+
.exists(db);
|
|
17
|
+
if (alreadyMigrated)
|
|
18
|
+
continue;
|
|
19
|
+
console.log(`running migration ${id}...`);
|
|
20
|
+
await migration(db);
|
|
21
|
+
console.log('done');
|
|
22
|
+
await into(migrations)
|
|
23
|
+
.insert({ id, migratedAt: new Date() })
|
|
24
|
+
.run(db);
|
|
25
|
+
}
|
|
26
|
+
};
|
package/dist/esm/index.js
CHANGED
|
@@ -591,6 +591,16 @@ class QueryBuilder {
|
|
|
591
591
|
// @TODO calls query engine with the query object
|
|
592
592
|
return db.query(toSelectStatement(this.query));
|
|
593
593
|
}
|
|
594
|
+
/**
|
|
595
|
+
* @public Checks whether any rows exist matching the query.
|
|
596
|
+
* @since 0.1.6
|
|
597
|
+
* @version 1
|
|
598
|
+
*/
|
|
599
|
+
async exists(db) {
|
|
600
|
+
// @TODO optimize this, maybe do a SELECT 1 or something
|
|
601
|
+
const results = await db.query(toSelectStatement({ ...this.query, limit: 1, offset: 0 }));
|
|
602
|
+
return results.length > 0;
|
|
603
|
+
}
|
|
594
604
|
/**
|
|
595
605
|
* @public Sets a limit on the number of rows to be returned.
|
|
596
606
|
* @since 0.1.0
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type IDatabase } from './index';
|
|
2
|
+
/**
|
|
3
|
+
* @private Defines the type for a migration function.
|
|
4
|
+
* @since 0.1.6
|
|
5
|
+
* @version 1
|
|
6
|
+
*/
|
|
7
|
+
type Migration = (db: IDatabase) => Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* @public Defines and runs migrations against the given database.
|
|
10
|
+
* @since 0.1.6
|
|
11
|
+
* @version 1
|
|
12
|
+
*/
|
|
13
|
+
export declare const defineMigrations: (migs: Record<string, Migration>) => (db: IDatabase) => Promise<void>;
|
|
14
|
+
export {};
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1133,6 +1133,12 @@ declare class QueryBuilder<T extends Record<string, Aliased<string, Table>>, S e
|
|
|
1133
1133
|
* @version 1
|
|
1134
1134
|
*/
|
|
1135
1135
|
all(db: IDatabase): Promise<Expand<InferSelection<Query<T, S>>>[]>;
|
|
1136
|
+
/**
|
|
1137
|
+
* @public Checks whether any rows exist matching the query.
|
|
1138
|
+
* @since 0.1.6
|
|
1139
|
+
* @version 1
|
|
1140
|
+
*/
|
|
1141
|
+
exists(db: IDatabase): Promise<boolean>;
|
|
1136
1142
|
/**
|
|
1137
1143
|
* @public Sets a limit on the number of rows to be returned.
|
|
1138
1144
|
* @since 0.1.0
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rwillians/qx",
|
|
3
3
|
"description": "A teeny tiny ORM for SQLite.",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.6",
|
|
5
5
|
"author": "Rafael Willians <me@rwillians.com>",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -41,6 +41,12 @@
|
|
|
41
41
|
"import": "./dist/esm/pretty-logger.js",
|
|
42
42
|
"require": "./dist/cjs/pretty-logger.js",
|
|
43
43
|
"default": "./dist/cjs/pretty-logger.js"
|
|
44
|
+
},
|
|
45
|
+
"./experimental-migration": {
|
|
46
|
+
"types": "./dist/types/experimental-migration.d.ts",
|
|
47
|
+
"import": "./dist/esm/experimental-migration.js",
|
|
48
|
+
"require": "./dist/cjs/experimental-migration.js",
|
|
49
|
+
"default": "./dist/cjs/experimental-migration.js"
|
|
44
50
|
}
|
|
45
51
|
},
|
|
46
52
|
"scripts": {
|