-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ed40d5
commit dcf7ef4
Showing
12 changed files
with
226 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,31 @@ | ||
import { areArraysEqual } from '@/common/utils/array'; | ||
import { | ||
getDistricts, | ||
getIslands, | ||
getProvinces, | ||
getRegencies, | ||
getVillages, | ||
} from '@/common/utils/data'; | ||
import { PrismaClient } from '@prisma/client'; | ||
import { Areas } from 'idn-area-data'; | ||
import { Seeder } from '../seeder'; | ||
import { Area, Seeder } from '../seeder'; | ||
|
||
export class MongodbSeeder extends Seeder { | ||
constructor(prisma: PrismaClient) { | ||
super(prisma); | ||
} | ||
|
||
async hasProvinceChanges(): Promise<boolean> { | ||
const [newProvinces, oldProvinces] = await Promise.all([ | ||
getProvinces(), | ||
this.prisma.province.findMany(), | ||
]); | ||
async deleteAreas(area: Area): Promise<number> { | ||
const mongoCollectionMap = { | ||
province: 'provinces', | ||
regency: 'regencies', | ||
district: 'districts', | ||
village: 'villages', | ||
island: 'islands', | ||
}; | ||
|
||
return !areArraysEqual(newProvinces, oldProvinces, ['code', 'name']); | ||
} | ||
|
||
async hasRegencyChanges(): Promise<boolean> { | ||
const [newRegencies, oldRegencies] = await Promise.all([ | ||
getRegencies(), | ||
this.prisma.regency.findMany(), | ||
]); | ||
|
||
return !areArraysEqual(newRegencies, oldRegencies, [ | ||
'code', | ||
'name', | ||
'provinceCode', | ||
]); | ||
} | ||
|
||
async hasDistrictChanges(): Promise<boolean> { | ||
const [newDistricts, oldDistricts] = await Promise.all([ | ||
getDistricts(), | ||
this.prisma.district.findMany(), | ||
]); | ||
|
||
return !areArraysEqual(newDistricts, oldDistricts, [ | ||
'code', | ||
'name', | ||
'regencyCode', | ||
]); | ||
} | ||
|
||
async hasIslandChanges(): Promise<boolean> { | ||
const [newIslands, oldIslands] = await Promise.all([ | ||
getIslands(), | ||
this.prisma.island.findMany(), | ||
]); | ||
|
||
return !areArraysEqual(newIslands, oldIslands, [ | ||
'code', | ||
'name', | ||
'regencyCode', | ||
'isPopulated', | ||
'isOutermostSmall', | ||
]); | ||
} | ||
|
||
async hasVillageChanges(): Promise<boolean> { | ||
const [newVillages, oldVillages] = await Promise.all([ | ||
getVillages(), | ||
this.prisma.village.findMany(), | ||
]); | ||
|
||
return !areArraysEqual(newVillages, oldVillages, [ | ||
'code', | ||
'name', | ||
'districtCode', | ||
]); | ||
} | ||
|
||
/** | ||
* Delete all data in a collection. | ||
*/ | ||
protected async deleteCollection(collection: Areas) { | ||
// Skip TypeScript checking because `$runCommandRaw()` method only available for mongodb provider. | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
return await this.prisma.$runCommandRaw({ | ||
delete: collection, | ||
// @ts-ignore Skip TypeScript checking because `$runCommandRaw()` method only available for mongodb provider. | ||
const res = await this.prisma.$runCommandRaw({ | ||
delete: mongoCollectionMap[area], | ||
deletes: [ | ||
{ | ||
q: {}, | ||
limit: 0, | ||
}, | ||
], | ||
}); | ||
} | ||
|
||
async deleteProvinces(): Promise<number> { | ||
const res = await this.deleteCollection('provinces'); | ||
return res.n as number; | ||
} | ||
|
||
async deleteRegencies(): Promise<number> { | ||
const res = await this.deleteCollection('regencies'); | ||
return res.n as number; | ||
} | ||
|
||
async deleteDistricts(): Promise<number> { | ||
const res = await this.deleteCollection('districts'); | ||
return res.n as number; | ||
} | ||
|
||
async deleteVillages(): Promise<number> { | ||
const res = await this.deleteCollection('villages'); | ||
return res.n as number; | ||
} | ||
|
||
async deleteIslands(): Promise<number> { | ||
const res = await this.deleteCollection('islands'); | ||
return res.n as number; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
prisma/mysql/migrations/20241123043002_create_seeder_logs_table/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
-- CreateTable | ||
CREATE TABLE `seeder_logs` ( | ||
`id` INTEGER NOT NULL AUTO_INCREMENT, | ||
`data_version` VARCHAR(255) NOT NULL, | ||
`created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), | ||
|
||
PRIMARY KEY (`id`) | ||
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
prisma/postgresql/migrations/20241122141040_create_seeder_logs_table/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
-- CreateTable | ||
CREATE TABLE "seeder_logs" ( | ||
"id" SERIAL NOT NULL, | ||
"data_version" VARCHAR(255) NOT NULL, | ||
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
CONSTRAINT "seeder_logs_pkey" PRIMARY KEY ("id") | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.