Skip to content

Commit 3c9d9ed

Browse files
committed
Relax numeric attribute constraints
1 parent 355d146 commit 3c9d9ed

File tree

10 files changed

+66
-20
lines changed

10 files changed

+66
-20
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'create-triplit-app': patch
3+
---
4+
5+
Automated version bump for create-triplit-app after dependency changes
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@triplit/cli': patch
3+
---
4+
5+
Automated version bump for @triplit/cli after dependency changes

.changeset/gorgeous-turtles-tickle.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@triplit/db': patch
3+
---
4+
5+
Relax numeric attribute constraints

packages/db/src/schema/data-types/types/type-codec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ export type Decoded<T extends DataType> = T extends DataType
7676

7777
type DecodedRecord<T extends RecordType> = T extends RecordType
7878
? {
79-
[K in StringKey<T['properties']> as IsPropertyReadRequired<
79+
[K in keyof T['properties'] as IsPropertyReadRequired<
8080
T['properties'][K]
8181
> extends true
8282
? K
8383
: never]: Decoded<T['properties'][K]>;
8484
} & {
85-
[K in StringKey<T['properties']> as IsPropertyReadOptional<
85+
[K in keyof T['properties'] as IsPropertyReadOptional<
8686
T['properties'][K]
8787
> extends true
8888
? K
@@ -114,13 +114,13 @@ export type WriteDecoded<T extends DataType> = T extends DataType
114114

115115
type WriteDecodedRecord<T extends RecordType> = T extends RecordType
116116
? {
117-
[K in StringKey<T['properties']> as IsPropertyWriteRequired<
117+
[K in keyof T['properties'] as IsPropertyWriteRequired<
118118
T['properties'][K]
119119
> extends true
120120
? K
121121
: never]: WriteDecoded<T['properties'][K]>;
122122
} & {
123-
[K in StringKey<T['properties']> as IsPropertyWriteOptional<
123+
[K in keyof T['properties'] as IsPropertyWriteOptional<
124124
T['properties'][K]
125125
> extends true
126126
? K

packages/db/src/schema/validation.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,6 @@ function validatePropertyName(name: string) {
226226
if (hasNoValue(name)) return 'property name is not defined';
227227
if (typeof name !== 'string') return 'property name is not a string';
228228
if (name.length === 0) return 'property name is empty';
229-
if (/^[0-9]/.test(name))
230-
return 'property name cannot start with a numeric character';
231229
if (!/^[a-zA-Z0-9_]+$/.test(name))
232230
return 'property name contains invalid characters - only alphanumeric characters and underscores are allowed.';
233231
}

packages/db/src/utils/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export type Not<T extends boolean> = T extends true ? false : true;
88
*
99
* This prevents `keyof` from returning `number | Symbol` keys
1010
*/
11+
// TODO: improve this to allow for numeric keys, when changing to keyof T & (string | number) some compiler error occurs
1112
export type StringKey<T> = keyof T & string;
1213

1314
/**

packages/db/test/schema-validation.spec.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -310,20 +310,6 @@ describe('collections validation', () => {
310310
).toBe(
311311
'schema collections definition is invalid: "test" is not a valid collection: collection schema is invalid: type record property "" is invalid: property name is empty'
312312
);
313-
expect(
314-
validateSchema({
315-
collections: {
316-
test: {
317-
schema: S.Schema({
318-
id: S.Id(),
319-
'1name': S.String(),
320-
}),
321-
},
322-
},
323-
})
324-
).toBe(
325-
'schema collections definition is invalid: "test" is not a valid collection: collection schema is invalid: type record property "1name" is invalid: property name cannot start with a numeric character'
326-
);
327313
expect(
328314
validateSchema({
329315
collections: {

packages/db/test/type-record.spec.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,3 +864,42 @@ describe('Nested Properties', () => {
864864
});
865865
});
866866
});
867+
868+
it('allows numeric keys', async () => {
869+
const db = new DB({
870+
schema: {
871+
collections: {
872+
test: {
873+
schema: S.Schema({
874+
id: S.Id(),
875+
24: S.String(),
876+
nested: S.Record({
877+
25: S.String(),
878+
}),
879+
}),
880+
},
881+
},
882+
},
883+
});
884+
await db.insert('test', {
885+
id: 'item1',
886+
24: 'value',
887+
nested: { 25: 'nested value' },
888+
});
889+
const result = await db.fetchById('test', 'item1');
890+
expect(result).toEqual({
891+
id: 'item1',
892+
24: 'value',
893+
nested: { 25: 'nested value' },
894+
});
895+
await db.update('test', 'item1', async (entity) => {
896+
entity[24] = 'updated value';
897+
entity.nested[25] = 'updated nested value';
898+
});
899+
const updatedResult = await db.fetchById('test', 'item1');
900+
expect(updatedResult).toEqual({
901+
id: 'item1',
902+
24: 'updated value',
903+
nested: { 25: 'updated nested value' },
904+
});
905+
});

packages/db/test/typecheck/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ export type ExhaustiveSchemaSelectAll = {
6666
defaultString: string;
6767
enumString: 'a' | 'b' | 'c';
6868
nullableEnumString?: 'a' | 'b' | 'c' | null | undefined;
69+
70+
42: string;
6971
};
7072

7173
export type ExhaustiveSchemaInsert = {
@@ -125,4 +127,6 @@ export type ExhaustiveSchemaInsert = {
125127
defaultString?: string | null | undefined;
126128
enumString: 'a' | 'b' | 'c';
127129
nullableEnumString?: 'a' | 'b' | 'c' | null | undefined;
130+
131+
42: string;
128132
};

packages/db/test/utils/exhaustive-schema.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ export const EXHAUSTIVE_SCHEMA = {
7474
enum: ['a', 'b', 'c'],
7575
nullable: true,
7676
}),
77+
78+
// Numeric key
79+
42: S.String(),
7780
}),
7881
relationships: {
7982
subquery: {

0 commit comments

Comments
 (0)