Skip to content

Commit

Permalink
Step back typeOrm Cli migration:generate
Browse files Browse the repository at this point in the history
  • Loading branch information
jepiqueau committed Feb 22, 2024
1 parent 10a88dd commit 7ee2144
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 596 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 5.6.1-1 (2024-02-22)

### Remove Features

- Remove the ability to run `migrations:generate` with the TypeOrm Cli as the way it has been implemented works from some Frameworks but not with Angular were developers could not anymore build there apps. see issue#516 Update 5.6.0.


# 5.6.0 (2024-02-17)

### Chore
Expand Down
2 changes: 1 addition & 1 deletion android/.settings/org.eclipse.buildship.core.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
connection.project.dir=
eclipse.preferences.version=1
gradle.user.home=
java.home=/usr
java.home=/Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home
jvm.arguments=
offline.mode=false
override.workspace.settings=true
Expand Down
75 changes: 54 additions & 21 deletions docs/TypeORM-Usage-From-5.6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@

- The `typeOrm` package now has a `capacitor` driver type that must be used with the `@capacitor-community/sqlite` capacitor plugin.

- Apps using the `@capacitor-community/sqlite` capacitor plugin cannot use the `CLI` of the `typeOrm` package. Developers were receiving a message stating that the "jeep-sqlite element is not present in the DOM" when trying to access it. This is a result of the database being created and stored in the DOM.
- Apps using the `@capacitor-community/sqlite` capacitor plugin cannot use the `CLI` of the `typeOrm` package. Developers trying to do so, will receive a message stating that the "jeep-sqlite element is not present in the DOM". The Web part of the plugin needs the DOM to create and store the database.

## New from release 5.6.0 of @capacitor-community/sqlite
- In release 5.6.0, I attempted to propose a workaround by utilizing certain Node.js packages like 'fs', 'os', and 'path' to facilitate the generation of initial and subsequent migration files. While this workaround worked smoothly on some frameworks using Vite (such as React and Svelte), unfortunately, I encountered difficulties with Angular due to the inability to find a suitable method for building the app.

- By incorporating Node.js code into few methods of the `@capacitor-community/sqlite` Web Interface, the `migration:generate` TypeORM CLI becomes accessible even when the DOM is not available, enabling the generation of initial migration files.
- In release 5.6.1.1, I decided to retract the proposal for the workaround and refrain from implementing it. As a consequence, migration files will need to be created manually.

- Before generating subsequent migration files following modifications to the entities, it's necessary to save the DOM database to the local disk.

- Because of the limitation imposed by the WellKnownDirectory in the `browser-fs-access` package utilized by `jeep-sqlite` for saving the database on the local disk, the `documents` directory has been designated. Therefore, when the file picker form is displayed, you need to navigate to the Documents/CapacitorSQLite/YOUR_APPLICATION_NAME directory to save the database.

## Applications/Tutorials

Expand Down Expand Up @@ -247,27 +244,63 @@ export const getCountOfElements = (async (connection: DataSource, entity:any):
});
```

## Modify the Scripts in Package.json File

```json
"scripts": {
...
"typeorm:migration:generate:initialAuthor": "npx typeorm-ts-node-esm migration:generate src/databases/migrations/author/InitialAuthorPost -d src/databases/datasources/AuthorDataSource.ts",
"postinstall": "node ./scripts/modify-typeorm.cjs"

}

```
## Create the initial migration file

- Under the `migrations/author` directory create a file `1708168009284-InitialAuthorPost.ts` where `1708168009284`is a timestamp and copy the following code

```ts
import { MigrationInterface, QueryRunner } from "typeorm";

export class InitialAuthorPost1708269296396 implements MigrationInterface {
name = 'InitialAuthorPost1708269296396'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE "category" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar NOT NULL, CONSTRAINT "UQ_23c05c292c439d77b0de816b500" UNIQUE ("name"))`);
await queryRunner.query(`CREATE TABLE "post" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "text" text NOT NULL, "authorId" integer)`);
await queryRunner.query(`CREATE TABLE "author" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar NOT NULL, "birthday" varchar, "email" varchar NOT NULL, CONSTRAINT "UQ_384deada87eb62ab31c5d5afae5" UNIQUE ("email"))`);
await queryRunner.query(`CREATE TABLE "post_categories_category" ("postId" integer NOT NULL, "categoryId" integer NOT NULL, PRIMARY KEY ("postId", "categoryId"))`);
await queryRunner.query(`CREATE INDEX "IDX_93b566d522b73cb8bc46f7405b" ON "post_categories_category" ("postId") `);
await queryRunner.query(`CREATE INDEX "IDX_a5e63f80ca58e7296d5864bd2d" ON "post_categories_category" ("categoryId") `);
await queryRunner.query(`CREATE TABLE "temporary_post" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "text" text NOT NULL, "authorId" integer, CONSTRAINT "FK_c6fb082a3114f35d0cc27c518e0" FOREIGN KEY ("authorId") REFERENCES "author" ("id") ON DELETE NO ACTION ON UPDATE NO ACTION)`);
await queryRunner.query(`INSERT INTO "temporary_post"("id", "title", "text", "authorId") SELECT "id", "title", "text", "authorId" FROM "post"`);
await queryRunner.query(`DROP TABLE "post"`);
await queryRunner.query(`ALTER TABLE "temporary_post" RENAME TO "post"`);
await queryRunner.query(`DROP INDEX "IDX_93b566d522b73cb8bc46f7405b"`);
await queryRunner.query(`DROP INDEX "IDX_a5e63f80ca58e7296d5864bd2d"`);
await queryRunner.query(`CREATE TABLE "temporary_post_categories_category" ("postId" integer NOT NULL, "categoryId" integer NOT NULL, CONSTRAINT "FK_93b566d522b73cb8bc46f7405bd" FOREIGN KEY ("postId") REFERENCES "post" ("id") ON DELETE CASCADE ON UPDATE CASCADE, CONSTRAINT "FK_a5e63f80ca58e7296d5864bd2d3" FOREIGN KEY ("categoryId") REFERENCES "category" ("id") ON DELETE CASCADE ON UPDATE CASCADE, PRIMARY KEY ("postId", "categoryId"))`);
await queryRunner.query(`INSERT INTO "temporary_post_categories_category"("postId", "categoryId") SELECT "postId", "categoryId" FROM "post_categories_category"`);
await queryRunner.query(`DROP TABLE "post_categories_category"`);
await queryRunner.query(`ALTER TABLE "temporary_post_categories_category" RENAME TO "post_categories_category"`);
await queryRunner.query(`CREATE INDEX "IDX_93b566d522b73cb8bc46f7405b" ON "post_categories_category" ("postId") `);
await queryRunner.query(`CREATE INDEX "IDX_a5e63f80ca58e7296d5864bd2d" ON "post_categories_category" ("categoryId") `);
}

## Generate the initial migration file
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "IDX_a5e63f80ca58e7296d5864bd2d"`);
await queryRunner.query(`DROP INDEX "IDX_93b566d522b73cb8bc46f7405b"`);
await queryRunner.query(`ALTER TABLE "post_categories_category" RENAME TO "temporary_post_categories_category"`);
await queryRunner.query(`CREATE TABLE "post_categories_category" ("postId" integer NOT NULL, "categoryId" integer NOT NULL, PRIMARY KEY ("postId", "categoryId"))`);
await queryRunner.query(`INSERT INTO "post_categories_category"("postId", "categoryId") SELECT "postId", "categoryId" FROM "temporary_post_categories_category"`);
await queryRunner.query(`DROP TABLE "temporary_post_categories_category"`);
await queryRunner.query(`CREATE INDEX "IDX_a5e63f80ca58e7296d5864bd2d" ON "post_categories_category" ("categoryId") `);
await queryRunner.query(`CREATE INDEX "IDX_93b566d522b73cb8bc46f7405b" ON "post_categories_category" ("postId") `);
await queryRunner.query(`ALTER TABLE "post" RENAME TO "temporary_post"`);
await queryRunner.query(`CREATE TABLE "post" ("id" integer PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar NOT NULL, "text" text NOT NULL, "authorId" integer)`);
await queryRunner.query(`INSERT INTO "post"("id", "title", "text", "authorId") SELECT "id", "title", "text", "authorId" FROM "temporary_post"`);
await queryRunner.query(`DROP TABLE "temporary_post"`);
await queryRunner.query(`DROP INDEX "IDX_a5e63f80ca58e7296d5864bd2d"`);
await queryRunner.query(`DROP INDEX "IDX_93b566d522b73cb8bc46f7405b"`);
await queryRunner.query(`DROP TABLE "post_categories_category"`);
await queryRunner.query(`DROP TABLE "author"`);
await queryRunner.query(`DROP TABLE "post"`);
await queryRunner.query(`DROP TABLE "category"`);
}

```bash
npm run postinstall
npm run typeorm:migration:generate:initialAuthor
}
```

- Now you must have a file like `1708168009284-InitialAuthorPost.ts`under the `migrations/author` directory

- Open the `index.ts`file under `migrations/author` directory and edit it with

```ts
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@
}
},
"dependencies": {
"jeep-sqlite": "^2.5.9"
"jeep-sqlite": "^2.5.10"
}
}
7 changes: 3 additions & 4 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ export default {
globals: {
'@capacitor/core': 'capacitorExports',
'localforage': 'localForage',
'sql.js': 'initSqlJs',
'fs': 'FS',
'path': 'Path',
'sql.js': 'initSqlJs'

},
sourcemap: true,
inlineDynamicImports: true,
Expand All @@ -22,5 +21,5 @@ export default {
inlineDynamicImports: true,
},
],
external: ['@capacitor/core', 'localforage', 'sql.js', 'fs', 'path'],
external: ['@capacitor/core', 'localforage', 'sql.js'],
};
111 changes: 0 additions & 111 deletions src/web-typeorm-utils/database.ts

This file was deleted.

Loading

0 comments on commit 7ee2144

Please sign in to comment.