electrodb 2.2.1 → 2.2.3
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/README.md +61 -6
- package/index.d.ts +53 -28
- package/index.js +3 -2
- package/package.json +1 -1
- package/src/schema.js +9 -0
package/README.md
CHANGED
|
@@ -5635,12 +5635,16 @@ let stores = await StoreLocations.malls({mallId}).query({buildingId, storeId}).g
|
|
|
5635
5635
|
ElectroDB using advanced dynamic typing techniques to automatically create types based on the configurations in your model. Changes to your model will automatically change the types returned by ElectroDB.
|
|
5636
5636
|
|
|
5637
5637
|
## Custom Attributes
|
|
5638
|
-
If you have a need for a custom attribute type (beyond those supported by ElectroDB) you can use the the export function `
|
|
5638
|
+
If you have a need for a custom attribute type (beyond those supported by ElectroDB) you can use the the export function `CustomAttributeType` or `OpaquePrimitiveType`. These functions can be passed a generic and that allow you to specify a custom attribute with ElectroDB:
|
|
5639
5639
|
|
|
5640
|
-
|
|
5640
|
+
### CustomAttributeType
|
|
5641
|
+
This function allows for a narrowing of ElectroDB's `any` type, which does not enforce runtime type checks. This can be useful for expressing complex attribute types.
|
|
5641
5642
|
|
|
5643
|
+
The function `CustomAttributeType` takes one argument, which is the "base" type of the attribute. For complex objects and arrays, the base object would be "any" but you can also use a base type like "string", "number", or "boolean" to accomplish (Opaque Keys)[#opaque-keys] which can be used as Composite Attributes.
|
|
5644
|
+
|
|
5645
|
+
In this example we accomplish a complex union type:
|
|
5642
5646
|
```typescript
|
|
5643
|
-
import { Entity,
|
|
5647
|
+
import { Entity, CustomAttributeType } from 'electrodb';
|
|
5644
5648
|
|
|
5645
5649
|
const table = 'workplace_table';
|
|
5646
5650
|
|
|
@@ -5654,7 +5658,6 @@ type PersonnelRole = {
|
|
|
5654
5658
|
contractEndDate: number;
|
|
5655
5659
|
};
|
|
5656
5660
|
|
|
5657
|
-
|
|
5658
5661
|
const person = new Entity({
|
|
5659
5662
|
model: {
|
|
5660
5663
|
entity: 'personnel',
|
|
@@ -5665,9 +5668,10 @@ const person = new Entity({
|
|
|
5665
5668
|
id: {
|
|
5666
5669
|
type: 'string'
|
|
5667
5670
|
},
|
|
5668
|
-
role:
|
|
5671
|
+
role: {
|
|
5672
|
+
type: CustomAttributeType<PersonnelRole>('any'),
|
|
5669
5673
|
required: true,
|
|
5670
|
-
}
|
|
5674
|
+
},
|
|
5671
5675
|
},
|
|
5672
5676
|
indexes: {
|
|
5673
5677
|
record: {
|
|
@@ -5684,6 +5688,57 @@ const person = new Entity({
|
|
|
5684
5688
|
}, { table });
|
|
5685
5689
|
```
|
|
5686
5690
|
|
|
5691
|
+
### Opaque Keys
|
|
5692
|
+
If you use Opaque Keys for identifiers or other primitive types, you can use the function `CustomAttributeType` and pass it the primitive base type of your key ('string', 'number', 'boolean'). This can be useful to gain more precise control over which properties can be used as entity identifiers, create unique unit types, etc.
|
|
5693
|
+
|
|
5694
|
+
```
|
|
5695
|
+
import { Entity, CustomAttributeType } from 'electrodb';
|
|
5696
|
+
|
|
5697
|
+
const UniqueKeySymbol: unique symbol = Symbol();
|
|
5698
|
+
type EmployeeID = string & {[UniqueKeySymbol]: any};
|
|
5699
|
+
|
|
5700
|
+
const UniqueAgeSymbol: unique symbol = Symbol();
|
|
5701
|
+
type Month = number & {[UniqueAgeSymbol]: any};
|
|
5702
|
+
|
|
5703
|
+
const table = 'workplace_table';
|
|
5704
|
+
|
|
5705
|
+
const person = new Entity({
|
|
5706
|
+
model: {
|
|
5707
|
+
entity: 'personnel',
|
|
5708
|
+
service: 'workplace',
|
|
5709
|
+
version: '1'
|
|
5710
|
+
},
|
|
5711
|
+
attributes: {
|
|
5712
|
+
employeeId: {
|
|
5713
|
+
type: CustomAttributeType<EmployeeID>('string')
|
|
5714
|
+
},
|
|
5715
|
+
firstName: {
|
|
5716
|
+
type: 'string',
|
|
5717
|
+
required: true,
|
|
5718
|
+
},
|
|
5719
|
+
lastName: {
|
|
5720
|
+
type: 'string',
|
|
5721
|
+
required: true,
|
|
5722
|
+
},
|
|
5723
|
+
ageInMonths: {
|
|
5724
|
+
type: CustomAttributeType<Month>('number')
|
|
5725
|
+
}
|
|
5726
|
+
},
|
|
5727
|
+
indexes: {
|
|
5728
|
+
record: {
|
|
5729
|
+
pk: {
|
|
5730
|
+
field: 'pk',
|
|
5731
|
+
composite: ['employeeId']
|
|
5732
|
+
},
|
|
5733
|
+
sk: {
|
|
5734
|
+
field: 'sk',
|
|
5735
|
+
composite: [],
|
|
5736
|
+
}
|
|
5737
|
+
}
|
|
5738
|
+
}
|
|
5739
|
+
}, { table });
|
|
5740
|
+
```
|
|
5741
|
+
|
|
5687
5742
|
## Exported Types
|
|
5688
5743
|
|
|
5689
5744
|
The following types are exported for easier use while using ElectroDB with TypeScript. The naming convention for the types include three different kinds:
|
package/index.d.ts
CHANGED
|
@@ -723,7 +723,7 @@ export interface BatchWriteOperationOptions<A extends string, F extends string,
|
|
|
723
723
|
}
|
|
724
724
|
|
|
725
725
|
export interface SetRecordActionOptions<A extends string, F extends string, C extends string, S extends Schema<A,F,C>, SetAttr,IndexCompositeAttributes,TableItem> {
|
|
726
|
-
go: UpdateRecordGo<
|
|
726
|
+
go: UpdateRecordGo<TableItem>;
|
|
727
727
|
params: ParamRecord<UpdateQueryParams>;
|
|
728
728
|
set: SetRecord<A,F,C,S, SetItem<A,F,C,S>,IndexCompositeAttributes,TableItem>;
|
|
729
729
|
remove: SetRecord<A,F,C,S, Array<keyof SetItem<A,F,C,S>>,IndexCompositeAttributes,TableItem>;
|
|
@@ -987,7 +987,7 @@ export type ServiceQueryRecordsGo<ResponseType, Options = QueryOptions> = <T = R
|
|
|
987
987
|
|
|
988
988
|
export type QueryRecordsGo<ResponseType, Options = QueryOptions> = <T = ResponseType>(options?: Options) => Promise<{ data: T, cursor: string | null }>;
|
|
989
989
|
|
|
990
|
-
export type UpdateRecordGo<ResponseType
|
|
990
|
+
export type UpdateRecordGo<ResponseType> = <T = ResponseType, Options extends UpdateQueryOptions = UpdateQueryOptions>(options?: Options) => Promise<{data: Partial<T>}>
|
|
991
991
|
|
|
992
992
|
export type PutRecordGo<ResponseType, Options = QueryOptions> = <T = ResponseType>(options?: Options) => Promise<{ data: T }>;
|
|
993
993
|
|
|
@@ -996,11 +996,6 @@ export type DeleteRecordOperationGo<ResponseType, Options = QueryOptions> = <T =
|
|
|
996
996
|
export type BatchWriteGo<ResponseType> = <O extends BulkOptions>(options?: O) =>
|
|
997
997
|
Promise<{ unprocessed: ResponseType }>
|
|
998
998
|
|
|
999
|
-
// export type PageRecord<ResponseType, CompositeAttributes> = (page?: (CompositeAttributes & OptionalDefaultEntityIdentifiers) | null, options?: PaginationOptions) => Promise<[
|
|
1000
|
-
// (CompositeAttributes & OptionalDefaultEntityIdentifiers) | null,
|
|
1001
|
-
// ResponseType
|
|
1002
|
-
// ]>;
|
|
1003
|
-
|
|
1004
999
|
export type ParamRecord<Options = ParamOptions> = <P>(options?: Options) => P;
|
|
1005
1000
|
|
|
1006
1001
|
export class ElectroError extends Error {
|
|
@@ -1450,16 +1445,23 @@ type StaticAttribute = {
|
|
|
1450
1445
|
readonly field?: string;
|
|
1451
1446
|
}
|
|
1452
1447
|
|
|
1453
|
-
type
|
|
1454
|
-
|
|
1455
|
-
|
|
1448
|
+
type CustomAttributeTypeName<T> = { readonly [CustomAttributeSymbol]: T };
|
|
1449
|
+
|
|
1450
|
+
type OpaquePrimitiveTypeName<T extends string | number | boolean> =
|
|
1451
|
+
T extends string ? 'string' & { readonly [OpaquePrimitiveSymbol]: T }
|
|
1452
|
+
: T extends number ? 'number' & { readonly [OpaquePrimitiveSymbol]: T }
|
|
1453
|
+
: T extends boolean ? 'boolean' & { readonly [OpaquePrimitiveSymbol]: T }
|
|
1454
|
+
: never;
|
|
1455
|
+
|
|
1456
|
+
type CustomAttribute = {
|
|
1457
|
+
readonly type: CustomAttributeTypeName<any> | OpaquePrimitiveTypeName<any>;
|
|
1456
1458
|
readonly required?: boolean;
|
|
1457
1459
|
readonly hidden?: boolean;
|
|
1458
1460
|
readonly readOnly?: boolean;
|
|
1459
|
-
readonly get?: (val:
|
|
1460
|
-
readonly set?: (val?:
|
|
1461
|
-
readonly default?:
|
|
1462
|
-
readonly validate?: ((val:
|
|
1461
|
+
readonly get?: (val: any, item: any) => any | undefined | void;
|
|
1462
|
+
readonly set?: (val?: any, item?: any) => any | undefined | void;
|
|
1463
|
+
readonly default?: any | (() => any);
|
|
1464
|
+
readonly validate?: ((val: any) => boolean) | ((val: any) => void) | ((val: any) => string | void);
|
|
1463
1465
|
readonly field?: string;
|
|
1464
1466
|
readonly watch?: ReadonlyArray<string> | "*";
|
|
1465
1467
|
};
|
|
@@ -1617,7 +1619,9 @@ type PartialDefinedKeys<T> = {
|
|
|
1617
1619
|
}
|
|
1618
1620
|
|
|
1619
1621
|
export type ItemAttribute<A extends Attribute> =
|
|
1620
|
-
A extends
|
|
1622
|
+
A['type'] extends OpaquePrimitiveTypeName<infer T>
|
|
1623
|
+
? T
|
|
1624
|
+
: A['type'] extends CustomAttributeTypeName<infer T>
|
|
1621
1625
|
? T
|
|
1622
1626
|
: A["type"] extends infer R
|
|
1623
1627
|
? R extends "string" ? string
|
|
@@ -1657,8 +1661,10 @@ export type ItemAttribute<A extends Attribute> =
|
|
|
1657
1661
|
: never
|
|
1658
1662
|
|
|
1659
1663
|
export type ReturnedAttribute<A extends Attribute> =
|
|
1660
|
-
A extends
|
|
1661
|
-
|
|
1664
|
+
A['type'] extends OpaquePrimitiveTypeName<infer T>
|
|
1665
|
+
? T
|
|
1666
|
+
: A['type'] extends CustomAttributeTypeName<infer T>
|
|
1667
|
+
? T
|
|
1662
1668
|
: A["type"] extends infer R
|
|
1663
1669
|
? R extends "static" ? never
|
|
1664
1670
|
: R extends "string" ? string
|
|
@@ -1712,7 +1718,9 @@ export type ReturnedAttribute<A extends Attribute> =
|
|
|
1712
1718
|
: never
|
|
1713
1719
|
|
|
1714
1720
|
export type CreatedAttribute<A extends Attribute> =
|
|
1715
|
-
A extends
|
|
1721
|
+
A['type'] extends OpaquePrimitiveTypeName<infer T>
|
|
1722
|
+
? T
|
|
1723
|
+
: A['type'] extends CustomAttributeTypeName<infer T>
|
|
1716
1724
|
? T
|
|
1717
1725
|
: A["type"] extends infer R
|
|
1718
1726
|
? R extends "static" ? never
|
|
@@ -1775,8 +1783,10 @@ export type CreatedItem<A extends string, F extends string, C extends string, S
|
|
|
1775
1783
|
}
|
|
1776
1784
|
|
|
1777
1785
|
export type EditableItemAttribute<A extends Attribute> =
|
|
1778
|
-
A extends
|
|
1779
|
-
|
|
1786
|
+
A['type'] extends OpaquePrimitiveTypeName<infer T>
|
|
1787
|
+
? T
|
|
1788
|
+
: A['type'] extends CustomAttributeTypeName<infer T>
|
|
1789
|
+
? T
|
|
1780
1790
|
: A extends ReadOnlyAttribute
|
|
1781
1791
|
? never
|
|
1782
1792
|
: A["type"] extends infer R
|
|
@@ -1822,7 +1832,9 @@ export type EditableItemAttribute<A extends Attribute> =
|
|
|
1822
1832
|
: never
|
|
1823
1833
|
|
|
1824
1834
|
export type UpdatableItemAttribute<A extends Attribute> =
|
|
1825
|
-
A extends
|
|
1835
|
+
A['type'] extends OpaquePrimitiveTypeName<infer T>
|
|
1836
|
+
? T
|
|
1837
|
+
: A['type'] extends CustomAttributeTypeName<infer T>
|
|
1826
1838
|
? T
|
|
1827
1839
|
: A extends ReadOnlyAttribute
|
|
1828
1840
|
? never
|
|
@@ -1884,7 +1896,9 @@ export type UpdatableItemAttribute<A extends Attribute> =
|
|
|
1884
1896
|
: never
|
|
1885
1897
|
|
|
1886
1898
|
export type RemovableItemAttribute<A extends Attribute> =
|
|
1887
|
-
A extends
|
|
1899
|
+
A['type'] extends OpaquePrimitiveTypeName<infer T>
|
|
1900
|
+
? T
|
|
1901
|
+
: A['type'] extends CustomAttributeTypeName<infer T>
|
|
1888
1902
|
? T
|
|
1889
1903
|
: A extends ReadOnlyAttribute | RequiredAttribute
|
|
1890
1904
|
? never
|
|
@@ -2068,7 +2082,7 @@ export type RemoveItem<A extends string, F extends string, C extends string, S e
|
|
|
2068
2082
|
export type AppendItem<A extends string, F extends string, C extends string, S extends Schema<A,F,C>> =
|
|
2069
2083
|
{
|
|
2070
2084
|
[
|
|
2071
|
-
P in keyof ItemTypeDescription<A,F,C,S> as ItemTypeDescription<A,F,C,S>[P] extends 'list' | 'any' | 'custom'
|
|
2085
|
+
P in keyof ItemTypeDescription<A,F,C,S> as ItemTypeDescription<A,F,C,S>[P] extends 'list' | 'any' | 'custom' | CustomAttributeTypeName<any>
|
|
2072
2086
|
? P
|
|
2073
2087
|
: never
|
|
2074
2088
|
]?: P extends keyof SetItem<A,F,C,S>
|
|
@@ -2079,7 +2093,7 @@ export type AppendItem<A extends string, F extends string, C extends string, S e
|
|
|
2079
2093
|
export type AddItem<A extends string, F extends string, C extends string, S extends Schema<A,F,C>> =
|
|
2080
2094
|
{
|
|
2081
2095
|
[
|
|
2082
|
-
P in keyof ItemTypeDescription<A,F,C,S> as ItemTypeDescription<A,F,C,S>[P] extends 'number' | 'any' | 'set' | 'custom'
|
|
2096
|
+
P in keyof ItemTypeDescription<A,F,C,S> as ItemTypeDescription<A,F,C,S>[P] extends 'number' | 'any' | 'set' | 'custom' | CustomAttributeTypeName<any> | OpaquePrimitiveTypeName<number>
|
|
2083
2097
|
? P
|
|
2084
2098
|
: never
|
|
2085
2099
|
]?: P extends keyof SetItem<A,F,C,S>
|
|
@@ -2090,7 +2104,7 @@ export type AddItem<A extends string, F extends string, C extends string, S exte
|
|
|
2090
2104
|
export type SubtractItem<A extends string, F extends string, C extends string, S extends Schema<A,F,C>> =
|
|
2091
2105
|
{
|
|
2092
2106
|
[
|
|
2093
|
-
P in keyof ItemTypeDescription<A,F,C,S> as ItemTypeDescription<A,F,C,S>[P] extends 'number' | 'any' | 'custom'
|
|
2107
|
+
P in keyof ItemTypeDescription<A,F,C,S> as ItemTypeDescription<A,F,C,S>[P] extends 'number' | 'any' | 'custom' | OpaquePrimitiveTypeName<number>
|
|
2094
2108
|
? P
|
|
2095
2109
|
: never
|
|
2096
2110
|
]?: P extends keyof SetItem<A,F,C,S>
|
|
@@ -2101,7 +2115,7 @@ export type SubtractItem<A extends string, F extends string, C extends string, S
|
|
|
2101
2115
|
export type DeleteItem<A extends string, F extends string, C extends string, S extends Schema<A,F,C>> =
|
|
2102
2116
|
{
|
|
2103
2117
|
[
|
|
2104
|
-
P in keyof ItemTypeDescription<A,F,C,S> as ItemTypeDescription<A,F,C,S>[P] extends 'any' | 'set' | 'custom'
|
|
2118
|
+
P in keyof ItemTypeDescription<A,F,C,S> as ItemTypeDescription<A,F,C,S>[P] extends 'any' | 'set' | 'custom' | CustomAttributeTypeName<any>
|
|
2105
2119
|
? P
|
|
2106
2120
|
: never
|
|
2107
2121
|
]?: P extends keyof SetItem<A,F,C,S>
|
|
@@ -2112,6 +2126,7 @@ export type DeleteItem<A extends string, F extends string, C extends string, S e
|
|
|
2112
2126
|
export declare const WhereSymbol: unique symbol;
|
|
2113
2127
|
export declare const UpdateDataSymbol: unique symbol;
|
|
2114
2128
|
export declare const CustomAttributeSymbol: unique symbol;
|
|
2129
|
+
export declare const OpaquePrimitiveSymbol: unique symbol;
|
|
2115
2130
|
|
|
2116
2131
|
export type WhereAttributeSymbol<T extends any> =
|
|
2117
2132
|
{ [WhereSymbol]: void }
|
|
@@ -2313,6 +2328,16 @@ type CustomAttributeDefinition<T> = {
|
|
|
2313
2328
|
readonly validate?: ((val: T) => boolean) | ((val: T) => void) | ((val: T) => string | void);
|
|
2314
2329
|
readonly field?: string;
|
|
2315
2330
|
readonly watch?: ReadonlyArray<string> | "*";
|
|
2316
|
-
}
|
|
2331
|
+
};
|
|
2317
2332
|
|
|
2318
|
-
|
|
2333
|
+
/** @depricated use 'CustomAttributeType' or 'OpaquePrimitiveType' instead */
|
|
2334
|
+
declare function createCustomAttribute<T, A extends Readonly<CustomAttributeDefinition<T>> = Readonly<CustomAttributeDefinition<T>>>(definition?: A): A & { type: CustomAttributeTypeName<T> };
|
|
2335
|
+
|
|
2336
|
+
declare function CustomAttributeType<T>(
|
|
2337
|
+
base: T extends string ? 'string'
|
|
2338
|
+
: T extends number ? 'number'
|
|
2339
|
+
: T extends boolean ? 'boolean'
|
|
2340
|
+
: 'any'
|
|
2341
|
+
): T extends string | number | boolean
|
|
2342
|
+
? OpaquePrimitiveTypeName<T>
|
|
2343
|
+
: CustomAttributeTypeName<T>;
|
package/index.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
const { Entity } = require("./src/entity");
|
|
2
2
|
const { Service } = require("./src/service");
|
|
3
|
-
const { createCustomAttribute } = require('./src/schema');
|
|
3
|
+
const { createCustomAttribute, CustomAttributeType } = require('./src/schema');
|
|
4
4
|
const { ElectroError, ElectroValidationError, ElectroUserValidationError, ElectroAttributeValidationError } = require('./src/errors');
|
|
5
5
|
|
|
6
6
|
module.exports = {
|
|
7
7
|
Entity,
|
|
8
8
|
Service,
|
|
9
|
-
createCustomAttribute,
|
|
10
9
|
ElectroError,
|
|
10
|
+
CustomAttributeType,
|
|
11
|
+
createCustomAttribute,
|
|
11
12
|
ElectroValidationError,
|
|
12
13
|
};
|
package/package.json
CHANGED
package/src/schema.js
CHANGED
|
@@ -1464,10 +1464,19 @@ function createCustomAttribute(definition = {}) {
|
|
|
1464
1464
|
};
|
|
1465
1465
|
}
|
|
1466
1466
|
|
|
1467
|
+
function CustomAttributeType(base) {
|
|
1468
|
+
const supported = ['string', 'number', 'boolean', 'any'];
|
|
1469
|
+
if (!supported.includes(base)) {
|
|
1470
|
+
throw new Error(`OpaquePrimitiveType only supports base types: ${u.commaSeparatedString(supported)}`);
|
|
1471
|
+
}
|
|
1472
|
+
return base;
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1467
1475
|
module.exports = {
|
|
1468
1476
|
Schema,
|
|
1469
1477
|
Attribute,
|
|
1470
1478
|
SetAttribute,
|
|
1471
1479
|
CastTypes,
|
|
1480
|
+
CustomAttributeType,
|
|
1472
1481
|
createCustomAttribute,
|
|
1473
1482
|
};
|