-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (65 loc) · 2.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
// dead simple Object Document Mapper for dynamodb, supporting single-table design.
const { Table } = require('./lib/table.js');
const { Schema } = require('./lib/schema.js');
const {
DocId, Timestamp, Binary,
DocIdField, TypeField, VersionField, CreatedAtField, UpdatedAtField
} = require('./lib/shared.js');
const createLogger = (loggingOptions) => {
if (typeof loggingOptions?.child === 'function') {
// if we were passed an existing pino logger (or abstract logger), or
// something that looks like one, create a child logger:
return loggingOptions.child({module:'dynamodm'});
} else if (loggingOptions){
// otherwise if logging options have been passed, create a new pino
// instance:
loggingOptions = (typeof loggingOptions === 'object')? loggingOptions : {};
return require('pino')(loggingOptions);
} else {
// otherwise logging is completely disabled
const logger = require('abstract-logging');
logger.child = () => logger;
return logger;
}
};
function DynamoDM(options) {
// global cross-table state:
const logger = createLogger(options?.logger);
const defaultOptions = Object.assign(Object.create(null), options, { logger });
const PublicAPI = {
// Public API
// Create a schema, merging in default options:
Schema: (name, schemaSource, schemaOptions={}) => {
return new Schema(name, schemaSource, Object.assign(Object.create(null), defaultOptions, schemaOptions));
},
// Create a table, merging in default options:
Table: (name, tableOptions={}) => {
// support both Table({name:...}) and Table(name, options);
if (typeof name === 'object') {
tableOptions = name;
} else {
tableOptions = Object.assign({name}, tableOptions);
}
return new Table(Object.assign(Object.create(null), defaultOptions, tableOptions));
},
// Special Schema types
DocId,
Timestamp,
Binary,
// Special Schema fields
DocIdField,
TypeField,
VersionField,
CreatedAtField,
UpdatedAtField,
};
return PublicAPI;
}
module.exports = DynamoDM;
module.exports.DynamoDM = DynamoDM;
module.exports.default = DynamoDM;
// provide a helpful error message if someone forgets to call the API generator:
module.exports.Table = module.exports.Schema = function incorrectUsage(){
throw new Error("DynamoDM must be called as a function to get an instance of the API, e.g. const DynamoDM = require('dynamodm')(options);");
};