@ronin/compiler 0.7.1 → 0.7.2

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,12 @@
1
1
  import { z } from 'zod';
2
2
 
3
+ declare const ExpressionSchema: z.ZodObject<{
4
+ __RONIN_EXPRESSION: z.ZodString;
5
+ }, "strip", z.ZodTypeAny, {
6
+ __RONIN_EXPRESSION: string;
7
+ }, {
8
+ __RONIN_EXPRESSION: string;
9
+ }>;
3
10
  declare const WithInstructionSchema: z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull, z.ZodAny]>, z.ZodEffects<z.ZodObject<{
4
11
  being: z.ZodOptional<z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull, z.ZodAny]>, z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull, z.ZodAny]>, "many">]>>;
5
12
  notBeing: z.ZodOptional<z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull, z.ZodAny]>, z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodNull, z.ZodAny]>, "many">]>>;
@@ -5684,6 +5691,7 @@ declare const QuerySchema: z.ZodObject<{
5684
5691
 
5685
5692
  type GetInstructions = z.infer<typeof GetInstructionsSchema>;
5686
5693
  type WithInstruction = z.infer<typeof WithInstructionSchema>;
5694
+ type Expression = z.infer<typeof ExpressionSchema>;
5687
5695
  type Query = z.infer<typeof QuerySchema>;
5688
5696
  interface Statement {
5689
5697
  statement: string;
@@ -5698,13 +5706,14 @@ type ModelFieldBasics = {
5698
5706
  unique?: boolean;
5699
5707
  required?: boolean;
5700
5708
  defaultValue?: unknown;
5709
+ check?: Expression;
5701
5710
  };
5702
5711
  type ModelFieldNormal = ModelFieldBasics & {
5703
5712
  type: 'string' | 'number' | 'boolean' | 'date' | 'json' | 'group';
5704
5713
  };
5705
5714
  type ModelFieldReferenceAction = 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT' | 'NO ACTION';
5706
5715
  type ModelFieldReference = ModelFieldBasics & {
5707
- type: 'reference';
5716
+ type: 'link';
5708
5717
  target: Omit<Partial<Model>, 'slug'> & Pick<Model, 'slug'>;
5709
5718
  kind?: 'one' | 'many';
5710
5719
  actions?: {
@@ -5787,11 +5796,11 @@ interface Model {
5787
5796
  tableAlias?: string;
5788
5797
  /**
5789
5798
  * If the model is used to associate two models with each other (in the case of
5790
- * many-cardinality reference fields), this property should contain the field to which
5791
- * the associative model should be mounted.
5799
+ * many-cardinality link fields), this property should contain the field slug to which
5800
+ * the associative model should be mounted on the source model.
5792
5801
  */
5793
5802
  associationSlug?: string;
5794
- fields?: Array<ModelField>;
5803
+ fields: Array<ModelField>;
5795
5804
  indexes?: Array<ModelIndex>;
5796
5805
  triggers?: Array<ModelTrigger>;
5797
5806
  presets?: Array<ModelPreset>;
package/dist/index.js CHANGED
@@ -187,7 +187,7 @@ var composeConditions = (models, model, statementParams, instructionName, value,
187
187
  { ...options, fieldSlug: options.fieldSlug }
188
188
  );
189
189
  }
190
- if (modelField.type === "reference" && isNested) {
190
+ if (modelField.type === "link" && isNested) {
191
191
  const keys = Object.keys(value);
192
192
  const values = Object.values(value);
193
193
  let recordTarget;
@@ -490,7 +490,7 @@ var SYSTEM_MODELS = [
490
490
  { slug: "type", type: "string", required: true },
491
491
  {
492
492
  slug: "model",
493
- type: "reference",
493
+ type: "link",
494
494
  target: { slug: "model" },
495
495
  required: true
496
496
  },
@@ -498,8 +498,8 @@ var SYSTEM_MODELS = [
498
498
  { slug: "defaultValue", type: "string" },
499
499
  { slug: "unique", type: "boolean" },
500
500
  { slug: "autoIncrement", type: "boolean" },
501
- // Only allowed for fields of type "reference".
502
- { slug: "target", type: "reference", target: { slug: "model" } },
501
+ // Only allowed for fields of type "link".
502
+ { slug: "target", type: "link", target: { slug: "model" } },
503
503
  { slug: "kind", type: "string" },
504
504
  { slug: "actions", type: "group" },
505
505
  { slug: "actions.onDelete", type: "string" },
@@ -516,7 +516,7 @@ var SYSTEM_MODELS = [
516
516
  { slug: "slug", type: "string", required: true },
517
517
  {
518
518
  slug: "model",
519
- type: "reference",
519
+ type: "link",
520
520
  target: { slug: "model" },
521
521
  required: true
522
522
  },
@@ -535,7 +535,7 @@ var SYSTEM_MODELS = [
535
535
  { slug: "slug", type: "string", required: true },
536
536
  {
537
537
  slug: "model",
538
- type: "reference",
538
+ type: "link",
539
539
  target: { slug: "model" },
540
540
  required: true
541
541
  },
@@ -552,7 +552,7 @@ var SYSTEM_MODELS = [
552
552
  { slug: "slug", type: "string", required: true },
553
553
  {
554
554
  slug: "model",
555
- type: "reference",
555
+ type: "link",
556
556
  target: { slug: "model" },
557
557
  required: true
558
558
  },
@@ -568,7 +568,7 @@ var addSystemModels = (models) => {
568
568
  const associativeModels = models.flatMap((model) => {
569
569
  const addedModels = [];
570
570
  for (const field of model.fields || []) {
571
- if (field.type === "reference" && !field.slug.startsWith("ronin.")) {
571
+ if (field.type === "link" && !field.slug.startsWith("ronin.")) {
572
572
  const relatedModel = getModelBySlug(models, field.target.slug);
573
573
  let fieldSlug = relatedModel.slug;
574
574
  if (field.kind === "many") {
@@ -580,12 +580,12 @@ var addSystemModels = (models) => {
580
580
  fields: [
581
581
  {
582
582
  slug: "source",
583
- type: "reference",
583
+ type: "link",
584
584
  target: { slug: model.slug }
585
585
  },
586
586
  {
587
587
  slug: "target",
588
- type: "reference",
588
+ type: "link",
589
589
  target: { slug: relatedModel.slug }
590
590
  }
591
591
  ]
@@ -600,7 +600,7 @@ var addSystemModels = (models) => {
600
600
  var addDefaultModelPresets = (list, model) => {
601
601
  const defaultPresets = [];
602
602
  for (const field of model.fields || []) {
603
- if (field.type === "reference" && !field.slug.startsWith("ronin.")) {
603
+ if (field.type === "link" && !field.slug.startsWith("ronin.")) {
604
604
  const relatedModel = getModelBySlug(list, field.target.slug);
605
605
  if (field.kind === "many") continue;
606
606
  defaultPresets.push({
@@ -611,7 +611,7 @@ var addDefaultModelPresets = (list, model) => {
611
611
  get: {
612
612
  [relatedModel.slug]: {
613
613
  with: {
614
- // Compare the `id` field of the related model to the reference field on
614
+ // Compare the `id` field of the related model to the link field on
615
615
  // the root model (`field.slug`).
616
616
  id: {
617
617
  [RONIN_MODEL_SYMBOLS.EXPRESSION]: `${RONIN_MODEL_SYMBOLS.FIELD_PARENT}${field.slug}`
@@ -629,7 +629,7 @@ var addDefaultModelPresets = (list, model) => {
629
629
  }
630
630
  const childModels = list.map((subModel) => {
631
631
  const field = subModel.fields?.find((field2) => {
632
- return field2.type === "reference" && field2.target.slug === model.slug;
632
+ return field2.type === "link" && field2.target.slug === model.slug;
633
633
  });
634
634
  if (!field) return null;
635
635
  return { model: subModel, field };
@@ -670,7 +670,7 @@ var mappedInstructions = {
670
670
  drop: "with"
671
671
  };
672
672
  var typesInSQLite = {
673
- reference: "TEXT",
673
+ link: "TEXT",
674
674
  string: "TEXT",
675
675
  date: "DATETIME",
676
676
  blob: "TEXT",
@@ -678,7 +678,7 @@ var typesInSQLite = {
678
678
  number: "INTEGER",
679
679
  json: "TEXT"
680
680
  };
681
- var getFieldStatement = (field) => {
681
+ var getFieldStatement = (model, field) => {
682
682
  if (field.type === "group") return null;
683
683
  let statement = `"${field.slug}" ${typesInSQLite[field.type]}`;
684
684
  if (field.slug === "id") statement += " PRIMARY KEY";
@@ -686,7 +686,11 @@ var getFieldStatement = (field) => {
686
686
  if (field.required === true) statement += " NOT NULL";
687
687
  if (typeof field.defaultValue !== "undefined")
688
688
  statement += ` DEFAULT ${field.defaultValue}`;
689
- if (field.type === "reference") {
689
+ if (typeof field.check !== "undefined") {
690
+ const symbol = getSymbol(field.check);
691
+ statement += ` CHECK (${parseFieldExpression(model, "to", symbol?.value)})`;
692
+ }
693
+ if (field.type === "link") {
690
694
  const actions = field.actions || {};
691
695
  const targetTable = convertToSnakeCase(pluralize(field.target.slug));
692
696
  statement += ` REFERENCES ${targetTable}("id")`;
@@ -846,13 +850,14 @@ var addModelQueries = (models, queryDetails, dependencyStatements) => {
846
850
  queryInstructions.to = modelWithPresets;
847
851
  }
848
852
  if (queryType === "create") {
849
- const { fields } = queryInstructions.to;
850
- const columns = fields.map(getFieldStatement).filter(Boolean);
853
+ const newModel = queryInstructions.to;
854
+ const { fields } = newModel;
855
+ const columns = fields.map((field) => getFieldStatement(newModel, field)).filter(Boolean);
851
856
  dependencyStatements.push({
852
857
  statement: `${statement} (${columns.join(", ")})`,
853
858
  params: []
854
859
  });
855
- models.push(queryInstructions.to);
860
+ models.push(newModel);
856
861
  } else if (queryType === "set") {
857
862
  const newSlug = queryInstructions.to?.pluralSlug;
858
863
  if (newSlug) {
@@ -879,7 +884,7 @@ var addModelQueries = (models, queryDetails, dependencyStatements) => {
879
884
  });
880
885
  }
881
886
  dependencyStatements.push({
882
- statement: `${statement} ADD COLUMN ${getFieldStatement(instructionList)}`,
887
+ statement: `${statement} ADD COLUMN ${getFieldStatement(targetModel, instructionList)}`,
883
888
  params: []
884
889
  });
885
890
  } else if (queryType === "set") {
@@ -1201,7 +1206,7 @@ var handleTo = (models, model, statementParams, queryType, dependencyStatements,
1201
1206
  for (const fieldSlug in toInstruction) {
1202
1207
  const fieldValue = toInstruction[fieldSlug];
1203
1208
  const fieldDetails = getFieldFromModel(model, fieldSlug, "to");
1204
- if (fieldDetails.field.type === "reference" && fieldDetails.field.kind === "many") {
1209
+ if (fieldDetails.field.type === "link" && fieldDetails.field.kind === "many") {
1205
1210
  delete toInstruction[fieldSlug];
1206
1211
  const associativeModelSlug = composeAssociationModelSlug(model, fieldDetails.field);
1207
1212
  const composeStatement = (subQueryType, value) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ronin/compiler",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {