pqb 0.9.1 → 0.9.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pqb",
3
- "version": "0.9.1",
3
+ "version": "0.9.2",
4
4
  "description": "Postgres query builder",
5
5
  "homepage": "https://orchid-orm.netlify.app/guide/query-builder-setup.html",
6
6
  "repository": {
@@ -187,6 +187,8 @@ export const indexToCode = (index: TableData.Index, t: string): Code[] => {
187
187
  const options: string[] = [];
188
188
  for (const key of optionsKeys) {
189
189
  const value = index.options[key as keyof typeof index.options];
190
+ if (value === null || value === undefined) continue;
191
+
190
192
  options.push(
191
193
  `${key}: ${
192
194
  typeof value === 'object'
@@ -350,7 +352,6 @@ export const columnIndexesToCode = (
350
352
  if (index.opclass) arr.push(`opclass: ${singleQuote(index.opclass)},`);
351
353
  if (index.order) arr.push(`order: ${singleQuote(index.order)},`);
352
354
  if (index.name) arr.push(`name: ${singleQuote(index.name)},`);
353
- if (index.unique) arr.push(`unique: true,`);
354
355
  if (index.using) arr.push(`using: ${singleQuote(index.using)},`);
355
356
  if (index.include)
356
357
  arr.push(
@@ -1,7 +1,7 @@
1
1
  import { Operator, Operators } from '../columnsOperators';
2
2
  import { JSONTypeAny } from './json';
3
3
  import { ColumnsShape } from './columnsSchema';
4
- import { RawExpression, StringKey } from '../common';
4
+ import { raw, RawExpression, StringKey } from '../common';
5
5
  import { MaybeArray } from '../utils';
6
6
  import { Query } from '../query';
7
7
  import { Code } from './code';
@@ -164,7 +164,15 @@ export const instantiateColumn = (
164
164
  params: ColumnFromDbParams,
165
165
  ): ColumnType => {
166
166
  const column = new (klass as unknown as new () => ColumnType)();
167
- Object.assign(column.data, params);
167
+
168
+ let data;
169
+ if (params.default !== null && params.default !== undefined) {
170
+ data = { ...params, default: raw(params.default) };
171
+ } else {
172
+ data = params;
173
+ }
174
+
175
+ Object.assign(column.data, data);
168
176
  return column as unknown as ColumnType;
169
177
  };
170
178
 
@@ -51,10 +51,13 @@ describe('date time columns', () => {
51
51
  assertType<typeof result, string>();
52
52
  });
53
53
 
54
- it('should have toCode', () => {
54
+ it('should have toCode, ignore default precision', () => {
55
55
  expect(new TimestampColumn().toCode('t')).toBe('t.timestamp()');
56
+
56
57
  expect(new TimestampColumn(10).toCode('t')).toBe('t.timestamp(10)');
57
58
 
59
+ expect(new TimestampColumn(6).toCode('t')).toBe('t.timestamp()');
60
+
58
61
  const now = new Date();
59
62
  const s = now.toISOString();
60
63
  expect(new TimestampColumn().min(now).max(now).toCode('t')).toBe(
@@ -76,10 +79,15 @@ describe('date time columns', () => {
76
79
  assertType<typeof result, string>();
77
80
  });
78
81
 
79
- it('should have toCode', () => {
82
+ it('should have toCode, ignore default precision', () => {
80
83
  expect(new TimestampWithTimeZoneColumn().toCode('t')).toBe(
81
84
  't.timestampWithTimeZone()',
82
85
  );
86
+
87
+ expect(new TimestampWithTimeZoneColumn(6).toCode('t')).toBe(
88
+ 't.timestampWithTimeZone()',
89
+ );
90
+
83
91
  expect(new TimestampWithTimeZoneColumn(10).toCode('t')).toBe(
84
92
  't.timestampWithTimeZone(10)',
85
93
  );
@@ -92,18 +92,27 @@ export abstract class DateTimeWithTimeZoneBaseClass<
92
92
  }
93
93
  }
94
94
 
95
+ const timestampToCode = <P extends number | undefined>(
96
+ self: TimestampColumn<P> | TimestampWithTimeZoneColumn<P>,
97
+ t: string,
98
+ ) => {
99
+ const { dateTimePrecision: p } = self.data;
100
+ return columnCode(
101
+ self,
102
+ t,
103
+ `${t}.${
104
+ self instanceof TimestampColumn ? 'timestamp' : 'timestampWithTimeZone'
105
+ }(${p && p !== 6 ? p : ''})${dateDataToCode(self.data)}`,
106
+ );
107
+ };
108
+
95
109
  // timestamp [ (p) ] [ without time zone ] 8 bytes both date and time (no time zone) 4713 BC 294276 AD 1 microsecond
96
110
  export class TimestampColumn<
97
111
  Precision extends number | undefined = undefined,
98
112
  > extends DateTimeBaseClass<Precision> {
99
113
  dataType = 'timestamp' as const;
100
114
  toCode(t: string): Code {
101
- const { dateTimePrecision } = this.data;
102
- return columnCode(
103
- this,
104
- t,
105
- `${t}.timestamp(${dateTimePrecision || ''})${dateDataToCode(this.data)}`,
106
- );
115
+ return timestampToCode(this, t);
107
116
  }
108
117
  }
109
118
 
@@ -114,14 +123,7 @@ export class TimestampWithTimeZoneColumn<
114
123
  dataType = 'timestamp with time zone' as const;
115
124
  baseDataType = 'timestamp' as const;
116
125
  toCode(t: string): Code {
117
- const { dateTimePrecision } = this.data;
118
- return columnCode(
119
- this,
120
- t,
121
- `${t}.timestampWithTimeZone(${dateTimePrecision || ''})${dateDataToCode(
122
- this.data,
123
- )}`,
124
- );
126
+ return timestampToCode(this, t);
125
127
  }
126
128
  }
127
129