|
15 | 15 | */
|
16 | 16 | import { create as createDomain, CreateDomainOptions, DomainDesign } from './Domain';
|
17 | 17 |
|
18 |
| -export interface ICreateOptions { |
19 |
| - domainDesign: DomainDesign; |
20 |
| - /** The name of the key column.*/ |
21 |
| - keyName: string; |
22 |
| - /** The type of the key column. Either "int" or "string". */ |
| 18 | +// The configuration options intermix the DomainDesign configuration with the CreateDomainOptions. |
| 19 | +// This can be very confusing, so we've limited the scope of which domain design configuration options are supported. |
| 20 | +// Users can specify all available DomainDesign configuration options by specifying `domainDesign` explicitly. |
| 21 | +export type DomainDesignOptions = Pick<DomainDesign, 'description' | 'fields' | 'indices' | 'name'>; |
| 22 | + |
| 23 | +export interface ListCreateOptions extends DomainDesignOptions, CreateDomainOptions { |
| 24 | + /** |
| 25 | + * @deprecated Use `options.keyName` instead. |
| 26 | + * The name of the key column. The `options.keyName` takes precedence if both are specified. |
| 27 | + */ |
| 28 | + keyName?: string; |
| 29 | + /** |
| 30 | + * @deprecated Use `kind` instead. |
| 31 | + * The type of the key column. Can be `IntList`, `VarList`, or `AutoIncrementInteger`. |
| 32 | + * The `kind` takes precedence if both are specified. |
| 33 | + * Note: If the `AutoIncrementInteger` configuration is desired and you're specifying `kind` then the |
| 34 | + * `kind` should be set to `IntList` and the `options.keyType` should be set to `AutoIncrementInteger`. |
| 35 | + */ |
23 | 36 | keyType?: string;
|
24 |
| - kind?: string; |
25 |
| - options?: any; |
26 | 37 | }
|
27 | 38 |
|
28 | 39 | /**
|
29 |
| - * Create a new list. |
30 |
| - * A primary key column must be specified with the properties 'keyName' and 'keyType'. If the key is not |
31 |
| - * provided in the domain design's array of fields, it will be automatically added to the domain. |
| 40 | + * Create a new List. |
| 41 | + * Lists support three types of primary key configurations; `IntList`, `VarList`, or `AutoIncrementInteger`. |
| 42 | + * These key configurations determine what "kind" of List is created. |
| 43 | + * If the key field configuration is not provided in the domain design's array of fields, |
| 44 | + * it will be automatically added to the domain. |
| 45 | + * Note: If a `domainDesign` is specified then it's configuration will take precedence over other `DomainDesign` |
| 46 | + * properties that are specified. |
32 | 47 | *
|
33 | 48 | * ```
|
| 49 | + * // Creates a List with a string primary key. |
| 50 | + * LABKEY.List.create({ |
| 51 | + * fields: [{ |
| 52 | + * name: 'partCode', rangeURI: 'string', |
| 53 | + * },{ |
| 54 | + * name: 'manufacturer', rangeURI: 'string', |
| 55 | + * },{ |
| 56 | + * name: 'purchaseDate', rangeURI: 'date', |
| 57 | + * }], |
| 58 | + * kind: 'VarList', |
| 59 | + * name: 'Parts', |
| 60 | + * options: { |
| 61 | + * keyName: 'partCode', |
| 62 | + * keyType: 'Varchar', |
| 63 | + * } |
| 64 | + * }); |
| 65 | + * |
| 66 | + * // Creates a List with an auto-incrementing primary key. |
34 | 67 | * LABKEY.List.create({
|
35 |
| - * name: "mylist", |
36 |
| - * keyType: "int", |
37 |
| - * keyName: "one", |
38 |
| - * description: "my first list", |
| 68 | + * description: 'teams in the league', |
| 69 | + * kind: 'IntList', |
39 | 70 | * fields: [{
|
40 |
| - * name: "one", rangeURI: "int" |
41 |
| - * },{ |
42 |
| - * name: "two", rangeURI: "multiLine", required: true |
43 |
| - * },{ |
44 |
| - * name: "three", rangeURI: "Attachment" |
45 |
| - * }] |
| 71 | + * name: 'rowId', rangeURI: 'int', |
| 72 | + * },{ |
| 73 | + * name: 'name', rangeURI: 'string', required: true, |
| 74 | + * },{ |
| 75 | + * name: 'slogan', rangeURI: 'multiLine', |
| 76 | + * },{ |
| 77 | + * name: 'logo', rangeURI: 'Attachment', |
| 78 | + * }], |
| 79 | + * name: 'Teams', |
| 80 | + * options: { |
| 81 | + * keyName: 'rowId', |
| 82 | + * keyType: 'AutoIncrementInteger', |
| 83 | + * } |
46 | 84 | * });
|
47 | 85 | * ```
|
48 | 86 | */
|
49 |
| -export function create(config: ICreateOptions) { |
| 87 | +export function create(config: ListCreateOptions) { |
| 88 | + // Separate out `ListCreateOptions` configuration |
| 89 | + const { keyName, keyType, ...options } = config; |
| 90 | + |
| 91 | + // Separate out `DomainDesignOptions` configuration |
| 92 | + const { description, fields, indices, name, ...createDomainOptions } = options; |
| 93 | + |
50 | 94 | const domainOptions: CreateDomainOptions = {
|
51 |
| - // not really awesome...intermixing interface with domain design. Separate these concerns. |
52 |
| - domainDesign: config as Partial<DomainDesign>, |
53 |
| - options: {}, |
| 95 | + ...createDomainOptions, |
| 96 | + options: createDomainOptions.options ?? {}, |
54 | 97 | };
|
55 | 98 |
|
| 99 | + // If a `domainDesign` is not explicitly provided then fallback to `DomainDesignOptions` options |
| 100 | + if (!domainOptions.domainDesign) { |
| 101 | + domainOptions.domainDesign = { description, fields, indices, name }; |
| 102 | + } |
| 103 | + |
56 | 104 | if (!domainOptions.domainDesign.name) {
|
57 | 105 | throw new Error('List name required');
|
58 | 106 | }
|
59 | 107 |
|
60 |
| - if (!config.kind) { |
61 |
| - if (config.keyType == 'int') { |
62 |
| - config.kind = 'IntList'; |
63 |
| - } else if (config.keyType == 'string') { |
64 |
| - config.kind = 'VarList'; |
| 108 | + // Handle `keyType` only if `kind` is not specified |
| 109 | + if (!domainOptions.kind) { |
| 110 | + if (keyType === 'int' || keyType === 'IntList') { |
| 111 | + domainOptions.kind = 'IntList'; |
| 112 | + } else if (keyType === 'string' || keyType === 'VarList') { |
| 113 | + domainOptions.kind = 'VarList'; |
| 114 | + domainOptions.options.keyType = 'Varchar'; |
| 115 | + } else if (keyType === 'AutoIncrementInteger') { |
| 116 | + domainOptions.kind = 'IntList'; |
| 117 | + domainOptions.options.keyType = 'AutoIncrementInteger'; |
| 118 | + } else { |
| 119 | + throw new Error('List kind or keyType required'); |
65 | 120 | }
|
66 | 121 | }
|
67 | 122 |
|
68 |
| - if (config.kind != 'IntList' && config.kind != 'VarList') { |
69 |
| - throw new Error('Domain kind or keyType required'); |
70 |
| - } |
71 |
| - domainOptions.kind = config.kind; |
72 |
| - |
73 |
| - if (!config.keyName) { |
74 |
| - throw new Error('List keyName required'); |
| 123 | + // Handle `keyName` only if `options.keyName` is not specified |
| 124 | + if (!domainOptions.options.keyName) { |
| 125 | + if (keyName) { |
| 126 | + domainOptions.options.keyName = keyName; |
| 127 | + } else { |
| 128 | + throw new Error('List keyName or options.keyName required'); |
| 129 | + } |
75 | 130 | }
|
76 |
| - domainOptions.options.keyName = config.keyName; |
77 | 131 |
|
78 | 132 | createDomain(domainOptions);
|
79 | 133 | }
|
0 commit comments