@pgpm/faker 0.4.0

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.
Files changed (27) hide show
  1. package/LICENSE +22 -0
  2. package/Makefile +6 -0
  3. package/README.md +438 -0
  4. package/__tests__/faker.test.ts +135 -0
  5. package/deploy/schemas/faker/procedures/utils.sql +740 -0
  6. package/deploy/schemas/faker/schema.sql +8 -0
  7. package/deploy/schemas/faker/tables/cities/fixtures/1602987351008_fixture.sql +3700 -0
  8. package/deploy/schemas/faker/tables/cities/table.sql +29 -0
  9. package/deploy/schemas/faker/tables/dictionary/fixtures/1602969089665_fixture.sql +6036 -0
  10. package/deploy/schemas/faker/tables/dictionary/table.sql +17 -0
  11. package/jest.config.js +15 -0
  12. package/launchql-faker.control +8 -0
  13. package/launchql.plan +10 -0
  14. package/package.json +29 -0
  15. package/revert/schemas/faker/procedures/utils.sql +51 -0
  16. package/revert/schemas/faker/schema.sql +7 -0
  17. package/revert/schemas/faker/tables/cities/fixtures/1602987351008_fixture.sql +5 -0
  18. package/revert/schemas/faker/tables/cities/table.sql +7 -0
  19. package/revert/schemas/faker/tables/dictionary/fixtures/1602969089665_fixture.sql +5 -0
  20. package/revert/schemas/faker/tables/dictionary/table.sql +7 -0
  21. package/sql/launchql-faker--0.4.6.sql +10408 -0
  22. package/verify/schemas/faker/procedures/utils.sql +47 -0
  23. package/verify/schemas/faker/schema.sql +7 -0
  24. package/verify/schemas/faker/tables/cities/fixtures/1602987351008_fixture.sql +5 -0
  25. package/verify/schemas/faker/tables/cities/table.sql +7 -0
  26. package/verify/schemas/faker/tables/dictionary/fixtures/1602969089665_fixture.sql +5 -0
  27. package/verify/schemas/faker/tables/dictionary/table.sql +7 -0
@@ -0,0 +1,740 @@
1
+ -- Deploy schemas/faker/procedures/utils to pg
2
+
3
+ -- requires: schemas/faker/schema
4
+ -- requires: schemas/faker/tables/dictionary/table
5
+
6
+ BEGIN;
7
+
8
+
9
+ CREATE FUNCTION faker.word_type() returns text as $$
10
+ SELECT (CASE (RANDOM() * 2)::INT
11
+ WHEN 0 THEN 'adjectives'
12
+ WHEN 1 THEN 'colors'
13
+ WHEN 2 THEN 'animals'
14
+ END);
15
+ $$
16
+ LANGUAGE 'sql';
17
+
18
+ CREATE FUNCTION faker.word(wordtype text default null) returns text as $$
19
+ DECLARE
20
+ vword text;
21
+ vtype text;
22
+ BEGIN
23
+ IF (wordtype IS NOT NULL) THEN
24
+ vtype = wordtype;
25
+ ELSE
26
+ vtype = faker.word_type();
27
+ END IF;
28
+
29
+ SELECT word FROM faker.dictionary
30
+ WHERE type = vtype
31
+ OFFSET floor( random() * (select count(*) from faker.dictionary WHERE type = vtype ) ) LIMIT 1
32
+ INTO vword;
33
+
34
+ RETURN vword;
35
+
36
+ END;
37
+ $$
38
+ LANGUAGE 'plpgsql' VOLATILE;
39
+
40
+ CREATE FUNCTION faker.word(wordtypes text[]) returns text as $$
41
+ BEGIN
42
+ RETURN faker.word(wordtypes[faker.integer(1, cardinality(wordtypes))]::text);
43
+ END;
44
+ $$
45
+ LANGUAGE 'plpgsql' VOLATILE;
46
+
47
+ CREATE FUNCTION faker.gender(gender text default null) returns text as $$
48
+ DECLARE
49
+ BEGIN
50
+ IF (gender IS NOT NULL) THEN
51
+ RETURN gender;
52
+ END IF;
53
+ RETURN (CASE (RANDOM() * 1)::INT
54
+ WHEN 0 THEN 'M'
55
+ WHEN 1 THEN 'F'
56
+ END);
57
+ END;
58
+ $$
59
+ LANGUAGE 'plpgsql' VOLATILE;
60
+
61
+ CREATE FUNCTION faker.username() returns text as $$
62
+ DECLARE
63
+ BEGIN
64
+ RETURN (CASE (RANDOM() * 2)::INT
65
+ WHEN 0 THEN faker.word() || (RANDOM() * 100)::INT
66
+ WHEN 1 THEN faker.word() || '.' || faker.word() || (RANDOM() * 100)::INT
67
+ WHEN 2 THEN faker.word()
68
+ END);
69
+ END;
70
+ $$
71
+ LANGUAGE 'plpgsql' VOLATILE;
72
+
73
+ CREATE FUNCTION faker.name(gender text default null) returns text as $$
74
+ DECLARE
75
+
76
+ BEGIN
77
+ IF (gender IS NULL) THEN
78
+ gender = faker.gender();
79
+ END IF;
80
+
81
+ IF (gender = 'M') THEN
82
+ RETURN initcap(faker.word('boys'));
83
+ ELSE
84
+ RETURN initcap(faker.word('girls'));
85
+ END IF;
86
+
87
+ END;
88
+ $$
89
+ LANGUAGE 'plpgsql' VOLATILE;
90
+
91
+ CREATE FUNCTION faker.surname() returns text as $$
92
+ BEGIN
93
+ RETURN initcap(faker.word('surname'));
94
+ END;
95
+ $$
96
+ LANGUAGE 'plpgsql' VOLATILE;
97
+
98
+ CREATE FUNCTION faker.fullname(gender text default null) returns text as $$
99
+ BEGIN
100
+ RETURN initcap(faker.name(gender)) || ' ' || initcap(faker.word('surname'));
101
+ END;
102
+ $$
103
+ LANGUAGE 'plpgsql' VOLATILE;
104
+
105
+ CREATE FUNCTION faker.business() returns text as $$
106
+ BEGIN
107
+ RETURN (CASE (RANDOM() * 4)::INT
108
+ WHEN 0 THEN array_to_string( ARRAY[faker.word('bizname'), faker.word('bizsurname') || ',', faker.word('bizsuffix')]::text[], ' ')
109
+ WHEN 1 THEN array_to_string( ARRAY[faker.word('bizname'), faker.word('bizsurname')]::text[], ' ')
110
+ WHEN 2 THEN array_to_string( ARRAY[faker.word('bizname'), faker.word('bizsurname')]::text[], ' ')
111
+ WHEN 3 THEN array_to_string( ARRAY[faker.word('bizname') || faker.word('bizpostfix'), faker.word('bizsurname') ]::text[], ' ')
112
+ WHEN 4 THEN array_to_string( ARRAY[faker.word('bizname') || faker.word('bizpostfix'), faker.word('bizsurname') || ',', faker.word('bizsuffix')]::text[], ' ')
113
+ END);
114
+ END;
115
+ $$
116
+ LANGUAGE 'plpgsql' VOLATILE;
117
+
118
+
119
+ CREATE FUNCTION faker.city(state text default null) returns text as $$
120
+ DECLARE
121
+ vcity text;
122
+ BEGIN
123
+
124
+ IF (state IS NOT NULL) THEN
125
+
126
+ SELECT city FROM faker.cities
127
+ WHERE cities.state = city.state
128
+ OFFSET floor( random() * (select count(*) from faker.cities WHERE cities.state = city.state ) ) LIMIT 1
129
+ INTO vcity;
130
+
131
+ ELSE
132
+
133
+ SELECT city FROM faker.cities
134
+ OFFSET floor( random() * (select count(*) from faker.cities ) ) LIMIT 1
135
+ INTO vcity;
136
+
137
+ END IF;
138
+
139
+
140
+ RETURN vcity;
141
+
142
+ END;
143
+ $$
144
+ LANGUAGE 'plpgsql' VOLATILE;
145
+
146
+ CREATE FUNCTION faker.zip(city text default null) returns int as $$
147
+ DECLARE
148
+ vzips int[];
149
+ BEGIN
150
+
151
+ IF (city IS NOT NULL) THEN
152
+
153
+ SELECT zips FROM faker.cities
154
+ WHERE cities.city = zip.city
155
+ OFFSET floor( random() * (select count(*) from faker.cities WHERE cities.city = zip.city ) ) LIMIT 1
156
+ INTO vzips;
157
+
158
+ ELSE
159
+
160
+ SELECT zips FROM faker.cities
161
+ OFFSET floor( random() * (select count(*) from faker.cities ) ) LIMIT 1
162
+ INTO vzips;
163
+
164
+ END IF;
165
+
166
+
167
+ RETURN vzips[ faker.integer(1, cardinality(vzips)) ];
168
+
169
+ END;
170
+ $$
171
+ LANGUAGE 'plpgsql' VOLATILE;
172
+
173
+
174
+ CREATE FUNCTION faker.lnglat(x1 float, y1 float, x2 float, y2 float) returns point as $$
175
+ DECLARE
176
+ vlat float;
177
+ vlng float;
178
+ BEGIN
179
+
180
+ RETURN Point(
181
+ faker.float(least(x1, x2), greatest(x1, x2)),
182
+ faker.float(least(y1, y2), greatest(y1, y2))
183
+ );
184
+
185
+ END;
186
+ $$
187
+ LANGUAGE 'plpgsql' VOLATILE;
188
+
189
+ CREATE FUNCTION faker.lnglat(city text default null) returns point as $$
190
+ DECLARE
191
+ vlat float;
192
+ vlng float;
193
+ BEGIN
194
+
195
+ IF (city IS NOT NULL) THEN
196
+ SELECT lat, lng FROM faker.cities
197
+ WHERE cities.city = lnglat.city
198
+ OFFSET floor( random() * (select count(*) from faker.cities WHERE cities.city = lnglat.city ) ) LIMIT 1
199
+ INTO vlat, vlng;
200
+ ELSE
201
+ SELECT lat, lng FROM faker.cities
202
+ OFFSET floor( random() * (select count(*) from faker.cities ) ) LIMIT 1
203
+ INTO vlat, vlng;
204
+ END IF;
205
+
206
+ RETURN Point(vlng, vlat);
207
+
208
+ END;
209
+ $$
210
+ LANGUAGE 'plpgsql' VOLATILE;
211
+
212
+
213
+
214
+ CREATE FUNCTION faker.phone() returns text as $$
215
+ BEGIN
216
+
217
+ RETURN concat('+1 ',
218
+
219
+ '(',
220
+
221
+ array_to_string(ARRAY[
222
+ faker.integer(0,9),
223
+ faker.integer(0,9),
224
+ faker.integer(0,9)
225
+ ]::text[], ''),
226
+
227
+ ') ',
228
+
229
+ array_to_string(ARRAY[
230
+ faker.integer(0,9),
231
+ faker.integer(0,9),
232
+ faker.integer(0,9)
233
+ ]::text[], ''),
234
+
235
+ '-',
236
+
237
+ array_to_string(ARRAY[
238
+ faker.integer(0,9),
239
+ faker.integer(0,9),
240
+ faker.integer(0,9),
241
+ faker.integer(0,9)
242
+ ]::text[], '')
243
+
244
+ );
245
+
246
+ END;
247
+ $$
248
+ LANGUAGE 'plpgsql' VOLATILE;
249
+
250
+ CREATE FUNCTION faker.street() returns text as $$
251
+ BEGIN
252
+ RETURN faker.word('street');
253
+ END;
254
+ $$
255
+ LANGUAGE 'plpgsql' VOLATILE;
256
+
257
+ CREATE FUNCTION faker.state(state text default null) returns text as $$
258
+ DECLARE
259
+ vstate text;
260
+ BEGIN
261
+
262
+ IF (state IS NULL) THEN
263
+ SELECT distinct(c.state) FROM faker.cities c
264
+ OFFSET floor( random() * (select count(distinct(c2.state)) from faker.cities c2 ) ) LIMIT 1
265
+ INTO vstate;
266
+ ELSE
267
+ vstate = state;
268
+ END IF;
269
+
270
+ RETURN vstate;
271
+
272
+ END;
273
+ $$
274
+ LANGUAGE 'plpgsql' VOLATILE;
275
+
276
+ CREATE FUNCTION faker.address(state text default null, city text default null) returns text as $$
277
+ DECLARE
278
+ vcity text;
279
+ vstate text;
280
+ vstreet text;
281
+ vstreetnum int;
282
+ vzips int[];
283
+ vzip int;
284
+ BEGIN
285
+
286
+ IF (state IS NULL) THEN
287
+ vstate = faker.state();
288
+ ELSE
289
+ vstate = state;
290
+ END IF;
291
+
292
+ SELECT c.city, c.zips FROM faker.cities c
293
+ WHERE c.state = vstate
294
+ OFFSET floor( random() * (select count(*) from faker.cities WHERE cities.state = vstate ) ) LIMIT 1
295
+ INTO vcity, vzips;
296
+
297
+ vstreetnum = faker.integer(1, 3000);
298
+ vstreet = faker.street();
299
+ vzip = vzips[ faker.integer(1, cardinality(vzips)) ];
300
+
301
+ RETURN concat(vstreetnum::text, ' ', vstreet, E'\n', vcity, ', ', vstate, ' ', vzip::text);
302
+
303
+ END;
304
+ $$
305
+ LANGUAGE 'plpgsql' VOLATILE;
306
+
307
+ CREATE FUNCTION faker.tags(min int default 1, max int default 5, dict text default 'tag') returns citext[] as $$
308
+ DECLARE
309
+ words text[];
310
+ lim int = faker.integer(min,max);
311
+ BEGIN
312
+
313
+ SELECT ARRAY (
314
+ SELECT word FROM faker.dictionary
315
+ WHERE type = dict
316
+ OFFSET floor( random() * (select count(*) from faker.dictionary WHERE type = dict ) ) LIMIT lim
317
+ ) INTO words;
318
+
319
+ RETURN words::citext[];
320
+ END;
321
+ $$
322
+ LANGUAGE 'plpgsql' VOLATILE;
323
+
324
+ CREATE FUNCTION faker.sentence(unit text default 'word', min int default 7, max int default 20, cat text[] default ARRAY['lorem']::text[], period text default '.') returns text as $$
325
+ DECLARE
326
+ num int = faker.integer(min, max);
327
+ txt text;
328
+ vtype text;
329
+ n int;
330
+ c int;
331
+ BEGIN
332
+
333
+ IF (unit = 'word') THEN
334
+ txt = initcap(faker.word(cat));
335
+ FOR n IN
336
+ SELECT * FROM generate_series(1, num) g(n)
337
+ LOOP
338
+ txt = txt || ' ' || faker.word(cat);
339
+ END LOOP;
340
+ RETURN txt || period;
341
+ ELSEIF (unit = 'char' OR unit = 'chars') THEN
342
+ txt = initcap(faker.word(cat));
343
+ c = char_length(txt);
344
+ IF (c = num) THEN
345
+ RETURN concat(txt, period);
346
+ END IF;
347
+ IF (c > num) THEN
348
+ RETURN substring(txt from 1 for num) || period;
349
+ END IF;
350
+ WHILE (c < num)
351
+ LOOP
352
+ txt = txt || ' ' || faker.word(cat);
353
+ c = char_length(txt);
354
+ END LOOP;
355
+ IF (c = num) THEN
356
+ RETURN txt || period;
357
+ END IF;
358
+ IF (c > num) THEN
359
+ RETURN substring(txt from 1 for num) || period;
360
+ END IF;
361
+ RETURN txt || period;
362
+ END IF;
363
+ RAISE EXCEPTION 'faker.sentence() bad input';
364
+ END;
365
+ $$
366
+ LANGUAGE 'plpgsql' VOLATILE;
367
+
368
+ CREATE FUNCTION faker.paragraph(unit text default 'word', min int default 7, max int default 20, cat text[] default ARRAY['lorem']::text[]) returns text as $$
369
+ DECLARE
370
+ num int = faker.integer(min, max);
371
+ txt text;
372
+ words text[];
373
+ n int;
374
+ needscaps boolean = false;
375
+ BEGIN
376
+ txt = faker.sentence(unit, min, max, cat, '');
377
+ words = string_to_array(txt, ' ');
378
+ txt = '';
379
+
380
+ FOR n IN
381
+ SELECT * FROM generate_series(1, cardinality(words)) g(n)
382
+ LOOP
383
+ IF (needscaps IS TRUE) THEN
384
+ txt = concat(txt, ' ', initcap(words[n]));
385
+ ELSE
386
+ txt = concat(txt, ' ', words[n]);
387
+ END IF;
388
+
389
+ IF (faker.integer(1,100) > 70) THEN
390
+ txt = txt || '.';
391
+ needscaps = true;
392
+ ELSE
393
+ needscaps = false;
394
+ END IF;
395
+
396
+ END LOOP;
397
+
398
+ IF (trim(txt) ~ '\.$') THEN
399
+ RETURN trim(txt);
400
+ ELSE
401
+ RETURN trim(txt || '.');
402
+ END IF;
403
+
404
+ END;
405
+ $$
406
+ LANGUAGE 'plpgsql' VOLATILE;
407
+
408
+ CREATE FUNCTION faker.email() returns text as $$
409
+ SELECT
410
+ faker.word() || (RANDOM() * 100)::INT || '@' || (
411
+ CASE (RANDOM() * 3)::INT
412
+ WHEN 0 THEN 'gmail'
413
+ WHEN 1 THEN 'hotmail'
414
+ WHEN 2 THEN 'yahoo'
415
+ WHEN 3 THEN faker.word()
416
+ END
417
+ ) || '.com' AS email
418
+ $$
419
+ LANGUAGE 'sql';
420
+
421
+ CREATE FUNCTION faker.uuid() returns uuid as $$
422
+ SELECT
423
+ uuid_generate_v4();
424
+ $$
425
+ LANGUAGE 'sql';
426
+
427
+ CREATE FUNCTION faker.token(bytes int default 16) returns text as $$
428
+ SELECT
429
+ encode( gen_random_bytes( bytes ), 'hex' )
430
+ $$
431
+ LANGUAGE 'sql';
432
+
433
+ CREATE FUNCTION faker.password() returns text as $$
434
+ DECLARE
435
+ chars text[] = regexp_split_to_array('!@#$%^&*():";''<>?,./~`'::text, '');
436
+ num_special int = faker.integer(0, 4);
437
+ n int = 0;
438
+ pass text;
439
+ BEGIN
440
+
441
+ IF (num_special = 0) THEN
442
+ pass = encode( gen_random_bytes( 16 ), 'hex' );
443
+ ELSE
444
+ pass = encode( gen_random_bytes( 4 ), 'hex' );
445
+ FOR n IN
446
+ SELECT * FROM generate_series(1, num_special) g(n)
447
+ LOOP
448
+ pass = pass ||
449
+ encode( gen_random_bytes( faker.integer(1,4) ), 'hex' ) ||
450
+ chars[ faker.integer(1, cardinality(chars)) ] ||
451
+ encode( gen_random_bytes( faker.integer(1,4) ), 'hex' );
452
+
453
+ END LOOP;
454
+ END IF;
455
+
456
+
457
+ RETURN pass;
458
+
459
+ END;
460
+ $$
461
+ LANGUAGE 'plpgsql' VOLATILE;
462
+
463
+ CREATE FUNCTION faker.hostname() returns text as $$
464
+ SELECT
465
+ faker.word() || '.' || (
466
+ CASE (RANDOM() * 4)::INT
467
+ WHEN 0 THEN 'com'
468
+ WHEN 1 THEN 'net'
469
+ WHEN 2 THEN 'io'
470
+ WHEN 3 THEN 'org'
471
+ WHEN 4 THEN 'co'
472
+ END
473
+ )
474
+ $$
475
+ LANGUAGE 'sql';
476
+
477
+ CREATE FUNCTION faker.time_unit() returns text as $$
478
+ SELECT
479
+ (
480
+ CASE (RANDOM() * 5)::INT
481
+ WHEN 0 THEN 'millisecond'
482
+ WHEN 1 THEN 'second'
483
+ WHEN 2 THEN 'minute'
484
+ WHEN 3 THEN 'hour'
485
+ WHEN 4 THEN 'day'
486
+ WHEN 5 THEN 'week'
487
+ END
488
+ )
489
+ $$
490
+ LANGUAGE 'sql';
491
+
492
+ CREATE FUNCTION faker.float(min float default 0, max float default 100) returns float as $$
493
+ DECLARE
494
+ num float;
495
+ high float;
496
+ low float;
497
+ BEGIN
498
+ high = greatest(min, max);
499
+ low = least(min, max);
500
+ num = (RANDOM() * ( high - low )) + low;
501
+ RETURN num;
502
+ END;
503
+ $$
504
+ LANGUAGE 'plpgsql' VOLATILE;
505
+
506
+ CREATE FUNCTION faker.integer(min int default 0, max int default 100) returns int as $$
507
+ DECLARE
508
+ num int;
509
+ BEGIN
510
+ min = ceil(min);
511
+ max = floor(max);
512
+ num = floor(RANDOM() * ( max - min + 1 )) + min;
513
+ RETURN num;
514
+ END;
515
+ $$
516
+ LANGUAGE 'plpgsql' VOLATILE;
517
+
518
+ CREATE FUNCTION faker.date(min int default 1, max int default 100, future boolean default false) returns date as $$
519
+ DECLARE
520
+ d date;
521
+ num int = faker.integer(min, max);
522
+ BEGIN
523
+ IF (future) THEN
524
+ d = now()::date + num;
525
+ ELSE
526
+ d = now()::date - num;
527
+ END IF;
528
+ RETURN d;
529
+ END;
530
+ $$
531
+ LANGUAGE 'plpgsql' VOLATILE;
532
+
533
+ CREATE FUNCTION faker.birthdate(min int default 1, max int default 100) returns date as $$
534
+ DECLARE
535
+ d date;
536
+ years int = faker.integer(min, max);
537
+ days int = faker.integer(1, 365);
538
+ itv interval;
539
+ BEGIN
540
+ itv = concat(years, ' years')::interval + concat(days, ' days')::interval;
541
+ d = now()::date - itv;
542
+ RETURN d;
543
+ END;
544
+ $$
545
+ LANGUAGE 'plpgsql' VOLATILE;
546
+
547
+ CREATE FUNCTION faker.interval() returns interval as $$
548
+ DECLARE
549
+ ival text;
550
+ BEGIN
551
+ SELECT faker.time_unit() INTO ival;
552
+ ival = (RANDOM() * 100)::text || ' ' || ival;
553
+ RETURN ival::interval;
554
+ END;
555
+ $$
556
+ LANGUAGE 'plpgsql' VOLATILE;
557
+
558
+
559
+ CREATE FUNCTION faker.interval(min int, max int) returns interval as $$
560
+ SELECT (faker.integer(min, max)::text || ' ' || 'seconds')::interval;
561
+ $$
562
+ LANGUAGE 'sql' VOLATILE;
563
+
564
+ CREATE FUNCTION faker.boolean() returns boolean as $$
565
+ BEGIN
566
+ RETURN (
567
+ CASE (RANDOM() * 1)::INT
568
+ WHEN 0 THEN TRUE
569
+ WHEN 1 THEN FALSE
570
+ END
571
+ );
572
+ END;
573
+ $$
574
+ LANGUAGE 'plpgsql' VOLATILE;
575
+
576
+ CREATE FUNCTION faker.timestamptz(future boolean default false) returns timestamptz as $$
577
+ DECLARE
578
+ ival interval;
579
+ t timestamptz;
580
+ BEGIN
581
+ ival = faker.interval();
582
+ IF (future) THEN
583
+ SELECT now() + ival INTO t;
584
+ ELSE
585
+ SELECT now() - ival INTO t;
586
+ END IF;
587
+ RETURN t;
588
+ END;
589
+ $$
590
+ LANGUAGE 'plpgsql' VOLATILE;
591
+
592
+ CREATE FUNCTION faker.mime() returns text as $$
593
+ SELECT faker.word('mime');
594
+ $$
595
+ LANGUAGE 'sql';
596
+
597
+ CREATE FUNCTION faker.ext(mime text default faker.mime()) returns text as $$
598
+ DECLARE
599
+ ext text;
600
+ BEGIN
601
+ IF (mime IS NULL) THEN
602
+ ext = faker.word(faker.mime());
603
+ ELSE
604
+ ext = faker.word(mime);
605
+ END IF;
606
+ RETURN ext;
607
+ END;
608
+ $$
609
+ LANGUAGE 'plpgsql' VOLATILE;
610
+
611
+
612
+ CREATE FUNCTION faker.image_mime() returns text as $$
613
+ SELECT
614
+ (
615
+ CASE (RANDOM() * 3)::INT
616
+ WHEN 0 THEN 'image/svg+xml'
617
+ WHEN 1 THEN 'image/png'
618
+ WHEN 2 THEN 'image/gif'
619
+ WHEN 3 THEN 'image/jpeg'
620
+ END
621
+ )
622
+ $$
623
+ LANGUAGE 'sql';
624
+
625
+ CREATE FUNCTION faker.image(width int default null, height int default null) returns image as $$
626
+ DECLARE
627
+ w int;
628
+ h int;
629
+ obj jsonb = '{}'::jsonb;
630
+ url text;
631
+ BEGIN
632
+ IF (width IS NULL) THEN
633
+ w = faker.integer(800,1200);
634
+ ELSE
635
+ w = width;
636
+ END IF;
637
+ IF (height IS NULL) THEN
638
+ h = faker.integer(800,1200);
639
+ ELSE
640
+ h = height;
641
+ END IF;
642
+
643
+ url = concat('https://picsum.photos/', w::text, '/', h::text);
644
+
645
+ obj = jsonb_set(obj, '{url}', to_jsonb(url));
646
+ obj = jsonb_set(obj, '{mime}', to_jsonb(faker.image_mime()));
647
+
648
+ RETURN obj;
649
+
650
+ END;
651
+ $$
652
+ LANGUAGE 'plpgsql' VOLATILE;
653
+
654
+ CREATE FUNCTION faker.profilepic() returns image as $$
655
+ DECLARE
656
+ obj jsonb = '{}'::jsonb;
657
+ vurl text = '';
658
+ gndr text = 'women';
659
+ BEGIN
660
+ IF ((RANDOM() * 2)::INT = 0) THEN
661
+ gndr = 'men';
662
+ END IF;
663
+ vurl = concat('https://randomuser.me/api/portraits/', gndr, '/', faker.integer(1, 99) , '.jpg');
664
+ obj = jsonb_set(obj, '{url}', to_jsonb(vurl::text));
665
+ obj = jsonb_set(obj, '{mime}', to_jsonb('image/jpeg'::text));
666
+ RETURN obj;
667
+ END;
668
+ $$
669
+ LANGUAGE 'plpgsql' VOLATILE;
670
+
671
+ CREATE FUNCTION faker.profilepic(gender text) returns image as $$
672
+ DECLARE
673
+ obj jsonb = '{}'::jsonb;
674
+ vurl text = '';
675
+ gndr text = 'women';
676
+ BEGIN
677
+ IF (gender = 'M') THEN
678
+ gndr = 'men';
679
+ END IF;
680
+ vurl = concat('https://randomuser.me/api/portraits/', gndr, '/', faker.integer(1, 99) , '.jpg');
681
+ obj = jsonb_set(obj, '{url}', to_jsonb(vurl::text));
682
+ obj = jsonb_set(obj, '{mime}', to_jsonb('image/jpeg'::text));
683
+ RETURN obj;
684
+ END;
685
+ $$
686
+ LANGUAGE 'plpgsql' VOLATILE;
687
+
688
+ CREATE FUNCTION faker.file(mime text default null) returns text as $$
689
+ BEGIN
690
+ RETURN concat(faker.word(), '.', faker.ext(mime));
691
+ END;
692
+ $$
693
+ LANGUAGE 'plpgsql' VOLATILE;
694
+
695
+ CREATE FUNCTION faker.url(mime text default null) returns url as $$
696
+ DECLARE
697
+ obj jsonb = '{}'::jsonb;
698
+ url text;
699
+ BEGIN
700
+ url = concat('https://', faker.hostname(), '/', faker.file(mime));
701
+ RETURN url;
702
+ END;
703
+ $$
704
+ LANGUAGE 'plpgsql' VOLATILE;
705
+
706
+ CREATE FUNCTION faker.upload(mime text default null) returns upload as $$
707
+ BEGIN
708
+ RETURN faker.url(mime);
709
+ END;
710
+ $$
711
+ LANGUAGE 'plpgsql' VOLATILE;
712
+
713
+ CREATE FUNCTION faker.ip(mime text default null) returns text as $$
714
+ BEGIN
715
+ RETURN
716
+ array_to_string(ARRAY[
717
+ faker.integer(0,255),
718
+ faker.integer(0,255),
719
+ faker.integer(0,255),
720
+ faker.integer(0,255)
721
+ ]::text[], '.');
722
+ END;
723
+ $$
724
+ LANGUAGE 'plpgsql' VOLATILE;
725
+
726
+ CREATE FUNCTION faker.attachment(mime text default null) returns attachment as $$
727
+ DECLARE
728
+ obj jsonb = '{}'::jsonb;
729
+ BEGIN
730
+ IF (mime IS NULL) THEN
731
+ mime = faker.mime();
732
+ END IF;
733
+ obj = jsonb_set(obj, '{url}', to_jsonb(faker.url(mime)::text));
734
+ obj = jsonb_set(obj, '{mime}', to_jsonb(mime));
735
+ RETURN obj;
736
+ END;
737
+ $$
738
+ LANGUAGE 'plpgsql' VOLATILE;
739
+
740
+ COMMIT;