@rwillians/qx 0.1.4 → 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
|
@@ -69,6 +69,14 @@ class ColumnPropsBuilder {
|
|
|
69
69
|
throw new Error('Cannot make a nullable column a primary key');
|
|
70
70
|
return new ColumnPropsBuilder({ ...this.props, primaryKey: true });
|
|
71
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* @public Marks the column as unique.
|
|
74
|
+
* @since 0.1.0
|
|
75
|
+
* @version 1
|
|
76
|
+
*/
|
|
77
|
+
unique() {
|
|
78
|
+
return new ColumnPropsBuilder({ ...this.props, unique: true });
|
|
79
|
+
}
|
|
72
80
|
}
|
|
73
81
|
/**
|
|
74
82
|
* @public A way to define a column with a custom standard schema.
|
|
@@ -592,6 +600,16 @@ class QueryBuilder {
|
|
|
592
600
|
// @TODO calls query engine with the query object
|
|
593
601
|
return db.query(toSelectStatement(this.query));
|
|
594
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
|
+
}
|
|
595
613
|
/**
|
|
596
614
|
* @public Sets a limit on the number of rows to be returned.
|
|
597
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
|
@@ -66,6 +66,14 @@ class ColumnPropsBuilder {
|
|
|
66
66
|
throw new Error('Cannot make a nullable column a primary key');
|
|
67
67
|
return new ColumnPropsBuilder({ ...this.props, primaryKey: true });
|
|
68
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* @public Marks the column as unique.
|
|
71
|
+
* @since 0.1.0
|
|
72
|
+
* @version 1
|
|
73
|
+
*/
|
|
74
|
+
unique() {
|
|
75
|
+
return new ColumnPropsBuilder({ ...this.props, unique: true });
|
|
76
|
+
}
|
|
69
77
|
}
|
|
70
78
|
/**
|
|
71
79
|
* @public A way to define a column with a custom standard schema.
|
|
@@ -583,6 +591,16 @@ class QueryBuilder {
|
|
|
583
591
|
// @TODO calls query engine with the query object
|
|
584
592
|
return db.query(toSelectStatement(this.query));
|
|
585
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
|
+
}
|
|
586
604
|
/**
|
|
587
605
|
* @public Sets a limit on the number of rows to be returned.
|
|
588
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
|
@@ -299,6 +299,14 @@ declare class ColumnPropsBuilder<T extends ColumnProps = ColumnProps> {
|
|
|
299
299
|
primaryKey(): ColumnPropsBuilder<Expand<T & {
|
|
300
300
|
primaryKey: true;
|
|
301
301
|
}>>;
|
|
302
|
+
/**
|
|
303
|
+
* @public Marks the column as unique.
|
|
304
|
+
* @since 0.1.0
|
|
305
|
+
* @version 1
|
|
306
|
+
*/
|
|
307
|
+
unique(): ColumnPropsBuilder<Expand<T & {
|
|
308
|
+
unique: true;
|
|
309
|
+
}>>;
|
|
302
310
|
}
|
|
303
311
|
/**
|
|
304
312
|
* @public Built-in column types.
|
|
@@ -1125,6 +1133,12 @@ declare class QueryBuilder<T extends Record<string, Aliased<string, Table>>, S e
|
|
|
1125
1133
|
* @version 1
|
|
1126
1134
|
*/
|
|
1127
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>;
|
|
1128
1142
|
/**
|
|
1129
1143
|
* @public Sets a limit on the number of rows to be returned.
|
|
1130
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": {
|