@unchainedshop/core-countries 3.1.10 → 4.0.0-rc.2
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/lib/db/CountriesCollection.d.ts.map +1 -1
- package/lib/db/CountriesCollection.js +2 -2
- package/lib/db/CountriesCollection.js.map +1 -1
- package/lib/module/configureCountriesModule.d.ts +7 -11
- package/lib/module/configureCountriesModule.d.ts.map +1 -1
- package/lib/module/configureCountriesModule.js +11 -11
- package/lib/module/configureCountriesModule.js.map +1 -1
- package/package.json +6 -6
- package/src/db/CountriesCollection.ts +2 -2
- package/src/module/configureCountriesModule.ts +24 -20
- package/src/migrations/addMigrations.ts +0 -40
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CountriesCollection.d.ts","sourceRoot":"","sources":["../../src/db/CountriesCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,
|
|
1
|
+
{"version":3,"file":"CountriesCollection.d.ts","sourceRoot":"","sources":["../../src/db/CountriesCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAiD,MAAM,wBAAwB,CAAC;AAChG,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAEhD,eAAO,MAAM,mBAAmB,GAAU,IAAI,OAAO,CAAC,EAAE,yCAuBvD,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { buildDbIndexes } from '@unchainedshop/mongodb';
|
|
1
|
+
import { buildDbIndexes, isDocumentDBCompatModeEnabled } from '@unchainedshop/mongodb';
|
|
2
2
|
export const CountriesCollection = async (db) => {
|
|
3
3
|
const Countries = db.collection('countries');
|
|
4
4
|
await buildDbIndexes(Countries, [
|
|
@@ -8,7 +8,7 @@ export const CountriesCollection = async (db) => {
|
|
|
8
8
|
deleted: 1,
|
|
9
9
|
},
|
|
10
10
|
},
|
|
11
|
-
{
|
|
11
|
+
!isDocumentDBCompatModeEnabled() && {
|
|
12
12
|
index: { isoCode: 'text', _id: 'text' },
|
|
13
13
|
options: {
|
|
14
14
|
weights: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"CountriesCollection.js","sourceRoot":"","sources":["../../src/db/CountriesCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,cAAc,EAAE,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"CountriesCollection.js","sourceRoot":"","sources":["../../src/db/CountriesCollection.ts"],"names":[],"mappings":"AAAA,OAAO,EAAW,cAAc,EAAE,6BAA6B,EAAE,MAAM,wBAAwB,CAAC;AAGhG,MAAM,CAAC,MAAM,mBAAmB,GAAG,KAAK,EAAE,EAAc,EAAE,EAAE;IAC1D,MAAM,SAAS,GAAG,EAAE,CAAC,UAAU,CAAU,WAAW,CAAC,CAAC;IAEtD,MAAM,cAAc,CAAU,SAAS,EAAE;QACvC,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,CAAC,EAAE,EAAE,OAAO,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE;QACpD;YACE,KAAK,EAAE;gBACL,OAAO,EAAE,CAAC;aACX;SACF;QACD,CAAC,6BAA6B,EAAE,IAAI;YAClC,KAAK,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE;YACvC,OAAO,EAAE;gBACP,OAAO,EAAE;oBACP,GAAG,EAAE,CAAC;oBACN,OAAO,EAAE,CAAC;iBACX;gBACD,IAAI,EAAE,2BAA2B;aAClC;SACF;KACF,CAAC,CAAC;IAEH,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC"}
|
|
@@ -6,30 +6,26 @@ export type Country = {
|
|
|
6
6
|
isActive?: boolean;
|
|
7
7
|
defaultCurrencyCode?: string;
|
|
8
8
|
} & TimestampFields;
|
|
9
|
-
export type CountryQuery = {
|
|
9
|
+
export type CountryQuery = mongodb.Filter<Country> & {
|
|
10
10
|
includeInactive?: boolean;
|
|
11
11
|
queryString?: string;
|
|
12
12
|
};
|
|
13
|
-
export declare const buildFindSelector: ({ includeInactive, queryString }: CountryQuery) =>
|
|
14
|
-
|
|
15
|
-
$text?: any;
|
|
16
|
-
deleted?: Date;
|
|
17
|
-
};
|
|
18
|
-
export declare const configureCountriesModule: ({ db, migrationRepository, }: ModuleInput<Record<string, never>>) => Promise<{
|
|
13
|
+
export declare const buildFindSelector: ({ includeInactive, queryString, ...rest }: CountryQuery) => mongodb.Filter<Country>;
|
|
14
|
+
export declare const configureCountriesModule: ({ db }: ModuleInput<Record<string, never>>) => Promise<{
|
|
19
15
|
count: (query: CountryQuery) => Promise<number>;
|
|
20
16
|
findCountry: ({ countryId, isoCode, }: {
|
|
21
17
|
countryId?: string;
|
|
22
18
|
isoCode?: string;
|
|
23
19
|
}) => Promise<Country>;
|
|
24
|
-
findCountries: ({ limit, offset, sort,
|
|
20
|
+
findCountries: ({ limit, offset, sort, ...query }: CountryQuery & {
|
|
25
21
|
limit?: number;
|
|
26
22
|
offset?: number;
|
|
27
|
-
sort?:
|
|
28
|
-
}, options?: mongodb.FindOptions) => Promise<
|
|
23
|
+
sort?: SortOption[];
|
|
24
|
+
}, options?: mongodb.FindOptions) => Promise<Country[]>;
|
|
29
25
|
countryExists: ({ countryId }: {
|
|
30
26
|
countryId: string;
|
|
31
27
|
}) => Promise<boolean>;
|
|
32
|
-
name(country: Country,
|
|
28
|
+
name(country: Country, locale: Intl.Locale): string;
|
|
33
29
|
flagEmoji(country: Country): string;
|
|
34
30
|
isBase(country: Country): boolean;
|
|
35
31
|
create: (doc: Country) => Promise<string>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configureCountriesModule.d.ts","sourceRoot":"","sources":["../../src/module/configureCountriesModule.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"configureCountriesModule.d.ts","sourceRoot":"","sources":["../../src/module/configureCountriesModule.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,eAAe,EAC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CAAC,CAAC,CAAC"}
|
|
@@ -1,22 +1,22 @@
|
|
|
1
|
+
import { assertDocumentDBCompatMode, } from '@unchainedshop/mongodb';
|
|
1
2
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
2
3
|
import { generateDbFilterById, buildSortOptions, generateDbObjectId } from '@unchainedshop/mongodb';
|
|
3
4
|
import { SortDirection } from '@unchainedshop/utils';
|
|
4
5
|
import { systemLocale } from '@unchainedshop/utils';
|
|
5
6
|
import { CountriesCollection } from '../db/CountriesCollection.js';
|
|
6
|
-
import addMigrations from '../migrations/addMigrations.js';
|
|
7
7
|
const COUNTRY_EVENTS = ['COUNTRY_CREATE', 'COUNTRY_UPDATE', 'COUNTRY_REMOVE'];
|
|
8
|
-
export const buildFindSelector = ({ includeInactive = false, queryString = '' }) => {
|
|
9
|
-
const selector = { deleted: null };
|
|
8
|
+
export const buildFindSelector = ({ includeInactive = false, queryString = '', ...rest }) => {
|
|
9
|
+
const selector = { ...rest, deleted: null };
|
|
10
10
|
if (!includeInactive)
|
|
11
11
|
selector.isActive = true;
|
|
12
|
-
if (queryString)
|
|
12
|
+
if (queryString) {
|
|
13
|
+
assertDocumentDBCompatMode();
|
|
13
14
|
selector.$text = { $search: queryString };
|
|
15
|
+
}
|
|
14
16
|
return selector;
|
|
15
17
|
};
|
|
16
|
-
export const configureCountriesModule = async ({ db
|
|
18
|
+
export const configureCountriesModule = async ({ db }) => {
|
|
17
19
|
registerEvents(COUNTRY_EVENTS);
|
|
18
|
-
// Migration
|
|
19
|
-
addMigrations(migrationRepository);
|
|
20
20
|
const Countries = await CountriesCollection(db);
|
|
21
21
|
return {
|
|
22
22
|
count: async (query) => {
|
|
@@ -26,9 +26,9 @@ export const configureCountriesModule = async ({ db, migrationRepository, }) =>
|
|
|
26
26
|
findCountry: async ({ countryId, isoCode, }) => {
|
|
27
27
|
return Countries.findOne(countryId ? generateDbFilterById(countryId) : { isoCode });
|
|
28
28
|
},
|
|
29
|
-
findCountries: async ({ limit, offset, sort,
|
|
29
|
+
findCountries: async ({ limit, offset, sort, ...query }, options) => {
|
|
30
30
|
const defaultSort = [{ key: 'created', value: SortDirection.ASC }];
|
|
31
|
-
const countries = Countries.find(buildFindSelector(
|
|
31
|
+
const countries = Countries.find(buildFindSelector(query), {
|
|
32
32
|
skip: offset,
|
|
33
33
|
limit,
|
|
34
34
|
sort: buildSortOptions(sort || defaultSort),
|
|
@@ -42,8 +42,8 @@ export const configureCountriesModule = async ({ db, migrationRepository, }) =>
|
|
|
42
42
|
});
|
|
43
43
|
return !!countryCount;
|
|
44
44
|
},
|
|
45
|
-
name(country,
|
|
46
|
-
return new Intl.DisplayNames([
|
|
45
|
+
name(country, locale) {
|
|
46
|
+
return new Intl.DisplayNames([locale], { type: 'region', fallback: 'code' }).of(country.isoCode);
|
|
47
47
|
},
|
|
48
48
|
flagEmoji(country) {
|
|
49
49
|
const letterToLetterEmoji = (letter) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"configureCountriesModule.js","sourceRoot":"","sources":["../../src/module/configureCountriesModule.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"configureCountriesModule.js","sourceRoot":"","sources":["../../src/module/configureCountriesModule.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,0BAA0B,GAC3B,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,IAAI,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,EAAE,oBAAoB,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AACpG,OAAO,EAAE,aAAa,EAAc,MAAM,sBAAsB,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,8BAA8B,CAAC;AAcnE,MAAM,cAAc,GAAa,CAAC,gBAAgB,EAAE,gBAAgB,EAAE,gBAAgB,CAAC,CAAC;AAExF,MAAM,CAAC,MAAM,iBA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AO,YAAY,CAAC;QACtB,CAAC;KACF,CAAC;AACJ,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/core-countries",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-rc.2",
|
|
4
4
|
"main": "lib/countries-index.js",
|
|
5
5
|
"types": "lib/countries-index.d.ts",
|
|
6
6
|
"type": "module",
|
|
@@ -31,12 +31,12 @@
|
|
|
31
31
|
},
|
|
32
32
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
33
33
|
"dependencies": {
|
|
34
|
-
"@unchainedshop/events": "^
|
|
35
|
-
"@unchainedshop/utils": "^
|
|
34
|
+
"@unchainedshop/events": "^4.0.0-rc.2",
|
|
35
|
+
"@unchainedshop/utils": "^4.0.0-rc.2"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@types/node": "^22.
|
|
39
|
-
"tsx": "^4.19.
|
|
40
|
-
"typescript": "^5.8.
|
|
38
|
+
"@types/node": "^22.15.3",
|
|
39
|
+
"tsx": "^4.19.4",
|
|
40
|
+
"typescript": "^5.8.3"
|
|
41
41
|
}
|
|
42
42
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mongodb, buildDbIndexes } from '@unchainedshop/mongodb';
|
|
1
|
+
import { mongodb, buildDbIndexes, isDocumentDBCompatModeEnabled } from '@unchainedshop/mongodb';
|
|
2
2
|
import { Country } from '../countries-index.js';
|
|
3
3
|
|
|
4
4
|
export const CountriesCollection = async (db: mongodb.Db) => {
|
|
@@ -11,7 +11,7 @@ export const CountriesCollection = async (db: mongodb.Db) => {
|
|
|
11
11
|
deleted: 1,
|
|
12
12
|
},
|
|
13
13
|
},
|
|
14
|
-
{
|
|
14
|
+
!isDocumentDBCompatModeEnabled() && {
|
|
15
15
|
index: { isoCode: 'text', _id: 'text' },
|
|
16
16
|
options: {
|
|
17
17
|
weights: {
|
|
@@ -1,10 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
mongodb,
|
|
3
|
+
TimestampFields,
|
|
4
|
+
ModuleInput,
|
|
5
|
+
assertDocumentDBCompatMode,
|
|
6
|
+
} from '@unchainedshop/mongodb';
|
|
2
7
|
import { emit, registerEvents } from '@unchainedshop/events';
|
|
3
8
|
import { generateDbFilterById, buildSortOptions, generateDbObjectId } from '@unchainedshop/mongodb';
|
|
4
9
|
import { SortDirection, SortOption } from '@unchainedshop/utils';
|
|
5
10
|
import { systemLocale } from '@unchainedshop/utils';
|
|
6
11
|
import { CountriesCollection } from '../db/CountriesCollection.js';
|
|
7
|
-
import addMigrations from '../migrations/addMigrations.js';
|
|
8
12
|
|
|
9
13
|
export type Country = {
|
|
10
14
|
_id?: string;
|
|
@@ -13,29 +17,30 @@ export type Country = {
|
|
|
13
17
|
defaultCurrencyCode?: string;
|
|
14
18
|
} & TimestampFields;
|
|
15
19
|
|
|
16
|
-
export type CountryQuery = {
|
|
20
|
+
export type CountryQuery = mongodb.Filter<Country> & {
|
|
17
21
|
includeInactive?: boolean;
|
|
18
22
|
queryString?: string;
|
|
19
23
|
};
|
|
20
24
|
|
|
21
25
|
const COUNTRY_EVENTS: string[] = ['COUNTRY_CREATE', 'COUNTRY_UPDATE', 'COUNTRY_REMOVE'];
|
|
22
26
|
|
|
23
|
-
export const buildFindSelector = ({
|
|
24
|
-
|
|
27
|
+
export const buildFindSelector = ({
|
|
28
|
+
includeInactive = false,
|
|
29
|
+
queryString = '',
|
|
30
|
+
...rest
|
|
31
|
+
}: CountryQuery) => {
|
|
32
|
+
const selector: mongodb.Filter<Country> = { ...rest, deleted: null };
|
|
25
33
|
if (!includeInactive) selector.isActive = true;
|
|
26
|
-
if (queryString)
|
|
34
|
+
if (queryString) {
|
|
35
|
+
assertDocumentDBCompatMode();
|
|
36
|
+
selector.$text = { $search: queryString };
|
|
37
|
+
}
|
|
27
38
|
return selector;
|
|
28
39
|
};
|
|
29
40
|
|
|
30
|
-
export const configureCountriesModule = async ({
|
|
31
|
-
db,
|
|
32
|
-
migrationRepository,
|
|
33
|
-
}: ModuleInput<Record<string, never>>) => {
|
|
41
|
+
export const configureCountriesModule = async ({ db }: ModuleInput<Record<string, never>>) => {
|
|
34
42
|
registerEvents(COUNTRY_EVENTS);
|
|
35
43
|
|
|
36
|
-
// Migration
|
|
37
|
-
addMigrations(migrationRepository);
|
|
38
|
-
|
|
39
44
|
const Countries = await CountriesCollection(db);
|
|
40
45
|
|
|
41
46
|
return {
|
|
@@ -59,17 +64,16 @@ export const configureCountriesModule = async ({
|
|
|
59
64
|
limit,
|
|
60
65
|
offset,
|
|
61
66
|
sort,
|
|
62
|
-
|
|
63
|
-
queryString,
|
|
67
|
+
...query
|
|
64
68
|
}: CountryQuery & {
|
|
65
69
|
limit?: number;
|
|
66
70
|
offset?: number;
|
|
67
|
-
sort?:
|
|
71
|
+
sort?: SortOption[];
|
|
68
72
|
},
|
|
69
73
|
options?: mongodb.FindOptions,
|
|
70
|
-
): Promise<
|
|
74
|
+
): Promise<Country[]> => {
|
|
71
75
|
const defaultSort = [{ key: 'created', value: SortDirection.ASC }] as SortOption[];
|
|
72
|
-
const countries = Countries.find(buildFindSelector(
|
|
76
|
+
const countries = Countries.find(buildFindSelector(query), {
|
|
73
77
|
skip: offset,
|
|
74
78
|
limit,
|
|
75
79
|
sort: buildSortOptions(sort || defaultSort),
|
|
@@ -88,8 +92,8 @@ export const configureCountriesModule = async ({
|
|
|
88
92
|
return !!countryCount;
|
|
89
93
|
},
|
|
90
94
|
|
|
91
|
-
name(country: Country,
|
|
92
|
-
return new Intl.DisplayNames([
|
|
95
|
+
name(country: Country, locale: Intl.Locale) {
|
|
96
|
+
return new Intl.DisplayNames([locale], { type: 'region', fallback: 'code' }).of(country.isoCode);
|
|
93
97
|
},
|
|
94
98
|
|
|
95
99
|
flagEmoji(country: Country) {
|
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { MigrationRepository } from '@unchainedshop/mongodb';
|
|
2
|
-
import { CountriesCollection } from '../db/CountriesCollection.js';
|
|
3
|
-
import { Country } from '../countries-index.js';
|
|
4
|
-
|
|
5
|
-
export default function addMigrations(repository: MigrationRepository) {
|
|
6
|
-
repository?.register({
|
|
7
|
-
id: 20241220123500,
|
|
8
|
-
name: 'Remove defaultCurrencyId from countries and add defaultCurrencyCode',
|
|
9
|
-
up: async () => {
|
|
10
|
-
const Currencies = await repository.db.collection<{ _id: string; isoCode: string }>('currencies');
|
|
11
|
-
const Countries = await CountriesCollection(repository.db);
|
|
12
|
-
|
|
13
|
-
const allCurrencies = await Currencies.find({}).toArray();
|
|
14
|
-
const allCountries = await Countries.find({}).toArray();
|
|
15
|
-
|
|
16
|
-
const currencyMap = allCurrencies.reduce(
|
|
17
|
-
(acc, currency) => {
|
|
18
|
-
acc[currency._id] = currency.isoCode;
|
|
19
|
-
return acc;
|
|
20
|
-
},
|
|
21
|
-
{} as Record<string, any>,
|
|
22
|
-
);
|
|
23
|
-
|
|
24
|
-
for (const country of allCountries) {
|
|
25
|
-
await Countries.updateOne(
|
|
26
|
-
{ _id: country._id, defaultCurrencyId: { $exists: true } },
|
|
27
|
-
{
|
|
28
|
-
$set: {
|
|
29
|
-
defaultCurrencyCode:
|
|
30
|
-
currencyMap[(country as Country & { defaultCurrencyId: string }).defaultCurrencyId],
|
|
31
|
-
},
|
|
32
|
-
$unset: {
|
|
33
|
-
defaultCurrencyId: 1,
|
|
34
|
-
},
|
|
35
|
-
},
|
|
36
|
-
);
|
|
37
|
-
}
|
|
38
|
-
},
|
|
39
|
-
});
|
|
40
|
-
}
|