@instantdb/platform 0.22.95-experimental.surgical.20386947966.1 → 0.22.95
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/.turbo/turbo-build.log +9 -9
- package/dist/commonjs/index.d.ts +0 -1
- package/dist/commonjs/index.d.ts.map +1 -1
- package/dist/commonjs/index.js +1 -9
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/schema.d.ts.map +1 -1
- package/dist/commonjs/schema.js +14 -12
- package/dist/commonjs/schema.js.map +1 -1
- package/dist/esm/index.d.ts +0 -1
- package/dist/esm/index.d.ts.map +1 -1
- package/dist/esm/index.js +0 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/schema.d.ts.map +1 -1
- package/dist/esm/schema.js +15 -13
- package/dist/esm/schema.js.map +1 -1
- package/dist/standalone/index.js +1182 -1271
- package/dist/standalone/index.umd.cjs +25 -32
- package/package.json +3 -3
- package/src/index.ts +0 -10
- package/src/schema.ts +24 -16
- package/dist/commonjs/schemaCodegen.d.ts +0 -37
- package/dist/commonjs/schemaCodegen.d.ts.map +0 -1
- package/dist/commonjs/schemaCodegen.js +0 -112
- package/dist/commonjs/schemaCodegen.js.map +0 -1
- package/dist/esm/schemaCodegen.d.ts +0 -37
- package/dist/esm/schemaCodegen.d.ts.map +0 -1
- package/dist/esm/schemaCodegen.js +0 -103
- package/dist/esm/schemaCodegen.js.map +0 -1
- package/src/schemaCodegen.ts +0 -183
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@instantdb/platform",
|
|
3
|
-
"version": "0.22.95
|
|
3
|
+
"version": "0.22.95",
|
|
4
4
|
"description": "Instant's platform package for managing Instant apps.",
|
|
5
5
|
"homepage": "https://github.com/instantdb/instant/tree/main/client/packages/platform",
|
|
6
6
|
"repository": {
|
|
@@ -54,8 +54,8 @@
|
|
|
54
54
|
"dependencies": {
|
|
55
55
|
"@babel/parser": "^8.0.0-beta.0",
|
|
56
56
|
"@babel/types": "^8.0.0-beta.0",
|
|
57
|
-
"@instantdb/core": "0.22.95
|
|
58
|
-
"@instantdb/version": "0.22.95
|
|
57
|
+
"@instantdb/core": "0.22.95",
|
|
58
|
+
"@instantdb/version": "0.22.95"
|
|
59
59
|
},
|
|
60
60
|
"scripts": {
|
|
61
61
|
"test": "vitest",
|
package/src/index.ts
CHANGED
|
@@ -56,13 +56,3 @@ export {
|
|
|
56
56
|
type MigrationTxTypes,
|
|
57
57
|
type Identifier,
|
|
58
58
|
} from './migrations.ts';
|
|
59
|
-
|
|
60
|
-
export {
|
|
61
|
-
formatKey,
|
|
62
|
-
renderAttrCall,
|
|
63
|
-
renderAttrProperty,
|
|
64
|
-
renderEntityProperty,
|
|
65
|
-
renderLinkValue,
|
|
66
|
-
renderLinkProperty,
|
|
67
|
-
renderLinksObject,
|
|
68
|
-
} from './schemaCodegen.ts';
|
package/src/schema.ts
CHANGED
|
@@ -15,13 +15,9 @@ import {
|
|
|
15
15
|
indentLines,
|
|
16
16
|
joinWithTrailingSep,
|
|
17
17
|
sortedEntries,
|
|
18
|
+
formatKey,
|
|
18
19
|
GenericSchemaDef,
|
|
19
20
|
} from './util.ts';
|
|
20
|
-
import {
|
|
21
|
-
formatKey,
|
|
22
|
-
renderEntityProperty,
|
|
23
|
-
renderLinksObject,
|
|
24
|
-
} from './schemaCodegen.ts';
|
|
25
21
|
|
|
26
22
|
export type InstantAPIPlatformSchema = {
|
|
27
23
|
refs: Record<string, InstantDBAttr>;
|
|
@@ -262,6 +258,18 @@ export type InstantAPISchemaPushStep =
|
|
|
262
258
|
| InstantAPISchemaPushCheckDataTypeStep
|
|
263
259
|
| InstantAPISchemaPushRemoveDataTypeStep;
|
|
264
260
|
|
|
261
|
+
function attrDefToCodeString([name, attr]: [
|
|
262
|
+
string,
|
|
263
|
+
DataAttrDef<string, boolean, boolean>,
|
|
264
|
+
]) {
|
|
265
|
+
const type =
|
|
266
|
+
(attr.metadata.derivedType as any)?.type || attr.valueType || 'any';
|
|
267
|
+
const unique = attr.config.unique ? '.unique()' : '';
|
|
268
|
+
const index = attr.config.indexed ? '.indexed()' : '';
|
|
269
|
+
const required = attr.required ? '' : '.optional()';
|
|
270
|
+
return `${formatKey(name)}: i.${type}()${unique}${index}${required}`;
|
|
271
|
+
}
|
|
272
|
+
|
|
265
273
|
function entityDefToCodeStr(
|
|
266
274
|
name: string,
|
|
267
275
|
edef: EntityDef<
|
|
@@ -270,10 +278,14 @@ function entityDefToCodeStr(
|
|
|
270
278
|
any
|
|
271
279
|
>,
|
|
272
280
|
) {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
281
|
+
const attrBlock = joinWithTrailingSep(
|
|
282
|
+
sortedEntries(edef.attrs).map(attrDefToCodeString),
|
|
283
|
+
',\n',
|
|
284
|
+
',',
|
|
285
|
+
);
|
|
286
|
+
|
|
287
|
+
// a block of code for each entity
|
|
288
|
+
return `${formatKey(name)}: i.entity({${attrBlock.length ? '\n' : ''}${indentLines(attrBlock, 2)}${attrBlock.length ? '\n' : ''}})`;
|
|
277
289
|
}
|
|
278
290
|
|
|
279
291
|
export function identEtype(ident: InstantDBIdent) {
|
|
@@ -326,7 +338,7 @@ function roomDefToCodeStr(room: RoomsDef[string]) {
|
|
|
326
338
|
}
|
|
327
339
|
|
|
328
340
|
if (room.topics) {
|
|
329
|
-
ret += `\n
|
|
341
|
+
ret += `\n "topics": {`;
|
|
330
342
|
|
|
331
343
|
for (const [topicName, topicConfig] of Object.entries(room.topics)) {
|
|
332
344
|
ret += `\n${indentLines(entityDefToCodeStr(topicName, topicConfig), 6)},`;
|
|
@@ -343,7 +355,7 @@ function roomsCodeStr(rooms: RoomsDef) {
|
|
|
343
355
|
let ret = '{';
|
|
344
356
|
|
|
345
357
|
for (const [roomType, roomDef] of Object.entries(rooms)) {
|
|
346
|
-
ret += `\n ${formatKey(roomType
|
|
358
|
+
ret += `\n ${formatKey(roomType)}: ${roomDefToCodeStr(roomDef)},`;
|
|
347
359
|
}
|
|
348
360
|
ret += ret === '{' ? '}' : '\n}';
|
|
349
361
|
|
|
@@ -383,11 +395,7 @@ export function generateSchemaTypescriptFile(
|
|
|
383
395
|
// run \`push schema\` again to enforce the types.`
|
|
384
396
|
: '';
|
|
385
397
|
|
|
386
|
-
const linksEntriesCode =
|
|
387
|
-
keyOptions: { alwaysQuote: true, quote: '"' },
|
|
388
|
-
stringQuote: '"',
|
|
389
|
-
style: 'json',
|
|
390
|
-
});
|
|
398
|
+
const linksEntriesCode = JSON.stringify(newSchema.links, null, 2).trim();
|
|
391
399
|
|
|
392
400
|
// rooms
|
|
393
401
|
const rooms = prevSchema?.rooms || {};
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import type { AttrsDefs, DataAttrDef, LinkDef, LinksDef } from '@instantdb/core';
|
|
2
|
-
type AnyLink = LinkDef<any, any, any, any, any, any, any>;
|
|
3
|
-
type KeyFormatOptions = {
|
|
4
|
-
alwaysQuote?: boolean;
|
|
5
|
-
quote?: '"' | "'";
|
|
6
|
-
};
|
|
7
|
-
type EntityRenderOptions = {
|
|
8
|
-
indent?: number;
|
|
9
|
-
typeParamsByAttr?: Record<string, string | null | undefined>;
|
|
10
|
-
keyOptions?: KeyFormatOptions;
|
|
11
|
-
trailingComma?: boolean;
|
|
12
|
-
};
|
|
13
|
-
type LinkRenderOptions = {
|
|
14
|
-
indent?: number;
|
|
15
|
-
keyOptions?: KeyFormatOptions;
|
|
16
|
-
stringQuote?: '"' | "'";
|
|
17
|
-
style?: 'js' | 'json';
|
|
18
|
-
};
|
|
19
|
-
type LinksObjectOptions = {
|
|
20
|
-
indent?: number;
|
|
21
|
-
newlineForEmpty?: boolean;
|
|
22
|
-
keyOptions?: KeyFormatOptions;
|
|
23
|
-
stringQuote?: '"' | "'";
|
|
24
|
-
style?: 'js' | 'json';
|
|
25
|
-
};
|
|
26
|
-
export declare function formatKey(name: string, options?: KeyFormatOptions): string;
|
|
27
|
-
export declare function renderAttrCall(attr: DataAttrDef<string, boolean, boolean>, typeParams?: string | null): string;
|
|
28
|
-
export declare function renderAttrProperty(name: string, attr: DataAttrDef<string, boolean, boolean>, options?: {
|
|
29
|
-
typeParams?: string | null;
|
|
30
|
-
keyOptions?: KeyFormatOptions;
|
|
31
|
-
}): string;
|
|
32
|
-
export declare function renderEntityProperty(name: string, attrs: AttrsDefs, options?: EntityRenderOptions): string;
|
|
33
|
-
export declare function renderLinkValue(link: AnyLink, options?: LinkRenderOptions): string;
|
|
34
|
-
export declare function renderLinkProperty(name: string, link: AnyLink, options?: LinkRenderOptions): string;
|
|
35
|
-
export declare function renderLinksObject(links: LinksDef<any>, options?: LinksObjectOptions): string;
|
|
36
|
-
export {};
|
|
37
|
-
//# sourceMappingURL=schemaCodegen.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"schemaCodegen.d.ts","sourceRoot":"","sources":["../../src/schemaCodegen.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EACT,WAAW,EACX,OAAO,EACP,QAAQ,EACT,MAAM,iBAAiB,CAAC;AAKzB,KAAK,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;AAE1D,KAAK,gBAAgB,GAAG;IACtB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,KAAK,CAAC,EAAE,GAAG,GAAG,GAAG,CAAC;CACnB,CAAC;AAEF,KAAK,mBAAmB,GAAG;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;IAC7D,UAAU,CAAC,EAAE,gBAAgB,CAAC;IAC9B,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,KAAK,iBAAiB,GAAG;IAC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B,OAAO,GAAE,kBAAuB,UAgBjC"}
|
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.formatKey = formatKey;
|
|
4
|
-
exports.renderAttrCall = renderAttrCall;
|
|
5
|
-
exports.renderAttrProperty = renderAttrProperty;
|
|
6
|
-
exports.renderEntityProperty = renderEntityProperty;
|
|
7
|
-
exports.renderLinkValue = renderLinkValue;
|
|
8
|
-
exports.renderLinkProperty = renderLinkProperty;
|
|
9
|
-
exports.renderLinksObject = renderLinksObject;
|
|
10
|
-
const util_ts_1 = require("./util.js");
|
|
11
|
-
const DEFAULT_INDENT = 2;
|
|
12
|
-
function formatKey(name, options = {}) {
|
|
13
|
-
var _a;
|
|
14
|
-
const quote = (_a = options.quote) !== null && _a !== void 0 ? _a : "'";
|
|
15
|
-
if (!options.alwaysQuote && isValidIdentifier(name)) {
|
|
16
|
-
return name;
|
|
17
|
-
}
|
|
18
|
-
return `${quote}${name}${quote}`;
|
|
19
|
-
}
|
|
20
|
-
function renderAttrCall(attr, typeParams) {
|
|
21
|
-
var _a, _b;
|
|
22
|
-
const type = ((_b = (_a = attr.metadata) === null || _a === void 0 ? void 0 : _a.derivedType) === null || _b === void 0 ? void 0 : _b.type) || attr.valueType || 'any';
|
|
23
|
-
const unique = attr.config.unique ? '.unique()' : '';
|
|
24
|
-
const index = attr.config.indexed ? '.indexed()' : '';
|
|
25
|
-
const required = attr.required ? '' : '.optional()';
|
|
26
|
-
return `i.${type}${typeParams !== null && typeParams !== void 0 ? typeParams : ''}()${unique}${index}${required}`;
|
|
27
|
-
}
|
|
28
|
-
function renderAttrProperty(name, attr, options = {}) {
|
|
29
|
-
return `${formatKey(name, options.keyOptions)}: ${renderAttrCall(attr, options.typeParams)}`;
|
|
30
|
-
}
|
|
31
|
-
function renderEntityProperty(name, attrs, options = {}) {
|
|
32
|
-
var _a;
|
|
33
|
-
const indent = (_a = options.indent) !== null && _a !== void 0 ? _a : DEFAULT_INDENT;
|
|
34
|
-
const keyOptions = options.keyOptions;
|
|
35
|
-
const attrBlock = (0, util_ts_1.sortedEntries)(attrs)
|
|
36
|
-
.map(([attrName, attrDef]) => {
|
|
37
|
-
var _a, _b;
|
|
38
|
-
return renderAttrProperty(attrName, attrDef, {
|
|
39
|
-
typeParams: (_b = (_a = options.typeParamsByAttr) === null || _a === void 0 ? void 0 : _a[attrName]) !== null && _b !== void 0 ? _b : null,
|
|
40
|
-
keyOptions,
|
|
41
|
-
});
|
|
42
|
-
})
|
|
43
|
-
.filter(Boolean);
|
|
44
|
-
const attrBlockText = options.trailingComma
|
|
45
|
-
? (0, util_ts_1.joinWithTrailingSep)(attrBlock, ',\n', ',')
|
|
46
|
-
: attrBlock.join(',\n');
|
|
47
|
-
const inner = attrBlockText.length
|
|
48
|
-
? `\n${(0, util_ts_1.indentLines)(attrBlockText, indent)}\n`
|
|
49
|
-
: '';
|
|
50
|
-
return `${formatKey(name, keyOptions)}: i.entity({${inner}})`;
|
|
51
|
-
}
|
|
52
|
-
function renderLinkValue(link, options = {}) {
|
|
53
|
-
var _a, _b, _c;
|
|
54
|
-
const indent = (_a = options.indent) !== null && _a !== void 0 ? _a : DEFAULT_INDENT;
|
|
55
|
-
const indentStr = ' '.repeat(indent);
|
|
56
|
-
const keyOptions = options.keyOptions;
|
|
57
|
-
const q = (_b = options.stringQuote) !== null && _b !== void 0 ? _b : "'";
|
|
58
|
-
const style = (_c = options.style) !== null && _c !== void 0 ? _c : 'js';
|
|
59
|
-
const forwardLines = [
|
|
60
|
-
`${formatKey('on', keyOptions)}: ${q}${link.forward.on}${q}`,
|
|
61
|
-
`${formatKey('has', keyOptions)}: ${q}${link.forward.has}${q}`,
|
|
62
|
-
`${formatKey('label', keyOptions)}: ${q}${link.forward.label}${q}`,
|
|
63
|
-
];
|
|
64
|
-
if (link.forward.required) {
|
|
65
|
-
forwardLines.push(`${formatKey('required', keyOptions)}: true`);
|
|
66
|
-
}
|
|
67
|
-
if (link.forward.onDelete) {
|
|
68
|
-
forwardLines.push(`${formatKey('onDelete', keyOptions)}: ${q}${link.forward.onDelete}${q}`);
|
|
69
|
-
}
|
|
70
|
-
const reverseLines = [
|
|
71
|
-
`${formatKey('on', keyOptions)}: ${q}${link.reverse.on}${q}`,
|
|
72
|
-
`${formatKey('has', keyOptions)}: ${q}${link.reverse.has}${q}`,
|
|
73
|
-
`${formatKey('label', keyOptions)}: ${q}${link.reverse.label}${q}`,
|
|
74
|
-
];
|
|
75
|
-
if (link.reverse.onDelete) {
|
|
76
|
-
reverseLines.push(`${formatKey('onDelete', keyOptions)}: ${q}${link.reverse.onDelete}${q}`);
|
|
77
|
-
}
|
|
78
|
-
const forwardBodyLines = style === 'json' ? forwardLines : forwardLines.map((line) => `${line},`);
|
|
79
|
-
const reverseBodyLines = style === 'json' ? reverseLines : reverseLines.map((line) => `${line},`);
|
|
80
|
-
return [
|
|
81
|
-
'{',
|
|
82
|
-
`${indentStr}${formatKey('forward', keyOptions)}: {`,
|
|
83
|
-
`${indentStr}${indentStr}${forwardBodyLines.join(`${style === 'json' ? ',' : ''}\n${indentStr}${indentStr}`)}`,
|
|
84
|
-
`${indentStr}}${style === 'json' ? ',' : ','}`,
|
|
85
|
-
`${indentStr}${formatKey('reverse', keyOptions)}: {`,
|
|
86
|
-
`${indentStr}${indentStr}${reverseBodyLines.join(`${style === 'json' ? ',' : ''}\n${indentStr}${indentStr}`)}`,
|
|
87
|
-
`${indentStr}}${style === 'json' ? '' : ','}`,
|
|
88
|
-
'}',
|
|
89
|
-
].join('\n');
|
|
90
|
-
}
|
|
91
|
-
function renderLinkProperty(name, link, options = {}) {
|
|
92
|
-
return `${formatKey(name, options.keyOptions)}: ${renderLinkValue(link, options)}`;
|
|
93
|
-
}
|
|
94
|
-
function renderLinksObject(links, options = {}) {
|
|
95
|
-
var _a;
|
|
96
|
-
const indent = (_a = options.indent) !== null && _a !== void 0 ? _a : DEFAULT_INDENT;
|
|
97
|
-
const entries = (0, util_ts_1.sortedEntries)(links).map(([name, link]) => renderLinkProperty(name, link, {
|
|
98
|
-
indent,
|
|
99
|
-
keyOptions: options.keyOptions,
|
|
100
|
-
stringQuote: options.stringQuote,
|
|
101
|
-
style: options.style,
|
|
102
|
-
}));
|
|
103
|
-
if (!entries.length && !options.newlineForEmpty) {
|
|
104
|
-
return '{}';
|
|
105
|
-
}
|
|
106
|
-
const inner = entries.length ? (0, util_ts_1.indentLines)(entries.join(',\n'), indent) : '';
|
|
107
|
-
return `{\n${inner}\n}`;
|
|
108
|
-
}
|
|
109
|
-
function isValidIdentifier(name) {
|
|
110
|
-
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
|
|
111
|
-
}
|
|
112
|
-
//# sourceMappingURL=schemaCodegen.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"schemaCodegen.js","sourceRoot":"","sources":["../../src/schemaCodegen.ts"],"names":[],"mappings":";;AAuCA,8BAMC;AAED,wCAUC;AAED,gDASC;AAED,oDAsBC;AAED,0CAqDC;AAED,gDASC;AAED,8CAkBC;AA5KD,uCAA4E;AAE5E,MAAM,cAAc,GAAG,CAAC,CAAC;AA+BzB,SAAgB,SAAS,CAAC,IAAY,EAAE,UAA4B,EAAE;;IACpE,MAAM,KAAK,GAAG,MAAA,OAAO,CAAC,KAAK,mCAAI,GAAG,CAAC;IACnC,IAAI,CAAC,OAAO,CAAC,WAAW,IAAI,iBAAiB,CAAC,IAAI,CAAC,EAAE,CAAC;QACpD,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,GAAG,KAAK,GAAG,IAAI,GAAG,KAAK,EAAE,CAAC;AACnC,CAAC;AAED,SAAgB,cAAc,CAC5B,IAA2C,EAC3C,UAA0B;;IAE1B,MAAM,IAAI,GACR,CAAA,MAAC,MAAA,IAAI,CAAC,QAAQ,0CAAE,WAAmB,0CAAE,IAAI,KAAI,IAAI,CAAC,SAAS,IAAI,KAAK,CAAC;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;QACN,UAAU,EAAE,OAAO,CAAC,UAAU;QAC9B,WAAW,EAAE,OAAO,CAAC,WAAW;QAChC,KAAK,EAAE,OAAO,CAAC,KAAK;KACrB,CAAC,CACH,CAAC;IACF,IAAI,CAAC,OAAO,CAAC,MAAM,IAAI,CAAC,OAAO,CAAC,eAAe,EAAE,CAAC;QAChD,OAAO,IAAI,CAAC;IACd,CAAC;IACD,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,IAAA,qBAAW,EAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAC7E,OAAO,MAAM,KAAK,KAAK,CAAC;AAC1B,CAAC;AAED,SAAS,iBAAiB,CAAC,IAAY;IACrC,OAAO,4BAA4B,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACjD,CAAC","sourcesContent":["import type {\n AttrsDefs,\n DataAttrDef,\n LinkDef,\n LinksDef,\n} from '@instantdb/core';\nimport { indentLines, joinWithTrailingSep, sortedEntries } from './util.ts';\n\nconst DEFAULT_INDENT = 2;\n\ntype AnyLink = LinkDef<any, any, any, any, any, any, any>;\n\ntype KeyFormatOptions = {\n alwaysQuote?: boolean;\n quote?: '\"' | \"'\";\n};\n\ntype EntityRenderOptions = {\n indent?: number;\n typeParamsByAttr?: Record<string, string | null | undefined>;\n keyOptions?: KeyFormatOptions;\n trailingComma?: boolean;\n};\n\ntype LinkRenderOptions = {\n indent?: number;\n keyOptions?: KeyFormatOptions;\n stringQuote?: '\"' | \"'\";\n style?: 'js' | 'json';\n};\n\ntype LinksObjectOptions = {\n indent?: number;\n newlineForEmpty?: boolean;\n keyOptions?: KeyFormatOptions;\n stringQuote?: '\"' | \"'\";\n style?: 'js' | 'json';\n};\n\nexport function formatKey(name: string, options: KeyFormatOptions = {}) {\n const quote = options.quote ?? \"'\";\n if (!options.alwaysQuote && isValidIdentifier(name)) {\n return name;\n }\n return `${quote}${name}${quote}`;\n}\n\nexport function renderAttrCall(\n attr: DataAttrDef<string, boolean, boolean>,\n typeParams?: string | null,\n) {\n const type =\n (attr.metadata?.derivedType as any)?.type || attr.valueType || 'any';\n const unique = attr.config.unique ? '.unique()' : '';\n const index = attr.config.indexed ? '.indexed()' : '';\n const required = attr.required ? '' : '.optional()';\n return `i.${type}${typeParams ?? ''}()${unique}${index}${required}`;\n}\n\nexport function renderAttrProperty(\n name: string,\n attr: DataAttrDef<string, boolean, boolean>,\n options: { typeParams?: string | null; keyOptions?: KeyFormatOptions } = {},\n) {\n return `${formatKey(name, options.keyOptions)}: ${renderAttrCall(\n attr,\n options.typeParams,\n )}`;\n}\n\nexport function renderEntityProperty(\n name: string,\n attrs: AttrsDefs,\n options: EntityRenderOptions = {},\n) {\n const indent = options.indent ?? DEFAULT_INDENT;\n const keyOptions = options.keyOptions;\n const attrBlock = sortedEntries(attrs)\n .map(([attrName, attrDef]) =>\n renderAttrProperty(attrName, attrDef, {\n typeParams: options.typeParamsByAttr?.[attrName] ?? null,\n keyOptions,\n }),\n )\n .filter(Boolean);\n const attrBlockText = options.trailingComma\n ? joinWithTrailingSep(attrBlock, ',\\n', ',')\n : attrBlock.join(',\\n');\n const inner = attrBlockText.length\n ? `\\n${indentLines(attrBlockText, indent)}\\n`\n : '';\n return `${formatKey(name, keyOptions)}: i.entity({${inner}})`;\n}\n\nexport function renderLinkValue(\n link: AnyLink,\n options: LinkRenderOptions = {},\n) {\n const indent = options.indent ?? DEFAULT_INDENT;\n const indentStr = ' '.repeat(indent);\n const keyOptions = options.keyOptions;\n const q = options.stringQuote ?? \"'\";\n const style = options.style ?? 'js';\n const forwardLines = [\n `${formatKey('on', keyOptions)}: ${q}${link.forward.on}${q}`,\n `${formatKey('has', keyOptions)}: ${q}${link.forward.has}${q}`,\n `${formatKey('label', keyOptions)}: ${q}${link.forward.label}${q}`,\n ];\n if (link.forward.required) {\n forwardLines.push(`${formatKey('required', keyOptions)}: true`);\n }\n if (link.forward.onDelete) {\n forwardLines.push(\n `${formatKey('onDelete', keyOptions)}: ${q}${link.forward.onDelete}${q}`,\n );\n }\n\n const reverseLines = [\n `${formatKey('on', keyOptions)}: ${q}${link.reverse.on}${q}`,\n `${formatKey('has', keyOptions)}: ${q}${link.reverse.has}${q}`,\n `${formatKey('label', keyOptions)}: ${q}${link.reverse.label}${q}`,\n ];\n if (link.reverse.onDelete) {\n reverseLines.push(\n `${formatKey('onDelete', keyOptions)}: ${q}${link.reverse.onDelete}${q}`,\n );\n }\n\n const forwardBodyLines =\n style === 'json' ? forwardLines : forwardLines.map((line) => `${line},`);\n const reverseBodyLines =\n style === 'json' ? reverseLines : reverseLines.map((line) => `${line},`);\n\n return [\n '{',\n `${indentStr}${formatKey('forward', keyOptions)}: {`,\n `${indentStr}${indentStr}${forwardBodyLines.join(\n `${style === 'json' ? ',' : ''}\\n${indentStr}${indentStr}`,\n )}`,\n `${indentStr}}${style === 'json' ? ',' : ','}`,\n `${indentStr}${formatKey('reverse', keyOptions)}: {`,\n `${indentStr}${indentStr}${reverseBodyLines.join(\n `${style === 'json' ? ',' : ''}\\n${indentStr}${indentStr}`,\n )}`,\n `${indentStr}}${style === 'json' ? '' : ','}`,\n '}',\n ].join('\\n');\n}\n\nexport function renderLinkProperty(\n name: string,\n link: AnyLink,\n options: LinkRenderOptions = {},\n) {\n return `${formatKey(name, options.keyOptions)}: ${renderLinkValue(\n link,\n options,\n )}`;\n}\n\nexport function renderLinksObject(\n links: LinksDef<any>,\n options: LinksObjectOptions = {},\n) {\n const indent = options.indent ?? DEFAULT_INDENT;\n const entries = sortedEntries(links).map(([name, link]) =>\n renderLinkProperty(name, link, {\n indent,\n keyOptions: options.keyOptions,\n stringQuote: options.stringQuote,\n style: options.style,\n }),\n );\n if (!entries.length && !options.newlineForEmpty) {\n return '{}';\n }\n const inner = entries.length ? indentLines(entries.join(',\\n'), indent) : '';\n return `{\\n${inner}\\n}`;\n}\n\nfunction isValidIdentifier(name: string) {\n return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);\n}\n"]}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import type { AttrsDefs, DataAttrDef, LinkDef, LinksDef } from '@instantdb/core';
|
|
2
|
-
type AnyLink = LinkDef<any, any, any, any, any, any, any>;
|
|
3
|
-
type KeyFormatOptions = {
|
|
4
|
-
alwaysQuote?: boolean;
|
|
5
|
-
quote?: '"' | "'";
|
|
6
|
-
};
|
|
7
|
-
type EntityRenderOptions = {
|
|
8
|
-
indent?: number;
|
|
9
|
-
typeParamsByAttr?: Record<string, string | null | undefined>;
|
|
10
|
-
keyOptions?: KeyFormatOptions;
|
|
11
|
-
trailingComma?: boolean;
|
|
12
|
-
};
|
|
13
|
-
type LinkRenderOptions = {
|
|
14
|
-
indent?: number;
|
|
15
|
-
keyOptions?: KeyFormatOptions;
|
|
16
|
-
stringQuote?: '"' | "'";
|
|
17
|
-
style?: 'js' | 'json';
|
|
18
|
-
};
|
|
19
|
-
type LinksObjectOptions = {
|
|
20
|
-
indent?: number;
|
|
21
|
-
newlineForEmpty?: boolean;
|
|
22
|
-
keyOptions?: KeyFormatOptions;
|
|
23
|
-
stringQuote?: '"' | "'";
|
|
24
|
-
style?: 'js' | 'json';
|
|
25
|
-
};
|
|
26
|
-
export declare function formatKey(name: string, options?: KeyFormatOptions): string;
|
|
27
|
-
export declare function renderAttrCall(attr: DataAttrDef<string, boolean, boolean>, typeParams?: string | null): string;
|
|
28
|
-
export declare function renderAttrProperty(name: string, attr: DataAttrDef<string, boolean, boolean>, options?: {
|
|
29
|
-
typeParams?: string | null;
|
|
30
|
-
keyOptions?: KeyFormatOptions;
|
|
31
|
-
}): string;
|
|
32
|
-
export declare function renderEntityProperty(name: string, attrs: AttrsDefs, options?: EntityRenderOptions): string;
|
|
33
|
-
export declare function renderLinkValue(link: AnyLink, options?: LinkRenderOptions): string;
|
|
34
|
-
export declare function renderLinkProperty(name: string, link: AnyLink, options?: LinkRenderOptions): string;
|
|
35
|
-
export declare function renderLinksObject(links: LinksDef<any>, options?: LinksObjectOptions): string;
|
|
36
|
-
export {};
|
|
37
|
-
//# sourceMappingURL=schemaCodegen.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"schemaCodegen.d.ts","sourceRoot":"","sources":["../../src/schemaCodegen.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,SAAS,EACT,WAAW,EACX,OAAO,EACP,QAAQ,EACT,MAAM,iBAAiB,CAAC;AAKzB,KAAK,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC;AAE1D,KAAK,gBAAgB,GAAG;IACtB,WAAW,CAAC,EAAE,OAAO,CAAC;IACtB,KAAK,CAAC,EAAE,GAAG,GAAG,GAAG,CAAC;CACnB,CAAC;AAEF,KAAK,mBAAmB,GAAG;IACzB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,gBAAgB,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;IAC7D,UAAU,CAAC,EAAE,gBAAgB,CAAC;IAC9B,aAAa,CAAC,EAAE,OAAO,CAAC;CACzB,CAAC;AAEF,KAAK,iBAAiB,GAAG;IACvB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,gBAAgB,CAAC;IAC9B,WAAW,CAAC,EAAE,GAAG,GAAG,GAAG,CAAC;IACxB,KAAK,CAAC,EAAE,IAAI,GAAG,MAAM,CAAC;CACvB,CAAC;AAEF,KAAK,kBAAkB,GAAG;IACxB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,UAAU,CAAC,EAAE,gBAAgB,CAAC;IAC9B,WAAW,CAAC,EAAE,GAAG,GAAG,GAAG,CAAC;IACxB,KAAK,CAAC,EAAE,IAAI,GAAG,MAAM,CAAC;CACvB,CAAC;AAEF,wBAAgB,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,gBAAqB,UAMrE;AAED,wBAAgB,cAAc,CAC5B,IAAI,EAAE,WAAW,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,EAC3C,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,UAQ3B;AAED,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,WAAW,CAAC,MAAM,EAAE,OAAO,EAAE,OAAO,CAAC,EAC3C,OAAO,GAAE;IAAE,UAAU,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,UAAU,CAAC,EAAE,gBAAgB,CAAA;CAAO,UAM5E;AAED,wBAAgB,oBAAoB,CAClC,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,SAAS,EAChB,OAAO,GAAE,mBAAwB,UAmBlC;AAED,wBAAgB,eAAe,CAC7B,IAAI,EAAE,OAAO,EACb,OAAO,GAAE,iBAAsB,UAmDhC;AAED,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,OAAO,EACb,OAAO,GAAE,iBAAsB,UAMhC;AAED,wBAAgB,iBAAiB,CAC/B,KAAK,EAAE,QAAQ,CAAC,GAAG,CAAC,EACpB,OAAO,GAAE,kBAAuB,UAgBjC"}
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import { indentLines, joinWithTrailingSep, sortedEntries } from "./util.js";
|
|
2
|
-
const DEFAULT_INDENT = 2;
|
|
3
|
-
export function formatKey(name, options = {}) {
|
|
4
|
-
var _a;
|
|
5
|
-
const quote = (_a = options.quote) !== null && _a !== void 0 ? _a : "'";
|
|
6
|
-
if (!options.alwaysQuote && isValidIdentifier(name)) {
|
|
7
|
-
return name;
|
|
8
|
-
}
|
|
9
|
-
return `${quote}${name}${quote}`;
|
|
10
|
-
}
|
|
11
|
-
export function renderAttrCall(attr, typeParams) {
|
|
12
|
-
var _a, _b;
|
|
13
|
-
const type = ((_b = (_a = attr.metadata) === null || _a === void 0 ? void 0 : _a.derivedType) === null || _b === void 0 ? void 0 : _b.type) || attr.valueType || 'any';
|
|
14
|
-
const unique = attr.config.unique ? '.unique()' : '';
|
|
15
|
-
const index = attr.config.indexed ? '.indexed()' : '';
|
|
16
|
-
const required = attr.required ? '' : '.optional()';
|
|
17
|
-
return `i.${type}${typeParams !== null && typeParams !== void 0 ? typeParams : ''}()${unique}${index}${required}`;
|
|
18
|
-
}
|
|
19
|
-
export function renderAttrProperty(name, attr, options = {}) {
|
|
20
|
-
return `${formatKey(name, options.keyOptions)}: ${renderAttrCall(attr, options.typeParams)}`;
|
|
21
|
-
}
|
|
22
|
-
export function renderEntityProperty(name, attrs, options = {}) {
|
|
23
|
-
var _a;
|
|
24
|
-
const indent = (_a = options.indent) !== null && _a !== void 0 ? _a : DEFAULT_INDENT;
|
|
25
|
-
const keyOptions = options.keyOptions;
|
|
26
|
-
const attrBlock = sortedEntries(attrs)
|
|
27
|
-
.map(([attrName, attrDef]) => {
|
|
28
|
-
var _a, _b;
|
|
29
|
-
return renderAttrProperty(attrName, attrDef, {
|
|
30
|
-
typeParams: (_b = (_a = options.typeParamsByAttr) === null || _a === void 0 ? void 0 : _a[attrName]) !== null && _b !== void 0 ? _b : null,
|
|
31
|
-
keyOptions,
|
|
32
|
-
});
|
|
33
|
-
})
|
|
34
|
-
.filter(Boolean);
|
|
35
|
-
const attrBlockText = options.trailingComma
|
|
36
|
-
? joinWithTrailingSep(attrBlock, ',\n', ',')
|
|
37
|
-
: attrBlock.join(',\n');
|
|
38
|
-
const inner = attrBlockText.length
|
|
39
|
-
? `\n${indentLines(attrBlockText, indent)}\n`
|
|
40
|
-
: '';
|
|
41
|
-
return `${formatKey(name, keyOptions)}: i.entity({${inner}})`;
|
|
42
|
-
}
|
|
43
|
-
export function renderLinkValue(link, options = {}) {
|
|
44
|
-
var _a, _b, _c;
|
|
45
|
-
const indent = (_a = options.indent) !== null && _a !== void 0 ? _a : DEFAULT_INDENT;
|
|
46
|
-
const indentStr = ' '.repeat(indent);
|
|
47
|
-
const keyOptions = options.keyOptions;
|
|
48
|
-
const q = (_b = options.stringQuote) !== null && _b !== void 0 ? _b : "'";
|
|
49
|
-
const style = (_c = options.style) !== null && _c !== void 0 ? _c : 'js';
|
|
50
|
-
const forwardLines = [
|
|
51
|
-
`${formatKey('on', keyOptions)}: ${q}${link.forward.on}${q}`,
|
|
52
|
-
`${formatKey('has', keyOptions)}: ${q}${link.forward.has}${q}`,
|
|
53
|
-
`${formatKey('label', keyOptions)}: ${q}${link.forward.label}${q}`,
|
|
54
|
-
];
|
|
55
|
-
if (link.forward.required) {
|
|
56
|
-
forwardLines.push(`${formatKey('required', keyOptions)}: true`);
|
|
57
|
-
}
|
|
58
|
-
if (link.forward.onDelete) {
|
|
59
|
-
forwardLines.push(`${formatKey('onDelete', keyOptions)}: ${q}${link.forward.onDelete}${q}`);
|
|
60
|
-
}
|
|
61
|
-
const reverseLines = [
|
|
62
|
-
`${formatKey('on', keyOptions)}: ${q}${link.reverse.on}${q}`,
|
|
63
|
-
`${formatKey('has', keyOptions)}: ${q}${link.reverse.has}${q}`,
|
|
64
|
-
`${formatKey('label', keyOptions)}: ${q}${link.reverse.label}${q}`,
|
|
65
|
-
];
|
|
66
|
-
if (link.reverse.onDelete) {
|
|
67
|
-
reverseLines.push(`${formatKey('onDelete', keyOptions)}: ${q}${link.reverse.onDelete}${q}`);
|
|
68
|
-
}
|
|
69
|
-
const forwardBodyLines = style === 'json' ? forwardLines : forwardLines.map((line) => `${line},`);
|
|
70
|
-
const reverseBodyLines = style === 'json' ? reverseLines : reverseLines.map((line) => `${line},`);
|
|
71
|
-
return [
|
|
72
|
-
'{',
|
|
73
|
-
`${indentStr}${formatKey('forward', keyOptions)}: {`,
|
|
74
|
-
`${indentStr}${indentStr}${forwardBodyLines.join(`${style === 'json' ? ',' : ''}\n${indentStr}${indentStr}`)}`,
|
|
75
|
-
`${indentStr}}${style === 'json' ? ',' : ','}`,
|
|
76
|
-
`${indentStr}${formatKey('reverse', keyOptions)}: {`,
|
|
77
|
-
`${indentStr}${indentStr}${reverseBodyLines.join(`${style === 'json' ? ',' : ''}\n${indentStr}${indentStr}`)}`,
|
|
78
|
-
`${indentStr}}${style === 'json' ? '' : ','}`,
|
|
79
|
-
'}',
|
|
80
|
-
].join('\n');
|
|
81
|
-
}
|
|
82
|
-
export function renderLinkProperty(name, link, options = {}) {
|
|
83
|
-
return `${formatKey(name, options.keyOptions)}: ${renderLinkValue(link, options)}`;
|
|
84
|
-
}
|
|
85
|
-
export function renderLinksObject(links, options = {}) {
|
|
86
|
-
var _a;
|
|
87
|
-
const indent = (_a = options.indent) !== null && _a !== void 0 ? _a : DEFAULT_INDENT;
|
|
88
|
-
const entries = sortedEntries(links).map(([name, link]) => renderLinkProperty(name, link, {
|
|
89
|
-
indent,
|
|
90
|
-
keyOptions: options.keyOptions,
|
|
91
|
-
stringQuote: options.stringQuote,
|
|
92
|
-
style: options.style,
|
|
93
|
-
}));
|
|
94
|
-
if (!entries.length && !options.newlineForEmpty) {
|
|
95
|
-
return '{}';
|
|
96
|
-
}
|
|
97
|
-
const inner = entries.length ? indentLines(entries.join(',\n'), indent) : '';
|
|
98
|
-
return `{\n${inner}\n}`;
|
|
99
|
-
}
|
|
100
|
-
function isValidIdentifier(name) {
|
|
101
|
-
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
|
|
102
|
-
}
|
|
103
|
-
//# sourceMappingURL=schemaCodegen.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"schemaCodegen.js","sourceRoot":"","sources":["../../src/schemaCodegen.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,WAAW,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE5E,MAAM,cAAc,GAAG,CAAC,CAAC;AA+BzB,MAAM,UAAU,SAAS,CAAC,IAAY,E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Aa,EACb,UAA6B,EAAE;IAE/B,OAAO,GAAG,SAAS,CAAC,IAAI,EAAE,OAAO,CAAC,UAAU,CAAC,KAAK,eAAe,CAC/D,IAAI,EACJ,OAAO,CACR,EAAE,CAAC;AACN,CAAC;AAED,MAAM,UAAU,iBAAiB,CAC/B,KAAoB,EACpB,UAA8B,EAAE;;IAEhC,MAAM,MAAM,GAAG,MAAA,OAAO,CAAC,MAAM,mCAAI,cAAc,CAAC;IAChD,MAAM,OAAO,GAAG,aAAa,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,IAAI,CAAC,EAAE,EAAE,CACxD,kBAAkB,CAAC,IAAI,EAAE,IAAI,EAAE;QAC7B,MAAM;QACN,UAAU,EAAE,OAAO,CAAC,UAAU;QAC9B,WAAW,EAAE,OAAO,CAAC,WAAW;QAChC,KAAK,EAAE,OAAO,CAAC,KAAK;KACrB,CAAC,CACH,CAAC;IACF,IAAI,CAAC,OAAO,CAAC,MAAM,IAAI,CAAC,OAAO,CAAC,eAAe,EAAE,CAAC;QAChD,OAAO,IAAI,CAAC;IACd,CAAC;IACD,MAAM,KAAK,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAC7E,OAAO,MAAM,KAAK,KAAK,CAAC;AAC1B,CAAC;AAED,SAAS,iBAAiB,CAAC,IAAY;IACrC,OAAO,4BAA4B,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACjD,CAAC","sourcesContent":["import type {\n AttrsDefs,\n DataAttrDef,\n LinkDef,\n LinksDef,\n} from '@instantdb/core';\nimport { indentLines, joinWithTrailingSep, sortedEntries } from './util.ts';\n\nconst DEFAULT_INDENT = 2;\n\ntype AnyLink = LinkDef<any, any, any, any, any, any, any>;\n\ntype KeyFormatOptions = {\n alwaysQuote?: boolean;\n quote?: '\"' | \"'\";\n};\n\ntype EntityRenderOptions = {\n indent?: number;\n typeParamsByAttr?: Record<string, string | null | undefined>;\n keyOptions?: KeyFormatOptions;\n trailingComma?: boolean;\n};\n\ntype LinkRenderOptions = {\n indent?: number;\n keyOptions?: KeyFormatOptions;\n stringQuote?: '\"' | \"'\";\n style?: 'js' | 'json';\n};\n\ntype LinksObjectOptions = {\n indent?: number;\n newlineForEmpty?: boolean;\n keyOptions?: KeyFormatOptions;\n stringQuote?: '\"' | \"'\";\n style?: 'js' | 'json';\n};\n\nexport function formatKey(name: string, options: KeyFormatOptions = {}) {\n const quote = options.quote ?? \"'\";\n if (!options.alwaysQuote && isValidIdentifier(name)) {\n return name;\n }\n return `${quote}${name}${quote}`;\n}\n\nexport function renderAttrCall(\n attr: DataAttrDef<string, boolean, boolean>,\n typeParams?: string | null,\n) {\n const type =\n (attr.metadata?.derivedType as any)?.type || attr.valueType || 'any';\n const unique = attr.config.unique ? '.unique()' : '';\n const index = attr.config.indexed ? '.indexed()' : '';\n const required = attr.required ? '' : '.optional()';\n return `i.${type}${typeParams ?? ''}()${unique}${index}${required}`;\n}\n\nexport function renderAttrProperty(\n name: string,\n attr: DataAttrDef<string, boolean, boolean>,\n options: { typeParams?: string | null; keyOptions?: KeyFormatOptions } = {},\n) {\n return `${formatKey(name, options.keyOptions)}: ${renderAttrCall(\n attr,\n options.typeParams,\n )}`;\n}\n\nexport function renderEntityProperty(\n name: string,\n attrs: AttrsDefs,\n options: EntityRenderOptions = {},\n) {\n const indent = options.indent ?? DEFAULT_INDENT;\n const keyOptions = options.keyOptions;\n const attrBlock = sortedEntries(attrs)\n .map(([attrName, attrDef]) =>\n renderAttrProperty(attrName, attrDef, {\n typeParams: options.typeParamsByAttr?.[attrName] ?? null,\n keyOptions,\n }),\n )\n .filter(Boolean);\n const attrBlockText = options.trailingComma\n ? joinWithTrailingSep(attrBlock, ',\\n', ',')\n : attrBlock.join(',\\n');\n const inner = attrBlockText.length\n ? `\\n${indentLines(attrBlockText, indent)}\\n`\n : '';\n return `${formatKey(name, keyOptions)}: i.entity({${inner}})`;\n}\n\nexport function renderLinkValue(\n link: AnyLink,\n options: LinkRenderOptions = {},\n) {\n const indent = options.indent ?? DEFAULT_INDENT;\n const indentStr = ' '.repeat(indent);\n const keyOptions = options.keyOptions;\n const q = options.stringQuote ?? \"'\";\n const style = options.style ?? 'js';\n const forwardLines = [\n `${formatKey('on', keyOptions)}: ${q}${link.forward.on}${q}`,\n `${formatKey('has', keyOptions)}: ${q}${link.forward.has}${q}`,\n `${formatKey('label', keyOptions)}: ${q}${link.forward.label}${q}`,\n ];\n if (link.forward.required) {\n forwardLines.push(`${formatKey('required', keyOptions)}: true`);\n }\n if (link.forward.onDelete) {\n forwardLines.push(\n `${formatKey('onDelete', keyOptions)}: ${q}${link.forward.onDelete}${q}`,\n );\n }\n\n const reverseLines = [\n `${formatKey('on', keyOptions)}: ${q}${link.reverse.on}${q}`,\n `${formatKey('has', keyOptions)}: ${q}${link.reverse.has}${q}`,\n `${formatKey('label', keyOptions)}: ${q}${link.reverse.label}${q}`,\n ];\n if (link.reverse.onDelete) {\n reverseLines.push(\n `${formatKey('onDelete', keyOptions)}: ${q}${link.reverse.onDelete}${q}`,\n );\n }\n\n const forwardBodyLines =\n style === 'json' ? forwardLines : forwardLines.map((line) => `${line},`);\n const reverseBodyLines =\n style === 'json' ? reverseLines : reverseLines.map((line) => `${line},`);\n\n return [\n '{',\n `${indentStr}${formatKey('forward', keyOptions)}: {`,\n `${indentStr}${indentStr}${forwardBodyLines.join(\n `${style === 'json' ? ',' : ''}\\n${indentStr}${indentStr}`,\n )}`,\n `${indentStr}}${style === 'json' ? ',' : ','}`,\n `${indentStr}${formatKey('reverse', keyOptions)}: {`,\n `${indentStr}${indentStr}${reverseBodyLines.join(\n `${style === 'json' ? ',' : ''}\\n${indentStr}${indentStr}`,\n )}`,\n `${indentStr}}${style === 'json' ? '' : ','}`,\n '}',\n ].join('\\n');\n}\n\nexport function renderLinkProperty(\n name: string,\n link: AnyLink,\n options: LinkRenderOptions = {},\n) {\n return `${formatKey(name, options.keyOptions)}: ${renderLinkValue(\n link,\n options,\n )}`;\n}\n\nexport function renderLinksObject(\n links: LinksDef<any>,\n options: LinksObjectOptions = {},\n) {\n const indent = options.indent ?? DEFAULT_INDENT;\n const entries = sortedEntries(links).map(([name, link]) =>\n renderLinkProperty(name, link, {\n indent,\n keyOptions: options.keyOptions,\n stringQuote: options.stringQuote,\n style: options.style,\n }),\n );\n if (!entries.length && !options.newlineForEmpty) {\n return '{}';\n }\n const inner = entries.length ? indentLines(entries.join(',\\n'), indent) : '';\n return `{\\n${inner}\\n}`;\n}\n\nfunction isValidIdentifier(name: string) {\n return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);\n}\n"]}
|
package/src/schemaCodegen.ts
DELETED
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
AttrsDefs,
|
|
3
|
-
DataAttrDef,
|
|
4
|
-
LinkDef,
|
|
5
|
-
LinksDef,
|
|
6
|
-
} from '@instantdb/core';
|
|
7
|
-
import { indentLines, joinWithTrailingSep, sortedEntries } from './util.ts';
|
|
8
|
-
|
|
9
|
-
const DEFAULT_INDENT = 2;
|
|
10
|
-
|
|
11
|
-
type AnyLink = LinkDef<any, any, any, any, any, any, any>;
|
|
12
|
-
|
|
13
|
-
type KeyFormatOptions = {
|
|
14
|
-
alwaysQuote?: boolean;
|
|
15
|
-
quote?: '"' | "'";
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
type EntityRenderOptions = {
|
|
19
|
-
indent?: number;
|
|
20
|
-
typeParamsByAttr?: Record<string, string | null | undefined>;
|
|
21
|
-
keyOptions?: KeyFormatOptions;
|
|
22
|
-
trailingComma?: boolean;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
type LinkRenderOptions = {
|
|
26
|
-
indent?: number;
|
|
27
|
-
keyOptions?: KeyFormatOptions;
|
|
28
|
-
stringQuote?: '"' | "'";
|
|
29
|
-
style?: 'js' | 'json';
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
type LinksObjectOptions = {
|
|
33
|
-
indent?: number;
|
|
34
|
-
newlineForEmpty?: boolean;
|
|
35
|
-
keyOptions?: KeyFormatOptions;
|
|
36
|
-
stringQuote?: '"' | "'";
|
|
37
|
-
style?: 'js' | 'json';
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export function formatKey(name: string, options: KeyFormatOptions = {}) {
|
|
41
|
-
const quote = options.quote ?? "'";
|
|
42
|
-
if (!options.alwaysQuote && isValidIdentifier(name)) {
|
|
43
|
-
return name;
|
|
44
|
-
}
|
|
45
|
-
return `${quote}${name}${quote}`;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
export function renderAttrCall(
|
|
49
|
-
attr: DataAttrDef<string, boolean, boolean>,
|
|
50
|
-
typeParams?: string | null,
|
|
51
|
-
) {
|
|
52
|
-
const type =
|
|
53
|
-
(attr.metadata?.derivedType as any)?.type || attr.valueType || 'any';
|
|
54
|
-
const unique = attr.config.unique ? '.unique()' : '';
|
|
55
|
-
const index = attr.config.indexed ? '.indexed()' : '';
|
|
56
|
-
const required = attr.required ? '' : '.optional()';
|
|
57
|
-
return `i.${type}${typeParams ?? ''}()${unique}${index}${required}`;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function renderAttrProperty(
|
|
61
|
-
name: string,
|
|
62
|
-
attr: DataAttrDef<string, boolean, boolean>,
|
|
63
|
-
options: { typeParams?: string | null; keyOptions?: KeyFormatOptions } = {},
|
|
64
|
-
) {
|
|
65
|
-
return `${formatKey(name, options.keyOptions)}: ${renderAttrCall(
|
|
66
|
-
attr,
|
|
67
|
-
options.typeParams,
|
|
68
|
-
)}`;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function renderEntityProperty(
|
|
72
|
-
name: string,
|
|
73
|
-
attrs: AttrsDefs,
|
|
74
|
-
options: EntityRenderOptions = {},
|
|
75
|
-
) {
|
|
76
|
-
const indent = options.indent ?? DEFAULT_INDENT;
|
|
77
|
-
const keyOptions = options.keyOptions;
|
|
78
|
-
const attrBlock = sortedEntries(attrs)
|
|
79
|
-
.map(([attrName, attrDef]) =>
|
|
80
|
-
renderAttrProperty(attrName, attrDef, {
|
|
81
|
-
typeParams: options.typeParamsByAttr?.[attrName] ?? null,
|
|
82
|
-
keyOptions,
|
|
83
|
-
}),
|
|
84
|
-
)
|
|
85
|
-
.filter(Boolean);
|
|
86
|
-
const attrBlockText = options.trailingComma
|
|
87
|
-
? joinWithTrailingSep(attrBlock, ',\n', ',')
|
|
88
|
-
: attrBlock.join(',\n');
|
|
89
|
-
const inner = attrBlockText.length
|
|
90
|
-
? `\n${indentLines(attrBlockText, indent)}\n`
|
|
91
|
-
: '';
|
|
92
|
-
return `${formatKey(name, keyOptions)}: i.entity({${inner}})`;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export function renderLinkValue(
|
|
96
|
-
link: AnyLink,
|
|
97
|
-
options: LinkRenderOptions = {},
|
|
98
|
-
) {
|
|
99
|
-
const indent = options.indent ?? DEFAULT_INDENT;
|
|
100
|
-
const indentStr = ' '.repeat(indent);
|
|
101
|
-
const keyOptions = options.keyOptions;
|
|
102
|
-
const q = options.stringQuote ?? "'";
|
|
103
|
-
const style = options.style ?? 'js';
|
|
104
|
-
const forwardLines = [
|
|
105
|
-
`${formatKey('on', keyOptions)}: ${q}${link.forward.on}${q}`,
|
|
106
|
-
`${formatKey('has', keyOptions)}: ${q}${link.forward.has}${q}`,
|
|
107
|
-
`${formatKey('label', keyOptions)}: ${q}${link.forward.label}${q}`,
|
|
108
|
-
];
|
|
109
|
-
if (link.forward.required) {
|
|
110
|
-
forwardLines.push(`${formatKey('required', keyOptions)}: true`);
|
|
111
|
-
}
|
|
112
|
-
if (link.forward.onDelete) {
|
|
113
|
-
forwardLines.push(
|
|
114
|
-
`${formatKey('onDelete', keyOptions)}: ${q}${link.forward.onDelete}${q}`,
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const reverseLines = [
|
|
119
|
-
`${formatKey('on', keyOptions)}: ${q}${link.reverse.on}${q}`,
|
|
120
|
-
`${formatKey('has', keyOptions)}: ${q}${link.reverse.has}${q}`,
|
|
121
|
-
`${formatKey('label', keyOptions)}: ${q}${link.reverse.label}${q}`,
|
|
122
|
-
];
|
|
123
|
-
if (link.reverse.onDelete) {
|
|
124
|
-
reverseLines.push(
|
|
125
|
-
`${formatKey('onDelete', keyOptions)}: ${q}${link.reverse.onDelete}${q}`,
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const forwardBodyLines =
|
|
130
|
-
style === 'json' ? forwardLines : forwardLines.map((line) => `${line},`);
|
|
131
|
-
const reverseBodyLines =
|
|
132
|
-
style === 'json' ? reverseLines : reverseLines.map((line) => `${line},`);
|
|
133
|
-
|
|
134
|
-
return [
|
|
135
|
-
'{',
|
|
136
|
-
`${indentStr}${formatKey('forward', keyOptions)}: {`,
|
|
137
|
-
`${indentStr}${indentStr}${forwardBodyLines.join(
|
|
138
|
-
`${style === 'json' ? ',' : ''}\n${indentStr}${indentStr}`,
|
|
139
|
-
)}`,
|
|
140
|
-
`${indentStr}}${style === 'json' ? ',' : ','}`,
|
|
141
|
-
`${indentStr}${formatKey('reverse', keyOptions)}: {`,
|
|
142
|
-
`${indentStr}${indentStr}${reverseBodyLines.join(
|
|
143
|
-
`${style === 'json' ? ',' : ''}\n${indentStr}${indentStr}`,
|
|
144
|
-
)}`,
|
|
145
|
-
`${indentStr}}${style === 'json' ? '' : ','}`,
|
|
146
|
-
'}',
|
|
147
|
-
].join('\n');
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export function renderLinkProperty(
|
|
151
|
-
name: string,
|
|
152
|
-
link: AnyLink,
|
|
153
|
-
options: LinkRenderOptions = {},
|
|
154
|
-
) {
|
|
155
|
-
return `${formatKey(name, options.keyOptions)}: ${renderLinkValue(
|
|
156
|
-
link,
|
|
157
|
-
options,
|
|
158
|
-
)}`;
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
export function renderLinksObject(
|
|
162
|
-
links: LinksDef<any>,
|
|
163
|
-
options: LinksObjectOptions = {},
|
|
164
|
-
) {
|
|
165
|
-
const indent = options.indent ?? DEFAULT_INDENT;
|
|
166
|
-
const entries = sortedEntries(links).map(([name, link]) =>
|
|
167
|
-
renderLinkProperty(name, link, {
|
|
168
|
-
indent,
|
|
169
|
-
keyOptions: options.keyOptions,
|
|
170
|
-
stringQuote: options.stringQuote,
|
|
171
|
-
style: options.style,
|
|
172
|
-
}),
|
|
173
|
-
);
|
|
174
|
-
if (!entries.length && !options.newlineForEmpty) {
|
|
175
|
-
return '{}';
|
|
176
|
-
}
|
|
177
|
-
const inner = entries.length ? indentLines(entries.join(',\n'), indent) : '';
|
|
178
|
-
return `{\n${inner}\n}`;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
function isValidIdentifier(name: string) {
|
|
182
|
-
return /^[A-Za-z_$][A-Za-z0-9_$]*$/.test(name);
|
|
183
|
-
}
|