@ronin/compiler 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.ts CHANGED
@@ -4664,10 +4664,15 @@ type SchemaFieldBasics = {
4664
4664
  type SchemaFieldNormal = SchemaFieldBasics & {
4665
4665
  type: 'string' | 'number' | 'boolean' | 'date' | 'json' | 'group';
4666
4666
  };
4667
+ type SchemaFieldReferenceAction = 'CASCADE' | 'RESTRICT' | 'SET NULL' | 'SET DEFAULT' | 'NO ACTION';
4667
4668
  type SchemaFieldReference = SchemaFieldBasics & {
4668
4669
  type: 'reference';
4669
- target: string;
4670
+ target: Omit<Partial<Schema>, 'pluralSlug'> & Pick<Schema, 'pluralSlug'>;
4670
4671
  kind?: 'one' | 'many';
4672
+ actions?: {
4673
+ onDelete?: SchemaFieldReferenceAction;
4674
+ onUpdate?: SchemaFieldReferenceAction;
4675
+ };
4671
4676
  };
4672
4677
  type SchemaField = SchemaFieldNormal | SchemaFieldReference;
4673
4678
  interface Schema {
package/dist/index.js CHANGED
@@ -159,8 +159,7 @@ var SYSTEM_FIELDS = [
159
159
  },
160
160
  {
161
161
  name: "RONIN - Created By",
162
- type: "reference",
163
- target: "account",
162
+ type: "string",
164
163
  slug: "ronin.createdBy"
165
164
  },
166
165
  {
@@ -170,8 +169,7 @@ var SYSTEM_FIELDS = [
170
169
  },
171
170
  {
172
171
  name: "RONIN - Updated By",
173
- type: "reference",
174
- target: "account",
172
+ type: "string",
175
173
  slug: "ronin.updatedBy"
176
174
  }
177
175
  ];
@@ -203,12 +201,17 @@ var SYSTEM_SCHEMAS = [
203
201
  { slug: "name", type: "string" },
204
202
  { slug: "slug", type: "string", required: true },
205
203
  { slug: "type", type: "string", required: true },
206
- { slug: "schema", type: "reference", target: "schema" },
207
- { slug: "target", type: "reference", target: "schema" },
204
+ { slug: "schema", type: "reference", target: { pluralSlug: "schemas" } },
208
205
  { slug: "required", type: "boolean" },
209
206
  { slug: "defaultValue", type: "string" },
210
207
  { slug: "unique", type: "boolean" },
211
- { slug: "autoIncrement", type: "boolean" }
208
+ { slug: "autoIncrement", type: "boolean" },
209
+ // Only allowed for fields of type "reference".
210
+ { slug: "target", type: "reference", target: { pluralSlug: "schemas" } },
211
+ { slug: "kind", type: "string" },
212
+ { slug: "actions", type: "group" },
213
+ { slug: "actions.onDelete", type: "string" },
214
+ { slug: "actions.onUpdate", type: "string" }
212
215
  ]
213
216
  }
214
217
  ];
@@ -218,7 +221,7 @@ var addSystemSchemas = (schemas) => {
218
221
  const defaultIncluding = {};
219
222
  for (const field of schema.fields || []) {
220
223
  if (field.type === "reference" && !field.slug.startsWith("ronin.")) {
221
- const relatedSchema = getSchemaBySlug(list, field.target);
224
+ const relatedSchema = getSchemaBySlug(list, field.target.pluralSlug);
222
225
  let fieldSlug = relatedSchema.slug;
223
226
  if (field.kind === "many") {
224
227
  fieldSlug = composeAssociationSchemaSlug(schema, field);
@@ -229,12 +232,12 @@ var addSystemSchemas = (schemas) => {
229
232
  {
230
233
  slug: "source",
231
234
  type: "reference",
232
- target: schema.slug
235
+ target: schema
233
236
  },
234
237
  {
235
238
  slug: "target",
236
239
  type: "reference",
237
- target: relatedSchema.slug
240
+ target: relatedSchema
238
241
  }
239
242
  ]
240
243
  });
@@ -250,7 +253,7 @@ var addSystemSchemas = (schemas) => {
250
253
  }
251
254
  }
252
255
  };
253
- const relatedSchemaToModify = list.find((schema2) => schema2.slug === field.target);
256
+ const relatedSchemaToModify = getSchemaBySlug(list, field.target.pluralSlug);
254
257
  if (!relatedSchemaToModify) throw new Error("Missing related schema");
255
258
  relatedSchemaToModify.including = {
256
259
  [schema.pluralSlug]: {
@@ -293,6 +296,16 @@ var getFieldStatement = (field) => {
293
296
  if (field.required === true) statement += " NOT NULL";
294
297
  if (typeof field.defaultValue !== "undefined")
295
298
  statement += ` DEFAULT ${field.defaultValue}`;
299
+ if (field.type === "reference") {
300
+ const actions = field.actions || {};
301
+ const targetTable = convertToSnakeCase(field.target.pluralSlug);
302
+ statement += ` REFERENCES ${targetTable}("id")`;
303
+ for (const trigger in actions) {
304
+ const triggerName = trigger.toUpperCase().slice(2);
305
+ const action = actions[trigger];
306
+ statement += ` ON ${triggerName} ${action}`;
307
+ }
308
+ }
296
309
  return statement;
297
310
  };
298
311
  var addSchemaQueries = (queryDetails, writeStatements) => {
@@ -496,7 +509,7 @@ var composeConditions = (schemas, schema, statementValues, instructionName, valu
496
509
  if (keys.length === 1 && keys[0] === "id") {
497
510
  recordTarget = values[0];
498
511
  } else {
499
- const relatedSchema = getSchemaBySlug(schemas, schemaField.target);
512
+ const relatedSchema = getSchemaBySlug(schemas, schemaField.target.pluralSlug);
500
513
  const subQuery = {
501
514
  get: {
502
515
  [relatedSchema.slug]: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ronin/compiler",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "description": "Compiles RONIN queries to SQL statements.",
6
6
  "publishConfig": {