@instantdb/core 0.22.96-experimental.drewh-ts-target.20761590091.1 → 0.22.96-experimental.surgical.20765817844.1
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/dist/commonjs/Connection.js +51 -50
- package/dist/commonjs/Connection.js.map +1 -1
- package/dist/commonjs/InMemoryStorage.js +32 -13
- package/dist/commonjs/InMemoryStorage.js.map +1 -1
- package/dist/commonjs/IndexedDBStorage.js +217 -193
- package/dist/commonjs/IndexedDBStorage.js.map +1 -1
- package/dist/commonjs/InstantError.js +0 -1
- package/dist/commonjs/InstantError.js.map +1 -1
- package/dist/commonjs/Reactor.js +610 -566
- package/dist/commonjs/Reactor.js.map +1 -1
- package/dist/commonjs/StorageAPI.js +70 -51
- package/dist/commonjs/StorageAPI.js.map +1 -1
- package/dist/commonjs/SyncTable.js +81 -68
- package/dist/commonjs/SyncTable.js.map +1 -1
- package/dist/commonjs/WindowNetworkListener.js +13 -2
- package/dist/commonjs/WindowNetworkListener.js.map +1 -1
- package/dist/commonjs/__types__/fieldsTypeTest.js +16 -8
- package/dist/commonjs/__types__/fieldsTypeTest.js.map +1 -1
- package/dist/commonjs/__types__/useDatesTypeTest.js +6 -3
- package/dist/commonjs/__types__/useDatesTypeTest.js.map +1 -1
- package/dist/commonjs/authAPI.js +79 -62
- package/dist/commonjs/authAPI.js.map +1 -1
- package/dist/commonjs/createRouteHandler.js +15 -5
- package/dist/commonjs/createRouteHandler.js.map +1 -1
- package/dist/commonjs/datalog.js +1 -1
- package/dist/commonjs/datalog.js.map +1 -1
- package/dist/commonjs/devtool.js +8 -26
- package/dist/commonjs/devtool.js.map +1 -1
- package/dist/commonjs/framework.d.ts.map +1 -1
- package/dist/commonjs/framework.js +152 -142
- package/dist/commonjs/framework.js.map +1 -1
- package/dist/commonjs/index.js +190 -204
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/instaml.js +30 -44
- package/dist/commonjs/instaml.js.map +1 -1
- package/dist/commonjs/instaql.js +33 -25
- package/dist/commonjs/instaql.js.map +1 -1
- package/dist/commonjs/parseSchemaFromJSON.js +7 -6
- package/dist/commonjs/parseSchemaFromJSON.js.map +1 -1
- package/dist/commonjs/presence.js +8 -7
- package/dist/commonjs/presence.js.map +1 -1
- package/dist/commonjs/queryValidation.js +2 -1
- package/dist/commonjs/queryValidation.js.map +1 -1
- package/dist/commonjs/schema.js +6 -8
- package/dist/commonjs/schema.js.map +1 -1
- package/dist/commonjs/schemaTypes.js +3 -22
- package/dist/commonjs/schemaTypes.js.map +1 -1
- package/dist/commonjs/store.js +38 -29
- package/dist/commonjs/store.js.map +1 -1
- package/dist/commonjs/transactionValidation.js +2 -1
- package/dist/commonjs/transactionValidation.js.map +1 -1
- package/dist/commonjs/utils/Deferred.js +0 -3
- package/dist/commonjs/utils/Deferred.js.map +1 -1
- package/dist/commonjs/utils/PersistedObject.js +233 -216
- package/dist/commonjs/utils/PersistedObject.js.map +1 -1
- package/dist/commonjs/utils/fetch.js +19 -9
- package/dist/commonjs/utils/fetch.js.map +1 -1
- package/dist/commonjs/utils/linkIndex.js +4 -2
- package/dist/commonjs/utils/linkIndex.js.map +1 -1
- package/dist/commonjs/utils/object.js +1 -1
- package/dist/commonjs/utils/object.js.map +1 -1
- package/dist/esm/Connection.js +51 -50
- package/dist/esm/Connection.js.map +1 -1
- package/dist/esm/InMemoryStorage.js +32 -13
- package/dist/esm/InMemoryStorage.js.map +1 -1
- package/dist/esm/IndexedDBStorage.js +217 -193
- package/dist/esm/IndexedDBStorage.js.map +1 -1
- package/dist/esm/InstantError.js +0 -1
- package/dist/esm/InstantError.js.map +1 -1
- package/dist/esm/Reactor.js +610 -566
- package/dist/esm/Reactor.js.map +1 -1
- package/dist/esm/StorageAPI.js +70 -51
- package/dist/esm/StorageAPI.js.map +1 -1
- package/dist/esm/SyncTable.js +81 -68
- package/dist/esm/SyncTable.js.map +1 -1
- package/dist/esm/WindowNetworkListener.js +13 -2
- package/dist/esm/WindowNetworkListener.js.map +1 -1
- package/dist/esm/__types__/fieldsTypeTest.js +16 -8
- package/dist/esm/__types__/fieldsTypeTest.js.map +1 -1
- package/dist/esm/__types__/useDatesTypeTest.js +6 -3
- package/dist/esm/__types__/useDatesTypeTest.js.map +1 -1
- package/dist/esm/authAPI.js +79 -62
- package/dist/esm/authAPI.js.map +1 -1
- package/dist/esm/createRouteHandler.js +15 -5
- package/dist/esm/createRouteHandler.js.map +1 -1
- package/dist/esm/datalog.js +1 -1
- package/dist/esm/datalog.js.map +1 -1
- package/dist/esm/devtool.js +8 -26
- package/dist/esm/devtool.js.map +1 -1
- package/dist/esm/framework.d.ts.map +1 -1
- package/dist/esm/framework.js +152 -142
- package/dist/esm/framework.js.map +1 -1
- package/dist/esm/index.js +190 -204
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/instaml.js +30 -44
- package/dist/esm/instaml.js.map +1 -1
- package/dist/esm/instaql.js +33 -25
- package/dist/esm/instaql.js.map +1 -1
- package/dist/esm/parseSchemaFromJSON.js +7 -6
- package/dist/esm/parseSchemaFromJSON.js.map +1 -1
- package/dist/esm/presence.js +8 -7
- package/dist/esm/presence.js.map +1 -1
- package/dist/esm/queryValidation.js +2 -1
- package/dist/esm/queryValidation.js.map +1 -1
- package/dist/esm/schema.js +6 -8
- package/dist/esm/schema.js.map +1 -1
- package/dist/esm/schemaTypes.js +3 -22
- package/dist/esm/schemaTypes.js.map +1 -1
- package/dist/esm/store.js +38 -29
- package/dist/esm/store.js.map +1 -1
- package/dist/esm/transactionValidation.js +2 -1
- package/dist/esm/transactionValidation.js.map +1 -1
- package/dist/esm/utils/Deferred.js +0 -3
- package/dist/esm/utils/Deferred.js.map +1 -1
- package/dist/esm/utils/PersistedObject.js +233 -216
- package/dist/esm/utils/PersistedObject.js.map +1 -1
- package/dist/esm/utils/fetch.js +19 -9
- package/dist/esm/utils/fetch.js.map +1 -1
- package/dist/esm/utils/linkIndex.js +4 -2
- package/dist/esm/utils/linkIndex.js.map +1 -1
- package/dist/esm/utils/object.js +1 -1
- package/dist/esm/utils/object.js.map +1 -1
- package/dist/standalone/index.js +2367 -2610
- package/dist/standalone/index.umd.cjs +3 -3
- package/package.json +2 -2
- package/src/framework.ts +1 -0
package/dist/esm/schema.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,WAAW,EACX,gBAAgB,GAOjB,MAAM,kBAAkB,CAAC;AAE1B,aAAa;AACb,MAAM;AAEN;;;;;;;;;;;;;GAaG;AACH,SAAS,KAAK,CAGZ,QAA8B,EAAE,KAAY;IAC5C,OAAO,IAAI,gBAAgB,CACzB,uBAAuB,CAA8B,QAAQ,EAAE,KAAK,CAAC;IACrE,0EAA0E;IAC1E,2DAA2D;IAC3D,2EAA2E;IAC3E,0EAA0E;IAC1E,4CAA4C;IAC5C,KAAsB,EACtB,SAAoC,CACrC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,SAAS,MAAM,CACb,KAAY;IAEZ,OAAO,IAAI,SAAS,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;AAClC,CAAC;AAED,SAAS,MAAM;IAKb,OAAO,IAAI,WAAW,CAAC,QAAQ,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;AAChD,CAAC;AAED,SAAS,MAAM;IACb,OAAO,IAAI,WAAW,CAAC,QAAQ,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;AAChD,CAAC;AAED,SAAS,OAAO;IACd,OAAO,IAAI,WAAW,CAAC,SAAS,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;AACjD,CAAC;AAED,SAAS,IAAI;IACX,OAAO,IAAI,WAAW,CAAC,MAAM,EAAE,IAAI,EAAE,KAAK,CAAC,CAAC;AAC9C,CAAC;AAED,SAAS,IAAI;IACX,OAAO,IAAI,WAAW,CAAC,MAAM,EAAE,IAA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kCAClB,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,GACpB,UAAU,CAAC,GAAG,CAAC,IAAI,CAAC,EACvB;KACH,CAAC,CACH,CAAC;IAEF,OAAO,gBAAoC,CAAC;AAC9C,CAAC;AAOD;;;;;;;;;;;;;;;;GAgBG;AACH,SAAS,MAAM,CAIb,EACA,QAAQ,EACR,KAAK,EACL,KAAK,GAKN;IACC,MAAM,QAAQ,GAAG,CAAC,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,EAAE,CAAU,CAAC;IACxC,MAAM,QAAQ,GAAG,CAAC,KAAK,aAAL,KAAK,cAAL,KAAK,GAAI,EAAE,CAAU,CAAC;IACxC,OAAO,IAAI,gBAAgB,CACzB,uBAAuB,CAA8B,QAAQ,EAAE,QAAQ,CAAC;IACxE,0EAA0E;IAC1E,2DAA2D;IAC3D,2EAA2E;IAC3E,0EAA0E;IAC1E,4CAA4C;IAC5C,QAAyB,EACzB,QAAQ,CACT,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,MAAM,CAAC,GAAG;IACf,aAAa;IACb,KAAK;IACL,MAAM;IACN,MAAM;IACN,cAAc;IACd,MAAM;IACN,MAAM;IACN,OAAO;IACP,IAAI;IACJ,IAAI;IACJ,GAAG;CACJ,CAAC","sourcesContent":["import {\n EntityDef,\n DataAttrDef,\n InstantSchemaDef,\n type EntitiesDef,\n type AttrsDefs,\n type EntitiesWithLinks,\n type LinksDef,\n type RoomsDef,\n type UnknownRooms,\n} from './schemaTypes.ts';\n\n// ==========\n// API\n\n/**\n * @deprecated\n * `i.graph` is deprecated. Use `i.schema` instead.\n *\n * @example\n * // Before\n * i.graph(entities, links).withRoomSchema<RoomType>();\n *\n * // After\n * i.schema({ entities, links, rooms })\n *\n * @see\n * https://instantdb.com/docs/modeling-data\n */\nfunction graph<\n EntitiesWithoutLinks extends EntitiesDef,\n const Links extends LinksDef<EntitiesWithoutLinks>,\n>(entities: EntitiesWithoutLinks, links: Links) {\n return new InstantSchemaDef(\n enrichEntitiesWithLinks<EntitiesWithoutLinks, Links>(entities, links),\n // (XXX): LinksDef<any> stems from TypeScript’s inability to reconcile the\n // type EntitiesWithLinks<EntitiesWithoutLinks, Links> with\n // EntitiesWithoutLinks. TypeScript is strict about ensuring that types are\n // correctly aligned and does not allow for substituting a type that might\n // be broader or have additional properties.\n links as LinksDef<any>,\n undefined as unknown as UnknownRooms,\n );\n}\n\n/**\n * Creates an entity definition, to be used in conjunction with `i.graph`.\n *\n * @see https://instantdb.com/docs/modeling-data\n * @example\n * {\n * posts: i.entity({\n * title: i.string(),\n * body: i.string(),\n * }),\n * comments: i.entity({\n * body: i.string(),\n * })\n * }\n */\nfunction entity<Attrs extends AttrsDefs>(\n attrs: Attrs,\n): EntityDef<Attrs, {}, void> {\n return new EntityDef(attrs, {});\n}\n\nfunction string<StringEnum extends string = string>(): DataAttrDef<\n StringEnum,\n true,\n false\n> {\n return new DataAttrDef('string', true, false);\n}\n\nfunction number(): DataAttrDef<number, true, false> {\n return new DataAttrDef('number', true, false);\n}\n\nfunction boolean(): DataAttrDef<boolean, true, false> {\n return new DataAttrDef('boolean', true, false);\n}\n\nfunction date(): DataAttrDef<Date, true, false> {\n return new DataAttrDef('date', true, false);\n}\n\nfunction json<T = any>(): DataAttrDef<T, true, false> {\n return new DataAttrDef('json', true, false);\n}\n\nfunction any(): DataAttrDef<any, true, false> {\n return new DataAttrDef('json', true, false);\n}\n\n// ==========\n// internal\n\nfunction enrichEntitiesWithLinks<\n EntitiesWithoutLinks extends EntitiesDef,\n Links extends LinksDef<any>,\n EnrichedEntities = EntitiesWithLinks<EntitiesWithoutLinks, Links>,\n>(entities: EntitiesWithoutLinks, links: Links): EnrichedEntities {\n const linksIndex: LinksIndex = { fwd: {}, rev: {} };\n\n for (const linkDef of Object.values(links)) {\n linksIndex.fwd[linkDef.forward.on as string] ||= {};\n linksIndex.rev[linkDef.reverse.on as string] ||= {};\n\n linksIndex.fwd[linkDef.forward.on as string][linkDef.forward.label] = {\n entityName: linkDef.reverse.on as string,\n cardinality: linkDef.forward.has,\n };\n\n linksIndex.rev[linkDef.reverse.on as string][linkDef.reverse.label] = {\n entityName: linkDef.forward.on as string,\n cardinality: linkDef.reverse.has,\n };\n }\n\n const enrichedEntities = Object.fromEntries(\n Object.entries(entities).map(([name, def]) => [\n name,\n new EntityDef(def.attrs, {\n ...linksIndex.fwd[name],\n ...linksIndex.rev[name],\n }),\n ]),\n );\n\n return enrichedEntities as EnrichedEntities;\n}\n\ntype LinksIndex = Record<\n 'fwd' | 'rev',\n Record<string, Record<string, { entityName: string; cardinality: string }>>\n>;\n\n/**\n * Lets you define a schema for your database.\n *\n * You can define entities, links between entities, and if you use\n * presence, you can define rooms.\n *\n * You can push this schema to your database with the CLI,\n * or use it inside `init`, to get typesafety and autocompletion.\n *\n * @see https://instantdb.com/docs/modeling-data\n * @example\n * i.schema({\n * entities: { },\n * links: { },\n * rooms: { }\n * });\n */\nfunction schema<\n EntitiesWithoutLinks extends EntitiesDef,\n const Links extends LinksDef<EntitiesWithoutLinks>,\n Rooms extends RoomsDef = {},\n>({\n entities,\n links,\n rooms,\n}: {\n entities: EntitiesWithoutLinks;\n links?: Links;\n rooms?: Rooms;\n}) {\n const linksDef = (links ?? {}) as Links;\n const roomsDef = (rooms ?? {}) as Rooms;\n return new InstantSchemaDef(\n enrichEntitiesWithLinks<EntitiesWithoutLinks, Links>(entities, linksDef),\n // (XXX): LinksDef<any> stems from TypeScript's inability to reconcile the\n // type EntitiesWithLinks<EntitiesWithoutLinks, Links> with\n // EntitiesWithoutLinks. TypeScript is strict about ensuring that types are\n // correctly aligned and does not allow for substituting a type that might\n // be broader or have additional properties.\n linksDef as LinksDef<any>,\n roomsDef,\n );\n}\n\nexport const i = {\n // constructs\n graph,\n schema,\n entity,\n // value types\n string,\n number,\n boolean,\n date,\n json,\n any,\n};\n"]}
|
package/dist/esm/schemaTypes.js
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
1
|
export class DataAttrDef {
|
|
2
|
-
valueType;
|
|
3
|
-
required;
|
|
4
|
-
isIndexed;
|
|
5
|
-
config;
|
|
6
|
-
metadata = {};
|
|
7
2
|
constructor(valueType, required, isIndexed, config = { indexed: false, unique: false }) {
|
|
8
3
|
this.valueType = valueType;
|
|
9
4
|
this.required = required;
|
|
10
5
|
this.isIndexed = isIndexed;
|
|
11
6
|
this.config = config;
|
|
7
|
+
this.metadata = {};
|
|
12
8
|
}
|
|
13
9
|
/**
|
|
14
10
|
* @deprecated Only use this temporarily for attributes that you want
|
|
@@ -22,29 +18,19 @@ export class DataAttrDef {
|
|
|
22
18
|
return new DataAttrDef(this.valueType, false, this.isIndexed, this.config);
|
|
23
19
|
}
|
|
24
20
|
unique() {
|
|
25
|
-
return new DataAttrDef(this.valueType, this.required, this.isIndexed, {
|
|
26
|
-
...this.config,
|
|
27
|
-
unique: true,
|
|
28
|
-
});
|
|
21
|
+
return new DataAttrDef(this.valueType, this.required, this.isIndexed, Object.assign(Object.assign({}, this.config), { unique: true }));
|
|
29
22
|
}
|
|
30
23
|
indexed() {
|
|
31
|
-
return new DataAttrDef(this.valueType, this.required, true, {
|
|
32
|
-
...this.config,
|
|
33
|
-
indexed: true,
|
|
34
|
-
});
|
|
24
|
+
return new DataAttrDef(this.valueType, this.required, true, Object.assign(Object.assign({}, this.config), { indexed: true }));
|
|
35
25
|
}
|
|
36
26
|
}
|
|
37
27
|
export class LinkAttrDef {
|
|
38
|
-
cardinality;
|
|
39
|
-
entityName;
|
|
40
28
|
constructor(cardinality, entityName) {
|
|
41
29
|
this.cardinality = cardinality;
|
|
42
30
|
this.entityName = entityName;
|
|
43
31
|
}
|
|
44
32
|
}
|
|
45
33
|
export class EntityDef {
|
|
46
|
-
attrs;
|
|
47
|
-
links;
|
|
48
34
|
constructor(attrs, links) {
|
|
49
35
|
this.attrs = attrs;
|
|
50
36
|
this.links = links;
|
|
@@ -54,9 +40,6 @@ export class EntityDef {
|
|
|
54
40
|
}
|
|
55
41
|
}
|
|
56
42
|
export class InstantSchemaDef {
|
|
57
|
-
entities;
|
|
58
|
-
links;
|
|
59
|
-
rooms;
|
|
60
43
|
constructor(entities, links, rooms) {
|
|
61
44
|
this.entities = entities;
|
|
62
45
|
this.links = links;
|
|
@@ -92,8 +75,6 @@ export class InstantSchemaDef {
|
|
|
92
75
|
* @see https://instantdb.com/docs/modeling-data
|
|
93
76
|
*/
|
|
94
77
|
export class InstantGraph {
|
|
95
|
-
entities;
|
|
96
|
-
links;
|
|
97
78
|
constructor(entities, links) {
|
|
98
79
|
this.entities = entities;
|
|
99
80
|
this.links = links;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"schemaTypes.js","sourceRoot":"","sources":["../../src/schemaTypes.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,WAAW;IAQb;IACA;IACA;IAEA;IAPF,QAAQ,GAA4B,EAAE,CAAC;IAE9C,YACS,SAAqB,EACrB,QAAoB,EACpB,SAAoB,EAEpB,SAIH,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE;QAR9B,cAAS,GAAT,SAAS,CAAY;QACrB,aAAQ,GAAR,QAAQ,CAAY;QACpB,cAAS,GAAT,SAAS,CAAW;QAEpB,WAAM,GAAN,MAAM,CAIwB;IACpC,CAAC;IAEJ;;;;OAIG;IACH,cAAc;QACZ,OAAO,IAAI,WAAW,CACpB,IAAI,CAAC,SAAS,EACd,KAAwB,EACxB,IAAI,CAAC,SAAS,EACd,IAAI,CAAC,MAAM,CACZ,CAAC;IACJ,CAAC;IAED,QAAQ;QACN,OAAO,IAAI,WAAW,CACpB,IAAI,CAAC,SAAS,EACd,KAAK,EACL,IAAI,CAAC,SAAS,EACd,IAAI,CAAC,MAAM,CACZ,CAAC;IACJ,CAAC;IAED,MAAM;QACJ,OAAO,IAAI,WAAW,CACpB,IAAI,CAAC,SAAS,EACd,IAAI,CAAC,QAAQ,EACb,IAAI,CAAC,SAAS,EACd;YACE,GAAG,IAAI,CAAC,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X,CAAC;IACJ,CAAC;CACF;AAqGD,MAAM,OAAO,uBAAwB,SAAQ,gBAI5C;CAAG","sourcesContent":["import type { RoomSchemaShape } from './presence.ts';\nimport { Expand } from './queryTypes.ts';\n\nexport class DataAttrDef<\n ValueType,\n IsRequired extends RequirementKind,\n IsIndexed extends boolean,\n> {\n public metadata: Record<string, unknown> = {};\n\n constructor(\n public valueType: ValueTypes,\n public required: IsRequired,\n public isIndexed: IsIndexed,\n\n public config: {\n indexed: boolean;\n unique: boolean;\n // clientValidator?: (value: ValueType) => boolean;\n } = { indexed: false, unique: false },\n ) {}\n\n /**\n * @deprecated Only use this temporarily for attributes that you want\n * to treat as required in frontend code but can’t yet mark as required\n * and enforced for backend\n */\n clientRequired() {\n return new DataAttrDef<ValueType, true, IsIndexed>(\n this.valueType,\n false as unknown as true,\n this.isIndexed,\n this.config,\n );\n }\n\n optional() {\n return new DataAttrDef<ValueType, false, IsIndexed>(\n this.valueType,\n false,\n this.isIndexed,\n this.config,\n );\n }\n\n unique() {\n return new DataAttrDef<ValueType, IsRequired, IsIndexed>(\n this.valueType,\n this.required,\n this.isIndexed,\n {\n ...this.config,\n unique: true,\n },\n );\n }\n\n indexed() {\n return new DataAttrDef<ValueType, IsRequired, true>(\n this.valueType,\n this.required,\n true,\n {\n ...this.config,\n indexed: true,\n },\n );\n }\n\n // clientValidate(clientValidator: (value: ValueType) => boolean) {\n // return new DataAttrDef(this.valueType, this.required, {\n // ...this.config,\n // clientValidator,\n // });\n // }\n}\n\nexport class LinkAttrDef<\n Cardinality extends CardinalityKind,\n EntityName extends string,\n> {\n constructor(\n public cardinality: Cardinality,\n public entityName: EntityName,\n ) {}\n}\n\nexport interface IContainEntitiesAndLinks<\n Entities extends EntitiesDef,\n Links extends LinksDef<Entities>,\n> {\n entities: Entities;\n links: Links;\n}\n\n// ==========\n// base types\n\nexport type ValueTypes = 'string' | 'number' | 'boolean' | 'date' | 'json';\n\nexport type CardinalityKind = 'one' | 'many';\n\n// true - force required\n// false - optional, not required\nexport type RequirementKind = true | false;\n\nexport type AttrsDefs = Record<string, DataAttrDef<any, any, any>>;\n\nexport class EntityDef<\n Attrs extends AttrsDefs,\n Links extends Record<string, LinkAttrDef<any, any>>,\n AsType,\n> {\n constructor(\n public attrs: Attrs,\n public links: Links,\n ) {}\n\n asType<_AsType extends Partial<MappedAttrs<Attrs, boolean>>>() {\n return new EntityDef<Attrs, Links, _AsType>(this.attrs, this.links);\n }\n}\n\nexport type EntityDefFromSchema<\n S extends IContainEntitiesAndLinks<any, any>,\n K extends keyof S['entities'],\n> = {\n [k in keyof S['entities'][K]['attrs']]: S['entities'][K] extends EntityDef<\n any,\n any,\n any\n >\n ? S['entities'][K]['attrs'][k]\n : never;\n};\n\nexport type EntitiesDef = Record<string, EntityDef<any, any, any>>;\n\nexport type LinksDef<Entities extends EntitiesDef> = Record<\n string,\n LinkDef<\n Entities,\n keyof Entities,\n string,\n CardinalityKind,\n keyof Entities,\n string,\n CardinalityKind\n >\n>;\n\nexport type LinkDef<\n Entities extends EntitiesDef,\n FwdEntity extends keyof Entities,\n FwdAttr extends string,\n FwdCardinality extends CardinalityKind,\n RevEntity extends keyof Entities,\n RevAttr extends string,\n RevCardinality extends CardinalityKind,\n> = {\n forward: {\n on: FwdEntity;\n label: FwdAttr;\n has: FwdCardinality;\n required?: RequirementKind;\n onDelete?: 'cascade';\n };\n reverse: {\n on: RevEntity;\n label: RevAttr;\n has: RevCardinality;\n onDelete?: 'cascade';\n };\n};\n\n// ==========\n// derived types\ntype IsEmptyOrIndexSignature<T> = keyof T extends never\n ? true\n : string extends keyof T\n ? true\n : false;\n\nexport type EntitiesWithLinks<\n Entities extends EntitiesDef,\n Links extends LinksDef<Entities>,\n> = {\n [EntityName in keyof Entities]: EntityDef<\n Entities[EntityName]['attrs'],\n EntityForwardLinksMap<EntityName, Entities, Links> &\n EntityReverseLinksMap<EntityName, Entities, Links>,\n Entities[EntityName] extends EntityDef<any, any, infer O>\n ? O extends void\n ? void\n : O\n : void\n >;\n};\n\ntype EntityForwardLinksMap<\n EntityName extends keyof Entities,\n Entities extends EntitiesDef,\n Links extends LinksDef<Entities>,\n LinkIndexFwd = LinksIndexedByEntity<Entities, Links, 'reverse'>,\n> =\n IsEmptyOrIndexSignature<Links> extends true\n ? {}\n : EntityName extends keyof LinkIndexFwd\n ? {\n [LinkName in keyof LinkIndexFwd[EntityName]]: LinkIndexFwd[EntityName][LinkName] extends LinkDef<\n Entities,\n infer RelatedEntityName,\n any,\n any,\n any,\n any,\n infer Cardinality\n >\n ? {\n entityName: RelatedEntityName;\n cardinality: Cardinality;\n }\n : never;\n }\n : {};\n\ntype EntityReverseLinksMap<\n EntityName extends keyof Entities,\n Entities extends EntitiesDef,\n Links extends LinksDef<Entities>,\n RevLinkIndex = LinksIndexedByEntity<Entities, Links, 'forward'>,\n> =\n IsEmptyOrIndexSignature<Links> extends true\n ? {}\n : EntityName extends keyof RevLinkIndex\n ? {\n [LinkName in keyof RevLinkIndex[EntityName]]: RevLinkIndex[EntityName][LinkName] extends LinkDef<\n Entities,\n any,\n any,\n infer Cardinality,\n infer RelatedEntityName,\n any,\n any\n >\n ? {\n entityName: RelatedEntityName;\n cardinality: Cardinality;\n }\n : never;\n }\n : {};\n\ntype LinksIndexedByEntity<\n Entities extends EntitiesDef,\n Links extends LinksDef<Entities>,\n Direction extends 'forward' | 'reverse',\n> = {\n [FwdEntity in keyof Entities]: {\n [LinkName in keyof Links as Links[LinkName][Direction]['on'] extends FwdEntity\n ? Links[LinkName][Direction]['label']\n : never]: Links[LinkName] extends LinkDef<\n Entities,\n infer FwdEntity,\n infer FwdAttr,\n infer FwdCardinality,\n infer RevEntity,\n infer RevAttr,\n infer RevCardinality\n >\n ? LinkDef<\n Entities,\n FwdEntity,\n FwdAttr,\n FwdCardinality,\n RevEntity,\n RevAttr,\n RevCardinality\n >\n : never;\n };\n};\n\ntype RequiredKeys<Attrs extends AttrsDefs> = {\n [K in keyof Attrs]: Attrs[K] extends DataAttrDef<any, infer R, any>\n ? R extends true\n ? K\n : never\n : never;\n}[keyof Attrs];\n\ntype OptionalKeys<Attrs extends AttrsDefs> = {\n [K in keyof Attrs]: Attrs[K] extends DataAttrDef<any, infer R, any>\n ? R extends false\n ? K\n : never\n : never;\n}[keyof Attrs];\n\n/**\n * MappedAttrs:\n * - Required keys => `key: ValueType`\n * - Optional keys => `key?: ValueType`\n */\ntype MappedAttrs<Attrs extends AttrsDefs, UseDates extends boolean> = {\n [K in RequiredKeys<Attrs>]: Attrs[K] extends DataAttrDef<infer V, any, any>\n ? V extends Date\n ? UseDates extends true\n ? V\n : string | number\n : V\n : never;\n} & {\n [K in OptionalKeys<Attrs>]?: Attrs[K] extends DataAttrDef<infer V, any, any>\n ? V extends Date\n ? UseDates extends true\n ? V\n : string | number\n : V\n : never;\n};\n\nexport type ResolveEntityAttrs<\n EDef extends EntityDef<any, any, any>,\n UseDates extends boolean = false,\n ResolvedAttrs = MappedAttrs<EDef['attrs'], UseDates>,\n> =\n EDef extends EntityDef<any, any, infer AsType>\n ? AsType extends void\n ? ResolvedAttrs\n : Omit<ResolvedAttrs, keyof AsType> & AsType\n : ResolvedAttrs;\n\nexport type ResolveAttrs<\n Entities extends EntitiesDef,\n EntityName extends keyof Entities,\n UseDates extends boolean,\n> = ResolveEntityAttrs<Entities[EntityName], UseDates>;\n\nexport type RoomsFromDef<RDef extends RoomsDef> = {\n [RoomName in keyof RDef]: {\n presence: Expand<ResolveEntityAttrs<RDef[RoomName]['presence']>>;\n topics: {\n [TopicName in keyof RDef[RoomName]['topics']]: Expand<\n ResolveEntityAttrs<NonNullable<RDef[RoomName]['topics']>[TopicName]>\n >;\n };\n };\n};\n\nexport type RoomsOf<S> =\n S extends InstantSchemaDef<any, any, infer RDef> ? RoomsFromDef<RDef> : never;\n\nexport type PresenceOf<\n S,\n RoomType extends keyof RoomsOf<S>,\n> = RoomsOf<S>[RoomType] extends { presence: infer P } ? P : {};\n\nexport type TopicsOf<\n S,\n RoomType extends keyof RoomsOf<S>,\n> = RoomsOf<S>[RoomType] extends { topics: infer T } ? T : {};\n\nexport type TopicOf<\n S,\n RoomType extends keyof RoomsOf<S>,\n TopicType extends keyof TopicsOf<S, RoomType>,\n> = TopicsOf<S, RoomType>[TopicType];\n\ninterface RoomDef {\n presence: EntityDef<any, any, any>;\n topics?: {\n [TopicName: string]: EntityDef<any, any, any>;\n };\n}\n\nexport interface RoomsDef {\n [RoomType: string]: RoomDef;\n}\n\nexport class InstantSchemaDef<\n Entities extends EntitiesDef,\n Links extends LinksDef<Entities>,\n Rooms extends RoomsDef,\n> implements IContainEntitiesAndLinks<Entities, Links>\n{\n constructor(\n public entities: Entities,\n public links: Links,\n public rooms: Rooms,\n ) {}\n\n /**\n * @deprecated\n * `withRoomSchema` is deprecated. Define your schema in `rooms` directly:\n *\n * @example\n * // Before:\n * const schema = i.schema({\n * // ...\n * }).withRoomSchema<RoomSchema>()\n *\n * // After\n * const schema = i.schema({\n * rooms: {\n * // ...\n * }\n * })\n *\n * @see https://instantdb.com/docs/presence-and-topics#typesafety\n */\n withRoomSchema<_RoomSchema extends RoomSchemaShape>() {\n type RDef = RoomDefFromShape<_RoomSchema>;\n return new InstantSchemaDef<Entities, Links, RDef>(\n this.entities,\n this.links,\n {} as RDef,\n );\n }\n}\n\n/**\n * @deprecated\n * `i.graph` is deprecated. Use `i.schema` instead.\n *\n * @see https://instantdb.com/docs/modeling-data\n */\nexport class InstantGraph<\n Entities extends EntitiesDef,\n Links extends LinksDef<Entities>,\n RoomSchema extends RoomSchemaShape = {},\n> implements IContainEntitiesAndLinks<Entities, Links>\n{\n constructor(\n public entities: Entities,\n public links: Links,\n ) {}\n\n withRoomSchema<_RoomSchema extends RoomSchemaShape>() {\n return new InstantGraph<Entities, Links, _RoomSchema>(\n this.entities,\n this.links,\n );\n }\n}\n\ntype EntityDefFromRoomSlice<Shape extends { [k: string]: any }> = EntityDef<\n {\n [AttrName in keyof Shape]: DataAttrDef<\n Shape[AttrName],\n Shape[AttrName] extends undefined ? false : true,\n any\n >;\n },\n any,\n void\n>;\n\ntype RoomDefFromShape<RoomSchema extends RoomSchemaShape> = {\n [RoomName in keyof RoomSchema]: {\n presence: EntityDefFromRoomSlice<\n NonNullable<RoomSchema[RoomName]['presence']>\n >;\n topics: {\n [TopicName in keyof RoomSchema[RoomName]['topics']]: EntityDefFromRoomSlice<\n NonNullable<RoomSchema[RoomName]['topics'][TopicName]>\n >;\n };\n };\n};\n\ntype EntityDefFromShape<Shape, K extends keyof Shape> = EntityDef<\n {\n [AttrName in keyof Shape[K]]: DataAttrDef<\n Shape[K][AttrName],\n Shape[K][AttrName] extends undefined ? false : true,\n any\n >;\n },\n {\n [LinkName in keyof Shape]: LinkAttrDef<\n 'many',\n LinkName extends string ? LinkName : string\n >;\n },\n void\n>;\n\n/**\n * If you were using the old `schema` types, you can use this to help you\n * migrate.\n *\n * @example\n * // Before\n * const db = init<Schema, Rooms>({...})\n *\n * // After\n * const db = init<BackwardsCompatibleSchema<Schema, Rooms>>({...})\n */\nexport type BackwardsCompatibleSchema<\n Shape extends { [k: string]: any },\n RoomSchema extends RoomSchemaShape = {},\n> = InstantSchemaDef<\n { [K in keyof Shape]: EntityDefFromShape<Shape, K> },\n UnknownLinks<EntitiesDef>,\n RoomDefFromShape<RoomSchema>\n>;\n\n// ----------\n// InstantUnknownSchema\n\nexport type UnknownEntity = EntityDef<\n {\n id: DataAttrDef<string, true, true>;\n [AttrName: string]: DataAttrDef<any, boolean, boolean>;\n },\n { [LinkName: string]: LinkAttrDef<'many', string> },\n void\n>;\n\nexport type UnknownEntities = {\n [EntityName: string]: UnknownEntity;\n};\n\nexport interface UnknownLinks<Entities extends EntitiesDef> {\n [LinkName: string]: LinkDef<\n Entities,\n string,\n string,\n 'many',\n string,\n string,\n 'many'\n >;\n}\n\nexport interface UnknownRooms {\n [RoomName: string]: {\n presence: EntityDef<any, any, any>;\n topics: {\n [TopicName: string]: EntityDef<any, any, any>;\n };\n };\n}\n\nexport class InstantUnknownSchemaDef extends InstantSchemaDef<\n UnknownEntities,\n UnknownLinks<UnknownEntities>,\n UnknownRooms\n> {}\n\nexport type InstantUnknownSchema = InstantUnknownSchemaDef;\n\nexport type CreateParams<\n Schema extends IContainEntitiesAndLinks<any, any>,\n EntityName extends keyof Schema['entities'],\n> = {\n [AttrName in RequiredKeys<\n Schema['entities'][EntityName]['attrs']\n >]: Schema['entities'][EntityName]['attrs'][AttrName] extends DataAttrDef<\n infer ValueType,\n any,\n any\n >\n ? ValueType extends Date\n ? string | number | Date\n : ValueType\n : never;\n} & {\n [AttrName in OptionalKeys<\n Schema['entities'][EntityName]['attrs']\n >]?: Schema['entities'][EntityName]['attrs'][AttrName] extends DataAttrDef<\n infer ValueType,\n false,\n any\n >\n ? (ValueType extends Date ? string | number | Date : ValueType) | null\n : never;\n};\n\nexport type UpdateParams<\n Schema extends IContainEntitiesAndLinks<any, any>,\n EntityName extends keyof Schema['entities'],\n> = {\n [AttrName in keyof Schema['entities'][EntityName]['attrs']]?: Schema['entities'][EntityName]['attrs'][AttrName] extends DataAttrDef<\n infer ValueType,\n infer IsRequired,\n any\n >\n ? IsRequired extends true\n ? ValueType extends Date\n ? string | number | Date\n : ValueType\n : (ValueType extends Date ? string | number | Date : ValueType) | null\n : never;\n};\n\nexport type UpdateOpts = {\n upsert?: boolean | undefined;\n};\n\nexport type LinkParams<\n Schema extends IContainEntitiesAndLinks<any, any>,\n EntityName extends keyof Schema['entities'],\n> = {\n [LinkName in keyof Schema['entities'][EntityName]['links']]?: Schema['entities'][EntityName]['links'][LinkName] extends LinkAttrDef<\n infer Cardinality,\n any\n >\n ? Cardinality extends 'one'\n ? string\n : string | string[]\n : never;\n};\n\nexport type RuleParams = {\n [key: string]: any;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"schemaTypes.js","sourceRoot":"","sources":["../../src/schemaTypes.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,WAAW;IAOtB,YACS,SAAqB,EACrB,QAAoB,EACpB,SAA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,KAAK,CAAC,CAAC;IACtE,CAAC;CACF;AAmQD,MAAM,OAAO,gBAAgB;IAM3B,YACS,QAAkB,EAClB,KAAY,EACZ,KAAY;QAFZ,aAAQ,GAAR,QAAQ,CAAU;QAClB,UAAK,GAAL,KAAK,CAAO;QACZ,UAAK,GAAL,KAAK,CAAO;IAClB,CAAC;IAEJ;;;;;;;;;;;;;;;;;;OAkBG;IACH,cAAc;QAEZ,OAAO,IAAI,gBAAgB,CACzB,IAAI,CAAC,QAAQ,EACb,IAAI,CAAC,KAAK,EACV,EAAU,CACX,CAAC;IACJ,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,OAAO,YAAY;IAMvB,YACS,QAAkB,EAClB,KAAY;QADZ,aAAQ,GAAR,QAAQ,CAAU;QAClB,UAAK,GAAL,KAAK,CAAO;IAClB,CAAC;IAEJ,cAAc;QACZ,OAAO,IAAI,YAAY,CACrB,IAAI,CAAC,QAAQ,EACb,IAAI,CAAC,KAAK,CACX,CAAC;IACJ,CAAC;CACF;AAqGD,MAAM,OAAO,uBAAwB,SAAQ,gBAI5C;CAAG","sourcesContent":["import type { RoomSchemaShape } from './presence.ts';\nimport { Expand } from './queryTypes.ts';\n\nexport class DataAttrDef<\n ValueType,\n IsRequired extends RequirementKind,\n IsIndexed extends boolean,\n> {\n public metadata: Record<string, unknown> = {};\n\n constructor(\n public valueType: ValueTypes,\n public required: IsRequired,\n public isIndexed: IsIndexed,\n\n public config: {\n indexed: boolean;\n unique: boolean;\n // clientValidator?: (value: ValueType) => boolean;\n } = { indexed: false, unique: false },\n ) {}\n\n /**\n * @deprecated Only use this temporarily for attributes that you want\n * to treat as required in frontend code but can’t yet mark as required\n * and enforced for backend\n */\n clientRequired() {\n return new DataAttrDef<ValueType, true, IsIndexed>(\n this.valueType,\n false as unknown as true,\n this.isIndexed,\n this.config,\n );\n }\n\n optional() {\n return new DataAttrDef<ValueType, false, IsIndexed>(\n this.valueType,\n false,\n this.isIndexed,\n this.config,\n );\n }\n\n unique() {\n return new DataAttrDef<ValueType, IsRequired, IsIndexed>(\n this.valueType,\n this.required,\n this.isIndexed,\n {\n ...this.config,\n unique: true,\n },\n );\n }\n\n indexed() {\n return new DataAttrDef<ValueType, IsRequired, true>(\n this.valueType,\n this.required,\n true,\n {\n ...this.config,\n indexed: true,\n },\n );\n }\n\n // clientValidate(clientValidator: (value: ValueType) => boolean) {\n // return new DataAttrDef(this.valueType, this.required, {\n // ...this.config,\n // clientValidator,\n // });\n // }\n}\n\nexport class LinkAttrDef<\n Cardinality extends CardinalityKind,\n EntityName extends string,\n> {\n constructor(\n public cardinality: Cardinality,\n public entityName: EntityName,\n ) {}\n}\n\nexport interface IContainEntitiesAndLinks<\n Entities extends EntitiesDef,\n Links extends LinksDef<Entities>,\n> {\n entities: Entities;\n links: Links;\n}\n\n// ==========\n// base types\n\nexport type ValueTypes = 'string' | 'number' | 'boolean' | 'date' | 'json';\n\nexport type CardinalityKind = 'one' | 'many';\n\n// true - force required\n// false - optional, not required\nexport type RequirementKind = true | false;\n\nexport type AttrsDefs = Record<string, DataAttrDef<any, any, any>>;\n\nexport class EntityDef<\n Attrs extends AttrsDefs,\n Links extends Record<string, LinkAttrDef<any, any>>,\n AsType,\n> {\n constructor(\n public attrs: Attrs,\n public links: Links,\n ) {}\n\n asType<_AsType extends Partial<MappedAttrs<Attrs, boolean>>>() {\n return new EntityDef<Attrs, Links, _AsType>(this.attrs, this.links);\n }\n}\n\nexport type EntityDefFromSchema<\n S extends IContainEntitiesAndLinks<any, any>,\n K extends keyof S['entities'],\n> = {\n [k in keyof S['entities'][K]['attrs']]: S['entities'][K] extends EntityDef<\n any,\n any,\n any\n >\n ? S['entities'][K]['attrs'][k]\n : never;\n};\n\nexport type EntitiesDef = Record<string, EntityDef<any, any, any>>;\n\nexport type LinksDef<Entities extends EntitiesDef> = Record<\n string,\n LinkDef<\n Entities,\n keyof Entities,\n string,\n CardinalityKind,\n keyof Entities,\n string,\n CardinalityKind\n >\n>;\n\nexport type LinkDef<\n Entities extends EntitiesDef,\n FwdEntity extends keyof Entities,\n FwdAttr extends string,\n FwdCardinality extends CardinalityKind,\n RevEntity extends keyof Entities,\n RevAttr extends string,\n RevCardinality extends CardinalityKind,\n> = {\n forward: {\n on: FwdEntity;\n label: FwdAttr;\n has: FwdCardinality;\n required?: RequirementKind;\n onDelete?: 'cascade';\n };\n reverse: {\n on: RevEntity;\n label: RevAttr;\n has: RevCardinality;\n onDelete?: 'cascade';\n };\n};\n\n// ==========\n// derived types\ntype IsEmptyOrIndexSignature<T> = keyof T extends never\n ? true\n : string extends keyof T\n ? true\n : false;\n\nexport type EntitiesWithLinks<\n Entities extends EntitiesDef,\n Links extends LinksDef<Entities>,\n> = {\n [EntityName in keyof Entities]: EntityDef<\n Entities[EntityName]['attrs'],\n EntityForwardLinksMap<EntityName, Entities, Links> &\n EntityReverseLinksMap<EntityName, Entities, Links>,\n Entities[EntityName] extends EntityDef<any, any, infer O>\n ? O extends void\n ? void\n : O\n : void\n >;\n};\n\ntype EntityForwardLinksMap<\n EntityName extends keyof Entities,\n Entities extends EntitiesDef,\n Links extends LinksDef<Entities>,\n LinkIndexFwd = LinksIndexedByEntity<Entities, Links, 'reverse'>,\n> =\n IsEmptyOrIndexSignature<Links> extends true\n ? {}\n : EntityName extends keyof LinkIndexFwd\n ? {\n [LinkName in keyof LinkIndexFwd[EntityName]]: LinkIndexFwd[EntityName][LinkName] extends LinkDef<\n Entities,\n infer RelatedEntityName,\n any,\n any,\n any,\n any,\n infer Cardinality\n >\n ? {\n entityName: RelatedEntityName;\n cardinality: Cardinality;\n }\n : never;\n }\n : {};\n\ntype EntityReverseLinksMap<\n EntityName extends keyof Entities,\n Entities extends EntitiesDef,\n Links extends LinksDef<Entities>,\n RevLinkIndex = LinksIndexedByEntity<Entities, Links, 'forward'>,\n> =\n IsEmptyOrIndexSignature<Links> extends true\n ? {}\n : EntityName extends keyof RevLinkIndex\n ? {\n [LinkName in keyof RevLinkIndex[EntityName]]: RevLinkIndex[EntityName][LinkName] extends LinkDef<\n Entities,\n any,\n any,\n infer Cardinality,\n infer RelatedEntityName,\n any,\n any\n >\n ? {\n entityName: RelatedEntityName;\n cardinality: Cardinality;\n }\n : never;\n }\n : {};\n\ntype LinksIndexedByEntity<\n Entities extends EntitiesDef,\n Links extends LinksDef<Entities>,\n Direction extends 'forward' | 'reverse',\n> = {\n [FwdEntity in keyof Entities]: {\n [LinkName in keyof Links as Links[LinkName][Direction]['on'] extends FwdEntity\n ? Links[LinkName][Direction]['label']\n : never]: Links[LinkName] extends LinkDef<\n Entities,\n infer FwdEntity,\n infer FwdAttr,\n infer FwdCardinality,\n infer RevEntity,\n infer RevAttr,\n infer RevCardinality\n >\n ? LinkDef<\n Entities,\n FwdEntity,\n FwdAttr,\n FwdCardinality,\n RevEntity,\n RevAttr,\n RevCardinality\n >\n : never;\n };\n};\n\ntype RequiredKeys<Attrs extends AttrsDefs> = {\n [K in keyof Attrs]: Attrs[K] extends DataAttrDef<any, infer R, any>\n ? R extends true\n ? K\n : never\n : never;\n}[keyof Attrs];\n\ntype OptionalKeys<Attrs extends AttrsDefs> = {\n [K in keyof Attrs]: Attrs[K] extends DataAttrDef<any, infer R, any>\n ? R extends false\n ? K\n : never\n : never;\n}[keyof Attrs];\n\n/**\n * MappedAttrs:\n * - Required keys => `key: ValueType`\n * - Optional keys => `key?: ValueType`\n */\ntype MappedAttrs<Attrs extends AttrsDefs, UseDates extends boolean> = {\n [K in RequiredKeys<Attrs>]: Attrs[K] extends DataAttrDef<infer V, any, any>\n ? V extends Date\n ? UseDates extends true\n ? V\n : string | number\n : V\n : never;\n} & {\n [K in OptionalKeys<Attrs>]?: Attrs[K] extends DataAttrDef<infer V, any, any>\n ? V extends Date\n ? UseDates extends true\n ? V\n : string | number\n : V\n : never;\n};\n\nexport type ResolveEntityAttrs<\n EDef extends EntityDef<any, any, any>,\n UseDates extends boolean = false,\n ResolvedAttrs = MappedAttrs<EDef['attrs'], UseDates>,\n> =\n EDef extends EntityDef<any, any, infer AsType>\n ? AsType extends void\n ? ResolvedAttrs\n : Omit<ResolvedAttrs, keyof AsType> & AsType\n : ResolvedAttrs;\n\nexport type ResolveAttrs<\n Entities extends EntitiesDef,\n EntityName extends keyof Entities,\n UseDates extends boolean,\n> = ResolveEntityAttrs<Entities[EntityName], UseDates>;\n\nexport type RoomsFromDef<RDef extends RoomsDef> = {\n [RoomName in keyof RDef]: {\n presence: Expand<ResolveEntityAttrs<RDef[RoomName]['presence']>>;\n topics: {\n [TopicName in keyof RDef[RoomName]['topics']]: Expand<\n ResolveEntityAttrs<NonNullable<RDef[RoomName]['topics']>[TopicName]>\n >;\n };\n };\n};\n\nexport type RoomsOf<S> =\n S extends InstantSchemaDef<any, any, infer RDef> ? RoomsFromDef<RDef> : never;\n\nexport type PresenceOf<\n S,\n RoomType extends keyof RoomsOf<S>,\n> = RoomsOf<S>[RoomType] extends { presence: infer P } ? P : {};\n\nexport type TopicsOf<\n S,\n RoomType extends keyof RoomsOf<S>,\n> = RoomsOf<S>[RoomType] extends { topics: infer T } ? T : {};\n\nexport type TopicOf<\n S,\n RoomType extends keyof RoomsOf<S>,\n TopicType extends keyof TopicsOf<S, RoomType>,\n> = TopicsOf<S, RoomType>[TopicType];\n\ninterface RoomDef {\n presence: EntityDef<any, any, any>;\n topics?: {\n [TopicName: string]: EntityDef<any, any, any>;\n };\n}\n\nexport interface RoomsDef {\n [RoomType: string]: RoomDef;\n}\n\nexport class InstantSchemaDef<\n Entities extends EntitiesDef,\n Links extends LinksDef<Entities>,\n Rooms extends RoomsDef,\n> implements IContainEntitiesAndLinks<Entities, Links>\n{\n constructor(\n public entities: Entities,\n public links: Links,\n public rooms: Rooms,\n ) {}\n\n /**\n * @deprecated\n * `withRoomSchema` is deprecated. Define your schema in `rooms` directly:\n *\n * @example\n * // Before:\n * const schema = i.schema({\n * // ...\n * }).withRoomSchema<RoomSchema>()\n *\n * // After\n * const schema = i.schema({\n * rooms: {\n * // ...\n * }\n * })\n *\n * @see https://instantdb.com/docs/presence-and-topics#typesafety\n */\n withRoomSchema<_RoomSchema extends RoomSchemaShape>() {\n type RDef = RoomDefFromShape<_RoomSchema>;\n return new InstantSchemaDef<Entities, Links, RDef>(\n this.entities,\n this.links,\n {} as RDef,\n );\n }\n}\n\n/**\n * @deprecated\n * `i.graph` is deprecated. Use `i.schema` instead.\n *\n * @see https://instantdb.com/docs/modeling-data\n */\nexport class InstantGraph<\n Entities extends EntitiesDef,\n Links extends LinksDef<Entities>,\n RoomSchema extends RoomSchemaShape = {},\n> implements IContainEntitiesAndLinks<Entities, Links>\n{\n constructor(\n public entities: Entities,\n public links: Links,\n ) {}\n\n withRoomSchema<_RoomSchema extends RoomSchemaShape>() {\n return new InstantGraph<Entities, Links, _RoomSchema>(\n this.entities,\n this.links,\n );\n }\n}\n\ntype EntityDefFromRoomSlice<Shape extends { [k: string]: any }> = EntityDef<\n {\n [AttrName in keyof Shape]: DataAttrDef<\n Shape[AttrName],\n Shape[AttrName] extends undefined ? false : true,\n any\n >;\n },\n any,\n void\n>;\n\ntype RoomDefFromShape<RoomSchema extends RoomSchemaShape> = {\n [RoomName in keyof RoomSchema]: {\n presence: EntityDefFromRoomSlice<\n NonNullable<RoomSchema[RoomName]['presence']>\n >;\n topics: {\n [TopicName in keyof RoomSchema[RoomName]['topics']]: EntityDefFromRoomSlice<\n NonNullable<RoomSchema[RoomName]['topics'][TopicName]>\n >;\n };\n };\n};\n\ntype EntityDefFromShape<Shape, K extends keyof Shape> = EntityDef<\n {\n [AttrName in keyof Shape[K]]: DataAttrDef<\n Shape[K][AttrName],\n Shape[K][AttrName] extends undefined ? false : true,\n any\n >;\n },\n {\n [LinkName in keyof Shape]: LinkAttrDef<\n 'many',\n LinkName extends string ? LinkName : string\n >;\n },\n void\n>;\n\n/**\n * If you were using the old `schema` types, you can use this to help you\n * migrate.\n *\n * @example\n * // Before\n * const db = init<Schema, Rooms>({...})\n *\n * // After\n * const db = init<BackwardsCompatibleSchema<Schema, Rooms>>({...})\n */\nexport type BackwardsCompatibleSchema<\n Shape extends { [k: string]: any },\n RoomSchema extends RoomSchemaShape = {},\n> = InstantSchemaDef<\n { [K in keyof Shape]: EntityDefFromShape<Shape, K> },\n UnknownLinks<EntitiesDef>,\n RoomDefFromShape<RoomSchema>\n>;\n\n// ----------\n// InstantUnknownSchema\n\nexport type UnknownEntity = EntityDef<\n {\n id: DataAttrDef<string, true, true>;\n [AttrName: string]: DataAttrDef<any, boolean, boolean>;\n },\n { [LinkName: string]: LinkAttrDef<'many', string> },\n void\n>;\n\nexport type UnknownEntities = {\n [EntityName: string]: UnknownEntity;\n};\n\nexport interface UnknownLinks<Entities extends EntitiesDef> {\n [LinkName: string]: LinkDef<\n Entities,\n string,\n string,\n 'many',\n string,\n string,\n 'many'\n >;\n}\n\nexport interface UnknownRooms {\n [RoomName: string]: {\n presence: EntityDef<any, any, any>;\n topics: {\n [TopicName: string]: EntityDef<any, any, any>;\n };\n };\n}\n\nexport class InstantUnknownSchemaDef extends InstantSchemaDef<\n UnknownEntities,\n UnknownLinks<UnknownEntities>,\n UnknownRooms\n> {}\n\nexport type InstantUnknownSchema = InstantUnknownSchemaDef;\n\nexport type CreateParams<\n Schema extends IContainEntitiesAndLinks<any, any>,\n EntityName extends keyof Schema['entities'],\n> = {\n [AttrName in RequiredKeys<\n Schema['entities'][EntityName]['attrs']\n >]: Schema['entities'][EntityName]['attrs'][AttrName] extends DataAttrDef<\n infer ValueType,\n any,\n any\n >\n ? ValueType extends Date\n ? string | number | Date\n : ValueType\n : never;\n} & {\n [AttrName in OptionalKeys<\n Schema['entities'][EntityName]['attrs']\n >]?: Schema['entities'][EntityName]['attrs'][AttrName] extends DataAttrDef<\n infer ValueType,\n false,\n any\n >\n ? (ValueType extends Date ? string | number | Date : ValueType) | null\n : never;\n};\n\nexport type UpdateParams<\n Schema extends IContainEntitiesAndLinks<any, any>,\n EntityName extends keyof Schema['entities'],\n> = {\n [AttrName in keyof Schema['entities'][EntityName]['attrs']]?: Schema['entities'][EntityName]['attrs'][AttrName] extends DataAttrDef<\n infer ValueType,\n infer IsRequired,\n any\n >\n ? IsRequired extends true\n ? ValueType extends Date\n ? string | number | Date\n : ValueType\n : (ValueType extends Date ? string | number | Date : ValueType) | null\n : never;\n};\n\nexport type UpdateOpts = {\n upsert?: boolean | undefined;\n};\n\nexport type LinkParams<\n Schema extends IContainEntitiesAndLinks<any, any>,\n EntityName extends keyof Schema['entities'],\n> = {\n [LinkName in keyof Schema['entities'][EntityName]['links']]?: Schema['entities'][EntityName]['links'][LinkName] extends LinkAttrDef<\n infer Cardinality,\n any\n >\n ? Cardinality extends 'one'\n ? string\n : string | string[]\n : never;\n};\n\nexport type RuleParams = {\n [key: string]: any;\n};\n"]}
|
package/dist/esm/store.js
CHANGED
|
@@ -2,13 +2,11 @@ import { create } from 'mutative';
|
|
|
2
2
|
import { immutableDeepMerge } from './utils/object.js';
|
|
3
3
|
import { coerceToDate } from "./utils/dates.js";
|
|
4
4
|
export class AttrsStoreClass {
|
|
5
|
-
attrs;
|
|
6
|
-
linkIndex;
|
|
7
|
-
_blobAttrs = null;
|
|
8
|
-
_primaryKeys = null;
|
|
9
|
-
_forwardIdents = null;
|
|
10
|
-
_revIdents = null;
|
|
11
5
|
constructor(attrs, linkIndex) {
|
|
6
|
+
this._blobAttrs = null;
|
|
7
|
+
this._primaryKeys = null;
|
|
8
|
+
this._forwardIdents = null;
|
|
9
|
+
this._revIdents = null;
|
|
12
10
|
this.attrs = attrs;
|
|
13
11
|
this.linkIndex = linkIndex;
|
|
14
12
|
}
|
|
@@ -30,7 +28,7 @@ export class AttrsStoreClass {
|
|
|
30
28
|
const attr = this.attrs[partialAttr.id];
|
|
31
29
|
if (!attr)
|
|
32
30
|
return;
|
|
33
|
-
this.attrs[partialAttr.id] = {
|
|
31
|
+
this.attrs[partialAttr.id] = Object.assign(Object.assign({}, attr), partialAttr);
|
|
34
32
|
this.resetAttrIndexes();
|
|
35
33
|
}
|
|
36
34
|
getAttr(id) {
|
|
@@ -221,6 +219,7 @@ export function createStore(attrsStore, triples, enableCardinalityInference, use
|
|
|
221
219
|
// then we drop the triple and have to wait for the server response
|
|
222
220
|
// to see the optimistic updates.
|
|
223
221
|
function resolveLookupRefs(store, triple) {
|
|
222
|
+
var _a, _b;
|
|
224
223
|
let eid;
|
|
225
224
|
// Check if `e` is a lookup ref
|
|
226
225
|
if (Array.isArray(triple[0])) {
|
|
@@ -233,7 +232,7 @@ function resolveLookupRefs(store, triple) {
|
|
|
233
232
|
}
|
|
234
233
|
// This would be a lot more efficient with a ave index
|
|
235
234
|
const triples = allMapValues(eMaps, 2);
|
|
236
|
-
eid = triples.find((x) => x[2] === v)
|
|
235
|
+
eid = (_a = triples.find((x) => x[2] === v)) === null || _a === void 0 ? void 0 : _a[0];
|
|
237
236
|
}
|
|
238
237
|
else {
|
|
239
238
|
eid = triple[0];
|
|
@@ -256,7 +255,7 @@ function resolveLookupRefs(store, triple) {
|
|
|
256
255
|
return null;
|
|
257
256
|
}
|
|
258
257
|
const triples = allMapValues(eMaps, 2);
|
|
259
|
-
const value = triples.find((x) => x[2] === v)
|
|
258
|
+
const value = (_b = triples.find((x) => x[2] === v)) === null || _b === void 0 ? void 0 : _b[0];
|
|
260
259
|
if (!value) {
|
|
261
260
|
return null;
|
|
262
261
|
}
|
|
@@ -316,6 +315,7 @@ function getCreatedAt(store, attr, triple) {
|
|
|
316
315
|
return createdAt || Date.now() * 10 + _seed++;
|
|
317
316
|
}
|
|
318
317
|
export function addTriple(store, attrsStore, rawTriple) {
|
|
318
|
+
var _a;
|
|
319
319
|
const triple = resolveLookupRefs(store, rawTriple);
|
|
320
320
|
if (!triple) {
|
|
321
321
|
return;
|
|
@@ -336,7 +336,7 @@ export function addTriple(store, attrsStore, rawTriple) {
|
|
|
336
336
|
// Reuse the created_at for a triple if it's already in the store.
|
|
337
337
|
// Prevents updates from temporarily pushing an entity to the top
|
|
338
338
|
// while waiting for the server response.
|
|
339
|
-
const t = existingTriple
|
|
339
|
+
const t = (_a = existingTriple === null || existingTriple === void 0 ? void 0 : existingTriple[3]) !== null && _a !== void 0 ? _a : getCreatedAt(store, attr, triple);
|
|
340
340
|
const enhancedTriple = [eid, aid, v, t];
|
|
341
341
|
if (hasEA(attr)) {
|
|
342
342
|
setInMap(store.eav, [eid, aid], new Map([[v, enhancedTriple]]));
|
|
@@ -351,6 +351,7 @@ export function addTriple(store, attrsStore, rawTriple) {
|
|
|
351
351
|
}
|
|
352
352
|
}
|
|
353
353
|
function mergeTriple(store, attrsStore, rawTriple) {
|
|
354
|
+
var _a;
|
|
354
355
|
const triple = resolveLookupRefs(store, rawTriple);
|
|
355
356
|
if (!triple) {
|
|
356
357
|
return;
|
|
@@ -364,7 +365,7 @@ function mergeTriple(store, attrsStore, rawTriple) {
|
|
|
364
365
|
const eavValuesMap = getInMap(store.eav, [eid, aid]);
|
|
365
366
|
if (!eavValuesMap)
|
|
366
367
|
return;
|
|
367
|
-
const currentTriple = eavValuesMap.values().next()
|
|
368
|
+
const currentTriple = (_a = eavValuesMap.values().next()) === null || _a === void 0 ? void 0 : _a.value;
|
|
368
369
|
if (!currentTriple)
|
|
369
370
|
return;
|
|
370
371
|
const currentValue = currentTriple[2];
|
|
@@ -379,6 +380,7 @@ function mergeTriple(store, attrsStore, rawTriple) {
|
|
|
379
380
|
setInMap(store.aev, [aid, eid], new Map([[updatedValue, enhancedTriple]]));
|
|
380
381
|
}
|
|
381
382
|
function deleteEntity(store, attrsStore, args) {
|
|
383
|
+
var _a, _b;
|
|
382
384
|
const [lookup, etype] = args;
|
|
383
385
|
const triple = resolveLookupRefs(store, [lookup]);
|
|
384
386
|
if (!triple) {
|
|
@@ -392,7 +394,7 @@ function deleteEntity(store, attrsStore, args) {
|
|
|
392
394
|
const attr = attrsStore.getAttr(a);
|
|
393
395
|
// delete cascade refs
|
|
394
396
|
if (attr && attr['on-delete-reverse'] === 'cascade') {
|
|
395
|
-
allMapValues(eMap.get(a), 1).forEach(([e, a, v]) => deleteEntity(store, attrsStore, [v, attr['reverse-identity']
|
|
397
|
+
allMapValues(eMap.get(a), 1).forEach(([e, a, v]) => { var _a; return deleteEntity(store, attrsStore, [v, (_a = attr['reverse-identity']) === null || _a === void 0 ? void 0 : _a[1]]); });
|
|
396
398
|
}
|
|
397
399
|
if (
|
|
398
400
|
// Fall back to deleting everything if we've rehydrated tx-steps from
|
|
@@ -401,7 +403,7 @@ function deleteEntity(store, attrsStore, args) {
|
|
|
401
403
|
// If we don't know about the attr, let's just get rid of it
|
|
402
404
|
!attr ||
|
|
403
405
|
// Make sure it matches the etype
|
|
404
|
-
attr['forward-identity']
|
|
406
|
+
((_a = attr['forward-identity']) === null || _a === void 0 ? void 0 : _a[1]) === etype) {
|
|
405
407
|
deleteInMap(store.aev, [a, id]);
|
|
406
408
|
deleteInMap(store.eav, [id, a]);
|
|
407
409
|
}
|
|
@@ -415,22 +417,23 @@ function deleteEntity(store, attrsStore, args) {
|
|
|
415
417
|
const vaeTriples = store.vae.get(id) && allMapValues(store.vae.get(id), 2);
|
|
416
418
|
if (vaeTriples) {
|
|
417
419
|
vaeTriples.forEach((triple) => {
|
|
420
|
+
var _a, _b, _c;
|
|
418
421
|
const [e, a, v] = triple;
|
|
419
422
|
const attr = attrsStore.getAttr(a);
|
|
420
|
-
if (!etype || !attr || attr['reverse-identity']
|
|
423
|
+
if (!etype || !attr || ((_a = attr['reverse-identity']) === null || _a === void 0 ? void 0 : _a[1]) === etype) {
|
|
421
424
|
deleteInMap(store.eav, [e, a, v]);
|
|
422
425
|
deleteInMap(store.aev, [a, e, v]);
|
|
423
426
|
deleteInMap(store.vae, [v, a, e]);
|
|
424
427
|
}
|
|
425
428
|
if (attr &&
|
|
426
429
|
attr['on-delete'] === 'cascade' &&
|
|
427
|
-
attr['reverse-identity']
|
|
428
|
-
deleteEntity(store, attrsStore, [e, attr['forward-identity']
|
|
430
|
+
((_b = attr['reverse-identity']) === null || _b === void 0 ? void 0 : _b[1]) === etype) {
|
|
431
|
+
deleteEntity(store, attrsStore, [e, (_c = attr['forward-identity']) === null || _c === void 0 ? void 0 : _c[1]]);
|
|
429
432
|
}
|
|
430
433
|
});
|
|
431
434
|
}
|
|
432
435
|
// Clear out vae index for `id` if we deleted all the reverse attributes
|
|
433
|
-
if (store.vae.get(id)
|
|
436
|
+
if (((_b = store.vae.get(id)) === null || _b === void 0 ? void 0 : _b.size) === 0) {
|
|
434
437
|
deleteInMap(store.vae, [id]);
|
|
435
438
|
}
|
|
436
439
|
}
|
|
@@ -518,8 +521,9 @@ export function allMapValues(m, level, res = []) {
|
|
|
518
521
|
return res;
|
|
519
522
|
}
|
|
520
523
|
function triplesByValue(store, m, v) {
|
|
524
|
+
var _a;
|
|
521
525
|
const res = [];
|
|
522
|
-
if (v
|
|
526
|
+
if (v === null || v === void 0 ? void 0 : v.hasOwnProperty('$not')) {
|
|
523
527
|
for (const candidate of m.keys()) {
|
|
524
528
|
if (v.$not !== candidate) {
|
|
525
529
|
res.push(m.get(candidate));
|
|
@@ -527,7 +531,7 @@ function triplesByValue(store, m, v) {
|
|
|
527
531
|
}
|
|
528
532
|
return res;
|
|
529
533
|
}
|
|
530
|
-
if (v
|
|
534
|
+
if (v === null || v === void 0 ? void 0 : v.hasOwnProperty('$isNull')) {
|
|
531
535
|
const { attrId, isNull, reverse } = v.$isNull;
|
|
532
536
|
if (reverse) {
|
|
533
537
|
for (const candidate of m.keys()) {
|
|
@@ -541,7 +545,7 @@ function triplesByValue(store, m, v) {
|
|
|
541
545
|
else {
|
|
542
546
|
const aMap = store.aev.get(attrId);
|
|
543
547
|
for (const candidate of m.keys()) {
|
|
544
|
-
const isValNull = !aMap || aMap.get(candidate)
|
|
548
|
+
const isValNull = !aMap || ((_a = aMap.get(candidate)) === null || _a === void 0 ? void 0 : _a.get(null)) || !aMap.get(candidate);
|
|
545
549
|
if (isNull ? isValNull : !isValNull) {
|
|
546
550
|
res.push(m.get(candidate));
|
|
547
551
|
}
|
|
@@ -549,7 +553,7 @@ function triplesByValue(store, m, v) {
|
|
|
549
553
|
}
|
|
550
554
|
return res;
|
|
551
555
|
}
|
|
552
|
-
if (v
|
|
556
|
+
if (v === null || v === void 0 ? void 0 : v.$comparator) {
|
|
553
557
|
// TODO: A sorted index would be nice here
|
|
554
558
|
return allMapValues(m, 1).filter(v.$op);
|
|
555
559
|
}
|
|
@@ -578,6 +582,7 @@ function whichIdx(e, a, v) {
|
|
|
578
582
|
return res;
|
|
579
583
|
}
|
|
580
584
|
export function getTriples(store, [e, a, v]) {
|
|
585
|
+
var _a, _b;
|
|
581
586
|
const idx = whichIdx(e, a, v);
|
|
582
587
|
switch (idx) {
|
|
583
588
|
case 'e': {
|
|
@@ -585,11 +590,11 @@ export function getTriples(store, [e, a, v]) {
|
|
|
585
590
|
return allMapValues(eMap, 2);
|
|
586
591
|
}
|
|
587
592
|
case 'ea': {
|
|
588
|
-
const aMap = store.eav.get(e)
|
|
593
|
+
const aMap = (_a = store.eav.get(e)) === null || _a === void 0 ? void 0 : _a.get(a);
|
|
589
594
|
return allMapValues(aMap, 1);
|
|
590
595
|
}
|
|
591
596
|
case 'eav': {
|
|
592
|
-
const aMap = store.eav.get(e)
|
|
597
|
+
const aMap = (_b = store.eav.get(e)) === null || _b === void 0 ? void 0 : _b.get(a);
|
|
593
598
|
if (!aMap) {
|
|
594
599
|
return [];
|
|
595
600
|
}
|
|
@@ -636,12 +641,13 @@ export function getTriples(store, [e, a, v]) {
|
|
|
636
641
|
}
|
|
637
642
|
}
|
|
638
643
|
export function getAsObject(store, attrs, e) {
|
|
644
|
+
var _a;
|
|
639
645
|
const obj = {};
|
|
640
646
|
if (!attrs) {
|
|
641
647
|
return obj;
|
|
642
648
|
}
|
|
643
649
|
for (const [label, attr] of attrs.entries()) {
|
|
644
|
-
const aMap = store.eav.get(e)
|
|
650
|
+
const aMap = (_a = store.eav.get(e)) === null || _a === void 0 ? void 0 : _a.get(attr.id);
|
|
645
651
|
const triples = allMapValues(aMap, 1);
|
|
646
652
|
for (const triple of triples) {
|
|
647
653
|
obj[label] = triple[2];
|
|
@@ -650,20 +656,23 @@ export function getAsObject(store, attrs, e) {
|
|
|
650
656
|
return obj;
|
|
651
657
|
}
|
|
652
658
|
export function getAttrByFwdIdentName(attrsStore, inputEtype, inputLabel) {
|
|
653
|
-
|
|
659
|
+
var _a;
|
|
660
|
+
return (_a = attrsStore.forwardIdents.get(inputEtype)) === null || _a === void 0 ? void 0 : _a.get(inputLabel);
|
|
654
661
|
}
|
|
655
662
|
export function getAttrByReverseIdentName(attrsStore, inputEtype, inputLabel) {
|
|
656
|
-
|
|
663
|
+
var _a;
|
|
664
|
+
return (_a = attrsStore.revIdents.get(inputEtype)) === null || _a === void 0 ? void 0 : _a.get(inputLabel);
|
|
657
665
|
}
|
|
658
666
|
export function getBlobAttrs(attrsStore, etype) {
|
|
659
667
|
return attrsStore.blobAttrs.get(etype);
|
|
660
668
|
}
|
|
661
669
|
export function getPrimaryKeyAttr(attrsStore, etype) {
|
|
670
|
+
var _a;
|
|
662
671
|
const fromPrimary = attrsStore.primaryKeys.get(etype);
|
|
663
672
|
if (fromPrimary) {
|
|
664
673
|
return fromPrimary;
|
|
665
674
|
}
|
|
666
|
-
return attrsStore.forwardIdents.get(etype)
|
|
675
|
+
return (_a = attrsStore.forwardIdents.get(etype)) === null || _a === void 0 ? void 0 : _a.get('id');
|
|
667
676
|
}
|
|
668
677
|
function findTriple(store, attrsStore, rawTriple) {
|
|
669
678
|
const triple = resolveLookupRefs(store, rawTriple);
|
|
@@ -686,7 +695,7 @@ export function transact(store, attrsStore, txSteps) {
|
|
|
686
695
|
if (action !== 'add-triple' && action !== 'deep-merge-triple') {
|
|
687
696
|
return true;
|
|
688
697
|
}
|
|
689
|
-
const mode = opts
|
|
698
|
+
const mode = opts === null || opts === void 0 ? void 0 : opts.mode;
|
|
690
699
|
if (mode !== 'create' && mode !== 'update') {
|
|
691
700
|
return true;
|
|
692
701
|
}
|
|
@@ -696,7 +705,7 @@ export function transact(store, attrsStore, txSteps) {
|
|
|
696
705
|
const idAttr = getPrimaryKeyAttr(attrsStore, attr['forward-identity'][1]);
|
|
697
706
|
exists = !!findTriple(store, attrsStore, [
|
|
698
707
|
eid,
|
|
699
|
-
idAttr
|
|
708
|
+
idAttr === null || idAttr === void 0 ? void 0 : idAttr.id,
|
|
700
709
|
eid,
|
|
701
710
|
]);
|
|
702
711
|
}
|